Search in sources :

Example 31 with InvalidTokenException

use of org.springframework.security.oauth2.common.exceptions.InvalidTokenException in project vorto by eclipse.

the class PublicKeyHelper method toPublicKey.

public static PublicKey toPublicKey(String mod, String exp) {
    try {
        Decoder urlDecoder = Base64.getUrlDecoder();
        BigInteger modulus = new BigInteger(1, urlDecoder.decode(mod));
        BigInteger publicExponent = new BigInteger(1, urlDecoder.decode(exp));
        KeyFactory kf = KeyFactory.getInstance("RSA");
        return kf.generatePublic(new RSAPublicKeySpec(modulus, publicExponent));
    } catch (Exception e) {
        throw new InvalidTokenException("Problem converting the common keys to public keys", e);
    }
}
Also used : InvalidTokenException(org.springframework.security.oauth2.common.exceptions.InvalidTokenException) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) Decoder(java.util.Base64.Decoder) KeyFactory(java.security.KeyFactory) InvalidTokenException(org.springframework.security.oauth2.common.exceptions.InvalidTokenException)

Example 32 with InvalidTokenException

use of org.springframework.security.oauth2.common.exceptions.InvalidTokenException in project vorto by eclipse.

the class BoschIoTSuiteOAuthProviderAuthCode method createAuthentication.

private OAuth2Authentication createAuthentication(JwtToken accessToken) {
    Map<String, Object> tokenPayload = accessToken.getPayloadMap();
    Optional<String> email = Optional.ofNullable((String) tokenPayload.get(JWT_EMAIL));
    Optional<String> name = Optional.ofNullable((String) tokenPayload.get(JWT_NAME)).map(str -> str.split("@")[0]);
    String userId = getUserId(tokenPayload).orElseThrow(() -> new InvalidTokenException("Cannot generate a userId from your provided token. Maybe 'sub' or 'client_id' is not present in JWT token?"));
    return Optional.ofNullable(userAccountService.getUser(userId)).map(user -> createAuthentication(this.clientId, userId, name.orElse(userId), email.orElse(null), userNamespaceRoleService.getRolesOnAllNamespaces(user))).orElse(null);
}
Also used : java.util(java.util) UserNamespaceRoleService(org.eclipse.vorto.repository.services.UserNamespaceRoleService) Autowired(org.springframework.beans.factory.annotation.Autowired) IRole(org.eclipse.vorto.repository.domain.IRole) PublicKey(java.security.PublicKey) SpringUserUtils(org.eclipse.vorto.repository.oauth.internal.SpringUserUtils) Supplier(java.util.function.Supplier) JwtToken(org.eclipse.vorto.repository.oauth.internal.JwtToken) Value(org.springframework.beans.factory.annotation.Value) PublicKeyHelper(org.eclipse.vorto.repository.oauth.internal.PublicKeyHelper) Component(org.springframework.stereotype.Component) HttpServletRequest(javax.servlet.http.HttpServletRequest) JWT_CLIENT_ID(org.eclipse.vorto.repository.oauth.BoschIDOAuthProvider.JWT_CLIENT_ID) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) DefaultUserAccountService(org.eclipse.vorto.repository.account.impl.DefaultUserAccountService) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Authentication(org.springframework.security.core.Authentication) InvalidTokenException(org.springframework.security.oauth2.common.exceptions.InvalidTokenException) RestTemplate(org.springframework.web.client.RestTemplate) OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) InvalidTokenException(org.springframework.security.oauth2.common.exceptions.InvalidTokenException)

Example 33 with InvalidTokenException

use of org.springframework.security.oauth2.common.exceptions.InvalidTokenException in project vorto by eclipse.

the class BoschIDOAuthProvider method createAuthentication.

/**
 * Authenticates the user from the CIAM issued token by checking if the user is registered in the
 * Repository
 */
@Override
public OAuth2Authentication createAuthentication(HttpServletRequest httpRequest, JwtToken accessToken) {
    Map<String, Object> tokenPayload = accessToken.getPayloadMap();
    Optional<String> email = Optional.ofNullable((String) tokenPayload.get(JWT_EMAIL));
    Optional<String> name = Optional.ofNullable((String) tokenPayload.get(JWT_NAME)).map(str -> str.split("@")[0]);
    String userId = getUserId(tokenPayload).orElseThrow(() -> new InvalidTokenException("Cannot generate a userId from your provided token. Maybe 'sub' or 'client_id' is not present in JWT token?"));
    User user = userAccountService.getUser(userId);
    if (user == null) {
        throw new InvalidTokenException("User from token is not a registered user in the repository!");
    }
    return createAuthentication(this.ciamClientId, userId, name.orElse(userId), email.orElse(null), userNamespaceRoleService.getRolesOnAllNamespaces(user));
}
Also used : InvalidTokenException(org.springframework.security.oauth2.common.exceptions.InvalidTokenException) User(org.eclipse.vorto.repository.domain.User)

Aggregations

InvalidTokenException (org.springframework.security.oauth2.common.exceptions.InvalidTokenException)33 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)11 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)8 Test (org.junit.Test)7 Date (java.util.Date)4 OrcidOauth2TokenDetail (org.orcid.persistence.jpa.entities.OrcidOauth2TokenDetail)4 DBUnitTest (org.orcid.test.DBUnitTest)4 IOException (java.io.IOException)3 Map (java.util.Map)3 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)3 Authentication (org.springframework.security.core.Authentication)3 Jwt (org.springframework.security.jwt.Jwt)3 OAuth2AccessDeniedException (org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException)3 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)3 OAuth2Exception (org.springframework.security.oauth2.common.exceptions.OAuth2Exception)3 PublicKey (java.security.PublicKey)2 HashSet (java.util.HashSet)2 Supplier (java.util.function.Supplier)2 OrcidOAuth2Authentication (org.orcid.core.oauth.OrcidOAuth2Authentication)2 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)2