Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated Jwt-related functionality, replace by a different library #11063

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,11 @@
<version>${apache_httpclient.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>nimbus-jose-jwt</artifactId>
<version>9.41.2</version>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
*/

package org.cbioportal.security.token.oauth2;

import java.util.Map;
import java.util.Collection;
import java.io.IOException;

import com.nimbusds.jwt.JWTParser;
import com.nimbusds.jwt.JWT;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.cbioportal.security.util.ClaimRoleExtractorUtil;
Expand All @@ -42,11 +48,9 @@
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;

import java.io.IOException;
import java.util.Collection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OAuth2TokenAuthenticationProvider implements AuthenticationProvider {

Expand All @@ -55,8 +59,10 @@ public class OAuth2TokenAuthenticationProvider implements AuthenticationProvider

private final OAuth2TokenRefreshRestTemplate tokenRefreshRestTemplate;

private static final Logger LOG = LoggerFactory.getLogger(OAuth2TokenAuthenticationProvider.class);

public OAuth2TokenAuthenticationProvider(OAuth2TokenRefreshRestTemplate tokenRefreshRestTemplate) {
this.tokenRefreshRestTemplate = tokenRefreshRestTemplate;
this.tokenRefreshRestTemplate = tokenRefreshRestTemplate;
}
@Override
public boolean supports(Class<?> authentication) {
Expand All @@ -76,39 +82,31 @@ public Authentication authenticate(Authentication authentication) throws Authent

Collection<GrantedAuthority> authorities = extractAuthorities(accessToken);
String username = getUsername(accessToken);

return new OAuth2BearerAuthenticationToken(username, authorities);
}

// Read roles/authorities from JWT token.
private Collection<GrantedAuthority> extractAuthorities(final String token) throws BadCredentialsException {
try {
final Jwt tokenDecoded = JwtHelper.decode(token);
final String claims = tokenDecoded.getClaims();
final JWT tokenDecoded = JWTParser.parse(token);
final Map<String, Object> claims = tokenDecoded.getJWTClaimsSet().toJSONObject();
return GrantedAuthorityUtil.generateGrantedAuthoritiesFromRoles(ClaimRoleExtractorUtil.extractClientRoles(claims, jwtRolesPath));

} catch (Exception e) {
throw new BadCredentialsException("Authorities could not be extracted from access token.");
throw new BadCredentialsException("Authorities could not be extracted from access token: " + e.getMessage());
}
}

private String getUsername(final String token) {

final Jwt tokenDecoded = JwtHelper.decode(token);

final String claims = tokenDecoded.getClaims();
JsonNode claimsMap;
try {
claimsMap = new ObjectMapper().readTree(claims);
} catch (IOException e) {
throw new BadCredentialsException("User name could not be found in access token.");
}

if (! claimsMap.has("sub")) {
throw new BadCredentialsException("User name could not be found in access token.");
final JWT tokenDecoded = JWTParser.parse(token);
final Object sub = tokenDecoded.getJWTClaimsSet().getClaim("sub");
return (sub instanceof String) ? (String) sub : null;
} catch (Exception e) {
LOG.warn("Failed to parse token: authentiaction failed!", e);
return null;
}

return claimsMap.get("sub").asText();
}

}