1
|
package eu.dnetlib.uoaauthorizationlibrary.security;
|
2
|
|
3
|
import org.apache.log4j.Logger;
|
4
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
5
|
import org.mitre.openid.connect.model.UserInfo;
|
6
|
import org.springframework.security.core.Authentication;
|
7
|
import org.springframework.security.core.GrantedAuthority;
|
8
|
import org.springframework.security.core.context.SecurityContextHolder;
|
9
|
import org.springframework.security.core.userdetails.User;
|
10
|
import org.springframework.stereotype.Component;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.List;
|
14
|
|
15
|
@Component(value = "AuthorizationService")
|
16
|
public class AuthorizationService {
|
17
|
private final Logger log = Logger.getLogger(this.getClass());
|
18
|
|
19
|
public final String PORTAL_ADMIN = "PORTAL_ADMINISTRATOR";
|
20
|
public final String ANONYMOUS_USER = "ROLE_ANONYMOUS";
|
21
|
public final String REGISTERED_USER = "REGISTERED_USER";
|
22
|
|
23
|
public AuthorizationService() {
|
24
|
}
|
25
|
|
26
|
private String mapType(String type, boolean communityMap) {
|
27
|
if (type.equals("organization")) {
|
28
|
type = "institution";
|
29
|
} else if (type.equals("ri") && communityMap) {
|
30
|
type = "community";
|
31
|
}
|
32
|
return type;
|
33
|
}
|
34
|
|
35
|
/**
|
36
|
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
|
37
|
*/
|
38
|
public String curator(String type) {
|
39
|
return "CURATOR_" + mapType(type, true).toUpperCase();
|
40
|
}
|
41
|
|
42
|
/**
|
43
|
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
|
44
|
* <p>
|
45
|
* Id = EE, EGI, etc
|
46
|
*/
|
47
|
public String manager(String type, String id) {
|
48
|
return mapType(type, true).toUpperCase() + "_" + id.toUpperCase() + "_MANAGER";
|
49
|
}
|
50
|
|
51
|
/**
|
52
|
* Type = FUNDER | COMMUNITY | RI | INSTITUTION | PROJECT
|
53
|
* <p>
|
54
|
* Id = EE, EGI, etc
|
55
|
*/
|
56
|
public String member(String type, String id) {
|
57
|
return mapType(type, false).toUpperCase() + "_" + id.toUpperCase();
|
58
|
}
|
59
|
|
60
|
public List<String> getRoles() {
|
61
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
62
|
if (authentication != null) {
|
63
|
List<GrantedAuthority> authorities = (List<GrantedAuthority>) authentication.getAuthorities();
|
64
|
if (authorities != null) {
|
65
|
List<String> roles = new ArrayList<>();
|
66
|
authorities.forEach((authority) -> {
|
67
|
roles.add(authority.getAuthority());
|
68
|
});
|
69
|
return roles;
|
70
|
}
|
71
|
}
|
72
|
return null;
|
73
|
}
|
74
|
|
75
|
public String getAaiId() {
|
76
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
77
|
return authentication instanceof OIDCAuthenticationToken ? ((OIDCAuthenticationToken)authentication).getSub() : null;
|
78
|
}
|
79
|
|
80
|
public String getEmail() {
|
81
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
82
|
return authentication instanceof OIDCAuthenticationToken ? ((OIDCAuthenticationToken)authentication).getUserInfo().getEmail() : null;
|
83
|
}
|
84
|
}
|