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

box / box-java-sdk / #5076

07 Oct 2025 12:35PM UTC coverage: 37.132% (+0.007%) from 37.125%
#5076

push

github

web-flow
test: Change `Event.additionalDetails` field assertion in events test (box/box-codegen#858) (#1491)

18454 of 49699 relevant lines covered (37.13%)

0.37 hits per line

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

93.67
/src/main/java/com/box/sdkgen/box/ccgauth/BoxCCGAuth.java
1
package com.box.sdkgen.box.ccgauth;
2

3
import com.box.sdkgen.box.errors.BoxSDKError;
4
import com.box.sdkgen.box.tokenstorage.InMemoryTokenStorage;
5
import com.box.sdkgen.box.tokenstorage.TokenStorage;
6
import com.box.sdkgen.managers.authorization.AuthorizationManager;
7
import com.box.sdkgen.networking.auth.Authentication;
8
import com.box.sdkgen.networking.network.NetworkSession;
9
import com.box.sdkgen.schemas.accesstoken.AccessToken;
10
import com.box.sdkgen.schemas.postoauth2revoke.PostOAuth2Revoke;
11
import com.box.sdkgen.schemas.postoauth2token.PostOAuth2Token;
12
import com.box.sdkgen.schemas.postoauth2token.PostOAuth2TokenBoxSubjectTypeField;
13
import com.box.sdkgen.schemas.postoauth2token.PostOAuth2TokenGrantTypeField;
14
import com.box.sdkgen.schemas.postoauth2token.PostOAuth2TokenSubjectTokenTypeField;
15
import com.box.sdkgen.serialization.json.EnumWrapper;
16
import java.util.List;
17

18
public class BoxCCGAuth implements Authentication {
19

20
  /** Configuration object of Client Credentials Grant auth. */
21
  public final CCGConfig config;
22

23
  /**
24
   * An object responsible for storing token. If no custom implementation provided, the token will
25
   * be stored in memory.
26
   */
27
  public final TokenStorage tokenStorage;
28

29
  /**
30
   * The ID of the user or enterprise to authenticate as. If not provided, defaults to the
31
   * enterprise ID if set, otherwise defaults to the user ID.
32
   */
33
  public String subjectId;
34

35
  /** The type of the subject ID provided. Must be either 'user' or 'enterprise'. */
36
  public EnumWrapper<PostOAuth2TokenBoxSubjectTypeField> subjectType;
37

38
  public BoxCCGAuth(CCGConfig config) {
1✔
39
    this.config = config;
1✔
40
    this.tokenStorage = this.config.getTokenStorage();
1✔
41
    this.subjectId =
1✔
42
        (!(this.config.getUserId() == null)
1✔
43
            ? this.config.getUserId()
1✔
44
            : this.config.getEnterpriseId());
1✔
45
    this.subjectType =
1✔
46
        new EnumWrapper<PostOAuth2TokenBoxSubjectTypeField>(
47
            (!(this.config.getUserId() == null)
1✔
48
                ? PostOAuth2TokenBoxSubjectTypeField.USER
49
                : PostOAuth2TokenBoxSubjectTypeField.ENTERPRISE));
50
  }
1✔
51

52
  /** Get a new access token using CCG auth */
53
  public AccessToken refreshToken() {
54
    return refreshToken(null);
×
55
  }
56

57
  /**
58
   * Get a new access token using CCG auth
59
   *
60
   * @param networkSession An object to keep network session state
61
   */
62
  @Override
63
  public AccessToken refreshToken(NetworkSession networkSession) {
64
    AuthorizationManager authManager =
1✔
65
        new AuthorizationManager.Builder()
66
            .networkSession((!(networkSession == null) ? networkSession : new NetworkSession()))
1✔
67
            .build();
1✔
68
    AccessToken token =
1✔
69
        authManager.requestAccessToken(
1✔
70
            new PostOAuth2Token.Builder(PostOAuth2TokenGrantTypeField.CLIENT_CREDENTIALS)
71
                .clientId(this.config.getClientId())
1✔
72
                .clientSecret(this.config.getClientSecret())
1✔
73
                .boxSubjectType(this.subjectType)
1✔
74
                .boxSubjectId(this.subjectId)
1✔
75
                .build());
1✔
76
    this.tokenStorage.store(token);
1✔
77
    return token;
1✔
78
  }
79

80
  /** Return a current token or get a new one when not available. */
81
  public AccessToken retrieveToken() {
82
    return retrieveToken(null);
1✔
83
  }
84

85
  /**
86
   * Return a current token or get a new one when not available.
87
   *
88
   * @param networkSession An object to keep network session state
89
   */
90
  @Override
91
  public AccessToken retrieveToken(NetworkSession networkSession) {
92
    AccessToken oldToken = this.tokenStorage.get();
1✔
93
    if (oldToken == null) {
1✔
94
      AccessToken newToken = this.refreshToken(networkSession);
1✔
95
      return newToken;
1✔
96
    }
97
    return oldToken;
1✔
98
  }
99

100
  public String retrieveAuthorizationHeader() {
101
    return retrieveAuthorizationHeader(null);
×
102
  }
103

104
  @Override
105
  public String retrieveAuthorizationHeader(NetworkSession networkSession) {
106
    AccessToken token = this.retrieveToken(networkSession);
1✔
107
    return String.join("", "Bearer ", token.getAccessToken());
1✔
108
  }
109

110
  /**
111
   * Create a new BoxCCGAuth instance that uses the provided user ID as the subject ID. May be one
112
   * of this application's created App User. Depending on the configured User Access Level, may also
113
   * be any other App User or Managed User in the enterprise.
114
   * &lt;https://developer.box.com/en/guides/applications/&gt;
115
   * &lt;https://developer.box.com/en/guides/authentication/select/&gt;
116
   *
117
   * @param userId The id of the user to authenticate
118
   */
119
  public BoxCCGAuth withUserSubject(String userId) {
120
    return withUserSubject(userId, new InMemoryTokenStorage());
1✔
121
  }
122

123
  /**
124
   * Create a new BoxCCGAuth instance that uses the provided user ID as the subject ID. May be one
125
   * of this application's created App User. Depending on the configured User Access Level, may also
126
   * be any other App User or Managed User in the enterprise.
127
   * &lt;https://developer.box.com/en/guides/applications/&gt;
128
   * &lt;https://developer.box.com/en/guides/authentication/select/&gt;
129
   *
130
   * @param userId The id of the user to authenticate
131
   * @param tokenStorage Object responsible for storing token in newly created BoxCCGAuth. If no
132
   *     custom implementation provided, the token will be stored in memory.
133
   */
134
  public BoxCCGAuth withUserSubject(String userId, TokenStorage tokenStorage) {
135
    CCGConfig newConfig =
1✔
136
        new CCGConfig.Builder(this.config.getClientId(), this.config.getClientSecret())
1✔
137
            .enterpriseId(this.config.getEnterpriseId())
1✔
138
            .userId(userId)
1✔
139
            .tokenStorage(tokenStorage)
1✔
140
            .build();
1✔
141
    return new BoxCCGAuth(newConfig);
1✔
142
  }
143

144
  /**
145
   * Create a new BoxCCGAuth instance that uses the provided enterprise ID as the subject ID.
146
   *
147
   * @param enterpriseId The id of the enterprise to authenticate
148
   */
149
  public BoxCCGAuth withEnterpriseSubject(String enterpriseId) {
150
    return withEnterpriseSubject(enterpriseId, new InMemoryTokenStorage());
1✔
151
  }
152

153
  /**
154
   * Create a new BoxCCGAuth instance that uses the provided enterprise ID as the subject ID.
155
   *
156
   * @param enterpriseId The id of the enterprise to authenticate
157
   * @param tokenStorage Object responsible for storing token in newly created BoxCCGAuth. If no
158
   *     custom implementation provided, the token will be stored in memory.
159
   */
160
  public BoxCCGAuth withEnterpriseSubject(String enterpriseId, TokenStorage tokenStorage) {
161
    CCGConfig newConfig =
1✔
162
        new CCGConfig.Builder(this.config.getClientId(), this.config.getClientSecret())
1✔
163
            .enterpriseId(enterpriseId)
1✔
164
            .userId(null)
1✔
165
            .tokenStorage(tokenStorage)
1✔
166
            .build();
1✔
167
    return new BoxCCGAuth(newConfig);
1✔
168
  }
169

170
  /**
171
   * Downscope access token to the provided scopes. Returning a new access token with the provided
172
   * scopes, with the original access token unchanged.
173
   *
174
   * @param scopes The scope(s) to apply to the resulting token.
175
   * @param resource The file or folder to get a downscoped token for. If None and shared_link None,
176
   *     the resulting token will not be scoped down to just a single item. The resource should be a
177
   *     full URL to an item, e.g. https://api.box.com/2.0/files/123456.
178
   * @param sharedLink The shared link to get a downscoped token for. If None and item None, the
179
   *     resulting token will not be scoped down to just a single item.
180
   * @param networkSession An object to keep network session state
181
   */
182
  @Override
183
  public AccessToken downscopeToken(
184
      List<String> scopes, String resource, String sharedLink, NetworkSession networkSession) {
185
    AccessToken token = this.retrieveToken(networkSession);
1✔
186
    if (token == null) {
1✔
187
      throw new BoxSDKError(
×
188
          "No access token is available. Make an API call to retrieve a token before calling this method.");
189
    }
190
    AuthorizationManager authManager =
1✔
191
        new AuthorizationManager.Builder()
192
            .networkSession((!(networkSession == null) ? networkSession : new NetworkSession()))
1✔
193
            .build();
1✔
194
    AccessToken downscopedToken =
1✔
195
        authManager.requestAccessToken(
1✔
196
            new PostOAuth2Token.Builder(
197
                    PostOAuth2TokenGrantTypeField.URN_IETF_PARAMS_OAUTH_GRANT_TYPE_TOKEN_EXCHANGE)
198
                .subjectToken(token.getAccessToken())
1✔
199
                .subjectTokenType(
1✔
200
                    PostOAuth2TokenSubjectTokenTypeField
201
                        .URN_IETF_PARAMS_OAUTH_TOKEN_TYPE_ACCESS_TOKEN)
202
                .resource(resource)
1✔
203
                .scope(String.join(" ", scopes))
1✔
204
                .boxSharedLink(sharedLink)
1✔
205
                .build());
1✔
206
    return downscopedToken;
1✔
207
  }
208

209
  /** Revoke the current access token and remove it from token storage. */
210
  public void revokeToken() {
211
    revokeToken(null);
1✔
212
  }
1✔
213

214
  /**
215
   * Revoke the current access token and remove it from token storage.
216
   *
217
   * @param networkSession An object to keep network session state
218
   */
219
  @Override
220
  public void revokeToken(NetworkSession networkSession) {
221
    AccessToken oldToken = this.tokenStorage.get();
1✔
222
    if (oldToken == null) {
1✔
223
      return;
×
224
    }
225
    AuthorizationManager authManager =
1✔
226
        new AuthorizationManager.Builder()
227
            .networkSession((!(networkSession == null) ? networkSession : new NetworkSession()))
1✔
228
            .build();
1✔
229
    authManager.revokeAccessToken(
1✔
230
        new PostOAuth2Revoke.Builder()
231
            .clientId(this.config.getClientId())
1✔
232
            .clientSecret(this.config.getClientSecret())
1✔
233
            .token(oldToken.getAccessToken())
1✔
234
            .build());
1✔
235
    this.tokenStorage.clear();
1✔
236
  }
1✔
237

238
  public TokenStorage getTokenStorage() {
239
    return tokenStorage;
×
240
  }
241
}
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