Skip to content

Commit 946539f

Browse files
authored
fixes #25 Update Jwt to use string format of scopes for jackson (#26)
1 parent 10bd599 commit 946539f

File tree

5 files changed

+76
-19
lines changed

5 files changed

+76
-19
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.networknt</groupId>
66
<artifactId>http-client</artifactId>
7-
<version>1.0.9</version>
7+
<version>1.0.10</version>
88
<packaging>jar</packaging>
99
<description>generic http client module</description>
1010
<url>https://github.com/networknt/http-client</url>

src/main/java/com/networknt/client/oauth/Jwt.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929
public class Jwt {
3030

31-
protected Set<String> scopes = new HashSet<>();
31+
protected String scopes;
3232
protected Key key;
3333
/**
3434
* This is the client credentials token config if multiple auth servers are used.
@@ -140,19 +140,19 @@ public void setCcConfig(Map<String, Object> ccConfig) {
140140
this.ccConfig = ccConfig;
141141
}
142142

143-
public Set<String> getScopes() {
144-
return scopes;
143+
public Set<String> getScopesSet() {
144+
Set<String> scopesSet = new HashSet<>();
145+
if(StringUtils.isBlank(scopes)) return scopesSet;
146+
scopesSet.addAll(Arrays.asList(scopes.split("(\\s)+")));
147+
return scopesSet;
145148
}
146149

147-
public void setScopes(Set<String> scopes) {
148-
this.scopes = scopes;
150+
public String getScopes() {
151+
return scopes;
149152
}
150153

151-
public void setScopes(String scopesStr) {
152-
this.scopes = this.scopes == null ? new HashSet() : this.scopes;
153-
if(StringUtils.isNotBlank(scopesStr)) {
154-
scopes.addAll(Arrays.asList(scopesStr.split("(\\s)+")));
155-
}
154+
public void setScopes(String scopes) {
155+
this.scopes = scopes;
156156
}
157157

158158
public Key getKey() {
@@ -167,7 +167,7 @@ public static class Key {
167167
/**
168168
* scopes should be extendable by its children
169169
*/
170-
protected Set<String> scopes;
170+
protected String scopes;
171171
/**
172172
* serviceId should be extendable by its children
173173
*/
@@ -183,7 +183,8 @@ public boolean equals(Object obj) {
183183
return hashCode() == obj.hashCode();
184184
}
185185

186-
public Key(Set<String> scopes) {
186+
public Key(String serviceId, String scopes) {
187+
this.serviceId = serviceId;
187188
this.scopes = scopes;
188189
}
189190

@@ -192,13 +193,28 @@ public Key(String serviceId) {
192193
}
193194

194195
public Key() {
195-
this.scopes = new HashSet<>();
196+
196197
}
197198

198-
public Set<String> getScopes() {
199+
public String getScopes() {
199200
return scopes;
200201
}
201202

203+
public Set<String> getScopesSet() {
204+
Set<String> scopesSet = new HashSet<>();
205+
if(StringUtils.isBlank(scopes)) return scopesSet;
206+
scopesSet.addAll(Arrays.asList(scopes.split("(\\s)+")));
207+
return scopesSet;
208+
}
209+
210+
public void setScopes(String scopes) {
211+
this.scopes = scopes;
212+
}
213+
214+
public void setServiceId(String serviceId) {
215+
this.serviceId = serviceId;
216+
}
217+
202218
public String getServiceId() {
203219
return serviceId;
204220
}

src/main/java/com/networknt/client/oauth/OauthHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ private static Result<Jwt> getCCTokenRemotely(final Jwt jwt) {
723723
*/
724724
private static void setScope(TokenRequest tokenRequest, Jwt jwt) {
725725
if(jwt.getKey() != null && jwt.getKey().getScopes() != null && !jwt.getKey().getScopes().isEmpty()) {
726-
tokenRequest.setScope(new ArrayList<>() {{ addAll(jwt.getKey().getScopes()); }});
726+
tokenRequest.setScope(new ArrayList<>() {{ addAll(jwt.getKey().getScopesSet()); }});
727727
}
728728
}
729729

src/main/java/com/networknt/client/oauth/TokenManager.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ public Result<Jwt> getJwt(String requestPath, String scopes, String serviceId) {
125125
} else {
126126
// single auth server, keep the existing logic.
127127
if(scopes != null) {
128-
Set<String> scopeSet = new HashSet<>();
129-
scopeSet.addAll(Arrays.asList(scopes.split(" ")));
130-
return getJwt(new Jwt.Key(scopeSet), null);
128+
return getJwt(new Jwt.Key(scopes), null);
131129
}
132130
if(serviceId != null) {
133131
return getJwt(new Jwt.Key(serviceId), null);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.networknt.client.oauth;
2+
3+
import com.networknt.http.client.JsonMapper;
4+
5+
/**
6+
* Test the Jwt object that can be serialized and deserialized to or from a JSON string.
7+
*/
8+
public class JwtTest {
9+
public static void main(String[] args) {
10+
Jwt jwt = new Jwt();
11+
jwt.setJwt("jwt");
12+
jwt.setExpire(1000);
13+
jwt.setRenewing(true);
14+
jwt.setExpiredRetryTimeout(1000);
15+
jwt.setEarlyRetryTimeout(1000);
16+
jwt.setTokenRenewBeforeExpired(1000);
17+
jwt.setExpiredRefreshRetryDelay(1000);
18+
jwt.setEarlyRefreshRetryDelay(1000);
19+
jwt.setScopes("scope1 scope2");
20+
21+
System.out.println(jwt.getJwt());
22+
System.out.println(jwt.getExpire());
23+
System.out.println(jwt.isRenewing());
24+
System.out.println(jwt.getExpiredRetryTimeout());
25+
System.out.println(jwt.getEarlyRetryTimeout());
26+
System.out.println(jwt.getTokenRenewBeforeExpired());
27+
System.out.println(jwt.getExpiredRefreshRetryDelay());
28+
System.out.println(jwt.getEarlyRefreshRetryDelay());
29+
System.out.println(jwt.getScopes());
30+
String jsonString = JsonMapper.toJson(jwt);
31+
System.out.println(jsonString);
32+
jwt = JsonMapper.fromJson(jsonString, Jwt.class);
33+
System.out.println(jwt.getJwt());
34+
System.out.println(jwt.getExpire());
35+
System.out.println(jwt.isRenewing());
36+
System.out.println(jwt.getExpiredRetryTimeout());
37+
System.out.println(jwt.getEarlyRetryTimeout());
38+
System.out.println(jwt.getTokenRenewBeforeExpired());
39+
System.out.println(jwt.getExpiredRefreshRetryDelay());
40+
System.out.println(jwt.getEarlyRefreshRetryDelay());
41+
System.out.println(jwt.getScopes());
42+
}
43+
}

0 commit comments

Comments
 (0)