Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API-2194 [BE] Add client credentials auth support for Java SDK #131

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.bynder.sdk.sample;

import com.bynder.sdk.configuration.Configuration;
import com.bynder.sdk.configuration.HttpConnectionSettings;
import com.bynder.sdk.configuration.OAuthSettings;
import com.bynder.sdk.model.Brand;
import com.bynder.sdk.model.Media;
import com.bynder.sdk.model.MediaType;
import com.bynder.sdk.query.MediaQuery;
import com.bynder.sdk.query.OrderBy;
import com.bynder.sdk.service.BynderClient;
import com.bynder.sdk.service.asset.AssetService;
import com.bynder.sdk.service.oauth.OAuthService;
import com.bynder.sdk.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public class OAuthClientCredentialsSample {
private static final Logger LOG = LoggerFactory.getLogger(MediaSample.class);

public static void main(final String[] args) throws IOException, URISyntaxException {
/**
* Loads app.properties file under src/main/resources
*/
Properties appProperties = Utils.loadConfig("app");

// Initialize BynderClient with OAuth
OAuthSettings oAuthSettings = new OAuthSettings(appProperties.getProperty("CLIENT_ID"), appProperties.getProperty("CLIENT_SECRET"), null);
BynderClient client = BynderClient.Builder.create(
new Configuration.Builder(new URL(appProperties.getProperty("BASE_URL")))
.setOAuthSettings(oAuthSettings)
.setHttpConnectionSettings(new HttpConnectionSettings()).build());
List<String> scopes = Arrays.asList("offline", "asset:read", "asset:write", "asset.usage:read",
"asset.usage:write", "collection:read", "collection:write", "meta.assetbank:read",
"meta.assetbank:write", "meta.workflow:read");

// Initialize OAuthService
OAuthService oauthService = client.getOAuthService();

// use client credentials
oauthService.getAccessTokenClientCredentials(scopes).blockingSingle();

// Initialize asset service
AssetService assetService = client.getAssetService();

// Call the API to request for brands
List<Brand> brands = assetService.getBrands().blockingSingle().body();
if (brands != null && !brands.isEmpty()) {
for (Brand brand : brands) {
LOG.info("Brand ID: " + brand.getId());
LOG.info("Brand Name: " + brand.getName());
LOG.info("Brand Description: " + brand.getDescription());
}
}

// Call the API to request for media assets
MediaQuery mediaQuery = new MediaQuery().setType(MediaType.IMAGE).setOrderBy(OrderBy.NAME_DESC).setLimit(10).setPage(1);
List<Media> mediaList = assetService.getMediaList(mediaQuery).blockingSingle().body();
if (mediaList != null && !mediaList.isEmpty()) {
for (Media media : mediaList) {
LOG.info("Media ID: " + media.getId());
LOG.info("Media Name: " + media.getName());
}
}

System.exit(0);
}
}
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
Loading