|
10 | 10 |
|
11 | 11 | package org.zowe.apiml.security.common.token; |
12 | 12 |
|
| 13 | +import com.nimbusds.jwt.*; |
13 | 14 | import lombok.EqualsAndHashCode; |
14 | 15 | import lombok.Getter; |
| 16 | +import lombok.extern.slf4j.Slf4j; |
15 | 17 | import org.springframework.security.authentication.AbstractAuthenticationToken; |
16 | | -import org.zowe.apiml.security.common.login.LoginFilter; |
| 18 | +import org.springframework.security.core.parameters.P; |
17 | 19 |
|
| 20 | +import java.io.Serial; |
| 21 | +import java.text.ParseException; |
18 | 22 | import java.util.Collections; |
19 | | -import java.util.Optional; |
| 23 | +import java.util.Date; |
| 24 | +import java.util.List; |
20 | 25 |
|
21 | 26 | /** |
22 | 27 | * This object is added to security context after successful authentication. |
23 | 28 | * Contains username and valid JWT token. |
24 | 29 | */ |
25 | 30 | @EqualsAndHashCode(callSuper = false) |
| 31 | +@Slf4j |
26 | 32 | public class TokenAuthentication extends AbstractAuthenticationToken { |
27 | 33 |
|
| 34 | + @Serial |
| 35 | + //TODO: regenerate |
28 | 36 | private static final long serialVersionUID = 9187160928171618141L; |
29 | 37 |
|
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 | + |
32 | 45 | @Getter |
33 | 46 | private Type type; |
34 | 47 |
|
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; |
37 | 55 | } |
38 | 56 |
|
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 | + } |
41 | 68 | } |
42 | 69 |
|
43 | | - public TokenAuthentication(String username, String token) { |
44 | | - this(username, token, (Type) null); |
| 70 | + public JWT getJwt() { |
| 71 | + return jwt; |
45 | 72 | } |
46 | 73 |
|
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(); |
52 | 84 | } |
53 | 85 |
|
| 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 | + |
54 | 108 | /** |
55 | 109 | * @return the token that prove the username is correct |
56 | 110 | */ |
57 | 111 | @Override |
58 | 112 | public String getCredentials() { |
59 | | - return token; |
| 113 | + return jwt.getParsedString(); |
60 | 114 | } |
61 | 115 |
|
62 | 116 | /** |
63 | 117 | * @return the username being authenticated |
64 | 118 | */ |
65 | 119 | @Override |
66 | 120 | public String getPrincipal() { |
67 | | - return username; |
| 121 | + return queryResponse.getUserId(); |
68 | 122 | } |
69 | 123 |
|
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 | +// } |
81 | 135 |
|
82 | 136 | @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 | +// } |
87 | 141 |
|
88 | 142 | public enum Type { |
89 | 143 | JWT, |
90 | 144 | OIDC |
91 | 145 | } |
92 | 146 |
|
| 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 | + } |
93 | 167 | } |
0 commit comments