Skip to content
This repository has been archived by the owner on May 22, 2021. It is now read-only.

Commit

Permalink
Added Jwt as parameter of methods
Browse files Browse the repository at this point in the history
  • Loading branch information
PAException committed May 23, 2020
1 parent 366b0e3 commit 84f2331
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
14 changes: 14 additions & 0 deletions auth/src/main/java/com/gewia/common/auth/jwt/Jwt.java
Original file line number Diff line number Diff line change
@@ -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;

}
4 changes: 4 additions & 0 deletions auth/src/main/java/com/gewia/common/auth/jwt/JwtScopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ public boolean getResult() {
return containing;
}

public boolean hasScope(String scope) {
return this.scopes.contains(scope);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public abstract class SpringAuthentication implements InitializingBean {

@Override
public void afterPropertiesSet() {
interceptors = this.addAuthenticationInterceptors(interceptors);
this.addAuthenticationInterceptors(interceptors);
}

abstract public List<HandlerInterceptorAdapter> addAuthenticationInterceptors(List<HandlerInterceptorAdapter> authenticationInterceptors);
abstract public void addAuthenticationInterceptors(List<HandlerInterceptorAdapter> authenticationInterceptors);

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 84f2331

Please sign in to comment.