public Builder( Credential.AccessMethod method, HttpTransport transport, JsonFactory jsonFactory, GenericUrl tokenServerUrl, HttpExecuteInterceptor clientAuthentication, String clientId, String authorizationServerEncodedUrl) { super( method, transport, jsonFactory, tokenServerUrl, clientAuthentication, clientId, authorizationServerEncodedUrl); }
/** * 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 (!isConfiguredForOAuth()) { throw new NullPointerException("No OAuth client credentials"); } HttpExecuteInterceptor clientAuthenticator; if (forTokenRequest) { clientAuthenticator = getBasicAuthentication(); } else { clientAuthenticator = getClientParameters(); } return new AuthorizationCodeFlow( BearerToken.authorizationHeaderAccessMethod(), getHttpTransport(), JacksonFactory.getDefaultInstance(), new GenericUrl(TOKEN_ENDPOINT_URI), clientAuthenticator, getClientId(), AUTHORIZATION_ENDPOINT_URI); }
/** * @param method method of presenting the access token to the resource * server (for example * {@link BearerToken#authorizationHeaderAccessMethod}) * @param transport HTTP transport * @param jsonFactory JSON factory * @param tokenServerUrl token server URL * @param clientAuthentication client authentication or {@code null} for * none (see * {@link TokenRequest#setClientAuthentication(HttpExecuteInterceptor)} * ) * @param clientId client identifier * @param authorizationServerEncodedUrl authorization server encoded URL */ public Builder(AccessMethod method, HttpTransport transport, JsonFactory jsonFactory, GenericUrl tokenServerUrl, HttpExecuteInterceptor clientAuthentication, String clientId, String authorizationServerEncodedUrl) { super(method, transport, jsonFactory, tokenServerUrl, clientAuthentication, clientId, authorizationServerEncodedUrl); }
@Override public MicrosoftAuthorizationCodeTokenRequest setClientAuthentication( HttpExecuteInterceptor clientAuthentication) { Preconditions.checkNotNull(clientAuthentication); return (MicrosoftAuthorizationCodeTokenRequest) super.setClientAuthentication(clientAuthentication); }
@Override public void initialize(HttpRequest request) throws IOException { List<HttpIOExceptionHandler> ioExceptionHandlers = new ArrayList<>(); List<HttpUnsuccessfulResponseHandler> unsuccessfulResponseHandlers = new ArrayList<>(); List<HttpExecuteInterceptor> interceptors = new ArrayList<>(); List<HttpResponseInterceptor> responseInterceptors = new ArrayList<>(); for (HttpRequestInitializer initializer : initializers) { initializer.initialize(request); if (request.getIOExceptionHandler() != null) { ioExceptionHandlers.add(request.getIOExceptionHandler()); request.setIOExceptionHandler(null); } if (request.getUnsuccessfulResponseHandler() != null) { unsuccessfulResponseHandlers.add(request.getUnsuccessfulResponseHandler()); request.setUnsuccessfulResponseHandler(null); } if (request.getInterceptor() != null) { interceptors.add(request.getInterceptor()); request.setInterceptor(null); } if (request.getResponseInterceptor() != null) { responseInterceptors.add(request.getResponseInterceptor()); request.setResponseInterceptor(null); } } request.setIOExceptionHandler( makeIoExceptionHandler(ioExceptionHandlers)); request.setUnsuccessfulResponseHandler( makeUnsuccessfulResponseHandler(unsuccessfulResponseHandlers)); request.setInterceptor( makeInterceptor(interceptors)); request.setResponseInterceptor( makeResponseInterceptor(responseInterceptors)); }
private HttpExecuteInterceptor makeInterceptor( final Iterable<HttpExecuteInterceptor> interceptors) { return new HttpExecuteInterceptor() { @Override public void intercept(HttpRequest request) throws IOException { for (HttpExecuteInterceptor interceptor : interceptors) { interceptor.intercept(request); } } }; }
@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)); }
protected HttpExecuteInterceptor getHttpExecuteInterceptor() { return new ClientParametersAuthentication(clientId, clientSecret); }
/** * Returns a HTTP execute interceptor with only the client identifier as a * request parameter. * @return HTTP execute interceptor */ protected HttpExecuteInterceptor getClientParameters() { // Sets only the client identifier that is required in authorization // requests. return new ClientParametersAuthentication(getClientId(), null); }
@Test public void useCustomDataStore() throws Exception { Credential credential = new Credential.Builder(BearerToken.authorizationHeaderAccessMethod()) .setTransport(new MockHttpTransport()) .setJsonFactory(new MockJsonFactory()) .setClientAuthentication(Mockito.mock(HttpExecuteInterceptor.class)) .setTokenServerUrl(new GenericUrl(TOKEN_REQUEST_URL)) .build(); credential.setAccessToken("accessToken2"); credential.setRefreshToken("refreshToken2"); credential.setExpiresInSeconds(1000L); DataStore mockDataStore = Mockito.mock(DataStore.class); MockDataStoreFactory mockDataStoreFactory = new MockDataStoreFactory(mockDataStore); Mockito.when(mockDataStore.get(eq("userId"))).thenReturn(new StoredCredential(credential)); OAuth2Credentials oAuth2Credentials = new OAuth2Credentials.Builder() .setClientSecrets("CLIENT_ID", "CLIENT_SECRET") .setRedirectUri("http://redirect") .setHttpTransport(mockHttpTransport) .setCredentialDataStoreFactory(mockDataStoreFactory) .setScopes(Arrays.asList(Scope.PROFILE, Scope.REQUEST)) .build(); Credential storedCredential = oAuth2Credentials.authenticate("authorizationCode", "userId"); Credential loadedCredential = oAuth2Credentials.loadCredential("userId"); assertEquals("Refresh token does not match.", "refreshToken", storedCredential.getRefreshToken()); assertTrue("Expected expires_in between 0 and 3600. Was actually: " + storedCredential.getExpiresInSeconds(), storedCredential.getExpiresInSeconds() > 0 && storedCredential.getExpiresInSeconds() <= 3600); assertEquals("Access token does not match.", "accessToken", storedCredential.getAccessToken()); assertEquals("Access method (Bearer) does not match", BearerToken.authorizationHeaderAccessMethod().getClass(), storedCredential.getMethod().getClass()); assertEquals("Refresh token does not match.", "refreshToken2", loadedCredential.getRefreshToken()); assertTrue("Expected expires_in between 0 and 1000. Was actually: " + loadedCredential.getExpiresInSeconds(), loadedCredential.getExpiresInSeconds() > 0 && loadedCredential.getExpiresInSeconds() <= 1000L); assertEquals("Access token does not match.", "accessToken2", loadedCredential.getAccessToken()); assertEquals("Access method (Bearer) does not match", BearerToken.authorizationHeaderAccessMethod().getClass(), loadedCredential.getMethod().getClass()); }
@Override public Builder setClientAuthentication(HttpExecuteInterceptor clientAuthentication) { return (Builder) super.setClientAuthentication(clientAuthentication); }
/** * Constructs this object with a HTTP execute interceptor for * authentication. * @param authentication HTTP execute interceptor */ public RestService(HttpExecuteInterceptor authentication) { this.authentication = authentication; }
/** * Returns a HTTP execute interceptor with the client identifier and the * client secret for Basic authentication. * @return HTTP execute interceptor */ protected HttpExecuteInterceptor getBasicAuthentication() { return new BasicAuthentication(getClientId(), getClientSecret()); }