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

box / box-java-sdk-gen / #349

11 Jul 2025 02:24PM UTC coverage: 35.58% (-0.06%) from 35.639%
#349

push

github

web-flow
test: Improve names in transfer integration test (box/box-codegen#759) (#358)

16957 of 47659 relevant lines covered (35.58%)

0.36 hits per line

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

93.14
/src/main/java/com/box/sdkgen/box/jwtauth/BoxJWTAuth.java
1
package com.box.sdkgen.box.jwtauth;
2

3
import static com.box.sdkgen.internal.utils.UtilsManager.createJwtAssertion;
4
import static com.box.sdkgen.internal.utils.UtilsManager.entryOf;
5
import static com.box.sdkgen.internal.utils.UtilsManager.getEpochTimeInSeconds;
6
import static com.box.sdkgen.internal.utils.UtilsManager.getUuid;
7
import static com.box.sdkgen.internal.utils.UtilsManager.isBrowser;
8
import static com.box.sdkgen.internal.utils.UtilsManager.mapOf;
9

10
import com.box.sdkgen.box.errors.BoxSDKError;
11
import com.box.sdkgen.box.tokenstorage.InMemoryTokenStorage;
12
import com.box.sdkgen.box.tokenstorage.TokenStorage;
13
import com.box.sdkgen.internal.utils.JwtKey;
14
import com.box.sdkgen.internal.utils.JwtSignOptions;
15
import com.box.sdkgen.managers.authorization.AuthorizationManager;
16
import com.box.sdkgen.networking.auth.Authentication;
17
import com.box.sdkgen.networking.network.NetworkSession;
18
import com.box.sdkgen.schemas.accesstoken.AccessToken;
19
import com.box.sdkgen.schemas.postoauth2revoke.PostOAuth2Revoke;
20
import com.box.sdkgen.schemas.postoauth2token.PostOAuth2Token;
21
import com.box.sdkgen.schemas.postoauth2token.PostOAuth2TokenGrantTypeField;
22
import com.box.sdkgen.schemas.postoauth2token.PostOAuth2TokenSubjectTokenTypeField;
23
import java.util.List;
24
import java.util.Map;
25

26
public class BoxJWTAuth implements Authentication {
27

28
  public final JWTConfig config;
29

30
  public final TokenStorage tokenStorage;
31

32
  public String subjectId;
33

34
  public String subjectType;
35

36
  public BoxJWTAuth(JWTConfig config) {
1✔
37
    this.config = config;
1✔
38
    this.tokenStorage = this.config.getTokenStorage();
1✔
39
    this.subjectId =
1✔
40
        (!(this.config.getEnterpriseId() == null)
1✔
41
            ? this.config.getEnterpriseId()
1✔
42
            : this.config.getUserId());
1✔
43
    this.subjectType = (!(this.config.getEnterpriseId() == null) ? "enterprise" : "user");
1✔
44
  }
1✔
45

46
  public AccessToken refreshToken() {
47
    return refreshToken(null);
×
48
  }
49

50
  @Override
51
  public AccessToken refreshToken(NetworkSession networkSession) {
52
    if (isBrowser()) {
1✔
53
      throw new BoxSDKError("JWT auth is not supported in browser environment.");
×
54
    }
55
    Map<String, Object> claims =
1✔
56
        mapOf(
1✔
57
            entryOf("exp", getEpochTimeInSeconds() + 30),
1✔
58
            entryOf("box_sub_type", this.subjectType));
1✔
59
    JwtSignOptions jwtOptions =
1✔
60
        new JwtSignOptions(
61
            this.config.getAlgorithm(),
1✔
62
            "https://api.box.com/oauth2/token",
63
            this.config.getClientId(),
1✔
64
            this.subjectId,
65
            getUuid(),
1✔
66
            this.config.getJwtKeyId(),
1✔
67
            this.config.getPrivateKeyDecryptor());
1✔
68
    JwtKey jwtKey = new JwtKey(this.config.getPrivateKey(), this.config.getPrivateKeyPassphrase());
1✔
69
    String assertion = createJwtAssertion(claims, jwtKey, jwtOptions);
1✔
70
    AuthorizationManager authManager =
1✔
71
        new AuthorizationManager.Builder()
72
            .networkSession((!(networkSession == null) ? networkSession : new NetworkSession()))
1✔
73
            .build();
1✔
74
    AccessToken token =
1✔
75
        authManager.requestAccessToken(
1✔
76
            new PostOAuth2Token.Builder(
77
                    PostOAuth2TokenGrantTypeField.URN_IETF_PARAMS_OAUTH_GRANT_TYPE_JWT_BEARER)
78
                .assertion(assertion)
1✔
79
                .clientId(this.config.getClientId())
1✔
80
                .clientSecret(this.config.getClientSecret())
1✔
81
                .build());
1✔
82
    this.tokenStorage.store(token);
1✔
83
    return token;
1✔
84
  }
85

86
  public AccessToken retrieveToken() {
87
    return retrieveToken(null);
1✔
88
  }
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
  public BoxJWTAuth withUserSubject(String userId) {
111
    return withUserSubject(userId, new InMemoryTokenStorage());
1✔
112
  }
113

114
  public BoxJWTAuth withUserSubject(String userId, TokenStorage tokenStorage) {
115
    JWTConfig newConfig =
1✔
116
        new JWTConfig.Builder(
117
                this.config.getClientId(),
1✔
118
                this.config.getClientSecret(),
1✔
119
                this.config.getJwtKeyId(),
1✔
120
                this.config.getPrivateKey(),
1✔
121
                this.config.getPrivateKeyPassphrase())
1✔
122
            .enterpriseId(null)
1✔
123
            .userId(userId)
1✔
124
            .tokenStorage(tokenStorage)
1✔
125
            .build();
1✔
126
    BoxJWTAuth newAuth = new BoxJWTAuth(newConfig);
1✔
127
    return newAuth;
1✔
128
  }
129

130
  public BoxJWTAuth withEnterpriseSubject(String enterpriseId) {
131
    return withEnterpriseSubject(enterpriseId, new InMemoryTokenStorage());
1✔
132
  }
133

134
  public BoxJWTAuth withEnterpriseSubject(String enterpriseId, TokenStorage tokenStorage) {
135
    JWTConfig newConfig =
1✔
136
        new JWTConfig.Builder(
137
                this.config.getClientId(),
1✔
138
                this.config.getClientSecret(),
1✔
139
                this.config.getJwtKeyId(),
1✔
140
                this.config.getPrivateKey(),
1✔
141
                this.config.getPrivateKeyPassphrase())
1✔
142
            .enterpriseId(enterpriseId)
1✔
143
            .userId(null)
1✔
144
            .tokenStorage(tokenStorage)
1✔
145
            .build();
1✔
146
    BoxJWTAuth newAuth = new BoxJWTAuth(newConfig);
1✔
147
    return newAuth;
1✔
148
  }
149

150
  @Override
151
  public AccessToken downscopeToken(
152
      List<String> scopes, String resource, String sharedLink, NetworkSession networkSession) {
153
    AccessToken token = this.retrieveToken(networkSession);
1✔
154
    if (token == null) {
1✔
155
      throw new BoxSDKError(
×
156
          "No access token is available. Make an API call to retrieve a token before calling this method.");
157
    }
158
    AuthorizationManager authManager =
1✔
159
        new AuthorizationManager.Builder()
160
            .networkSession((!(networkSession == null) ? networkSession : new NetworkSession()))
1✔
161
            .build();
1✔
162
    AccessToken downscopedToken =
1✔
163
        authManager.requestAccessToken(
1✔
164
            new PostOAuth2Token.Builder(
165
                    PostOAuth2TokenGrantTypeField.URN_IETF_PARAMS_OAUTH_GRANT_TYPE_TOKEN_EXCHANGE)
166
                .subjectToken(token.getAccessToken())
1✔
167
                .subjectTokenType(
1✔
168
                    PostOAuth2TokenSubjectTokenTypeField
169
                        .URN_IETF_PARAMS_OAUTH_TOKEN_TYPE_ACCESS_TOKEN)
170
                .resource(resource)
1✔
171
                .scope(String.join(" ", scopes))
1✔
172
                .boxSharedLink(sharedLink)
1✔
173
                .build());
1✔
174
    return downscopedToken;
1✔
175
  }
176

177
  public void revokeToken() {
178
    revokeToken(null);
1✔
179
  }
1✔
180

181
  @Override
182
  public void revokeToken(NetworkSession networkSession) {
183
    AccessToken oldToken = this.tokenStorage.get();
1✔
184
    if (oldToken == null) {
1✔
185
      return;
×
186
    }
187
    AuthorizationManager authManager =
1✔
188
        new AuthorizationManager.Builder()
189
            .networkSession((!(networkSession == null) ? networkSession : new NetworkSession()))
1✔
190
            .build();
1✔
191
    authManager.revokeAccessToken(
1✔
192
        new PostOAuth2Revoke.Builder()
193
            .clientId(this.config.getClientId())
1✔
194
            .clientSecret(this.config.getClientSecret())
1✔
195
            .token(oldToken.getAccessToken())
1✔
196
            .build());
1✔
197
    this.tokenStorage.clear();
1✔
198
  }
1✔
199

200
  public JWTConfig getConfig() {
201
    return config;
×
202
  }
203

204
  public TokenStorage getTokenStorage() {
205
    return tokenStorage;
×
206
  }
207
}
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