• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Bynder / bynder-java-sdk / 12714880002

10 Jan 2025 06:11PM UTC coverage: 39.806% (-0.5%) from 40.281%
12714880002

push

github

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

* API-2194 add client credentials grant type

* API-2194 add client credentials sample

7 of 39 new or added lines in 3 files covered. (17.95%)

779 of 1957 relevant lines covered (39.81%)

0.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.0
/src/main/java/com/bynder/sdk/service/oauth/OAuthServiceImpl.java
1
/*
2
 * Copyright (c) 2019 Bynder B.V. All rights reserved.
3
 *
4
 * Licensed under the MIT License. See LICENSE file in the project root for full license
5
 * information.
6
 */
7
package com.bynder.sdk.service.oauth;
8

9
import com.bynder.sdk.api.OAuthApi;
10
import com.bynder.sdk.configuration.Configuration;
11
import com.bynder.sdk.model.oauth.GrantType;
12
import com.bynder.sdk.model.oauth.ResponseType;
13
import com.bynder.sdk.model.oauth.Token;
14
import com.bynder.sdk.query.decoder.QueryDecoder;
15
import com.bynder.sdk.query.oauth.TokenQuery;
16
import com.bynder.sdk.util.Utils;
17
import io.reactivex.Observable;
18
import retrofit2.Response;
19

20
import java.io.UnsupportedEncodingException;
21
import java.net.MalformedURLException;
22
import java.net.URL;
23
import java.util.List;
24
import java.util.Map;
25

26
public class OAuthServiceImpl implements OAuthService {
27

28
    /**
29
     * Instance of {@link OAuthApi} which handles the HTTP communication with the OAuth2
30
     * provider.
31
     */
32
    private final OAuthApi oauthClient;
33
    /**
34
     * Instance of {@link QueryDecoder} to decode query objects into API parameters.
35
     */
36
    private final QueryDecoder queryDecoder;
37
    /**
38
     * Configuration settings needed to get the OAuth info of the SDK client.
39
     */
40
    private Configuration configuration;
41

42
    /**
43
     * Initialises a new instance of the class.
44
     *
45
     * @param configuration Configuration settings.
46
     * @param oauthClient OAuth2 client instance.
47
     */
48
    OAuthServiceImpl(final Configuration configuration, final OAuthApi oauthClient,
49
        final QueryDecoder queryDecoder) {
1✔
50
        this.configuration = configuration;
1✔
51
        this.oauthClient = oauthClient;
1✔
52
        this.queryDecoder = queryDecoder;
1✔
53
    }
1✔
54

55
    /**
56
     * Check {@link OAuthService} for more information.
57
     */
58
    @Override
59
    public URL getAuthorizationUrl(final String state, final List<String> scopes)
60
        throws MalformedURLException, UnsupportedEncodingException, IllegalArgumentException {
61
        if (state == null || state.isEmpty()) {
1✔
62
            throw new IllegalArgumentException(state);
1✔
63
        }
64

65
        StringBuilder stringBuilder = new StringBuilder();
1✔
66
        stringBuilder.append(configuration.getBaseUrl());
1✔
67
        stringBuilder.append("/v6/authentication/oauth2/auth");
1✔
68
        stringBuilder.append("?client_id=")
1✔
69
            .append(Utils.encodeParameterValue(configuration.getOAuthSettings().getClientId()));
1✔
70
        stringBuilder.append("&redirect_uri=")
1✔
71
            .append(Utils.encodeParameterValue(configuration.getOAuthSettings().getRedirectUri().toString()));
1✔
72
        stringBuilder.append("&response_type=")
1✔
73
            .append(Utils.encodeParameterValue(ResponseType.CODE.toString()));
1✔
74
        stringBuilder.append("&scope=")
1✔
75
            .append(Utils.encodeParameterValue(String.join(" ", scopes)));
1✔
76
        stringBuilder.append("&state=").append(Utils.encodeParameterValue(state));
1✔
77

78
        return new URL(stringBuilder.toString());
1✔
79
    }
80

81
    /**
82
     * Check {@link OAuthService} for more information.
83
     */
84
    @Override
85
    public Observable<Token> getAccessToken(final String code, final List<String> scopes) {
86
        TokenQuery tokenQuery = new TokenQuery(configuration.getOAuthSettings().getClientId(),
1✔
87
            configuration.getOAuthSettings().getClientSecret(), configuration.getOAuthSettings().getRedirectUri(),
1✔
88
            GrantType.AUTHORIZATION_CODE, String.join(" ", scopes), code);
1✔
89

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

93
        return accessTokenObservable.map(response -> {
1✔
94
            Token token = response.body();
×
95
            token.setAccessTokenExpiration();
×
96
            configuration.getOAuthSettings().setToken(token);
×
97
            return token;
×
98
        });
99
    }
100

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(),
1✔
108
                configuration.getOAuthSettings().getClientSecret(), null,
1✔
109
                GrantType.CLIENT_CREDENTIALS, String.join(" ", scopes), null);
1✔
110

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

114
        return accessTokenObservable.map(response -> {
1✔
NEW
115
            Token token = response.body();
×
NEW
116
            token.setAccessTokenExpiration();
×
NEW
117
            configuration.getOAuthSettings().setToken(token);
×
NEW
118
            return token;
×
119
        });
120
    }
121

122
    /**
123
     * Check {@link OAuthService} for more information.
124
     */
125
    @Override
126
    public Observable<Token> refreshAccessToken() {
127
        TokenQuery tokenQuery = new TokenQuery(configuration.getOAuthSettings().getClientId(),
1✔
128
            configuration.getOAuthSettings().getClientSecret(), GrantType.REFRESH_TOKEN,
1✔
129
            configuration.getOAuthSettings().getToken().getRefreshToken());
1✔
130

131
        Map<String, String> params = queryDecoder.decode(tokenQuery);
1✔
132
        Observable<Response<Token>> refreshTokenObservable = oauthClient.getAccessToken(params);
1✔
133

134
        return refreshTokenObservable.map(response -> {
1✔
135
            Token token = response.body();
×
136
            token.setAccessTokenExpiration();
×
137
            configuration.getOAuthSettings().refreshToken(token);
×
138
            return token;
×
139
        });
140
    }
141
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc