001/******************************************************************************* 002 * Copyright 2014 The MITRE Corporation 003 * and the MIT Kerberos and Internet Trust Consortium 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 ******************************************************************************/ 017package org.mitre.oauth2.service.impl; 018 019import java.math.BigInteger; 020import java.security.SecureRandom; 021import java.util.Collection; 022import java.util.HashSet; 023 024import org.mitre.oauth2.model.ClientDetailsEntity; 025import org.mitre.oauth2.model.ClientDetailsEntity.AuthMethod; 026import org.mitre.oauth2.service.ClientDetailsEntityService; 027import org.springframework.beans.factory.annotation.Autowired; 028import org.springframework.security.core.GrantedAuthority; 029import org.springframework.security.core.authority.SimpleGrantedAuthority; 030import org.springframework.security.core.userdetails.User; 031import org.springframework.security.core.userdetails.UserDetails; 032import org.springframework.security.core.userdetails.UserDetailsService; 033import org.springframework.security.core.userdetails.UsernameNotFoundException; 034import org.springframework.stereotype.Service; 035 036import com.google.common.base.Strings; 037 038/** 039 * Shim layer to convert a ClientDetails service into a UserDetails service 040 * 041 * @author AANGANES 042 * 043 */ 044@Service("clientUserDetailsService") 045public class DefaultClientUserDetailsService implements UserDetailsService { 046 047 private static GrantedAuthority ROLE_CLIENT = new SimpleGrantedAuthority("ROLE_CLIENT"); 048 049 @Autowired 050 private ClientDetailsEntityService clientDetailsService; 051 052 @Override 053 public UserDetails loadUserByUsername(String clientId) throws UsernameNotFoundException { 054 055 ClientDetailsEntity client = clientDetailsService.loadClientByClientId(clientId); 056 057 if (client != null) { 058 059 String password = Strings.nullToEmpty(client.getClientSecret()); 060 061 if (client.getTokenEndpointAuthMethod() != null && 062 (client.getTokenEndpointAuthMethod().equals(AuthMethod.PRIVATE_KEY) || 063 client.getTokenEndpointAuthMethod().equals(AuthMethod.SECRET_JWT))) { 064 065 // Issue a random password each time to prevent password auth from being used (or skipped) 066 // for private key or shared key clients, see #715 067 068 password = new BigInteger(512, new SecureRandom()).toString(16); 069 } 070 071 boolean enabled = true; 072 boolean accountNonExpired = true; 073 boolean credentialsNonExpired = true; 074 boolean accountNonLocked = true; 075 Collection<GrantedAuthority> authorities = new HashSet<GrantedAuthority>(client.getAuthorities()); 076 authorities.add(ROLE_CLIENT); 077 078 return new User(clientId, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities); 079 } else { 080 throw new UsernameNotFoundException("Client not found: " + clientId); 081 } 082 083 } 084 085 public ClientDetailsEntityService getClientDetailsService() { 086 return clientDetailsService; 087 } 088 089 public void setClientDetailsService(ClientDetailsEntityService clientDetailsService) { 090 this.clientDetailsService = clientDetailsService; 091 } 092 093}