/** * @param expiresIn the time that the access token expires. * @param scope space delimited list of {@link Scope}s. * @param token the Uber API access token. * @param refreshToken the Uber API refresh token. * @param tokenType the Uber API token type. */ public AccessToken( long expiresIn, String scope, String token, String refreshToken, String tokenType) { expires_in = expiresIn; this.scope = Scope.parseScopes(scope); access_token = token; refresh_token = refreshToken; token_type = tokenType; }
public static String toStandardString(@Nonnull Collection<Scope> scopes) { StringBuilder stringBuilder = new StringBuilder(); int i = 0; for (Scope scope : scopes) { stringBuilder.append(scope.toString().toLowerCase()); if (i < scopes.size() - 1) { stringBuilder.append(' '); } i++; } return stringBuilder.toString(); } }
public static Set<Scope> parseScopes(String concatenatedScopes) { Set<Scope> scopes = new LinkedHashSet<>(); for (String scopeString : concatenatedScopes.split(" ")) { try { Scope scope = Scope.valueOf(scopeString.toUpperCase()); scopes.add(scope); } catch (IllegalArgumentException ex) { } } return scopes; }
@Test public void testParseScopesWithMixLevelScopes_shouldReturn() throws Exception { Set<Scope> scopes = Scope.parseScopes( Scope.HISTORY.getBitValue() | Scope.REQUEST.getBitValue() | Scope.PROFILE.getBitValue()); assertThat(scopes).contains(Scope.HISTORY, Scope.REQUEST, Scope.PROFILE); }
@ToJson String toJson(@OAuthScopes Set<Scope> scopes) { return Scope.toStandardString(scopes); } }
public static Set<Scope> parseScopes(int bitValues) { Set<Scope> scopes = new LinkedHashSet<>(); if (bitValues <= 0) { return scopes; } for (Scope scope : Scope.values()) { if ((bitValues & scope.mBitValue) == scope.mBitValue) { scopes.add(scope); } } return scopes; }
allScopes.add(scope.name().toLowerCase());
@Test public void testParseScopesWithMultipleGeneralScopes_shouldReturn() throws Exception { Set<Scope> scopes = Scope.parseScopes(Scope.HISTORY.getBitValue() | Scope.PROFILE.getBitValue()); assertThat(scopes).contains(Scope.HISTORY, Scope.PROFILE); }
@ToJson String toJson(@OAuthScopes Set<Scope> scopes) { return Scope.toStandardString(scopes); } }
public static Set<Scope> parseScopes(int bitValues) { Set<Scope> scopes = new LinkedHashSet<>(); if (bitValues <= 0) { return scopes; } for (Scope scope : Scope.values()) { if ((bitValues & scope.mBitValue) == scope.mBitValue) { scopes.add(scope); } } return scopes; }
/** * @param expiresIn the time that the access token expires. * @param scope space delimited list of {@link Scope}s. * @param token the Uber API access token. * @param refreshToken the Uber API refresh token. * @param tokenType the Uber API token type. */ public AccessToken( long expiresIn, String scope, String token, String refreshToken, String tokenType) { expires_in = expiresIn; this.scope = Scope.parseScopes(scope); access_token = token; refresh_token = refreshToken; token_type = tokenType; }
@Test public void testParseScopesWithOneScope_shouldReturn() throws Exception { Set<Scope> scopes = Scope.parseScopes(Scope.HISTORY.getBitValue()); assertThat(scopes).contains(Scope.HISTORY); }
@Test public void testToStandardStringMultiScopes_spaceDelimited() { Collection<Scope> scopes = Arrays.asList(Scope.HISTORY, Scope.ALL_TRIPS); assertThat(Scope.toStandardString(scopes)).isEqualTo("history all_trips"); }
public static Set<Scope> parseScopes(String concatenatedScopes) { Set<Scope> scopes = new LinkedHashSet<>(); for (String scopeString : concatenatedScopes.split(" ")) { try { Scope scope = Scope.valueOf(scopeString.toUpperCase()); scopes.add(scope); } catch (IllegalArgumentException ex) { } } return scopes; }
public static String toStandardString(@Nonnull Collection<Scope> scopes) { StringBuilder stringBuilder = new StringBuilder(); int i = 0; for (Scope scope : scopes) { stringBuilder.append(scope.toString().toLowerCase()); if (i < scopes.size() - 1) { stringBuilder.append(' '); } i++; } return stringBuilder.toString(); } }
@FromJson @OAuthScopes Set<Scope> fromJson(String scopes) { return Scope.parseScopes(scopes); }
@Test public void testToStandardStringOneScope_noSpace() { Collection<Scope> scopes = Arrays.asList(Scope.HISTORY); assertThat(Scope.toStandardString(scopes)).isEqualTo("history"); }
@FromJson @OAuthScopes Set<Scope> fromJson(String scopes) { return Scope.parseScopes(scopes); }
@Test public void toJson() throws Exception { AccessToken accessToken = new AccessToken(2592000, Arrays.asList(Scope.REQUEST, Scope.ALL_TRIPS, Scope.PROFILE), "Access999Token", "888RefreshToken", "Bearer"); Collection<Scope> scopes = accessToken.getScopes(); String json = jsonAdapter.toJson(accessToken); assertThat(json).containsOnlyOnce("\"scope\":\"" + Scope.toStandardString(scopes) + "\""); } }
@Test public void testCustomScopes_shouldIgnore() throws Exception { String scopeString = "history profile test"; Set<Scope> scopes = Scope.parseScopes(scopeString); assertThat(scopes).contains(Scope.HISTORY, Scope.PROFILE); assertThat(scopes.size()).isEqualTo(2); }