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