com.alibaba.spring.boot.rsocket.broker.security.JwtPrincipal Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alibaba-broker-spring-boot-starter Show documentation
Show all versions of alibaba-broker-spring-boot-starter Show documentation
Alibaba RSocket Broker Spring Boot Starter for extension
package com.alibaba.spring.boot.rsocket.broker.security;
import com.alibaba.rsocket.utils.MurmurHash3;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import javax.security.auth.Subject;
import java.util.*;
/**
* principal with JWT backend
*
* @author leijuan
*/
public class JwtPrincipal implements RSocketAppPrincipal {
/**
* token id
*/
private String tokenId;
private Integer hashcode;
private String subject;
private List audience;
private Set roles;
private Set authorities;
private Set serviceAccounts;
private Set organizations;
public JwtPrincipal() {
}
public JwtPrincipal(DecodedJWT decodedJWT, String credentials) {
this.hashcode = MurmurHash3.hash32(credentials);
this.subject = decodedJWT.getSubject();
this.audience = decodedJWT.getAudience();
Map claims = decodedJWT.getClaims();
this.serviceAccounts = new HashSet<>(decodedJWT.getClaim("sas").asList(String.class));
this.organizations = new HashSet<>(decodedJWT.getClaim("orgs").asList(String.class));
if (claims.containsKey("roles")) {
this.roles = new HashSet<>(decodedJWT.getClaim("roles").asList(String.class));
}
if (claims.containsKey("authorities")) {
this.authorities = new HashSet<>(decodedJWT.getClaim("authorities").asList(String.class));
}
}
public JwtPrincipal(String id, String subject, List audience, Set roles, Set authorities, Set serviceAccounts, Set organizations) {
this.tokenId = id;
this.hashcode = MurmurHash3.hash32(subject);
this.subject = subject;
this.audience = audience;
this.roles = roles;
this.authorities = authorities;
this.serviceAccounts = serviceAccounts;
this.organizations = organizations;
}
@Override
public String getTokenId() {
return this.tokenId;
}
@Override
public String getName() {
return subject;
}
public String getSubject() {
return subject;
}
public List getAudience() {
return audience;
}
public Set getRoles() {
return roles == null ? Collections.emptySet() : roles;
}
public Set getAuthorities() {
return authorities == null ? Collections.emptySet() : authorities;
}
public Set getServiceAccounts() {
return serviceAccounts;
}
public Set getOrganizations() {
return organizations;
}
@Override
public boolean implies(Subject subject) {
return false;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
JwtPrincipal that = (JwtPrincipal) o;
return this.hashcode.equals(that.hashcode);
}
@Override
public int hashCode() {
return hashcode;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy