Search in sources :

Example 1 with JwtToken

use of org.eclipse.vorto.repository.oauth.internal.JwtToken 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)

Aggregations

PublicKey (java.security.PublicKey)1 java.util (java.util)1 Supplier (java.util.function.Supplier)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 DefaultUserAccountService (org.eclipse.vorto.repository.account.impl.DefaultUserAccountService)1 IRole (org.eclipse.vorto.repository.domain.IRole)1 JWT_CLIENT_ID (org.eclipse.vorto.repository.oauth.BoschIDOAuthProvider.JWT_CLIENT_ID)1 JwtToken (org.eclipse.vorto.repository.oauth.internal.JwtToken)1 PublicKeyHelper (org.eclipse.vorto.repository.oauth.internal.PublicKeyHelper)1 SpringUserUtils (org.eclipse.vorto.repository.oauth.internal.SpringUserUtils)1 UserNamespaceRoleService (org.eclipse.vorto.repository.services.UserNamespaceRoleService)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Value (org.springframework.beans.factory.annotation.Value)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 Authentication (org.springframework.security.core.Authentication)1 InvalidTokenException (org.springframework.security.oauth2.common.exceptions.InvalidTokenException)1 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)1 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)1 Component (org.springframework.stereotype.Component)1 RestTemplate (org.springframework.web.client.RestTemplate)1