Replies: 8 comments 3 replies
-
/cc @sberyozkin |
Beta Was this translation helpful? Give feedback.
-
As far as I recall, Quarkus Amazon Lambda is not integrated with |
Beta Was this translation helpful? Give feedback.
-
@vladaman Just including |
Beta Was this translation helpful? Give feedback.
-
@sberyozkin we run Lambda behind AWS API Gateway (HTTP) and it does integration with Cognito. So we trust what @RequestScoped
public class CustomJsonWebToken implements Principal {
private String username;
private Set<String> groups;
private Set<String> scopes;
private Date authTime;
/**
* The "exp" (expiration time) claim identifies the expiration time on
* or after which the JWT MUST NOT be accepted for processing. The
* processing of the "exp" claim requires that the current date/time
* MUST be before the expiration date/time listed in the "exp" claim.
*/
private Date expireTime;
/**
* The "iat" (issued at) claim identifies the time at which the JWT was
* issued. This claim can be used to determine the age of the JWT. Its
* value MUST be a number containing a NumericDate value. Use of this
* claim is OPTIONAL.
*/
private Date issuedTime;
private String clientId;
/**
* The sub claim identifies the principal that is the subject of the JWT.
* In other other, it can hold the username of the user who you issued the token to.
*/
private String subject;
private String tokenUse;
private String issuer;
/**
* Example Payload in event.getRequestContext().getAuthorizer().getJwt();
* <p>
* APIGatewayV2HTTPEvent.RequestContext.Authorizer.JWT(
* claims={
* auth_time=1663606395,
* client_id=xxxx,
* cognito:groups=[super-admin eu-west-1_xxxx_Google],
* exp=1663609995,
* iat=1663606396,
* iss=https://cognito-idp.eu-west-1.amazonaws.com/eu-west-1_xxxx,
* jti=xxxxx,
* scope=aws.cognito.signin.user.admin phone openid profile email,
* sub=xxxx,
* token_use=access,
* username=google_xxxx65616,
* version=2}, scopes=null)
*
* @param event
*/
public CustomJsonWebToken(APIGatewayV2HTTPEvent event) {
APIGatewayV2HTTPEvent.RequestContext.Authorizer auth = event.getRequestContext().getAuthorizer();
if (auth == null) {
return;
}
final APIGatewayV2HTTPEvent.RequestContext.Authorizer.JWT jwt = event.getRequestContext().getAuthorizer().getJwt();
// Extract groups from JWT Token into a Set
if (jwt.getClaims().containsKey("cognito:groups")) {
final String[] grpArr = jwt.getClaims().get("cognito:groups").replaceAll("\\[|\\]", "").split("\\s");
this.groups = Arrays.stream(grpArr).collect(Collectors.toSet());
} else {
this.groups = Collections.emptySet();
}
this.scopes = Arrays.stream(jwt.getClaims().get("scope").split("\\s")).collect(Collectors.toSet());
this.username = jwt.getClaims().get("username");
this.authTime = new Date(Integer.parseInt(jwt.getClaims().get("auth_time")) * 1000L);
this.expireTime = new Date(Integer.parseInt(jwt.getClaims().get("exp")) * 1000L);
this.issuedTime = new Date(Integer.parseInt(jwt.getClaims().get("iat")) * 1000L);
this.clientId = jwt.getClaims().get("client_id");
this.tokenUse = jwt.getClaims().get("token_use");
this.subject = jwt.getClaims().get("sub");
this.issuer = jwt.getClaims().get("iss");
}
@Override
public String getName() {
return username;
}
@Override
public boolean implies(Subject subject) {
return Principal.super.implies(subject);
}
public String getSubject() {
return subject;
}
@Override
public String toString() {
return "CustomJsonWebToken{" +
"username='" + username + '\'' +
", groups=" + groups +
", scopes=" + scopes +
", authTime=" + authTime +
", expireTime=" + expireTime +
", issuedTime=" + issuedTime +
", clientId='" + clientId + '\'' +
", subject='" + subject + '\'' +
", tokenUse='" + tokenUse + '\'' +
", issuer='" + issuer + '\'' +
'}';
}
} |
Beta Was this translation helpful? Give feedback.
-
@vladaman Makes sense, this is a case where the incoming token has already been verified... It would be good though to have MP JWT |
Beta Was this translation helpful? Give feedback.
-
@vladaman Or may be you could also try |
Beta Was this translation helpful? Give feedback.
-
Have you seen this? https://quarkus.io/guides/amazon-lambda-http#custom-security-integration If you are using Cognito, the only thing the quarkus-amazon-lambda-http extension does is create a principal for you so that it can be propagated throughout the Quarkus security layers. If you are in need of RBAC, then you'll have to establish the SecurityIdentity yourself as per the docs. When I wrote this integration I was not sure if it was possible to map JWT claims with roles so you could do RBAC. Is there a reliable consistent way to map Cognito claims to Quarkus RBAC? |
Beta Was this translation helpful? Give feedback.
-
Hi @patriot1burke From the code example above,
seems most relevant, perhaps |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
We have integrated AWS API Gateway HTTP API + Cognito and JWT. When we include
Bearer
keyword in Authorization header we get 401 error from Quarkus.Returns 401 error
Response:
Following call succeeds (note, Bearer keyword is not included) but JsonWebToken is null:
application.properties:
How do I configure Quarkus to accept Authorization header via AWS API Gateway if it includes "Bearer"?
Beta Was this translation helpful? Give feedback.
All reactions