1 |
59336
|
k.triantaf
|
package eu.dnetlib.uoaauthorizationlibrary.security;
|
2 |
|
|
|
3 |
|
|
import eu.dnetlib.uoaauthorizationlibrary.utils.AuthorizationUtils;
|
4 |
|
|
import eu.dnetlib.uoaauthorizationlibrary.utils.UserInfo;
|
5 |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
6 |
|
|
import org.springframework.security.core.GrantedAuthority;
|
7 |
|
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
8 |
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
9 |
|
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
10 |
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
11 |
|
|
import org.springframework.stereotype.Service;
|
12 |
|
|
|
13 |
|
|
import java.util.ArrayList;
|
14 |
|
|
import java.util.List;
|
15 |
|
|
|
16 |
|
|
@Service
|
17 |
|
|
public class UserDetailsServiceImpl implements UserDetailsService {
|
18 |
|
|
|
19 |
|
|
private AuthorizationUtils utils;
|
20 |
|
|
|
21 |
|
|
@Autowired
|
22 |
|
|
public UserDetailsServiceImpl(AuthorizationUtils utils) {
|
23 |
|
|
this.utils = utils;
|
24 |
|
|
}
|
25 |
|
|
|
26 |
|
|
private List<GrantedAuthority> getAuthorities(List<String> roles) {
|
27 |
|
|
List<GrantedAuthority> authorities = new ArrayList<>();
|
28 |
|
|
for(String role : roles) {
|
29 |
|
|
authorities.add(new SimpleGrantedAuthority(role));
|
30 |
|
|
}
|
31 |
|
|
return authorities;
|
32 |
|
|
}
|
33 |
|
|
|
34 |
|
|
@Override
|
35 |
|
|
public UserDetails loadUserByUsername(String token) throws UsernameNotFoundException {
|
36 |
|
|
final UserInfo user = utils.getUserInfo(token);
|
37 |
|
|
|
38 |
|
|
if (user == null) {
|
39 |
|
|
throw new UsernameNotFoundException("invalid token: " + token);
|
40 |
|
|
}
|
41 |
|
|
|
42 |
|
|
return org.springframework.security.core.userdetails.User
|
43 |
59931
|
k.triantaf
|
.withUsername(user.getEmail()).password(user.getSub())
|
44 |
59336
|
k.triantaf
|
.authorities(getAuthorities(user.getRoles()))
|
45 |
|
|
.accountExpired(false)
|
46 |
|
|
.accountLocked(false)
|
47 |
|
|
.credentialsExpired(false)
|
48 |
|
|
.disabled(false)
|
49 |
|
|
.build();
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
}
|