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

Bynder / bynder-java-sdk / 12714578120

02 Dec 2024 07:04PM UTC coverage: 40.281%. Remained the same
12714578120

Pull #131

github

web-flow
update java sdk to version 2.2.25 (#129)
Pull Request #131: API-2194 [BE] Add client credentials auth support for Java SDK

7 of 11 new or added lines in 2 files covered. (63.64%)

773 of 1919 relevant lines covered (40.28%)

0.4 hits per line

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

80.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> refreshAccessToken() {
106
        TokenQuery tokenQuery = new TokenQuery(configuration.getOAuthSettings().getClientId(),
1✔
107
            configuration.getOAuthSettings().getClientSecret(), GrantType.REFRESH_TOKEN,
1✔
108
            configuration.getOAuthSettings().getToken().getRefreshToken());
1✔
109

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

113
        return refreshTokenObservable.map(response -> {
1✔
NEW
114
            Token token = response.body();
×
NEW
115
            token.setAccessTokenExpiration();
×
NEW
116
            configuration.getOAuthSettings().refreshToken(token);
×
NEW
117
            return token;
×
118
        });
119
    }
120
}
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

© 2026 Coveralls, Inc