/** * Creates a {@link Credential} objects with the given {@link TokensAndUrlAuthData} which supports * refreshing tokens. */ public Credential createCredential(TokensAndUrlAuthData authData) { return new Credential.Builder(BearerToken.authorizationHeaderAccessMethod()) .setTransport(httpTransport) .setJsonFactory(jsonFactory) .setClientAuthentication( new ClientParametersAuthentication(appCredentials.getKey(), appCredentials.getSecret())) .setTokenServerEncodedUrl(authData.getTokenServerEncodedUrl()) .build() .setAccessToken(authData.getAccessToken()) .setRefreshToken(authData.getRefreshToken()) .setExpiresInSeconds(EXPIRE_TIME_IN_SECONDS); } }
@Override protected HttpRequestFactory getRequestFactory() throws APIException { String accessToken = getAccessToken(); final Credential credential = new Credential.Builder(BearerToken.queryParameterAccessMethod()).build(); if (StringUtils.isNotBlank(accessToken)) { credential.setAccessToken(accessToken); } return httpTransport.createRequestFactory(credential); }
BearerToken.queryParameterAccessMethod(), HTTP_TRANSPORT, JSON_FACTORY,
public Builder() { super(BearerToken.authorizationHeaderAccessMethod()); setTokenServerEncodedUrl(GoogleOAuthConstants.TOKEN_SERVER_URL); }
@Override public void intercept(HttpRequest request) throws IOException { String accessToken = appIdentityService.getAccessToken(scopes).getAccessToken(); BearerToken.authorizationHeaderAccessMethod().intercept(request, accessToken); }
/** * @param transport HTTP transport * @param jsonFactory JSON factory */ public Builder(HttpTransport transport, JsonFactory jsonFactory) { super(BearerToken.authorizationHeaderAccessMethod()); setTransport(transport); setJsonFactory(jsonFactory); setTokenServerEncodedUrl(TOKEN_SERVER_ENCODED_URL); }
/** * * @param transport HTTP transport * @param jsonFactory JSON factory * @param clientId client identifier * @param clientSecret client secret * @param scopes collection of scopes to be joined by a space separator (or a single value * containing multiple space-separated scopes) * * @since 1.15 */ public Builder(HttpTransport transport, JsonFactory jsonFactory, String clientId, String clientSecret, Collection<String> scopes) { super(BearerToken.authorizationHeaderAccessMethod(), transport, jsonFactory, new GenericUrl( GoogleOAuthConstants.TOKEN_SERVER_URL), new ClientParametersAuthentication( clientId, clientSecret), clientId, GoogleOAuthConstants.AUTHORIZATION_SERVER_URL); setScopes(scopes); }
@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); }
@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); }
public AuthorizationCodeFlow getAuthorizationCodeFlow() { Credential.AccessMethod method = BearerToken.authorizationHeaderAccessMethod(); GenericUrl tokenServerUrl = new GenericUrl(tokenServerURL); HttpExecuteInterceptor clientAuthentication = new ClientParametersAuthentication(clientId, clientSecret); String authorizationServerUrl = authorizationServerURL; return new AuthorizationCodeFlow.Builder(method, HTTP_TRANSPORT, JSON_FACTORY, tokenServerUrl, clientAuthentication, clientId, authorizationServerUrl).setScopes(scopes) .setCredentialDataStore(getCredentialDataStore()) .build(); }
AuthorizationCodeFlow.Builder builder = new AuthorizationCodeFlow.Builder( BearerToken.authorizationHeaderAccessMethod(), httpTransport, new JacksonFactory(),
@Before public void setUp() { MockitoAnnotations.initMocks(this); credential = new Credential.Builder(BearerToken.authorizationHeaderAccessMethod()) .setTransport(new NetHttpTransport()) .setJsonFactory(Mockito.mock(JsonFactory.class)) .setClientAuthentication(Mockito.mock(HttpExecuteInterceptor.class)) .setTokenServerUrl(TOKEN_SERVER_URL).build(); oAuth2Helper = spy(new OAuth2Helper(libLogger, REFRESH_WINDOW_SECS)); }
/** * Returns a authorization code flow for OAuth requests. * @param forTokenRequest <code>true</code> for token requests, or * <code>false</code> for authorization requests * @return {@link AuthorizationCodeFlow} object * @throws NullPointerException if this object has no client credentials */ protected AuthorizationCodeFlow getAuthorizationCodeFlow( boolean forTokenRequest) { if (credentials == null) { throw new NullPointerException("No OAuth client credentials"); } HttpExecuteInterceptor clientAuthentication; if (forTokenRequest) { clientAuthentication = getBasicAuthentication(); } else { clientAuthentication = getClientParameters(); } return new AuthorizationCodeFlow( BearerToken.authorizationHeaderAccessMethod(), getHttpTransport(), JacksonFactory.getDefaultInstance(), new GenericUrl(TOKEN_ENDPOINT), clientAuthentication, credentials.getId(), AUTHORIZATION_ENDPOINT); }
/** * @param transport HTTP transport * @param jsonFactory JSON factory * @param clientSecrets Google client secrets * @param scopes collection of scopes to be joined by a space separator * * @since 1.15 */ public Builder(HttpTransport transport, JsonFactory jsonFactory, GoogleClientSecrets clientSecrets, Collection<String> scopes) { super(BearerToken.authorizationHeaderAccessMethod(), transport, jsonFactory, new GenericUrl( GoogleOAuthConstants.TOKEN_SERVER_URL), new ClientParametersAuthentication( clientSecrets.getDetails().getClientId(), clientSecrets.getDetails().getClientSecret()), clientSecrets.getDetails().getClientId(), GoogleOAuthConstants.AUTHORIZATION_SERVER_URL); setScopes(scopes); }
public Credential authorize() { String authorizationUrl = new AuthorizationCodeRequestUrl(AUTHORIZATION_SERVER_URL, OAuth2ClientCredentials.CLIENT_ID).setRedirectUri(URN_IETF_WG_OAUTH_2_0_OOB).setScopes(SCOPES).build(); System.out.println("Please point your browser to the following URL and copy the access token provided after having authorized this application to access your Google Drive:"); System.out.println(authorizationUrl); String code = readCode(); AuthorizationCodeFlow codeFlow = new AuthorizationCodeFlow.Builder( BearerToken.authorizationHeaderAccessMethod(), HTTP_TRANSPORT, JSON_FACTORY, new GenericUrl( TOKEN_SERVER_URL), new ClientParametersAuthentication(OAuth2ClientCredentials.CLIENT_ID, OAuth2ClientCredentials.CLIENT_SECRET), OAuth2ClientCredentials.CLIENT_ID, AUTHORIZATION_SERVER_URL ).setScopes(SCOPES).build(); try { TokenResponse response = codeFlow.newTokenRequest(code).setScopes(SCOPES).setRedirectUri(URN_IETF_WG_OAUTH_2_0_OOB).execute(); return codeFlow.createAndStoreCredential(response, null); } catch (IOException e) { throw new JDriveSyncException(JDriveSyncException.Reason.IOException, "Failed to execute token request and store credentials: " + e.getMessage(), e); } }
@Override public AuthorizationCodeFlow getAuthorizationCodeFlow() { Optional<String> businessResource = getOneDriveForBusinessResource(); if (businessResource.isPresent()) { String clientId = getClientId(); String clientSecret = getClientSecret(); String authorizationServerURL = getAuthorizationServerURL(); Credential.AccessMethod method = BearerToken.authorizationHeaderAccessMethod(); GenericUrl tokenServerUrl = new GenericUrl(getTokenServerURL()); HttpExecuteInterceptor clientAuthentication = new ClientParametersAuthentication(clientId, clientSecret); return new OneDriveAuthorizationCodeFlow.Builder(method, HTTP_TRANSPORT, JSON_FACTORY, tokenServerUrl, clientAuthentication, clientId, authorizationServerURL).setBusinessResource(businessResource.get()) .setScopes(getScopes()) .setCredentialDataStore( getCredentialDataStore()) .build(); } return super.getAuthorizationCodeFlow(); }
/** * Tests setting the headers. */ @SuppressWarnings("unchecked") @Test public void testSetHeaders() throws Exception { Object soapClient = new Object(); Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod()); AdManagerSession adManagerSession = new AdManagerSession.Builder() .withApplicationName("FooBar") .withOAuth2Credential(credential) .withEndpoint("https://ads.google.com") .withNetworkCode("networkCode") .build(); adManagerHttpHeaderHandler.setHttpHeaders(soapClient, adManagerSession); verify(soapClientHandler).putAllHttpHeaders(eq(soapClient), any(Map.class)); }
public AdManagerSessionTest(boolean isImmutable) { this.isImmutable = isImmutable; this.credential = new Credential(BearerToken.authorizationHeaderAccessMethod()); this.allSettingsBuilder = new AdManagerSession.Builder() .withApplicationName("FooBar") .withEndpoint("https://ads.google.com") .withOAuth2Credential(credential) .withNetworkCode("networkCode"); }
final AuthorizationCodeFlow authorizationFlow = new AuthorizationCodeFlow.Builder( BearerToken.authorizationHeaderAccessMethod(), new NetHttpTransport(), new JacksonFactory(),