/** * Sets the authorization header created from the session on the soap client. * * @param soapClient the SOAP client to set the HTTP header on * @param adsSession the session * @throws AuthenticationException if the authorization header could not be * created */ @SuppressWarnings("unchecked") /* See constructor comments. */ public void setAuthorization(Object soapClient, AdsSession adsSession) throws AuthenticationException { final String authorizationHeader = authorizationHeaderProvider.getAuthorizationHeader(adsSession, soapClientHandler.getEndpointAddress(soapClient)); soapClientHandler.putAllHttpHeaders(soapClient, new HashMap<String, String>() { { put("Authorization", authorizationHeader); } }); } }
@Before public void setUp() { MockitoAnnotations.initMocks(this); authorizationHeaderProvider = new AuthorizationHeaderProvider( oAuth2AuthorizationHeaderProvider, adsLibConfiguration, oAuth2Helper); }
/** * Gets a header value that can be set to the {@code Authorization} HTTP * header. The endpoint URL can be {@code null} if it's not needed for the * authentication mechanism (i.e. OAuth2). * * @param adsSession the session to pull authentication information from * @param endpointUrl the endpoint URL used for authentication mechanisms like * OAuth. * @return the authorization header * @throws AuthenticationException if the authorization header could not be * created * @throws IllegalArgumentException if no valid authentication information * exists within the session. */ public String getAuthorizationHeader(AdsSession adsSession, @Nullable String endpointUrl) throws AuthenticationException { if (adsSession instanceof OAuth2Compatible && ((OAuth2Compatible) adsSession).getOAuth2Credential() != null) { return getOAuth2Header((OAuth2Compatible) adsSession); } else { throw new IllegalArgumentException( "Session does not have any valid authentication mechanisms"); } }
/** * Sets the authorization header created from the session on the soap client. * * @param soapClient the SOAP client to set the HTTP header on * @param adsSession the session * @throws AuthenticationException if the authorization header could not be * created */ @SuppressWarnings("unchecked") /* See constructor comments. */ public void setAuthorization(Object soapClient, AdsSession adsSession) throws AuthenticationException { final String authorizationHeader = authorizationHeaderProvider.getAuthorizationHeader(adsSession, soapClientHandler.getEndpointAddress(soapClient)); soapClientHandler.putAllHttpHeaders(soapClient, new HashMap<String, String>() { { put("Authorization", authorizationHeader); } }); } }
/** * Gets a header value that can be set to the {@code Authorization} HTTP * header. The endpoint URL can be {@code null} if it's not needed for the * authentication mechanism (i.e. OAuth2). * * @param adsSession the session to pull authentication information from * @param endpointUrl the endpoint URL used for authentication mechanisms like * OAuth. * @return the authorization header * @throws AuthenticationException if the authorization header could not be * created * @throws IllegalArgumentException if no valid authentication information * exists within the session. */ public String getAuthorizationHeader(AdsSession adsSession, @Nullable String endpointUrl) throws AuthenticationException { if (adsSession instanceof OAuth2Compatible && ((OAuth2Compatible) adsSession).getOAuth2Credential() != null) { return getOAuth2Header((OAuth2Compatible) adsSession); } else { throw new IllegalArgumentException( "Session does not have any valid authentication mechanisms"); } }
@SuppressWarnings("unchecked") @Test public void testSetAuthorization() throws Exception { String authorizationHeaderValue = "IAmAuthorized"; final String endpoint = "endpoint"; Object soapClient = new Object(); AdsSession adsSession = () -> endpoint; @SuppressWarnings("rawtypes") ArgumentCaptor<Map> headers = ArgumentCaptor.forClass(Map.class); when(soapClientHandler.getEndpointAddress(soapClient)).thenReturn(endpoint); when(authorizationHeaderProvider.getAuthorizationHeader( same(adsSession), eq(endpoint))).thenReturn(authorizationHeaderValue); authorizationHeaderHandler.setAuthorization(soapClient, adsSession); verify(soapClientHandler).putAllHttpHeaders(eq(soapClient), headers.capture()); String actualAuthorizationHeaderValue = (String) headers.getValue().get("Authorization"); assertEquals(authorizationHeaderValue, actualAuthorizationHeaderValue); } }
@Test public void testGetAuthorizationHeader_oAuth2NoRefresh() throws Exception { final Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod()); OAuth2Session adsSession = () -> credential; when(oAuth2AuthorizationHeaderProvider.getOAuth2AuthorizationHeader( (OAuth2Compatible) adsSession)).thenReturn("OAuth2 Header"); when(adsLibConfiguration.isAutoRefreshOAuth2TokenEnabled()).thenReturn(false); assertEquals("OAuth2 Header", authorizationHeaderProvider.getAuthorizationHeader(adsSession, ENDPOINT_URL.toString())); verify(oAuth2Helper, times(0)).refreshCredential(credential); }
@Test public void testGetAuthorizationHeader_oAuth2Refresh() throws Exception { final Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod()); OAuth2Session adsSession = () -> credential; when(oAuth2AuthorizationHeaderProvider.getOAuth2AuthorizationHeader( (OAuth2Compatible) adsSession)).thenReturn("OAuth2 Header"); when(adsLibConfiguration.isAutoRefreshOAuth2TokenEnabled()).thenReturn(true); assertEquals("OAuth2 Header", authorizationHeaderProvider.getAuthorizationHeader(adsSession, ENDPOINT_URL.toString())); verify(oAuth2Helper).refreshCredential(credential); }
HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAuthorization( authorizationHeaderProvider.getAuthorizationHeader(session, reportUrl)); httpHeaders.setUserAgent(userAgentCombiner.getUserAgent(session.getUserAgent())); httpHeaders.set("developerToken", session.getDeveloperToken());
HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAuthorization( authorizationHeaderProvider.getAuthorizationHeader(session, reportUrl)); httpHeaders.setUserAgent(userAgentCombiner.getUserAgent(session.getUserAgent())); httpHeaders.set("developerToken", session.getDeveloperToken());
.withReportingConfiguration(reportingConfiguration) .build(); when(authorizationHeaderProvider.getAuthorizationHeader(session, ENDPOINT_URL.build())) .thenReturn("fooauthheader"); when(userAgentCombiner.getUserAgent(anyString())).thenReturn("foouseragent");