/** * 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); } }
public Builder() { super(BearerToken.authorizationHeaderAccessMethod()); setTokenServerEncodedUrl(GoogleOAuthConstants.TOKEN_SERVER_URL); }
/** * @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); }
@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 * @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_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); }
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(); }
@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)); }
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); } }
/** * @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); }
@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"); }
public Credential authorize(VerificationCodeReceiver receiver, String redirectUri) throws IOException { String code = receiver.waitForCode(); 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(SCOPE).build(); TokenResponse response = codeFlow.newTokenRequest(code) .setRedirectUri(redirectUri).setScopes(SCOPE).execute(); return codeFlow.createAndStoreCredential(response, null); }
public Credential authorize(VerificationCodeReceiver receiver, String redirectUri) throws IOException { String code = receiver.waitForCode(); AuthorizationCodeFlow codeFlow = new AuthorizationCodeFlow.Builder( BearerToken.authorizationHeaderAccessMethod(), HTTP_TRANSPORT, JSON_FACTORY, new GenericUrl(TOKEN_SERVER_URL), new ClientParametersAuthentication( BaseCloudExample.getAPIKey(), BaseCloudExample.getAPISecret()), BaseCloudExample.getAPIKey(), AUTHORIZATION_SERVER_URL).setScopes(SCOPES).build(); TokenResponse response = codeFlow.newTokenRequest(code) .setRedirectUri(redirectUri).setScopes(SCOPES).execute(); return codeFlow.createAndStoreCredential(response, null); }
@Test public void loadCredential() throws Exception { OAuth2Credentials oAuth2Credentials = new OAuth2Credentials.Builder() .setClientSecrets("CLIENT_ID", "CLIENT_SECRET") .setRedirectUri("http://redirect") .setHttpTransport(mockHttpTransport) .setScopes(Arrays.asList(Scope.PROFILE, Scope.REQUEST)) .build(); oAuth2Credentials.authenticate("authorizationCode", "userId"); Credential credential = oAuth2Credentials.loadCredential("userId"); assertEquals("Refresh token does not match.", "refreshToken", credential.getRefreshToken()); assertTrue("Expected expires_in between 0 and 3600. Was actually: " + credential.getExpiresInSeconds(), credential.getExpiresInSeconds() > 0 && credential.getExpiresInSeconds() <= 3600); assertEquals("Access token does not match.", "accessToken", credential.getAccessToken()); assertEquals("Access method (Bearer) does not match", BearerToken.authorizationHeaderAccessMethod().getClass(), credential.getMethod().getClass()); }
private Credential createCredentialWithRefreshToken( HttpTransport transport, JsonFactory jsonFactory, TokenResponse tokenResponse) { String clientId = settingsService.getSettings().getCalendarSettings().getGoogleCalendarSettings().getClientId(); String clientSecret = settingsService.getSettings().getCalendarSettings().getGoogleCalendarSettings().getClientSecret(); return new Credential.Builder(BearerToken.authorizationHeaderAccessMethod()).setTransport( transport) .setJsonFactory(jsonFactory) .setTokenServerUrl( new GenericUrl(GOOGLEAPIS_OAUTH2_V4_TOKEN)) .setClientAuthentication(new BasicAuthentication( clientId, clientSecret)) .build() .setFromTokenResponse(tokenResponse); }
@Test public void testCheckServiceClientPreconditions_passOAuth2() throws Exception { AdWordsServiceClientFactoryHelper helper = new AdWordsServiceClientFactoryHelper( adsServiceClientFactory, adsServiceDescriptorFactory, soapClientHandler, adsLibConfiguration); Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod()); AdWordsSession adWordsSession = new AdWordsSession.Builder() .withUserAgent("FooBar") .withEndpoint("https://www.google.com") .withOAuth2Credential(credential) .withDeveloperToken("developerToken") .build(); helper.checkServiceClientPreconditions(adWordsSession, com.google.api.ads.adwords.lib.factory.helper.testing.v201406.cm.TestService.class); } }
public AdWordsSessionTest(boolean isImmutable) { this.isImmutable = isImmutable; this.credential = new Credential(BearerToken.authorizationHeaderAccessMethod()); this.reportingConfiguration = new ReportingConfiguration.Builder().skipReportHeader(true).skipReportSummary(true).build(); this.allSettingsBuilder = new AdWordsSession.Builder() .withClientCustomerId("customer id") .withDeveloperToken("developer token") .withEndpoint("https://www.google.com") .enablePartialFailure() .enableValidateOnly() .withOAuth2Credential(credential) .withUserAgent("user agent") .withReportingConfiguration(reportingConfiguration); }