use of org.maxkey.authz.oauth2.provider.OAuth2Authentication in project epoch by Marshal7cc.
the class RequestHelper method getCurrentUser.
public static User getCurrentUser() {
// todo: fix better
User user = RequestHelper.currentUser.get();
if (user == null) {
SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
if (!(authentication instanceof OAuth2Authentication)) {
return ANONYMOUS_USER;
}
Authentication userAuthentication = ((OAuth2Authentication) authentication).getUserAuthentication();
if (!(userAuthentication.getDetails() instanceof Map)) {
return ANONYMOUS_USER;
}
Map<String, Object> details = Collections.unmodifiableMap((Map<String, Object>) userAuthentication.getDetails());
LinkedHashMap<String, String> principal = (LinkedHashMap<String, String>) details.get(FILED_PRINCIPAL);
user = new User();
user.setUserId(Long.parseLong(String.valueOf(principal.get(User.FILED_USER_ID))));
user.setUsername(principal.get(User.FILED_USER_NAME));
setCurrentUser(user);
return user;
}
return user;
}
use of org.maxkey.authz.oauth2.provider.OAuth2Authentication in project ballcat by ballcat-projects.
the class CustomRedisTokenStore method removeAccessToken.
public void removeAccessToken(String tokenValue) {
byte[] accessKey = serializeKey(ACCESS + tokenValue);
byte[] authKey = serializeKey(AUTH + tokenValue);
byte[] accessToRefreshKey = serializeKey(ACCESS_TO_REFRESH + tokenValue);
RedisConnection conn = getConnection();
try {
conn.openPipeline();
conn.get(accessKey);
conn.get(authKey);
conn.del(accessKey);
conn.del(accessToRefreshKey);
// Don't remove the refresh token - it's up to the caller to do that
conn.del(authKey);
List<Object> results = conn.closePipeline();
byte[] access = (byte[]) results.get(0);
byte[] auth = (byte[]) results.get(1);
// ==== 当序列化异常时,删除缓存 key ====
OAuth2Authentication authentication = null;
try {
authentication = deserializeAuthentication(auth);
} catch (SerializationException e) {
log.warn("[removeAccessToken] OAuth2Authentication 序列化异常", e);
}
if (authentication != null) {
String key = authenticationKeyGenerator.extractKey(authentication);
byte[] authToAccessKey = serializeKey(AUTH_TO_ACCESS + key);
byte[] unameKey = serializeKey(UNAME_TO_ACCESS + getApprovalKey(authentication));
byte[] clientId = serializeKey(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId());
conn.openPipeline();
conn.del(authToAccessKey);
conn.sRem(unameKey, access);
conn.sRem(clientId, access);
conn.del(serialize(ACCESS + key));
conn.closePipeline();
}
} finally {
conn.close();
}
}
use of org.maxkey.authz.oauth2.provider.OAuth2Authentication in project ballcat by ballcat-projects.
the class SharedStoredOpaqueTokenIntrospector method introspect.
/**
* @see DefaultTokenServices#loadAuthentication(java.lang.String)
* @param accessTokenValue token
* @return OAuth2User
*/
@Override
public OAuth2AuthenticatedPrincipal introspect(String accessTokenValue) {
OAuth2AccessToken accessToken = tokenStore.readAccessToken(accessTokenValue);
if (accessToken == null) {
throw new BadOpaqueTokenException("Invalid access token: " + accessTokenValue);
} else if (accessToken.isExpired()) {
tokenStore.removeAccessToken(accessToken);
throw new BadOpaqueTokenException("Access token expired: " + accessTokenValue);
}
OAuth2Authentication oAuth2Authentication = tokenStore.readAuthentication(accessToken);
if (oAuth2Authentication == null) {
// in case of race condition
throw new BadOpaqueTokenException("Invalid access token: " + accessTokenValue);
}
ClientPrincipal clientPrincipal = getClientPrincipal(oAuth2Authentication);
if (clientPrincipal != null) {
return clientPrincipal;
}
return (OAuth2User) oAuth2Authentication.getPrincipal();
}
use of org.maxkey.authz.oauth2.provider.OAuth2Authentication in project ballcat by ballcat-projects.
the class CustomTokenEnhancer method enhance.
/**
* 处理 token 增强
* @param accessToken token信息
* @param authentication 鉴权信息
* @return OAuth2AccessToken 增强后的token
*/
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
Authentication userAuthentication = authentication.getUserAuthentication();
if (userAuthentication == null) {
return accessToken;
}
Object principal = userAuthentication.getPrincipal();
if (principal instanceof User) {
User user = (User) principal;
// token 附属信息
Map<String, Object> additionalInfo = new HashMap<>(8);
// 用户基本信息
SysUserInfo sysUserInfo = getSysUserInfo(user);
additionalInfo.put(TokenAttributeNameConstants.INFO, sysUserInfo);
// 默认在登陆时只把角色和权限的信息返回
Map<String, Object> resultAttributes = new HashMap<>(2);
Map<String, Object> attributes = user.getAttributes();
resultAttributes.put(UserAttributeNameConstants.ROLE_CODES, attributes.get(UserAttributeNameConstants.ROLE_CODES));
resultAttributes.put(UserAttributeNameConstants.PERMISSIONS, attributes.get(UserAttributeNameConstants.PERMISSIONS));
additionalInfo.put(TokenAttributeNameConstants.ATTRIBUTES, resultAttributes);
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
}
return accessToken;
}
use of org.maxkey.authz.oauth2.provider.OAuth2Authentication in project JBM by numen06.
the class OpenJwtTokenService method loadAuthentication.
@Override
public OAuth2Authentication loadAuthentication(String accessToken) throws AuthenticationException, InvalidTokenException {
OAuth2Authentication oAuth2Authentication = tokenStore.readAuthentication(accessToken);
Map<String, ?> map = jwtAccessTokenConverter.convertAccessToken(readAccessToken(accessToken), oAuth2Authentication);
return defaultAccessTokenConverter.extractAuthentication(map);
}
Aggregations