1
|
package eu.dnetlib.repo.manager.service.controllers;
|
2
|
|
3
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
4
|
import org.springframework.beans.factory.annotation.Value;
|
5
|
import org.springframework.http.HttpStatus;
|
6
|
import org.springframework.http.ResponseEntity;
|
7
|
import org.springframework.security.access.prepost.PreAuthorize;
|
8
|
import org.springframework.security.core.GrantedAuthority;
|
9
|
import org.springframework.security.core.context.SecurityContextHolder;
|
10
|
import org.springframework.stereotype.Component;
|
11
|
|
12
|
import java.util.HashMap;
|
13
|
import java.util.List;
|
14
|
import java.util.Map;
|
15
|
import java.util.stream.Collectors;
|
16
|
|
17
|
@Component
|
18
|
public class UserApiImpl implements UserApi {
|
19
|
|
20
|
private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
|
21
|
.getLogger(UserApiImpl.class);
|
22
|
|
23
|
@Value("${oidc.issuer}")
|
24
|
private String oidc_issuer;
|
25
|
|
26
|
@Override
|
27
|
@PreAuthorize("hasRole('ROLE_USER')")
|
28
|
public ResponseEntity<Object> login() {
|
29
|
OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
|
30
|
LOGGER.debug("User authentication : " + authentication);
|
31
|
Map<String,Object> body = new HashMap<>();
|
32
|
body.put("sub",authentication.getSub());
|
33
|
if(authentication.getUserInfo().getName() == null || authentication.getUserInfo().getName().equals(""))
|
34
|
body.put("name",authentication.getUserInfo().getGivenName() + " " + authentication.getUserInfo().getFamilyName());
|
35
|
else
|
36
|
body.put("name",authentication.getUserInfo().getName());
|
37
|
|
38
|
body.put("email",authentication.getUserInfo().getEmail());
|
39
|
List<String> roles = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
|
40
|
body.put("role",roles);
|
41
|
|
42
|
return new ResponseEntity<>(body, HttpStatus.OK);
|
43
|
}
|
44
|
}
|