Search in sources :

Example 41 with ClientDetails

use of org.springframework.security.oauth2.provider.ClientDetails in project fw-cloud-framework by liuweijw.

the class AjaxLoginSuccessHandler method onAuthenticationSuccess.

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
    String header = request.getHeader(SecurityConstant.AUTHORIZATION);
    if (StringHelper.isBlank(header) || !header.startsWith(SecurityConstant.BASIC)) {
        throw new UnapprovedClientAuthenticationException("请求头中client信息为空");
    }
    try {
        String[] tokens = extractAndDecodeHeader(header);
        assert tokens.length == 2;
        String clientId = tokens[0];
        String clientSecret = tokens[1];
        JSONObject params = new JSONObject();
        params.put("clientId", clientId);
        params.put("clientSecret", clientSecret);
        params.put("authentication", authentication);
        ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientId);
        TokenRequest tokenRequest = new TokenRequest(MapUtil.newHashMap(), clientId, clientDetails.getScope(), "mobile");
        OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails);
        OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request, authentication);
        OAuth2AccessToken oAuth2AccessToken = authorizationServerTokenServices.createAccessToken(oAuth2Authentication);
        response.setCharacterEncoding(CommonConstant.UTF8);
        response.setContentType(CommonConstant.CONTENT_TYPE);
        PrintWriter printWriter = response.getWriter();
        printWriter.append(objectMapper.writeValueAsString(oAuth2AccessToken));
    } catch (IOException e) {
        throw new BadCredentialsException("Failed to decode basic authentication token");
    }
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) ClientDetails(org.springframework.security.oauth2.provider.ClientDetails) JSONObject(com.alibaba.fastjson.JSONObject) UnapprovedClientAuthenticationException(org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) TokenRequest(org.springframework.security.oauth2.provider.TokenRequest) IOException(java.io.IOException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) PrintWriter(java.io.PrintWriter)

Example 42 with ClientDetails

use of org.springframework.security.oauth2.provider.ClientDetails in project irida by phac-nml.

the class IridaWebSecurityConfig method tokenServices.

@Bean
@Primary
public ResourceServerTokenServices tokenServices(@Qualifier("clientDetails") ClientDetailsService clientDetails, @Qualifier("iridaTokenStore") TokenStore tokenStore) {
    DefaultTokenServices services = new DefaultTokenServices();
    services.setTokenStore(tokenStore);
    services.setSupportRefreshToken(true);
    services.setClientDetailsService(clientDetails);
    return services;
}
Also used : DefaultTokenServices(org.springframework.security.oauth2.provider.token.DefaultTokenServices) Primary(org.springframework.context.annotation.Primary) GenericFilterBean(org.springframework.web.filter.GenericFilterBean) Bean(org.springframework.context.annotation.Bean)

Example 43 with ClientDetails

use of org.springframework.security.oauth2.provider.ClientDetails in project irida by phac-nml.

the class UserRevListener method setClientId.

/**
 * Add the OAuth2 client ID to the revision listener if the user is
 * connecting via OAuth2
 *
 * @param entity
 *            The revision entity to modify if necessary
 */
private void setClientId(UserRevEntity entity) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    // OAuth2Authentication
    if (auth instanceof OAuth2Authentication) {
        try {
            logger.trace("Found OAuth2Authentication in session.  Storing clientId in revision.");
            OAuth2Authentication oAuth = (OAuth2Authentication) auth;
            String clientId = oAuth.getOAuth2Request().getClientId();
            IridaClientDetails clientDetails = clientRepo.loadClientDetailsByClientId(clientId);
            entity.setClientId(clientDetails.getId());
        } catch (NullPointerException ex) {
            throw new IllegalStateException("The OAuth2 client details are not in the session so it cannot be added to the revision.");
        }
    }
}
Also used : OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) IridaClientDetails(ca.corefacility.bioinformatics.irida.model.IridaClientDetails)

Example 44 with ClientDetails

use of org.springframework.security.oauth2.provider.ClientDetails in project seldon-core by SeldonIO.

the class ClientBuilder method build.

public ClientDetails build() {
    BaseClientDetails result = new BaseClientDetails();
    result.setClientId(clientId);
    result.setAuthorizedGrantTypes(authorizedGrantTypes);
    result.setAccessTokenValiditySeconds(accessTokenValiditySeconds);
    result.setRefreshTokenValiditySeconds(refreshTokenValiditySeconds);
    result.setRegisteredRedirectUri(registeredRedirectUris);
    result.setClientSecret(secret);
    result.setScope(scopes);
    result.setAuthorities(AuthorityUtils.createAuthorityList(authorities.toArray(new String[authorities.size()])));
    result.setResourceIds(resourceIds);
    result.setAdditionalInformation(additionalInformation);
    if (autoApprove) {
        result.setAutoApproveScopes(scopes);
    } else {
        result.setAutoApproveScopes(autoApproveScopes);
    }
    return result;
}
Also used : BaseClientDetails(org.springframework.security.oauth2.provider.client.BaseClientDetails)

Example 45 with ClientDetails

use of org.springframework.security.oauth2.provider.ClientDetails in project dhis2-core by dhis2.

the class DefaultClientDetailsUserDetailsService method loadUserByUsername.

public UserDetails loadUserByUsername(String username) {
    ClientDetails clientDetails;
    try {
        clientDetails = clientDetailsService.loadClientByClientId(username);
    } catch (NoSuchClientException e) {
        throw new UsernameNotFoundException(e.getMessage(), e);
    }
    String clientSecret = clientDetails.getClientSecret();
    if (clientSecret == null || clientSecret.trim().length() == 0) {
        clientSecret = emptyPassword;
    }
    return new User(username, clientSecret, clientDetails.getAuthorities());
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) ClientDetails(org.springframework.security.oauth2.provider.ClientDetails) User(org.springframework.security.core.userdetails.User) NoSuchClientException(org.springframework.security.oauth2.provider.NoSuchClientException)

Aggregations

ClientDetails (org.springframework.security.oauth2.provider.ClientDetails)44 Test (org.junit.Test)36 BaseClientDetails (org.springframework.security.oauth2.provider.client.BaseClientDetails)30 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)27 Authentication (org.springframework.security.core.Authentication)21 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)20 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)19 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)19 Date (java.util.Date)13 HashMap (java.util.HashMap)12 TokenRequest (org.springframework.security.oauth2.provider.TokenRequest)8 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)7 ClientDetailsService (org.springframework.security.oauth2.provider.ClientDetailsService)7 DBUnitTest (org.orcid.test.DBUnitTest)6 AnnotationConfigServletWebServerApplicationContext (org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext)6 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)6 OAuth2Exception (org.springframework.security.oauth2.common.exceptions.OAuth2Exception)6 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)6 OrcidOauth2ClientAuthentication (org.orcid.core.oauth.OrcidOauth2ClientAuthentication)5 InvalidClientException (org.springframework.security.oauth2.common.exceptions.InvalidClientException)5