Example usage for org.springframework.security.oauth2.provider OAuth2Authentication getUserAuthentication

List of usage examples for org.springframework.security.oauth2.provider OAuth2Authentication getUserAuthentication

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.provider OAuth2Authentication getUserAuthentication.

Prototype

public Authentication getUserAuthentication() 

Source Link

Document

The user authentication.

Usage

From source file:com.iflytek.edu.cloud.frame.utils.RestContextHolder.java

/**
 * password ????/* ww  w .j a  v  a  2  s . c o  m*/
 * 
 * @return
 */
public static String getUsername() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    if (authentication != null) {
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) authentication;
        if (oAuth2Authentication.getUserAuthentication() != null)
            return oAuth2Authentication.getUserAuthentication().getName();
    }

    return null;
}

From source file:org.zaizi.sensefy.api.utils.SensefyUserMapper.java

@SuppressWarnings("unchecked")
public static SensefyUser getSensefyUserFromPrincipal(Principal user) {

    SensefyUser sensefyUser = new SensefyUser();

    if (user != null) {
        OAuth2Authentication authUser = (OAuth2Authentication) user;
        if (authUser != null) {

            LinkedHashMap<String, Object> details = (LinkedHashMap<String, Object>) authUser
                    .getUserAuthentication().getDetails();

            if (details != null && !details.isEmpty() && details.containsKey("principal")) {
                LinkedHashMap<String, Object> principal = (LinkedHashMap<String, Object>) details
                        .get("principal");

                if (principal != null && !principal.isEmpty()) {
                    try {
                        BeanUtils.populate(sensefyUser, principal);
                    } catch (IllegalAccessException e) {
                        logger.debug(e.getMessage());
                    } catch (InvocationTargetException e) {
                        logger.debug(e.getMessage());
                    }/* w  w w .j a  v  a2 s.c  om*/
                }

            }

        }

    }
    return sensefyUser;
}

From source file:com.tce.spring.oauth2.controller.UserInfoEndpoint.java

@RequestMapping(value = "/userinfo")
@ResponseBody//from  ww w . java2  s.c o  m
public Object loginInfo(Principal principal) {
    OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) principal;
    return oAuth2Authentication.getUserAuthentication().getPrincipal();
}

From source file:org.cloudfoundry.identity.uaa.openid.UserInfoEndpoint.java

protected UaaPrincipal extractUaaPrincipal(OAuth2Authentication authentication) {
    Object object = authentication.getUserAuthentication().getPrincipal();
    if (object instanceof UaaPrincipal) {
        return (UaaPrincipal) object;
    }//  w  ww .j  ava  2s. c  o  m
    throw new IllegalStateException("User authentication could not be converted to UaaPrincipal");
}

From source file:io.curly.commons.github.GitHubAuthenticationMethodHandler.java

@SuppressWarnings("unchecked")
private User from(OAuth2Authentication auth) {
    Authentication userAuthentication = auth.getUserAuthentication();
    if (userAuthentication != null) {
        try {//from   ww  w. j  a v  a  2 s.co  m
            LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) userAuthentication.getDetails();
            return User.builder().id(String.valueOf(map.get("id")))
                    .avatarUrl(String.valueOf(map.get("avatar_url"))).email(String.valueOf(map.get("email")))
                    .name(String.valueOf(map.get("name"))).type(String.valueOf(map.get("type")))
                    .username(String.valueOf(map.get("login"))).build();

        } catch (ClassCastException e) {
            log.error("Cannot build User due to a ClassCastException {}", e);
        }
    }
    log.debug("Returning no user due to previous errors or no UserAuthentication detected");
    return null;
}

From source file:curly.commons.github.GitHubAuthenticationMethodHandler.java

@SuppressWarnings("unchecked")
private OctoUser from(OAuth2Authentication auth) {
    Authentication userAuthentication = auth.getUserAuthentication();
    if (userAuthentication != null) {
        try {//from   w w  w  .ja va  2s .  c om
            LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) userAuthentication.getDetails();
            return new OctoUser(String.valueOf(map.get("email")),
                    Boolean.valueOf(String.valueOf(map.get("hireable"))),
                    Long.valueOf(String.valueOf(map.get("id"))),
                    Integer.valueOf(String.valueOf(map.get("followers"))),
                    Integer.valueOf(String.valueOf(map.get("following"))),
                    Integer.valueOf(String.valueOf(map.get("public_repos"))),
                    String.valueOf(map.get("avatar_url")), String.valueOf(map.get("blog")),
                    String.valueOf(map.get("company")), null, String.valueOf(map.get("html_url")), null,
                    String.valueOf(map.get("login")), String.valueOf(map.get("name")),
                    String.valueOf(map.get("type")), String.valueOf(map.get("url")));

        } catch (ClassCastException e) {
            log.error("Cannot build OctoUser due to a ClassCastException {}", e);
        }
    }
    log.debug("Returning no user due to previous errors or no UserAuthentication detected");
    return null;
}

From source file:org.trustedanalytics.user.current.AuthDetailsFinder.java

@Override
public UUID findUserId(Authentication authentication) {
    if (authentication == null) {
        throw new IllegalArgumentException("Authentication argument must not be null");
    }/*from   w ww . j  a  v a  2 s .c  om*/
    OAuth2Authentication oauth2 = (OAuth2Authentication) authentication;
    AccessTokenDetails details = (AccessTokenDetails) oauth2.getUserAuthentication().getDetails();
    return details.getUserGuid();
}

From source file:com.example.ProxyAuthorizationServerTokenServices.java

@Override
public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) {
    Authentication user = authentication.getUserAuthentication();
    if (user instanceof CloudFoundryAuthentication) {
        return ((CloudFoundryAuthentication) user).getToken();
    }/*from  w  w w. ja va  2s .  c o m*/
    return null;
}

From source file:org.socialhistoryservices.pid.security.NAAuthentication.java

/**
 * Retrieves the user.//  www . jav a 2s  .  c  o m
 *
 * @return
 */
public List<String> authenticate() {

    final SecurityContext context = SecurityContextHolder.getContext();
    final OAuth2Authentication authentication = (OAuth2Authentication) context.getAuthentication();
    return NamingAuthority.getNaRole(authentication.getUserAuthentication());
}

From source file:org.osiam.auth.token.OsiamCompositeTokenGranter.java

public OAuth2AccessToken grant(String grantType, AuthorizationRequest authorizationRequest) {
    OAuth2AccessToken grant = super.grant(grantType, authorizationRequest);
    if (grant != null) {
        DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) grant;
        Map<String, Object> additionalInformation = new HashMap<String, Object>();
        additionalInformation.put("access_token", token.getValue());
        additionalInformation.put("expires_at", token.getExpiration());

        StringBuilder scopes = new StringBuilder();
        for (String scopeString : token.getScope()) {
            scopes.append(scopeString).append(" ");
        }/* w w  w . ja  v  a 2  s  .  co  m*/
        additionalInformation.put("scopes", scopes);

        if (token.getRefreshToken() != null) {
            DefaultExpiringOAuth2RefreshToken refreshToken = (DefaultExpiringOAuth2RefreshToken) token
                    .getRefreshToken();
            additionalInformation.put("refresh_token", refreshToken.getValue());
            additionalInformation.put("refresh_token_expires_at", refreshToken.getExpiration());
        }

        additionalInformation.put("token_type", token.getTokenType());
        additionalInformation.put("client_id", authorizationRequest.getClientId());

        OAuth2Authentication auth = tokenServices.loadAuthentication(token.getValue());

        if (auth.getUserAuthentication() != null && auth.getPrincipal() instanceof User) {
            User user = (User) auth.getPrincipal();
            additionalInformation.put("user_name", user.getUserName());
            additionalInformation.put("user_id", user.getId());
        }

        token.setAdditionalInformation(additionalInformation);
    }
    return grant;
}