Search in sources :

Example 36 with OAuth2Authentication

use of org.maxkey.authz.oauth2.provider.OAuth2Authentication in project JBM by numen06.

the class JbmClusterHelper method updateOpenClientAuthorities.

/**
 * 更新客户端权限
 * @param tokenStore
 * @param clientId
 * @param authorities
 */
public static void updateOpenClientAuthorities(TokenStore tokenStore, String clientId, Collection<? extends GrantedAuthority> authorities) {
    if (authorities == null) {
        return;
    }
    // 动态更新客户端生成的token
    Collection<OAuth2AccessToken> accessTokens = tokenStore.findTokensByClientId(clientId);
    if (accessTokens != null && !accessTokens.isEmpty()) {
        Iterator<OAuth2AccessToken> iterator = accessTokens.iterator();
        while (iterator.hasNext()) {
            OAuth2AccessToken token = iterator.next();
            OAuth2Authentication oAuth2Authentication = tokenStore.readAuthentication(token);
            if (oAuth2Authentication != null && oAuth2Authentication.isClientOnly()) {
                // 只更新客户端权限
                // 由于没有set方法,使用反射机制强制赋值
                BeanUtil.setFieldValue(oAuth2Authentication, "authorities", authorities);
                // 重新保存
                tokenStore.storeAccessToken(token, oAuth2Authentication);
            }
        }
    }
}
Also used : OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication)

Example 37 with OAuth2Authentication

use of org.maxkey.authz.oauth2.provider.OAuth2Authentication in project JBM by numen06.

the class JbmRedisTokenStore method removeAccessToken.

public void removeAccessToken(String tokenValue) {
    OAuth2AccessToken removed = (OAuth2AccessToken) redisTemplate.opsForValue().get(ACCESS + tokenValue);
    // caller to do that
    OAuth2Authentication authentication = (OAuth2Authentication) this.redisTemplate.opsForValue().get(AUTH + tokenValue);
    this.redisTemplate.delete(AUTH + tokenValue);
    redisTemplate.delete(ACCESS + tokenValue);
    this.redisTemplate.delete(ACCESS_TO_REFRESH + tokenValue);
    if (authentication != null) {
        this.redisTemplate.delete(AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication));
        String clientId = authentication.getOAuth2Request().getClientId();
        redisTemplate.opsForList().leftPop(UNAME_TO_ACCESS + getApprovalKey(clientId, authentication.getName()));
        redisTemplate.opsForList().leftPop(CLIENT_ID_TO_ACCESS + clientId);
        this.redisTemplate.delete(AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication));
    }
}
Also used : OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication)

Example 38 with OAuth2Authentication

use of org.maxkey.authz.oauth2.provider.OAuth2Authentication in project conformance-suite by openid-certification.

the class OIDCAuthenticationFacade method getPrincipal.

@Override
public ImmutableMap<String, String> getPrincipal() {
    OIDCAuthenticationToken token = getOIDC();
    OAuth2Authentication auth = getOAuth();
    if (token != null) {
        @SuppressWarnings("unchecked") ImmutableMap<String, String> prinicipal = (ImmutableMap<String, String>) token.getPrincipal();
        return prinicipal;
    } else if (auth != null) {
        // TODO: we might be able to build this off of other properties instead
        @SuppressWarnings("unchecked") ImmutableMap<String, String> prinicipal = (ImmutableMap<String, String>) auth.getPrincipal();
        return prinicipal;
    }
    return null;
}
Also used : OIDCAuthenticationToken(org.mitre.openid.connect.model.OIDCAuthenticationToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 39 with OAuth2Authentication

use of org.maxkey.authz.oauth2.provider.OAuth2Authentication in project conformance-suite by openid-certification.

the class OIDCAuthenticationFacade method getDisplayName.

@Override
public String getDisplayName() {
    OIDCAuthenticationToken token = getOIDC();
    OAuth2Authentication auth = getOAuth();
    if (token != null) {
        Map<String, String> principal = getPrincipal();
        if (principal != null) {
            String displayName = principal.toString();
            UserInfo userInfo = getUserInfo();
            if (userInfo != null) {
                if (!Strings.isNullOrEmpty(userInfo.getEmail())) {
                    displayName = userInfo.getEmail();
                } else if (!Strings.isNullOrEmpty(userInfo.getPreferredUsername())) {
                    displayName = userInfo.getPreferredUsername();
                } else if (!Strings.isNullOrEmpty(userInfo.getName())) {
                    displayName = userInfo.getName();
                }
                return displayName;
            }
            return displayName;
        }
    } else if (auth != null) {
        return auth.getName();
    }
    return "";
}
Also used : OIDCAuthenticationToken(org.mitre.openid.connect.model.OIDCAuthenticationToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) UserInfo(org.mitre.openid.connect.model.UserInfo)

Example 40 with OAuth2Authentication

use of org.maxkey.authz.oauth2.provider.OAuth2Authentication in project conformance-suite by openid-certification.

the class ApiTokenService method checkToken.

@SuppressWarnings("rawtypes")
private TokenInfo checkToken(String accessToken) {
    Map tokenInfoMap = tokenService.findToken(accessToken);
    if (tokenInfoMap == null) {
        return null;
    }
    JsonObject tokenInfoObj = (JsonObject) new Gson().toJsonTree(tokenInfoMap);
    OAuth2AccessToken token = new LocalOAuth2AccessToken(tokenInfoObj);
    OAuth2Request request = new OAuth2Request(Collections.emptyMap(), "", null, !token.isExpired(), Collections.emptySet(), null, null, null, null);
    OAuth2Authentication auth = new OAuth2Authentication(request, createAuth(tokenInfoObj));
    return new TokenInfo(token, auth);
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) Map(java.util.Map)

Aggregations

OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)538 Authentication (org.springframework.security.core.Authentication)211 Test (org.junit.Test)192 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)177 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)159 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)107 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)91 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)68 HashMap (java.util.HashMap)67 Date (java.util.Date)47 IsEmptyString.isEmptyString (org.hamcrest.text.IsEmptyString.isEmptyString)42 GrantedAuthority (org.springframework.security.core.GrantedAuthority)35 Map (java.util.Map)32 OAuth2RefreshToken (org.springframework.security.oauth2.common.OAuth2RefreshToken)30 BaseClientDetails (org.springframework.security.oauth2.provider.client.BaseClientDetails)30 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)29 InvalidTokenException (org.springframework.security.oauth2.common.exceptions.InvalidTokenException)28 OAuth2Authentication (org.maxkey.authz.oauth2.provider.OAuth2Authentication)27 UaaAuthentication (org.cloudfoundry.identity.uaa.authentication.UaaAuthentication)26 HashSet (java.util.HashSet)23