Skip to content

Commit 93264b6

Browse files
committed
API-2194 add client credentials grant type
1 parent 62fcd81 commit 93264b6

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

src/main/java/com/bynder/sdk/model/oauth/GrantType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
public enum GrantType {
1313

14-
AUTHORIZATION_CODE("authorization_code"), REFRESH_TOKEN("refresh_token");
14+
AUTHORIZATION_CODE("authorization_code"), REFRESH_TOKEN("refresh_token"), CLIENT_CREDENTIALS("client_credentials");
1515

1616
private final String name;
1717

src/main/java/com/bynder/sdk/query/oauth/TokenQuery.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public class TokenQuery {
3434
@ApiField(name = "redirect_uri")
3535
private URI redirectUri;
3636
/**
37-
* The authorization grant type. Possible values: {@link GrantType#AUTHORIZATION_CODE} and
38-
* {@link GrantType#REFRESH_TOKEN}.
37+
* The authorization grant type. Possible values: {@link GrantType#AUTHORIZATION_CODE}
38+
* ,{@link GrantType#REFRESH_TOKEN} and {@link GrantType#CLIENT_CREDENTIALS}.
3939
*/
4040
@ApiField(name = "grant_type")
4141
private GrantType grantType;

src/main/java/com/bynder/sdk/service/oauth/OAuthService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ URL getAuthorizationUrl(final String state, final List<String> scopes)
4949
*/
5050
Observable<Token> getAccessToken(final String code, final List<String> scopes);
5151

52+
/**
53+
* Gets an access token using the client credentials authorization grant.
54+
* @param scopes
55+
* @return {@link Observable} with {@link Token} information.
56+
*/
57+
Observable<Token> getAccessTokenClientCredentials(final List<String> scopes);
58+
5259
/**
5360
* Gets a new access token using the refresh token.
5461
*

src/main/java/com/bynder/sdk/service/oauth/OAuthServiceImpl.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,27 @@ public Observable<Token> getAccessToken(final String code, final List<String> sc
9898
});
9999
}
100100

101+
/**
102+
* Check {@link OAuthService} for more information.
103+
*/
104+
@Override
105+
public Observable<Token> getAccessTokenClientCredentials(final List<String> scopes) {
106+
// use grant type client credentials
107+
TokenQuery tokenQuery = new TokenQuery(configuration.getOAuthSettings().getClientId(),
108+
configuration.getOAuthSettings().getClientSecret(), null,
109+
GrantType.CLIENT_CREDENTIALS, String.join(" ", scopes), null);
110+
111+
Map<String, String> params = queryDecoder.decode(tokenQuery);
112+
Observable<Response<Token>> accessTokenObservable = oauthClient.getAccessToken(params);
113+
114+
return accessTokenObservable.map(response -> {
115+
Token token = response.body();
116+
token.setAccessTokenExpiration();
117+
configuration.getOAuthSettings().setToken(token);
118+
return token;
119+
});
120+
}
121+
101122
/**
102123
* Check {@link OAuthService} for more information.
103124
*/

src/test/java/com/bynder/sdk/service/oauth/OAuthServiceImplTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ public void getAccessToken() {
9595
verify(queryDecoder, times(1)).decode(any());
9696
}
9797

98+
@Test
99+
public void getAccessTokenClientCredentials() {
100+
oAuthService.getAccessTokenClientCredentials(EXPECTED_SCOPES);
101+
102+
verify(oauthClient, times(1)).getAccessToken(anyMap());
103+
verify(queryDecoder, times(1)).decode(any());
104+
}
105+
98106
@Test
99107
public void getRefreshToken() {
100108
oAuthService.refreshAccessToken();

0 commit comments

Comments
 (0)