Revision 61789
Added by Konstantinos Triantafyllou over 2 years ago
modules/uoa-authorization-library/tags/uoa-authorization-library-2.0.2/pom.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|
3 |
<modelVersion>4.0.0</modelVersion> |
|
4 |
<parent> |
|
5 |
<groupId>eu.dnetlib</groupId> |
|
6 |
<artifactId>dnet45-parent</artifactId> |
|
7 |
<version>1.0.0</version> |
|
8 |
</parent> |
|
9 |
<artifactId>uoa-authorization-library</artifactId> |
|
10 |
<version>2.0.2</version> |
|
11 |
<packaging>jar</packaging> |
|
12 |
<scm> |
|
13 |
<developerConnection>scm:svn:https://svn.driver.research-infrastructures.eu/driver/dnet45/modules/uoa-authorization-library/tags/uoa-authorization-library-2.0.2</developerConnection> |
|
14 |
</scm> |
|
15 |
<name>uoa-authorization-library</name> |
|
16 |
|
|
17 |
<properties> |
|
18 |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
|
19 |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> |
|
20 |
<java.version>1.8</java.version> |
|
21 |
<timestampAuthorizationLibrary>${maven.build.timestamp}</timestampAuthorizationLibrary> |
|
22 |
<maven.build.timestamp.format>E MMM dd HH:mm:ss z yyyy</maven.build.timestamp.format> |
|
23 |
</properties> |
|
24 |
<dependencyManagement> |
|
25 |
<dependencies> |
|
26 |
<dependency> |
|
27 |
<groupId>org.springframework.boot</groupId> |
|
28 |
<artifactId>spring-boot-dependencies</artifactId> |
|
29 |
<version>1.5.8.RELEASE</version> |
|
30 |
<type>pom</type> |
|
31 |
<scope>import</scope> |
|
32 |
</dependency> |
|
33 |
</dependencies> |
|
34 |
</dependencyManagement> |
|
35 |
<dependencies> |
|
36 |
<dependency> |
|
37 |
<groupId>org.springframework.boot</groupId> |
|
38 |
<artifactId>spring-boot-starter-web</artifactId> |
|
39 |
<exclusions> |
|
40 |
<exclusion> |
|
41 |
<groupId> org.springframework.boot</groupId> |
|
42 |
<artifactId>spring-boot-starter-logging</artifactId> |
|
43 |
</exclusion> |
|
44 |
</exclusions> |
|
45 |
</dependency> |
|
46 |
<!-- Starter for using Spring Security --> |
|
47 |
<dependency> |
|
48 |
<groupId>org.springframework.boot</groupId> |
|
49 |
<artifactId>spring-boot-starter-security</artifactId> |
|
50 |
</dependency> |
|
51 |
<dependency> |
|
52 |
<groupId>com.google.code.gson</groupId> |
|
53 |
<artifactId>gson</artifactId> |
|
54 |
<version>2.8.2</version> |
|
55 |
</dependency> |
|
56 |
<dependency> |
|
57 |
<groupId>log4j</groupId> |
|
58 |
<artifactId>log4j</artifactId> |
|
59 |
<version>1.2.17</version> |
|
60 |
</dependency> |
|
61 |
</dependencies> |
|
62 |
<build> |
|
63 |
<plugins> |
|
64 |
</plugins> |
|
65 |
<finalName>uoa-authorization-library</finalName> |
|
66 |
<resources> |
|
67 |
<resource> |
|
68 |
<directory>src/main/resources</directory> |
|
69 |
<filtering>true</filtering> |
|
70 |
</resource> |
|
71 |
</resources> |
|
72 |
</build> |
|
73 |
</project> |
modules/uoa-authorization-library/tags/uoa-authorization-library-2.0.2/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/AuthorizationService.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.security; |
|
2 |
|
|
3 |
import org.apache.log4j.Logger; |
|
4 |
import org.springframework.security.core.Authentication; |
|
5 |
import org.springframework.security.core.GrantedAuthority; |
|
6 |
import org.springframework.security.core.context.SecurityContextHolder; |
|
7 |
import org.springframework.stereotype.Component; |
|
8 |
|
|
9 |
import java.util.ArrayList; |
|
10 |
import java.util.List; |
|
11 |
import java.util.stream.Collectors; |
|
12 |
|
|
13 |
@Component(value = "AuthorizationService") |
|
14 |
public class AuthorizationService { |
|
15 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
16 |
|
|
17 |
public final String PORTAL_ADMIN = "PORTAL_ADMINISTRATOR"; |
|
18 |
public final String ANONYMOUS_USER = "ROLE_ANONYMOUS"; |
|
19 |
public final String REGISTERED_USER = "REGISTERED_USER"; |
|
20 |
|
|
21 |
|
|
22 |
private String mapType(String type, boolean communityMap) { |
|
23 |
if (type.equals("organization")) { |
|
24 |
type = "institution"; |
|
25 |
} else if (type.equals("ri") && communityMap) { |
|
26 |
type = "community"; |
|
27 |
} |
|
28 |
return type; |
|
29 |
} |
|
30 |
|
|
31 |
/** |
|
32 |
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT |
|
33 |
*/ |
|
34 |
public String curator(String type) { |
|
35 |
return "CURATOR_" + mapType(type, true).toUpperCase(); |
|
36 |
} |
|
37 |
|
|
38 |
/** |
|
39 |
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT |
|
40 |
* <p> |
|
41 |
* Id = EE, EGI, etc |
|
42 |
*/ |
|
43 |
public String manager(String type, String id) { |
|
44 |
return mapType(type, true).toUpperCase() + "_" + id.toUpperCase() + "_MANAGER"; |
|
45 |
} |
|
46 |
|
|
47 |
/** |
|
48 |
* Type = FUNDER | COMMUNITY | RI | INSTITUTION | PROJECT |
|
49 |
* <p> |
|
50 |
* Id = EE, EGI, etc |
|
51 |
*/ |
|
52 |
public String member(String type, String id) { |
|
53 |
return mapType(type, false).toUpperCase() + "_" + id.toUpperCase(); |
|
54 |
} |
|
55 |
|
|
56 |
public boolean isPortalAdmin() { |
|
57 |
return getRoles().stream().anyMatch(authority -> authority.equalsIgnoreCase(PORTAL_ADMIN)); |
|
58 |
} |
|
59 |
|
|
60 |
public boolean isCurator(String type) { |
|
61 |
return getRoles().stream().anyMatch(authority -> authority.equalsIgnoreCase(curator(type))); |
|
62 |
} |
|
63 |
|
|
64 |
public boolean isManager(String type, String id) { |
|
65 |
return getRoles().stream().anyMatch(authority -> authority.equalsIgnoreCase(manager(type, id))); |
|
66 |
} |
|
67 |
|
|
68 |
public boolean isMember(String type, String id) { |
|
69 |
return getRoles().stream().anyMatch(authority -> authority.equalsIgnoreCase(member(type, id))); |
|
70 |
} |
|
71 |
|
|
72 |
public List<String> getRoles() { |
|
73 |
OpenAIREAuthentication authentication = getAuthentication(); |
|
74 |
if (authentication != null && authentication.isAuthenticated()) { |
|
75 |
return authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList()); |
|
76 |
} |
|
77 |
return new ArrayList<>(); |
|
78 |
} |
|
79 |
|
|
80 |
public String getAaiId() { |
|
81 |
OpenAIREAuthentication authentication = getAuthentication(); |
|
82 |
if (authentication != null && authentication.isAuthenticated()) { |
|
83 |
return authentication.getUser().getSub(); |
|
84 |
} |
|
85 |
return null; |
|
86 |
} |
|
87 |
|
|
88 |
public String getEmail() { |
|
89 |
OpenAIREAuthentication authentication = getAuthentication(); |
|
90 |
if (authentication != null && authentication.isAuthenticated()) { |
|
91 |
return authentication.getUser().getEmail(); |
|
92 |
} |
|
93 |
return null; |
|
94 |
} |
|
95 |
|
|
96 |
private OpenAIREAuthentication getAuthentication() { |
|
97 |
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
|
98 |
if(authentication instanceof OpenAIREAuthentication) { |
|
99 |
return (OpenAIREAuthentication) authentication; |
|
100 |
} else { |
|
101 |
return null; |
|
102 |
} |
|
103 |
} |
|
104 |
} |
modules/uoa-authorization-library/tags/uoa-authorization-library-2.0.2/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/AuthorizationFilter.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.security; |
|
2 |
|
|
3 |
import eu.dnetlib.uoaauthorizationlibrary.utils.AuthorizationUtils; |
|
4 |
import org.apache.log4j.Logger; |
|
5 |
import org.springframework.beans.factory.annotation.Autowired; |
|
6 |
import org.springframework.security.core.context.SecurityContextHolder; |
|
7 |
import org.springframework.stereotype.Component; |
|
8 |
|
|
9 |
import javax.servlet.*; |
|
10 |
import javax.servlet.http.HttpServletRequest; |
|
11 |
import java.io.IOException; |
|
12 |
|
|
13 |
@Component |
|
14 |
public class AuthorizationFilter implements Filter { |
|
15 |
|
|
16 |
private final AuthorizationProvider authorizationProvider; |
|
17 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
18 |
|
|
19 |
@Autowired |
|
20 |
AuthorizationFilter(AuthorizationProvider authorizationProvider) { |
|
21 |
this.authorizationProvider = authorizationProvider; |
|
22 |
} |
|
23 |
|
|
24 |
@Override |
|
25 |
public void init(FilterConfig filterConfig) throws ServletException {} |
|
26 |
|
|
27 |
@Override |
|
28 |
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException { |
|
29 |
OpenAIREAuthentication auth = authorizationProvider.getAuthentication((HttpServletRequest) req); |
|
30 |
if(auth.isAuthenticated()) { |
|
31 |
SecurityContextHolder.getContext().setAuthentication(auth); |
|
32 |
} |
|
33 |
filterChain.doFilter(req, res); |
|
34 |
} |
|
35 |
|
|
36 |
@Override |
|
37 |
public void destroy() { |
|
38 |
|
|
39 |
} |
|
40 |
} |
modules/uoa-authorization-library/tags/uoa-authorization-library-2.0.2/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/AuthorizationFilterConfigurer.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.security; |
|
2 |
|
|
3 |
import eu.dnetlib.uoaauthorizationlibrary.utils.AuthorizationUtils; |
|
4 |
import org.springframework.security.config.annotation.SecurityConfigurerAdapter; |
|
5 |
import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
|
6 |
import org.springframework.security.web.DefaultSecurityFilterChain; |
|
7 |
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; |
|
8 |
|
|
9 |
public class AuthorizationFilterConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> { |
|
10 |
|
|
11 |
private final AuthorizationFilter filter; |
|
12 |
|
|
13 |
@Override |
|
14 |
public void init(HttpSecurity http) throws Exception { |
|
15 |
http.csrf().disable(); |
|
16 |
} |
|
17 |
|
|
18 |
public AuthorizationFilterConfigurer(AuthorizationFilter filter) { |
|
19 |
this.filter = filter; |
|
20 |
} |
|
21 |
|
|
22 |
@Override |
|
23 |
public void configure(HttpSecurity http) throws Exception { |
|
24 |
http.addFilterBefore(filter, UsernamePasswordAuthenticationFilter.class); |
|
25 |
} |
|
26 |
|
|
27 |
} |
modules/uoa-authorization-library/tags/uoa-authorization-library-2.0.2/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/AuthorizationProvider.java | ||
---|---|---|
1 |
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.stereotype.Component; |
|
7 |
|
|
8 |
import javax.servlet.http.HttpServletRequest; |
|
9 |
|
|
10 |
@Component |
|
11 |
public class AuthorizationProvider { |
|
12 |
|
|
13 |
private final AuthorizationUtils utils; |
|
14 |
|
|
15 |
@Autowired |
|
16 |
AuthorizationProvider(AuthorizationUtils utils) { |
|
17 |
this.utils = utils; |
|
18 |
} |
|
19 |
|
|
20 |
public OpenAIREAuthentication getAuthentication(HttpServletRequest request) { |
|
21 |
UserInfo user = utils.getUserInfo(request); |
|
22 |
if(user != null) { |
|
23 |
return new OpenAIREAuthentication(user); |
|
24 |
} |
|
25 |
return new OpenAIREAuthentication(); |
|
26 |
} |
|
27 |
} |
modules/uoa-authorization-library/tags/uoa-authorization-library-2.0.2/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/CorsConfig.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.security; |
|
2 |
|
|
3 |
import org.springframework.context.annotation.Configuration; |
|
4 |
import org.springframework.web.servlet.config.annotation.CorsRegistry; |
|
5 |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; |
|
6 |
|
|
7 |
@Configuration |
|
8 |
public class CorsConfig extends WebMvcConfigurerAdapter { |
|
9 |
|
|
10 |
@Override |
|
11 |
public void addCorsMappings(CorsRegistry registry) { |
|
12 |
registry.addMapping("/**") |
|
13 |
.allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS") |
|
14 |
.allowCredentials(true); |
|
15 |
} |
|
16 |
} |
modules/uoa-authorization-library/tags/uoa-authorization-library-2.0.2/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/OpenAIREAuthentication.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.security; |
|
2 |
|
|
3 |
import eu.dnetlib.uoaauthorizationlibrary.utils.UserInfo; |
|
4 |
import org.springframework.security.authentication.AbstractAuthenticationToken; |
|
5 |
|
|
6 |
public class OpenAIREAuthentication extends AbstractAuthenticationToken { |
|
7 |
private final UserInfo user; |
|
8 |
|
|
9 |
public OpenAIREAuthentication() { |
|
10 |
super(null); |
|
11 |
this.user = null; |
|
12 |
setAuthenticated(false); |
|
13 |
} |
|
14 |
|
|
15 |
public OpenAIREAuthentication(UserInfo user) { |
|
16 |
super(user.getAuthorities()); |
|
17 |
this.user = user; |
|
18 |
setAuthenticated(true); |
|
19 |
} |
|
20 |
|
|
21 |
@Override |
|
22 |
public Object getCredentials() { |
|
23 |
return getUser().getSub(); |
|
24 |
} |
|
25 |
|
|
26 |
@Override |
|
27 |
public Object getPrincipal() { |
|
28 |
return getUser(); |
|
29 |
} |
|
30 |
|
|
31 |
public UserInfo getUser() { |
|
32 |
return user; |
|
33 |
} |
|
34 |
} |
modules/uoa-authorization-library/tags/uoa-authorization-library-2.0.2/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/WebSecurityConfig.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.security; |
|
2 |
|
|
3 |
import eu.dnetlib.uoaauthorizationlibrary.utils.AuthorizationUtils; |
|
4 |
import org.springframework.beans.factory.annotation.Autowired; |
|
5 |
import org.springframework.context.annotation.ComponentScan; |
|
6 |
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; |
|
7 |
import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
|
8 |
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
|
9 |
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
|
10 |
import org.springframework.security.config.http.SessionCreationPolicy; |
|
11 |
|
|
12 |
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) |
|
13 |
@EnableWebSecurity |
|
14 |
@ComponentScan(basePackages = {"eu.dnetlib.uoaauthorizationlibrary.*"}) |
|
15 |
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
|
16 |
|
|
17 |
private final AuthorizationFilter filter; |
|
18 |
|
|
19 |
@Autowired |
|
20 |
WebSecurityConfig(AuthorizationFilter filter) { |
|
21 |
this.filter = filter; |
|
22 |
} |
|
23 |
|
|
24 |
@Override |
|
25 |
protected void configure(HttpSecurity http) throws Exception { |
|
26 |
http.apply(new AuthorizationFilterConfigurer(filter)); |
|
27 |
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); |
|
28 |
http.authorizeRequests().anyRequest().permitAll(); |
|
29 |
http.httpBasic().authenticationEntryPoint(new EntryPoint()); |
|
30 |
} |
|
31 |
|
|
32 |
} |
modules/uoa-authorization-library/tags/uoa-authorization-library-2.0.2/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/EntryPoint.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.security; |
|
2 |
|
|
3 |
import org.springframework.security.core.AuthenticationException; |
|
4 |
import org.springframework.security.web.AuthenticationEntryPoint; |
|
5 |
|
|
6 |
import javax.servlet.http.HttpServletRequest; |
|
7 |
import javax.servlet.http.HttpServletResponse; |
|
8 |
import java.io.IOException; |
|
9 |
|
|
10 |
public class EntryPoint implements AuthenticationEntryPoint { |
|
11 |
|
|
12 |
@Override |
|
13 |
public void commence(HttpServletRequest request, HttpServletResponse response, |
|
14 |
AuthenticationException authException) throws IOException { |
|
15 |
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage()); |
|
16 |
} |
|
17 |
|
|
18 |
} |
|
19 |
|
modules/uoa-authorization-library/tags/uoa-authorization-library-2.0.2/src/main/java/eu/dnetlib/uoaauthorizationlibrary/utils/AuthorizationUtils.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.utils; |
|
2 |
|
|
3 |
import eu.dnetlib.uoaauthorizationlibrary.configuration.SecurityConfig; |
|
4 |
import org.apache.log4j.Logger; |
|
5 |
import org.springframework.beans.factory.annotation.Autowired; |
|
6 |
import org.springframework.http.*; |
|
7 |
import org.springframework.stereotype.Component; |
|
8 |
import org.springframework.web.client.RestClientException; |
|
9 |
import org.springframework.web.client.RestTemplate; |
|
10 |
|
|
11 |
import javax.servlet.http.Cookie; |
|
12 |
import javax.servlet.http.HttpServletRequest; |
|
13 |
import java.util.Arrays; |
|
14 |
import java.util.Collections; |
|
15 |
|
|
16 |
@Component |
|
17 |
public class AuthorizationUtils { |
|
18 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
19 |
private final SecurityConfig securityConfig; |
|
20 |
private final static String TOKEN = "AccessToken"; |
|
21 |
private final static String SESSION = "OpenAIRESession"; |
|
22 |
|
|
23 |
@Autowired |
|
24 |
AuthorizationUtils(SecurityConfig securityConfig) { |
|
25 |
this.securityConfig = securityConfig; |
|
26 |
} |
|
27 |
|
|
28 |
private String getToken(HttpServletRequest request) { |
|
29 |
if (request.getCookies() == null) { |
|
30 |
return null; |
|
31 |
} |
|
32 |
for (Cookie c : request.getCookies()) { |
|
33 |
if (c.getName().equals(TOKEN)) { |
|
34 |
return c.getValue(); |
|
35 |
} |
|
36 |
} |
|
37 |
return null; |
|
38 |
} |
|
39 |
|
|
40 |
public UserInfo getUserInfo(HttpServletRequest request) { |
|
41 |
String url = securityConfig.getUserInfoUrl() + (securityConfig.isDeprecated()?getToken(request):""); |
|
42 |
RestTemplate restTemplate = new RestTemplate(); |
|
43 |
try { |
|
44 |
if(hasCookie(request)) { |
|
45 |
ResponseEntity<UserInfo> response = restTemplate.exchange(url, HttpMethod.GET, createHeaders(request), UserInfo.class); |
|
46 |
return response.getBody(); |
|
47 |
} |
|
48 |
return null; |
|
49 |
} catch (RestClientException e) { |
|
50 |
log.error(url + ":" + e.getMessage()); |
|
51 |
return null; |
|
52 |
} |
|
53 |
} |
|
54 |
|
|
55 |
private boolean hasCookie(HttpServletRequest request) { |
|
56 |
Cookie[] cookies = request.getCookies(); |
|
57 |
if(securityConfig.isDeprecated()) { |
|
58 |
return Arrays.stream(cookies).anyMatch(cookie -> cookie.getName().equalsIgnoreCase(TOKEN)); |
|
59 |
} else { |
|
60 |
return Arrays.stream(cookies).anyMatch(cookie -> cookie.getName().equalsIgnoreCase(SESSION)); |
|
61 |
} |
|
62 |
} |
|
63 |
|
|
64 |
private HttpEntity<HttpHeaders> createHeaders(HttpServletRequest request) { |
|
65 |
HttpHeaders headers = new HttpHeaders(); |
|
66 |
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); |
|
67 |
headers.set("Cookie", request.getHeader("Cookie")); |
|
68 |
return new HttpEntity<>(headers); |
|
69 |
} |
|
70 |
} |
modules/uoa-authorization-library/tags/uoa-authorization-library-2.0.2/src/main/java/eu/dnetlib/uoaauthorizationlibrary/utils/UserInfo.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.utils; |
|
2 |
|
|
3 |
import org.springframework.security.core.GrantedAuthority; |
|
4 |
import org.springframework.security.core.authority.SimpleGrantedAuthority; |
|
5 |
|
|
6 |
import java.util.HashSet; |
|
7 |
import java.util.Set; |
|
8 |
|
|
9 |
public class UserInfo { |
|
10 |
private String sub; |
|
11 |
private String name; |
|
12 |
private String given_name; |
|
13 |
private String family_name; |
|
14 |
private String email; |
|
15 |
private Set<String> roles; |
|
16 |
|
|
17 |
@Override |
|
18 |
public String toString() { |
|
19 |
return "UserInfo{" + |
|
20 |
"sub='" + sub + '\'' + |
|
21 |
"name='" + name + '\'' + |
|
22 |
", email='" + email + '\'' + |
|
23 |
", roles=" + roles + |
|
24 |
'}'; |
|
25 |
} |
|
26 |
|
|
27 |
public String getSub() { |
|
28 |
return sub; |
|
29 |
} |
|
30 |
|
|
31 |
public void setSub(String sub) { |
|
32 |
this.sub = sub; |
|
33 |
} |
|
34 |
|
|
35 |
public String getName() { |
|
36 |
return name; |
|
37 |
} |
|
38 |
|
|
39 |
public void setName(String name) { |
|
40 |
this.name = name; |
|
41 |
} |
|
42 |
|
|
43 |
public String getGiven_name() { |
|
44 |
return given_name; |
|
45 |
} |
|
46 |
|
|
47 |
public void setGiven_name(String given_name) { |
|
48 |
this.given_name = given_name; |
|
49 |
} |
|
50 |
|
|
51 |
public String getFamily_name() { |
|
52 |
return family_name; |
|
53 |
} |
|
54 |
|
|
55 |
public void setFamily_name(String family_name) { |
|
56 |
this.family_name = family_name; |
|
57 |
} |
|
58 |
|
|
59 |
public String getEmail() { |
|
60 |
return email; |
|
61 |
} |
|
62 |
|
|
63 |
public void setEmail(String email) { |
|
64 |
this.email = email; |
|
65 |
} |
|
66 |
|
|
67 |
public Set<String> getRoles() { |
|
68 |
return roles; |
|
69 |
} |
|
70 |
|
|
71 |
public void setRoles(Set<String> roles) { |
|
72 |
this.roles = roles; |
|
73 |
} |
|
74 |
|
|
75 |
public Set<GrantedAuthority> getAuthorities() { |
|
76 |
Set<GrantedAuthority> authorities = new HashSet<>(); |
|
77 |
for(String role : roles) { |
|
78 |
authorities.add(new SimpleGrantedAuthority(role)); |
|
79 |
} |
|
80 |
return authorities; |
|
81 |
} |
|
82 |
} |
modules/uoa-authorization-library/tags/uoa-authorization-library-2.0.2/src/main/java/eu/dnetlib/uoaauthorizationlibrary/configuration/SecurityConfig.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.configuration; |
|
2 |
|
|
3 |
import org.springframework.boot.context.properties.ConfigurationProperties; |
|
4 |
|
|
5 |
@ConfigurationProperties("authorization.security") |
|
6 |
public class SecurityConfig { |
|
7 |
|
|
8 |
private String userInfoUrl; |
|
9 |
|
|
10 |
public String getUserInfoUrl() { |
|
11 |
return userInfoUrl; |
|
12 |
} |
|
13 |
|
|
14 |
public void setUserInfoUrl(String userInfoUrl) { |
|
15 |
this.userInfoUrl = userInfoUrl; |
|
16 |
} |
|
17 |
|
|
18 |
/** @deprecated */ |
|
19 |
public boolean isDeprecated() { |
|
20 |
return userInfoUrl.contains("accessToken"); |
|
21 |
} |
|
22 |
} |
modules/uoa-authorization-library/tags/uoa-authorization-library-2.0.2/src/main/java/eu/dnetlib/uoaauthorizationlibrary/configuration/AuthorizationConfiguration.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.configuration; |
|
2 |
|
|
3 |
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
|
4 |
import org.springframework.context.annotation.*; |
|
5 |
|
|
6 |
@Configuration |
|
7 |
@EnableConfigurationProperties({SecurityConfig.class, GlobalVars.class}) |
|
8 |
@ComponentScan(basePackages = { "eu.dnetlib.uoaauthorizationlibrary" }) |
|
9 |
public class AuthorizationConfiguration { } |
modules/uoa-authorization-library/tags/uoa-authorization-library-2.0.2/src/main/java/eu/dnetlib/uoaauthorizationlibrary/configuration/GlobalVars.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.configuration; |
|
2 |
|
|
3 |
import org.springframework.boot.context.properties.ConfigurationProperties; |
|
4 |
|
|
5 |
import java.util.Date; |
|
6 |
|
|
7 |
@ConfigurationProperties("authorization.globalVars") |
|
8 |
public class GlobalVars { |
|
9 |
public static Date date = new Date(); |
|
10 |
private Date buildDate; |
|
11 |
|
|
12 |
public String getBuildDate() { |
|
13 |
if(buildDate == null) { |
|
14 |
return null; |
|
15 |
} |
|
16 |
return buildDate.toString(); |
|
17 |
} |
|
18 |
|
|
19 |
public void setBuildDate(Date buildDate) { |
|
20 |
this.buildDate = buildDate; |
|
21 |
} |
|
22 |
} |
modules/uoa-authorization-library/tags/uoa-authorization-library-2.0.2/src/main/java/eu/dnetlib/uoaauthorizationlibrary/controllers/AuthorizationLibraryCheckDeployController.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaauthorizationlibrary.controllers; |
|
2 |
|
|
3 |
import eu.dnetlib.uoaauthorizationlibrary.configuration.GlobalVars; |
|
4 |
import eu.dnetlib.uoaauthorizationlibrary.configuration.SecurityConfig; |
|
5 |
import org.apache.log4j.Logger; |
|
6 |
import org.springframework.beans.factory.annotation.Autowired; |
|
7 |
import org.springframework.security.access.prepost.PreAuthorize; |
|
8 |
import org.springframework.web.bind.annotation.CrossOrigin; |
|
9 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
10 |
import org.springframework.web.bind.annotation.RequestMethod; |
|
11 |
import org.springframework.web.bind.annotation.RestController; |
|
12 |
|
|
13 |
import java.util.HashMap; |
|
14 |
import java.util.Map; |
|
15 |
|
|
16 |
@RestController |
|
17 |
@CrossOrigin(origins = "*") |
|
18 |
@RequestMapping("/authorization-library") |
|
19 |
public class AuthorizationLibraryCheckDeployController { |
|
20 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
21 |
|
|
22 |
@Autowired |
|
23 |
private SecurityConfig securityConfig; |
|
24 |
|
|
25 |
@Autowired |
|
26 |
private GlobalVars globalVars; |
|
27 |
|
|
28 |
@RequestMapping(value = {"", "/health_check"}, method = RequestMethod.GET) |
|
29 |
public String hello() { |
|
30 |
log.debug("Hello from uoa-authorization-service!"); |
|
31 |
return "Hello from uoa-authorization-service!"; |
|
32 |
} |
|
33 |
|
|
34 |
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)") |
|
35 |
@RequestMapping(value = "/health_check/advanced", method = RequestMethod.GET) |
|
36 |
public Map<String, String> checkEverything() { |
|
37 |
Map<String, String> response = new HashMap<>(); |
|
38 |
response.put("authorization.security.userInfoUrl", securityConfig.getUserInfoUrl()); |
|
39 |
if(GlobalVars.date != null) { |
|
40 |
response.put("Date of deploy", GlobalVars.date.toString()); |
|
41 |
} |
|
42 |
if(globalVars.getBuildDate() != null) { |
|
43 |
response.put("Date of build", globalVars.getBuildDate()); |
|
44 |
} |
|
45 |
return response; |
|
46 |
} |
|
47 |
} |
modules/uoa-authorization-library/tags/uoa-authorization-library-2.0.2/src/main/resources/authorization.properties | ||
---|---|---|
1 |
#dev |
|
2 |
authorization.security.userInfoUrl = http://mpagasas.di.uoa.gr:8080/dnet-openaire-users-1.0.0-SNAPSHOT/api/users/getUserInfo?accessToken= |
|
3 |
authorization.globalVars.buildDate=@timestampAuthorizationLibrary@ |
|
4 |
|
|
5 |
#beta |
|
6 |
#authorization.security.userInfoUrl = https://beta.services.openaire.eu/uoa-user-management/api/users/getUserInfo?accessToken= |
|
7 |
|
|
8 |
#production |
|
9 |
#authorization.security.userInfoUrl = https://services.openaire.eu/uoa-user-management/api/users/getUserInfo?accessToken= |
Also available in: Unified diff
[maven-release-plugin] copy for tag uoa-authorization-library-2.0.2