Skip to content

Commit

Permalink
using sub from profile instead of token
Browse files Browse the repository at this point in the history
  • Loading branch information
dankle committed Sep 25, 2023
1 parent f8283c4 commit e2ebca6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,7 @@ public boolean isTokenValid(String token, String nonce) {
}
}

/**
* Validates the token and returns the corresponding user login.
*
* @param token the JWT
* @return corresponding user login or <code>null</code> if the JWT is invalid
*/
public String validateTokenAndGetLogin(String token) {
Map<String, Object> claims = validateTokenAndGetClaims(token);
if (claims == null) {
return null;
}
return (String) claims.get("sub");
}

private Map<String, Object> validateTokenAndGetClaims(String token) {

public Map<String, Object> validateTokenAndGetClaims(String token) {
try {
JWT jwt = validateToken(token);
if (jwt == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public String getTokenSigningKey() throws IOException {
}
}

public Profile getUserProfileFromRefreshToken(String refreshToken) throws IOException {
public Profile getUserProfileByRefreshToken(String refreshToken) throws IOException {
Token token = getTokenByRefreshToken(refreshToken);
return getProfile(token.accessToken());
}
Expand Down Expand Up @@ -240,6 +240,9 @@ public interface Token {
@JsonIgnoreProperties(ignoreUnknown = true)
public interface Profile {

@JsonProperty("sub")
String sub();

@JsonProperty("sAMAccountName")
String userId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,32 @@ public AuthenticationToken createToken(ServletRequest request, ServletResponse r
HttpServletRequest req = WebUtils.toHttp(request);

String bearerToken = extractTokenFromRequest(req);
String incomingToken = bearerToken != null ? bearerToken : SsoCookies.getTokenCookie(req);
String token = bearerToken != null ? bearerToken : SsoCookies.getTokenCookie(req);

if (incomingToken == null) {
if (token == null) {
return null;
}

String login = jwtAuthenticator.validateTokenAndGetLogin(incomingToken);
if (login == null) {
if (!jwtAuthenticator.isTokenValid(token)) {
return null;
}

String[] as = parseDomain(login);

SsoClient.Profile profile;
try {
profile = bearerToken != null ? ssoClient.getProfile(bearerToken) : ssoClient.getUserProfileFromRefreshToken(incomingToken);
SsoClient.Profile profile = bearerToken != null ? ssoClient.getProfile(bearerToken) :
ssoClient.getUserProfileByRefreshToken(SsoCookies.getRefreshCookie(req));

if (profile == null) {
return null;
}

String[] as = parseDomain(profile.sub());

return new SsoToken(as[0], as[1], profile.displayName(), profile.mail(), profile.userPrincipalName(), profile.nameInNamespace(), profile.groups());

} catch (IOException e) {

return null;
}
if (profile == null) {
return null;
}
return new SsoToken(as[0], as[1], profile.displayName(), profile.mail(), profile.userPrincipalName(), profile.nameInNamespace(), profile.groups());
}

@Override
Expand Down

0 comments on commit e2ebca6

Please sign in to comment.