Skip to content

Commit

Permalink
API-2194 add client credentials grant type
Browse files Browse the repository at this point in the history
  • Loading branch information
ahongbynder committed Jan 10, 2025
1 parent 62fcd81 commit 93264b6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/bynder/sdk/model/oauth/GrantType.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
public enum GrantType {

AUTHORIZATION_CODE("authorization_code"), REFRESH_TOKEN("refresh_token");
AUTHORIZATION_CODE("authorization_code"), REFRESH_TOKEN("refresh_token"), CLIENT_CREDENTIALS("client_credentials");

private final String name;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/bynder/sdk/query/oauth/TokenQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public class TokenQuery {
@ApiField(name = "redirect_uri")
private URI redirectUri;
/**
* The authorization grant type. Possible values: {@link GrantType#AUTHORIZATION_CODE} and
* {@link GrantType#REFRESH_TOKEN}.
* The authorization grant type. Possible values: {@link GrantType#AUTHORIZATION_CODE}
* ,{@link GrantType#REFRESH_TOKEN} and {@link GrantType#CLIENT_CREDENTIALS}.
*/
@ApiField(name = "grant_type")
private GrantType grantType;
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/bynder/sdk/service/oauth/OAuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ URL getAuthorizationUrl(final String state, final List<String> scopes)
*/
Observable<Token> getAccessToken(final String code, final List<String> scopes);

/**
* Gets an access token using the client credentials authorization grant.
* @param scopes
* @return {@link Observable} with {@link Token} information.
*/
Observable<Token> getAccessTokenClientCredentials(final List<String> scopes);

/**
* Gets a new access token using the refresh token.
*
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/bynder/sdk/service/oauth/OAuthServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ public Observable<Token> getAccessToken(final String code, final List<String> sc
});
}

/**
* Check {@link OAuthService} for more information.
*/
@Override
public Observable<Token> getAccessTokenClientCredentials(final List<String> scopes) {
// use grant type client credentials
TokenQuery tokenQuery = new TokenQuery(configuration.getOAuthSettings().getClientId(),
configuration.getOAuthSettings().getClientSecret(), null,
GrantType.CLIENT_CREDENTIALS, String.join(" ", scopes), null);

Map<String, String> params = queryDecoder.decode(tokenQuery);
Observable<Response<Token>> accessTokenObservable = oauthClient.getAccessToken(params);

return accessTokenObservable.map(response -> {
Token token = response.body();
token.setAccessTokenExpiration();
configuration.getOAuthSettings().setToken(token);
return token;
});
}

/**
* Check {@link OAuthService} for more information.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ public void getAccessToken() {
verify(queryDecoder, times(1)).decode(any());
}

@Test
public void getAccessTokenClientCredentials() {
oAuthService.getAccessTokenClientCredentials(EXPECTED_SCOPES);

verify(oauthClient, times(1)).getAccessToken(anyMap());
verify(queryDecoder, times(1)).decode(any());
}

@Test
public void getRefreshToken() {
oAuthService.refreshAccessToken();
Expand Down

0 comments on commit 93264b6

Please sign in to comment.