Skip to content

Commit a4c96b7

Browse files
committed
wip
Signed-off-by: Richard Salac <richard.salac@broadcom.com>
1 parent 6f1ad46 commit a4c96b7

18 files changed

Lines changed: 449 additions & 139 deletions

File tree

apiml-security-common/src/main/java/org/zowe/apiml/security/common/token/TokenAuthentication.java

Lines changed: 106 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,84 +10,158 @@
1010

1111
package org.zowe.apiml.security.common.token;
1212

13+
import com.nimbusds.jwt.*;
1314
import lombok.EqualsAndHashCode;
1415
import lombok.Getter;
16+
import lombok.extern.slf4j.Slf4j;
1517
import org.springframework.security.authentication.AbstractAuthenticationToken;
16-
import org.zowe.apiml.security.common.login.LoginFilter;
18+
import org.springframework.security.core.parameters.P;
1719

20+
import java.io.Serial;
21+
import java.text.ParseException;
1822
import java.util.Collections;
19-
import java.util.Optional;
23+
import java.util.Date;
24+
import java.util.List;
2025

2126
/**
2227
* This object is added to security context after successful authentication.
2328
* Contains username and valid JWT token.
2429
*/
2530
@EqualsAndHashCode(callSuper = false)
31+
@Slf4j
2632
public class TokenAuthentication extends AbstractAuthenticationToken {
2733

34+
@Serial
35+
//TODO: regenerate
2836
private static final long serialVersionUID = 9187160928171618141L;
2937

30-
private final String username;
31-
private final String token;
38+
private static final String DOMAIN_CLAIM_NAME = "dom";
39+
private static final String SCOPES = "scopes";
40+
41+
private final JWT jwt;
42+
private final JWTClaimsSet claims;
43+
private final QueryResponse queryResponse;
44+
3245
@Getter
3346
private Type type;
3447

35-
public TokenAuthentication(String token) {
36-
this(token, (Type) null);
48+
public TokenAuthentication(String tokenString) throws ParseException {
49+
super(Collections.emptyList());
50+
51+
this.jwt = JWTParser.parse(tokenString);
52+
this.claims = jwt.getJWTClaimsSet();
53+
this.queryResponse = parseQueryResponse(claims);
54+
this.type = null;
3755
}
3856

39-
public TokenAuthentication(String token, Type type) {
40-
this(null, token, type);
57+
public TokenAuthentication(String tokenString, Type type) {
58+
super(Collections.emptyList());
59+
60+
try {
61+
this.jwt = JWTParser.parse(tokenString);
62+
this.claims = jwt.getJWTClaimsSet();
63+
this.queryResponse = parseQueryResponse(claims);
64+
this.type = type;
65+
} catch (ParseException ex) {
66+
throw new TokenNotValidException("Token is not valid.", ex);
67+
}
4168
}
4269

43-
public TokenAuthentication(String username, String token) {
44-
this(username, token, (Type) null);
70+
public JWT getJwt() {
71+
return jwt;
4572
}
4673

47-
public TokenAuthentication(String username, String token, Type type) {
48-
super(Collections.emptyList());
49-
this.username = username;
50-
this.token = token;
51-
this.type = type;
74+
public boolean isExpired() {
75+
return queryResponse.isExpired();
76+
}
77+
78+
public Date getExpiration() {
79+
return queryResponse.getExpiration();
80+
}
81+
82+
public QueryResponse.Source getSource() {
83+
return queryResponse.getSource();
5284
}
5385

86+
public String getClaimAsString(String claimName) throws ParseException {
87+
return claims.getClaimAsString(claimName);
88+
}
89+
90+
public QueryResponse getQueryResponse() {
91+
return queryResponse;
92+
}
93+
94+
// public TokenAuthenticationEnhanced(String token, Type type) {
95+
// this(null, token, type);
96+
// }
97+
//
98+
// public TokenAuthenticationEnhanced(String username, String token) {
99+
// this(username, token, (Type) null);
100+
// }
101+
//
102+
// public TokenAuthenticationEnhanced(String username, String token, Type type) {
103+
// super(Collections.emptyList());
104+
// this.token = token;
105+
// this.type = type;
106+
// }
107+
54108
/**
55109
* @return the token that prove the username is correct
56110
*/
57111
@Override
58112
public String getCredentials() {
59-
return token;
113+
return jwt.getParsedString();
60114
}
61115

62116
/**
63117
* @return the username being authenticated
64118
*/
65119
@Override
66120
public String getPrincipal() {
67-
return username;
121+
return queryResponse.getUserId();
68122
}
69123

70-
/**
71-
* Creates the TokenAuthentication with fulfilled username (principal), token and marked as authenticated.
72-
* @param username Username, who is authenticated
73-
* @param token Token, which authenticate the user
74-
* @return TokenAuthentication marked as authenticated with username, token
75-
*/
76-
public static TokenAuthentication createAuthenticated(String username, String token, Type type) {
77-
final TokenAuthentication out = new TokenAuthentication(username, token, type);
78-
out.setAuthenticated(true);
79-
return out;
80-
}
124+
// /**
125+
// * Creates the TokenAuthentication with fulfilled username (principal), token and marked as authenticated.
126+
// * @param username Username, who is authenticated
127+
// * @param token Token, which authenticate the user
128+
// * @return TokenAuthentication marked as authenticated with username, token
129+
// */
130+
// public static TokenAuthenticationEnhanced createAuthenticated(String username, String token, Type type) {
131+
// final TokenAuthenticationEnhanced out = new TokenAuthenticationEnhanced(username, token, type);
132+
// out.setAuthenticated(true);
133+
// return out;
134+
// }
81135

82136
@SuppressWarnings("squid:S3655")
83-
public static TokenAuthentication createAuthenticatedFromHeader(String token, String authHeader) {
84-
var loginRequest = LoginFilter.getCredentialFromAuthorizationHeader(Optional.of(authHeader));
85-
return createAuthenticated(loginRequest.get().getUsername(), token, Type.JWT);
86-
}
137+
// public static TokenAuthenticationEnhanced createAuthenticatedFromHeader(String token, String authHeader) {
138+
// var loginRequest = LoginFilter.getCredentialFromAuthorizationHeader(Optional.of(authHeader));
139+
// return createAuthenticated(loginRequest.get().getUsername(), token, Type.JWT);
140+
// }
87141

88142
public enum Type {
89143
JWT,
90144
OIDC
91145
}
92146

147+
private QueryResponse parseQueryResponse(JWTClaimsSet claims) {
148+
Object scopesObject = claims.getClaim(SCOPES);
149+
List<String> scopes = Collections.emptyList();
150+
if (scopesObject instanceof List<?>) {
151+
scopes = (List<String>) scopesObject;
152+
}
153+
try {
154+
return new QueryResponse(
155+
claims.getClaimAsString(DOMAIN_CLAIM_NAME),
156+
claims.getSubject(),
157+
claims.getIssueTime(),
158+
claims.getExpirationTime(),
159+
claims.getIssuer(),
160+
scopes,
161+
QueryResponse.Source.valueByIssuer(claims.getIssuer())
162+
);
163+
} catch (ParseException e) {
164+
throw new TokenNotValidException(e.getMessage(), e);
165+
}
166+
}
93167
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* This program and the accompanying materials are made available under the terms of the
3+
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
4+
* https://www.eclipse.org/legal/epl-v20.html
5+
*
6+
* SPDX-License-Identifier: EPL-2.0
7+
*
8+
* Copyright Contributors to the Zowe Project.
9+
*/
10+
11+
package org.zowe.apiml.security.common.token;
12+
13+
import lombok.EqualsAndHashCode;
14+
import lombok.Getter;
15+
import org.springframework.security.authentication.AbstractAuthenticationToken;
16+
import org.zowe.apiml.security.common.login.LoginFilter;
17+
18+
import java.util.Collections;
19+
import java.util.Optional;
20+
21+
/**
22+
* This object is added to security context after successful authentication.
23+
* Contains username and valid JWT token.
24+
*/
25+
@EqualsAndHashCode(callSuper = false)
26+
public class TokenAuthentication_BCK extends AbstractAuthenticationToken {
27+
28+
private static final long serialVersionUID = 9187160928171618141L;
29+
30+
private final String username;
31+
private final String token;
32+
@Getter
33+
private Type type;
34+
35+
public TokenAuthentication_BCK(String token) {
36+
this(token, (Type) null);
37+
}
38+
39+
public TokenAuthentication_BCK(String token, Type type) {
40+
this(null, token, type);
41+
}
42+
43+
public TokenAuthentication_BCK(String username, String token) {
44+
this(username, token, (Type) null);
45+
}
46+
47+
public TokenAuthentication_BCK(String username, String token, Type type) {
48+
super(Collections.emptyList());
49+
this.username = username;
50+
this.token = token;
51+
this.type = type;
52+
}
53+
54+
/**
55+
* @return the token that prove the username is correct
56+
*/
57+
@Override
58+
public String getCredentials() {
59+
return token;
60+
}
61+
62+
/**
63+
* @return the username being authenticated
64+
*/
65+
@Override
66+
public String getPrincipal() {
67+
return username;
68+
}
69+
70+
/**
71+
* Creates the TokenAuthentication with fulfilled username (principal), token and marked as authenticated.
72+
* @param username Username, who is authenticated
73+
* @param token Token, which authenticate the user
74+
* @return TokenAuthentication marked as authenticated with username, token
75+
*/
76+
public static TokenAuthentication_BCK createAuthenticated(String username, String token, Type type) {
77+
final TokenAuthentication_BCK out = new TokenAuthentication_BCK(username, token, type);
78+
out.setAuthenticated(true);
79+
return out;
80+
}
81+
82+
@SuppressWarnings("squid:S3655")
83+
public static TokenAuthentication_BCK createAuthenticatedFromHeader(String token, String authHeader) {
84+
var loginRequest = LoginFilter.getCredentialFromAuthorizationHeader(Optional.of(authHeader));
85+
return createAuthenticated(loginRequest.get().getUsername(), token, Type.JWT);
86+
}
87+
88+
public enum Type {
89+
JWT,
90+
OIDC
91+
}
92+
93+
}

apiml/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ dependencies {
8585
testImplementation libs.rest.assured.web.test.client
8686
testImplementation libs.opentelemetry.sdk.testing
8787
testImplementation libs.opentelemetry.sdk.extension.autoconfigure.spi
88+
testImplementation libs.bundles.infinispan
8889

8990
compileOnly libs.lombok
9091
annotationProcessor libs.lombok
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.zowe.apiml.acceptance;
2+
3+
import com.netflix.discovery.EurekaClient;
4+
import com.netflix.discovery.converters.Auto;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.cache.CacheManager;
8+
9+
import org.infinispan.manager.EmbeddedCacheManager;
10+
11+
@AcceptanceTest
12+
public class CachesConfigurationTest {
13+
14+
@Autowired
15+
private CacheManager cacheManager;
16+
17+
@Autowired
18+
private EmbeddedCacheManager infinispanCacheManager;
19+
20+
@Test
21+
public void testCacheManager() {
22+
23+
24+
25+
System.out.println(">>>>>> ");
26+
}
27+
}

0 commit comments

Comments
 (0)