diff --git a/auth/src/main/java/com/gewia/common/auth/jwt/Jwt.java b/auth/src/main/java/com/gewia/common/auth/jwt/Jwt.java new file mode 100644 index 0000000..fd4f8f3 --- /dev/null +++ b/auth/src/main/java/com/gewia/common/auth/jwt/Jwt.java @@ -0,0 +1,14 @@ +package com.gewia.common.auth.jwt; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import java.util.UUID; + +@Getter +@AllArgsConstructor +public class Jwt { + + private final UUID userId; + private final JwtScopes userScopes; + +} diff --git a/auth/src/main/java/com/gewia/common/auth/jwt/JwtScopes.java b/auth/src/main/java/com/gewia/common/auth/jwt/JwtScopes.java index 3a5bfcb..160bacd 100644 --- a/auth/src/main/java/com/gewia/common/auth/jwt/JwtScopes.java +++ b/auth/src/main/java/com/gewia/common/auth/jwt/JwtScopes.java @@ -36,4 +36,8 @@ public boolean getResult() { return containing; } + public boolean hasScope(String scope) { + return this.scopes.contains(scope); + } + } diff --git a/spring-auth/src/main/java/com/gewia/common/spring/auth/SpringAuthentication.java b/spring-auth/src/main/java/com/gewia/common/spring/auth/SpringAuthentication.java index 594a060..219c068 100644 --- a/spring-auth/src/main/java/com/gewia/common/spring/auth/SpringAuthentication.java +++ b/spring-auth/src/main/java/com/gewia/common/spring/auth/SpringAuthentication.java @@ -15,9 +15,9 @@ public abstract class SpringAuthentication implements InitializingBean { @Override public void afterPropertiesSet() { - interceptors = this.addAuthenticationInterceptors(interceptors); + this.addAuthenticationInterceptors(interceptors); } - abstract public List addAuthenticationInterceptors(List authenticationInterceptors); + abstract public void addAuthenticationInterceptors(List authenticationInterceptors); } diff --git a/spring-auth/src/main/java/com/gewia/common/spring/auth/SpringAuthenticationWebConfig.java b/spring-auth/src/main/java/com/gewia/common/spring/auth/SpringAuthenticationWebConfig.java index 4daa459..afb427b 100644 --- a/spring-auth/src/main/java/com/gewia/common/spring/auth/SpringAuthenticationWebConfig.java +++ b/spring-auth/src/main/java/com/gewia/common/spring/auth/SpringAuthenticationWebConfig.java @@ -1,8 +1,11 @@ package com.gewia.common.spring.auth; -import com.auth0.jwt.interfaces.DecodedJWT; import java.util.List; +import java.util.UUID; import javax.servlet.http.HttpServletRequest; +import com.auth0.jwt.interfaces.DecodedJWT; +import com.gewia.common.auth.jwt.Jwt; +import com.gewia.common.auth.jwt.JwtScopes; import org.springframework.context.annotation.Configuration; import org.springframework.core.MethodParameter; import org.springframework.web.bind.support.WebDataBinderFactory; @@ -20,12 +23,15 @@ public class SpringAuthenticationWebConfig implements WebMvcConfigurer, HandlerM @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { - return ((HttpServletRequest) webRequest.getNativeRequest()).getAttribute("accessToken"); + DecodedJWT decodedJWT = (DecodedJWT) ((HttpServletRequest) webRequest.getNativeRequest()).getAttribute("accessToken"); + + return new Jwt(UUID.fromString(decodedJWT.getClaim("userId").asString()), + new JwtScopes(decodedJWT.getClaim("scopes").asList(String.class))); } @Override public boolean supportsParameter(MethodParameter parameter) { - return parameter.getParameterType().equals(DecodedJWT.class); + return parameter.getParameterType().equals(Jwt.class); } @Override