/** * Set the {@link SessionConfiguration} information */ public Builder setSessionConfiguration(SessionConfiguration configuration) { if (scopes != null) { this.scopes = new HashSet<>(configuration.getScopes()); } if (customScopes != null) { this.customScopes = new HashSet<>(configuration.getCustomScopes()); } this.clientId = configuration.getClientId(); this.clientSecret = configuration.getClientSecret(); this.redirectUri = configuration.getRedirectUri(); this.loginHost = configuration.getLoginHost(); return this; }
/** * Creates an {@link OAuth2Credentials} object that can be used by any of the servlets. */ public static OAuth2Credentials createOAuth2Credentials(SessionConfiguration sessionConfiguration) throws Exception { // Store the users OAuth2 credentials in their home directory. File credentialDirectory = new File(System.getProperty("user.home") + File.separator + ".uber_credentials"); credentialDirectory.setReadable(true, true); credentialDirectory.setWritable(true, true); // If you'd like to store them in memory or in a DB, any DataStoreFactory can be used. AbstractDataStoreFactory dataStoreFactory = new FileDataStoreFactory(credentialDirectory); // Build an OAuth2Credentials object with your secrets. return new OAuth2Credentials.Builder() .setCredentialDataStoreFactory(dataStoreFactory) .setRedirectUri(sessionConfiguration.getRedirectUri()) .setClientSecrets(sessionConfiguration.getClientId(), sessionConfiguration.getClientSecret()) .build(); }
@Test public void getRedirectUri_whenSetOnBuilder_setsOnConfiguration() throws Exception { SessionConfiguration config = new SessionConfiguration.Builder() .setClientId("clientId") .setRedirectUri("redirectUri") .build(); assertEquals("redirectUri", config.getRedirectUri()); }
@Test public void newBuilder_copiesConfiguration() throws Exception { SessionConfiguration config = new SessionConfiguration.Builder() .setClientId("clientId") .setRedirectUri("test") .build(); SessionConfiguration config2 = config.newBuilder().setClientId("clientId2").build(); assertEquals("clientId", config.getClientId()); assertEquals("clientId2", config2.getClientId()); assertEquals("test", config2.getRedirectUri()); }
/** * Creates an {@link OAuth2Credentials} object that can be used by any of the servlets. * * Throws an {@throws IOException} when no client ID or secret found in secrets.properties */ static OAuth2Credentials createOAuth2Credentials(SessionConfiguration config) throws IOException { return new OAuth2Credentials.Builder() .setCredentialDataStoreFactory(MemoryDataStoreFactory.getDefaultInstance()) .setRedirectUri(config.getRedirectUri()) .setScopes(config.getScopes()) .setClientSecrets(config.getClientId(), config.getClientSecret()) .build(); }