1
|
package eu.dnetlib.openaire.rest;
|
2
|
|
3
|
import eu.dnetlib.openaire.rest.inputHandler.UserHandler;
|
4
|
import org.apache.log4j.Logger;
|
5
|
|
6
|
import java.util.ArrayList;
|
7
|
import java.util.Arrays;
|
8
|
import java.util.List;
|
9
|
|
10
|
/**
|
11
|
* Created by sofia on 20/4/2017.
|
12
|
*/
|
13
|
public class Authorization {
|
14
|
|
15
|
private static Logger logger = Logger.getLogger(Authorization.class);
|
16
|
List<String> registeredRoles = new ArrayList<String>(Arrays.asList("Super Administrator", "Portal Administrator", "Expert - Community",
|
17
|
"Expert - Funder", "Curator - Claim", "Curator - Project", "Curator - Community", "Curator - Institution", "Cuthor", "Registered", "User Manager"));
|
18
|
List<String> claimCuratorRoles = new ArrayList<String>(Arrays.asList("Super Administrator", "Curator - Claim", "Portal Administrator"));
|
19
|
List<String> projectCuratorRoles = new ArrayList<String>(Arrays.asList("Curator - Project"));
|
20
|
List<String> communityCuratorRoles = new ArrayList<String>(Arrays.asList("Curator - Community"));
|
21
|
UserHandler userHandler = null;
|
22
|
String originServer = null;
|
23
|
|
24
|
// public static boolean isRegistered(String token) {
|
25
|
//
|
26
|
// Claims claims = Jwts.parser()
|
27
|
// .setSigningKey(DatatypeConverter.parseBase64Binary("my-very-secret"))
|
28
|
// .parseClaimsJws(token).getBody();
|
29
|
//
|
30
|
// if (claims.get("role").equals("1") || claims.get("role").equals("2")) {
|
31
|
// logger.debug("It's registered with role " + claims.get("role"));
|
32
|
// return true;
|
33
|
// } else {
|
34
|
// logger.debug("It's *not* registered with role " + claims.get("role"));
|
35
|
//
|
36
|
// return false;
|
37
|
// }
|
38
|
// }
|
39
|
//
|
40
|
// public static boolean isClaimCurator(String token) {
|
41
|
// Claims claims = Jwts.parser()
|
42
|
// .setSigningKey(DatatypeConverter.parseBase64Binary("my-very-secret"))
|
43
|
// .parseClaimsJws(token).getBody();
|
44
|
// if (claims.get("role").equals("2")) {
|
45
|
// logger.debug("It's admin with role " + claims.get("role"));
|
46
|
// return true;
|
47
|
// } else {
|
48
|
// logger.debug("It's *not* admin with role " + claims.get("role"));
|
49
|
// return false;
|
50
|
// }
|
51
|
// }
|
52
|
public boolean isRegistered(String token) {
|
53
|
UserInfo userInfo = userHandler.getUserInfo(token);
|
54
|
return isRegistered(userInfo);
|
55
|
|
56
|
}
|
57
|
public void logStatus(String token, String cookie) {
|
58
|
|
59
|
if(token == null || token.isEmpty()) {
|
60
|
logger.debug("No proper value: header \"X-XSRF-TOKEN\" has value " + token);
|
61
|
}else if(cookie == null || cookie.isEmpty() || !cookie.equals(token)) {
|
62
|
logger.debug("No proper value: cookie \"AccessToken\" has value " + cookie);
|
63
|
}else if( !cookie.equals(token)) {
|
64
|
logger.debug("No proper values:for cookie " + cookie + " and token "+token);
|
65
|
}
|
66
|
}
|
67
|
|
68
|
public boolean isRegistered(UserInfo userInfo) {
|
69
|
if (userInfo != null && userInfo.getEdu_person_entitlements() != null) {
|
70
|
|
71
|
return hasRole(userInfo.getEdu_person_entitlements(), registeredRoles);
|
72
|
} else {
|
73
|
return false;
|
74
|
}
|
75
|
|
76
|
}
|
77
|
|
78
|
public boolean isClaimCurator(String token) {
|
79
|
UserInfo userInfo = userHandler.getUserInfo(token);
|
80
|
return isClaimCurator(userInfo);
|
81
|
|
82
|
}
|
83
|
|
84
|
public boolean isClaimCurator(UserInfo userInfo) {
|
85
|
if (userInfo != null && userInfo.getEdu_person_entitlements() != null) {
|
86
|
|
87
|
return hasRole(userInfo.getEdu_person_entitlements(), claimCuratorRoles);
|
88
|
} else {
|
89
|
logger.debug(" User has no Valid UserInfo");
|
90
|
return false;
|
91
|
}
|
92
|
|
93
|
}
|
94
|
|
95
|
public boolean isCommunityCurator(String token) {
|
96
|
UserInfo userInfo = userHandler.getUserInfo(token);
|
97
|
return isCommunityCurator(userInfo);
|
98
|
|
99
|
}
|
100
|
|
101
|
public boolean isCommunityCurator(UserInfo userInfo) {
|
102
|
if (userInfo != null && userInfo.getEdu_person_entitlements() != null) {
|
103
|
|
104
|
return hasRole(userInfo.getEdu_person_entitlements(), communityCuratorRoles);
|
105
|
} else {
|
106
|
logger.debug(" User has no Valid UserInfo");
|
107
|
return false;
|
108
|
}
|
109
|
|
110
|
}
|
111
|
public boolean isProjectCurator(String token) {
|
112
|
UserInfo userInfo = userHandler.getUserInfo(token);
|
113
|
return isProjectCurator(userInfo);
|
114
|
|
115
|
}
|
116
|
|
117
|
public boolean isProjectCurator(UserInfo userInfo) {
|
118
|
if (userInfo != null && userInfo.getEdu_person_entitlements() != null) {
|
119
|
|
120
|
return hasRole(userInfo.getEdu_person_entitlements(), projectCuratorRoles);
|
121
|
} else {
|
122
|
return false;
|
123
|
}
|
124
|
|
125
|
}
|
126
|
|
127
|
public boolean hasRole(List<String> givenRoles, List<String> authorizedRoles) {
|
128
|
|
129
|
logger.debug("It's registered with role " + givenRoles);
|
130
|
for (String gRole : givenRoles) {
|
131
|
if (authorizedRoles.indexOf(gRole) != -1) {
|
132
|
return true;
|
133
|
}
|
134
|
}
|
135
|
logger.debug("Not Authorized. Authorized roles are" + authorizedRoles);
|
136
|
return false;
|
137
|
|
138
|
}
|
139
|
|
140
|
public boolean hasValidOrigin(String origin) {
|
141
|
logger.debug("Origin is "+origin +" originServer: "+originServer);
|
142
|
if (origin != null && origin.indexOf(originServer)!=-1) {
|
143
|
return true;
|
144
|
}
|
145
|
logger.debug("Not valid origin. Origin server is \"" + origin + "\", but expected value is \"" + originServer + "\". If the expec cted value is not right, check properties file. ");
|
146
|
return false;
|
147
|
}
|
148
|
|
149
|
public boolean hasBasicAuthorization(String token, String origin, String cookie){
|
150
|
if(token == null || token.isEmpty() || cookie == null || cookie.isEmpty() || !cookie.equals(token)|| !hasValidOrigin(origin)){
|
151
|
return false;
|
152
|
}
|
153
|
return true;
|
154
|
}
|
155
|
|
156
|
public List<String> getRegisteredRoles() {
|
157
|
return registeredRoles;
|
158
|
}
|
159
|
|
160
|
public void setRegisteredRoles(List<String> registeredRoles) {
|
161
|
this.registeredRoles = registeredRoles;
|
162
|
}
|
163
|
|
164
|
public List<String> getClaimCuratorRoles() {
|
165
|
return claimCuratorRoles;
|
166
|
}
|
167
|
|
168
|
public void setClaimCuratorRoles(List<String> claimCuratorRoles) {
|
169
|
this.claimCuratorRoles = claimCuratorRoles;
|
170
|
}
|
171
|
|
172
|
public List<String> getCommunityCuratorRoles() {
|
173
|
return communityCuratorRoles;
|
174
|
}
|
175
|
|
176
|
public void setCommunityCuratorRoles(List<String> communityCuratorRoles) {
|
177
|
this.communityCuratorRoles = communityCuratorRoles;
|
178
|
}
|
179
|
|
180
|
public List<String> getProjectCuratorRoles() {
|
181
|
return projectCuratorRoles;
|
182
|
}
|
183
|
|
184
|
public void setProjectCuratorRoles(List<String> projectCuratorRoles) {
|
185
|
this.projectCuratorRoles = projectCuratorRoles;
|
186
|
}
|
187
|
|
188
|
public UserHandler getUserHandler() {
|
189
|
return userHandler;
|
190
|
}
|
191
|
|
192
|
public void setUserHandler(UserHandler userHandler) {
|
193
|
this.userHandler = userHandler;
|
194
|
}
|
195
|
|
196
|
public String getOriginServer() {
|
197
|
return originServer;
|
198
|
}
|
199
|
|
200
|
public void setOriginServer(String originServer) {
|
201
|
this.originServer = originServer;
|
202
|
}
|
203
|
}
|