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

box / box-java-sdk / #5791

03 Dec 2025 11:45AM UTC coverage: 35.933% (-0.005%) from 35.938%
#5791

push

github

web-flow
docs: modify `can_view_path` description and add confidence scores for structured extract (box/box-openapi#566) (#1601)

2 of 12 new or added lines in 2 files covered. (16.67%)

9 existing lines in 6 files now uncovered.

18481 of 51432 relevant lines covered (35.93%)

0.36 hits per line

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

73.49
/src/main/java/com/box/sdkgen/box/oauth/BoxOAuth.java
1
package com.box.sdkgen.box.oauth;
2

3
import static com.box.sdkgen.internal.utils.UtilsManager.entryOf;
4
import static com.box.sdkgen.internal.utils.UtilsManager.mapOf;
5
import static com.box.sdkgen.internal.utils.UtilsManager.prepareParams;
6
import static com.box.sdkgen.serialization.json.JsonManager.sdToUrlParams;
7

8
import com.box.sdkgen.box.errors.BoxSDKError;
9
import com.box.sdkgen.box.tokenstorage.TokenStorage;
10
import com.box.sdkgen.managers.authorization.AuthorizationManager;
11
import com.box.sdkgen.networking.auth.Authentication;
12
import com.box.sdkgen.networking.network.NetworkSession;
13
import com.box.sdkgen.schemas.accesstoken.AccessToken;
14
import com.box.sdkgen.schemas.postoauth2revoke.PostOAuth2Revoke;
15
import com.box.sdkgen.schemas.postoauth2token.PostOAuth2Token;
16
import com.box.sdkgen.schemas.postoauth2token.PostOAuth2TokenGrantTypeField;
17
import com.box.sdkgen.schemas.postoauth2token.PostOAuth2TokenSubjectTokenTypeField;
18
import com.box.sdkgen.serialization.json.JsonManager;
19
import java.util.List;
20
import java.util.Map;
21

22
public class BoxOAuth implements Authentication {
23

24
  /** Configuration object of OAuth. */
25
  public final OAuthConfig config;
26

27
  /**
28
   * An object responsible for storing token. If no custom implementation provided, the token will
29
   * be stored in memory.
30
   */
31
  public final TokenStorage tokenStorage;
32

33
  public BoxOAuth(OAuthConfig config) {
1✔
34
    this.config = config;
1✔
35
    this.tokenStorage = this.config.getTokenStorage();
1✔
36
  }
1✔
37

38
  /** Get the authorization URL for the app user. */
39
  public String getAuthorizeUrl() {
40
    return getAuthorizeUrl(new GetAuthorizeUrlOptions());
1✔
41
  }
42

43
  /**
44
   * Get the authorization URL for the app user.
45
   *
46
   * @param options The options parameter
47
   */
48
  public String getAuthorizeUrl(GetAuthorizeUrlOptions options) {
49
    Map<String, String> paramsMap =
1✔
50
        prepareParams(
1✔
51
            mapOf(
1✔
52
                entryOf(
1✔
53
                    "client_id",
54
                    (!(options.getClientId() == null)
1✔
UNCOV
55
                        ? options.getClientId()
×
56
                        : this.config.getClientId())),
1✔
57
                entryOf(
1✔
58
                    "response_type",
59
                    (!(options.getResponseType() == null) ? options.getResponseType() : "code")),
1✔
60
                entryOf("redirect_uri", options.getRedirectUri()),
1✔
61
                entryOf("state", options.getState()),
1✔
62
                entryOf("scope", options.getScope())));
1✔
63
    return String.join(
1✔
64
        "",
65
        "https://account.box.com/api/oauth2/authorize?",
66
        sdToUrlParams(JsonManager.serialize(paramsMap)));
1✔
67
  }
68

69
  /**
70
   * Acquires token info using an authorization code.
71
   *
72
   * @param authorizationCode The authorization code to use to get tokens.
73
   */
74
  public AccessToken getTokensAuthorizationCodeGrant(String authorizationCode) {
75
    return getTokensAuthorizationCodeGrant(authorizationCode, null);
×
76
  }
77

78
  /**
79
   * Acquires token info using an authorization code.
80
   *
81
   * @param authorizationCode The authorization code to use to get tokens.
82
   * @param networkSession An object to keep network session state
83
   */
84
  public AccessToken getTokensAuthorizationCodeGrant(
85
      String authorizationCode, NetworkSession networkSession) {
86
    AuthorizationManager authManager =
×
87
        new AuthorizationManager.Builder()
88
            .networkSession((!(networkSession == null) ? networkSession : new NetworkSession()))
×
89
            .build();
×
90
    AccessToken token =
×
91
        authManager.requestAccessToken(
×
92
            new PostOAuth2Token.Builder(PostOAuth2TokenGrantTypeField.AUTHORIZATION_CODE)
93
                .code(authorizationCode)
×
94
                .clientId(this.config.getClientId())
×
95
                .clientSecret(this.config.getClientSecret())
×
96
                .build());
×
97
    this.tokenStorage.store(token);
×
98
    return token;
×
99
  }
100

101
  /**
102
   * Get the current access token. If the current access token is expired or not found, this method
103
   * will attempt to refresh the token.
104
   */
105
  public AccessToken retrieveToken() {
106
    return retrieveToken(null);
×
107
  }
108

109
  /**
110
   * Get the current access token. If the current access token is expired or not found, this method
111
   * will attempt to refresh the token.
112
   *
113
   * @param networkSession An object to keep network session state
114
   */
115
  @Override
116
  public AccessToken retrieveToken(NetworkSession networkSession) {
117
    AccessToken token = this.tokenStorage.get();
1✔
118
    if (token == null) {
1✔
119
      throw new BoxSDKError(
1✔
120
          "Access and refresh tokens not available. Authenticate before making any API call first.");
121
    }
122
    return token;
1✔
123
  }
124

125
  /** Get a new access token for the platform app user. */
126
  public AccessToken refreshToken() {
127
    return refreshToken(null);
×
128
  }
129

130
  /**
131
   * Get a new access token for the platform app user.
132
   *
133
   * @param networkSession An object to keep network session state
134
   */
135
  @Override
136
  public AccessToken refreshToken(NetworkSession networkSession) {
137
    AccessToken oldToken = this.tokenStorage.get();
1✔
138
    String tokenUsedForRefresh = (!(oldToken == null) ? oldToken.getRefreshToken() : null);
1✔
139
    AuthorizationManager authManager =
1✔
140
        new AuthorizationManager.Builder()
141
            .networkSession((!(networkSession == null) ? networkSession : new NetworkSession()))
1✔
142
            .build();
1✔
143
    AccessToken token =
1✔
144
        authManager.requestAccessToken(
×
145
            new PostOAuth2Token.Builder(PostOAuth2TokenGrantTypeField.REFRESH_TOKEN)
146
                .clientId(this.config.getClientId())
1✔
147
                .clientSecret(this.config.getClientSecret())
1✔
148
                .refreshToken(tokenUsedForRefresh)
1✔
149
                .build());
1✔
150
    this.tokenStorage.store(token);
×
151
    return token;
×
152
  }
153

154
  public String retrieveAuthorizationHeader() {
155
    return retrieveAuthorizationHeader(null);
×
156
  }
157

158
  @Override
159
  public String retrieveAuthorizationHeader(NetworkSession networkSession) {
160
    AccessToken token = this.retrieveToken(networkSession);
1✔
161
    return String.join("", "Bearer ", token.getAccessToken());
1✔
162
  }
163

164
  /**
165
   * Revoke an active Access Token, effectively logging a user out that has been previously
166
   * authenticated.
167
   */
168
  public void revokeToken() {
169
    revokeToken(null);
1✔
170
  }
1✔
171

172
  /**
173
   * Revoke an active Access Token, effectively logging a user out that has been previously
174
   * authenticated.
175
   *
176
   * @param networkSession An object to keep network session state
177
   */
178
  @Override
179
  public void revokeToken(NetworkSession networkSession) {
180
    AccessToken token = this.tokenStorage.get();
1✔
181
    if (token == null) {
1✔
182
      return;
×
183
    }
184
    AuthorizationManager authManager =
1✔
185
        new AuthorizationManager.Builder()
186
            .networkSession((!(networkSession == null) ? networkSession : new NetworkSession()))
1✔
187
            .build();
1✔
188
    authManager.revokeAccessToken(
1✔
189
        new PostOAuth2Revoke.Builder()
190
            .clientId(this.config.getClientId())
1✔
191
            .clientSecret(this.config.getClientSecret())
1✔
192
            .token(token.getAccessToken())
1✔
193
            .build());
1✔
194
  }
1✔
195

196
  /**
197
   * Downscope access token to the provided scopes. Returning a new access token with the provided
198
   * scopes, with the original access token unchanged.
199
   *
200
   * @param scopes The scope(s) to apply to the resulting token.
201
   * @param resource The file or folder to get a downscoped token for. If None and shared_link None,
202
   *     the resulting token will not be scoped down to just a single item. The resource should be a
203
   *     full URL to an item, e.g. https://api.box.com/2.0/files/123456.
204
   * @param sharedLink The shared link to get a downscoped token for. If None and item None, the
205
   *     resulting token will not be scoped down to just a single item.
206
   * @param networkSession An object to keep network session state
207
   */
208
  @Override
209
  public AccessToken downscopeToken(
210
      List<String> scopes, String resource, String sharedLink, NetworkSession networkSession) {
211
    AccessToken token = this.retrieveToken(networkSession);
1✔
212
    if (token == null || token.getAccessToken() == null) {
1✔
213
      throw new BoxSDKError("No access token is available.");
×
214
    }
215
    AuthorizationManager authManager =
1✔
216
        new AuthorizationManager.Builder()
217
            .networkSession((!(networkSession == null) ? networkSession : new NetworkSession()))
1✔
218
            .build();
1✔
219
    AccessToken downscopedToken =
1✔
220
        authManager.requestAccessToken(
1✔
221
            new PostOAuth2Token.Builder(
222
                    PostOAuth2TokenGrantTypeField.URN_IETF_PARAMS_OAUTH_GRANT_TYPE_TOKEN_EXCHANGE)
223
                .subjectToken(token.getAccessToken())
1✔
224
                .subjectTokenType(
1✔
225
                    PostOAuth2TokenSubjectTokenTypeField
226
                        .URN_IETF_PARAMS_OAUTH_TOKEN_TYPE_ACCESS_TOKEN)
227
                .resource(resource)
1✔
228
                .scope(String.join(" ", scopes))
1✔
229
                .boxSharedLink(sharedLink)
1✔
230
                .build());
1✔
231
    return downscopedToken;
1✔
232
  }
233

234
  public TokenStorage getTokenStorage() {
235
    return tokenStorage;
×
236
  }
237
}
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