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

box / box-java-sdk-gen / #17

17 Mar 2025 04:26PM UTC coverage: 35.208% (-0.005%) from 35.213%
#17

push

github

web-flow
feat: Expose token storage for authentication classes (box/box-codegen#682) (#250)

0 of 4 new or added lines in 4 files covered. (0.0%)

4 existing lines in 4 files now uncovered.

15234 of 43269 relevant lines covered (35.21%)

0.35 hits per line

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

93.33
/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
  public final CCGConfig config;
21

22
  public final TokenStorage tokenStorage;
23

24
  public String subjectId;
25

26
  public EnumWrapper<PostOAuth2TokenBoxSubjectTypeField> subjectType;
27

28
  public BoxCCGAuth(CCGConfig config) {
1✔
29
    this.config = config;
1✔
30
    this.tokenStorage = this.config.getTokenStorage();
1✔
31
    this.subjectId =
1✔
32
        (!(this.config.getUserId() == null)
1✔
33
            ? this.config.getUserId()
1✔
34
            : this.config.getEnterpriseId());
1✔
35
    this.subjectType =
1✔
36
        new EnumWrapper<PostOAuth2TokenBoxSubjectTypeField>(
37
            (!(this.config.getUserId() == null)
1✔
38
                ? PostOAuth2TokenBoxSubjectTypeField.USER
1✔
39
                : PostOAuth2TokenBoxSubjectTypeField.ENTERPRISE));
1✔
40
  }
1✔
41

42
  public AccessToken refreshToken() {
43
    return refreshToken(null);
×
44
  }
45

46
  @Override
47
  public AccessToken refreshToken(NetworkSession networkSession) {
48
    AuthorizationManager authManager = new AuthorizationManager();
1✔
49
    AccessToken token =
1✔
50
        authManager.requestAccessToken(
1✔
51
            new PostOAuth2Token.PostOAuth2TokenBuilder(
52
                    PostOAuth2TokenGrantTypeField.CLIENT_CREDENTIALS)
53
                .clientId(this.config.getClientId())
1✔
54
                .clientSecret(this.config.getClientSecret())
1✔
55
                .boxSubjectType(this.subjectType)
1✔
56
                .boxSubjectId(this.subjectId)
1✔
57
                .build());
1✔
58
    this.tokenStorage.store(token);
1✔
59
    return token;
1✔
60
  }
61

62
  public AccessToken retrieveToken() {
63
    return retrieveToken(null);
1✔
64
  }
65

66
  @Override
67
  public AccessToken retrieveToken(NetworkSession networkSession) {
68
    AccessToken oldToken = this.tokenStorage.get();
1✔
69
    if (oldToken == null) {
1✔
70
      AccessToken newToken = this.refreshToken(networkSession);
1✔
71
      return newToken;
1✔
72
    }
73
    return oldToken;
1✔
74
  }
75

76
  public String retrieveAuthorizationHeader() {
77
    return retrieveAuthorizationHeader(null);
×
78
  }
79

80
  @Override
81
  public String retrieveAuthorizationHeader(NetworkSession networkSession) {
82
    AccessToken token = this.retrieveToken(networkSession);
1✔
83
    return String.join("", "Bearer ", token.getAccessToken());
1✔
84
  }
85

86
  public BoxCCGAuth withUserSubject(String userId) {
87
    return withUserSubject(userId, new InMemoryTokenStorage());
1✔
88
  }
89

90
  public BoxCCGAuth withUserSubject(String userId, TokenStorage tokenStorage) {
91
    CCGConfig newConfig =
1✔
92
        new CCGConfig.CCGConfigBuilder(this.config.getClientId(), this.config.getClientSecret())
1✔
93
            .enterpriseId(this.config.getEnterpriseId())
1✔
94
            .userId(userId)
1✔
95
            .tokenStorage(tokenStorage)
1✔
96
            .build();
1✔
97
    return new BoxCCGAuth(newConfig);
1✔
98
  }
99

100
  public BoxCCGAuth withEnterpriseSubject(String enterpriseId) {
101
    return withEnterpriseSubject(enterpriseId, new InMemoryTokenStorage());
1✔
102
  }
103

104
  public BoxCCGAuth withEnterpriseSubject(String enterpriseId, TokenStorage tokenStorage) {
105
    CCGConfig newConfig =
1✔
106
        new CCGConfig.CCGConfigBuilder(this.config.getClientId(), this.config.getClientSecret())
1✔
107
            .enterpriseId(enterpriseId)
1✔
108
            .userId(null)
1✔
109
            .tokenStorage(tokenStorage)
1✔
110
            .build();
1✔
111
    return new BoxCCGAuth(newConfig);
1✔
112
  }
113

114
  @Override
115
  public AccessToken downscopeToken(
116
      List<String> scopes, String resource, String sharedLink, NetworkSession networkSession) {
117
    AccessToken token = this.tokenStorage.get();
1✔
118
    if (token == null) {
1✔
119
      throw new BoxSDKError(
×
120
          "No access token is available. Make an API call to retrieve a token before calling this method.");
121
    }
122
    AuthorizationManager authManager = new AuthorizationManager();
1✔
123
    AccessToken downscopedToken =
1✔
124
        authManager.requestAccessToken(
1✔
125
            new PostOAuth2Token.PostOAuth2TokenBuilder(
126
                    PostOAuth2TokenGrantTypeField.URN_IETF_PARAMS_OAUTH_GRANT_TYPE_TOKEN_EXCHANGE)
127
                .subjectToken(token.getAccessToken())
1✔
128
                .subjectTokenType(
1✔
129
                    PostOAuth2TokenSubjectTokenTypeField
130
                        .URN_IETF_PARAMS_OAUTH_TOKEN_TYPE_ACCESS_TOKEN)
131
                .resource(resource)
1✔
132
                .scope(String.join(" ", scopes))
1✔
133
                .boxSharedLink(sharedLink)
1✔
134
                .build());
1✔
135
    return downscopedToken;
1✔
136
  }
137

138
  public void revokeToken() {
139
    revokeToken(null);
1✔
140
  }
1✔
141

142
  @Override
143
  public void revokeToken(NetworkSession networkSession) {
144
    AccessToken oldToken = this.tokenStorage.get();
1✔
145
    if (oldToken == null) {
1✔
146
      return;
×
147
    }
148
    AuthorizationManager authManager = new AuthorizationManager();
1✔
149
    authManager.revokeAccessToken(
1✔
150
        new PostOAuth2Revoke.PostOAuth2RevokeBuilder()
151
            .clientId(this.config.getClientId())
1✔
152
            .clientSecret(this.config.getClientSecret())
1✔
153
            .token(oldToken.getAccessToken())
1✔
154
            .build());
1✔
155
    this.tokenStorage.clear();
1✔
156
  }
1✔
157

158
  public TokenStorage getTokenStorage() {
NEW
159
    return tokenStorage;
×
160
  }
161
}
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