1
|
package eu.dnetlib.repo.manager.service;
|
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.core.GrantedAuthority;
|
8
|
import org.springframework.security.core.context.SecurityContextHolder;
|
9
|
import org.springframework.stereotype.Service;
|
10
|
|
11
|
import java.util.HashMap;
|
12
|
import java.util.List;
|
13
|
import java.util.Map;
|
14
|
import java.util.stream.Collectors;
|
15
|
|
16
|
@Service("userService")
|
17
|
public class UserServiceImpl implements UserService {
|
18
|
|
19
|
private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
|
20
|
.getLogger(UserServiceImpl.class);
|
21
|
|
22
|
@Value("${oidc.issuer}")
|
23
|
private String oidc_issuer;
|
24
|
|
25
|
@Override
|
26
|
public ResponseEntity<Object> login() {
|
27
|
OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
|
28
|
LOGGER.debug("User authentication : " + authentication);
|
29
|
Map<String,Object> body = new HashMap<>();
|
30
|
body.put("sub",authentication.getSub());
|
31
|
if(authentication.getUserInfo().getName() == null || authentication.getUserInfo().getName().equals(""))
|
32
|
body.put("name",authentication.getUserInfo().getGivenName() + " " + authentication.getUserInfo().getFamilyName());
|
33
|
else
|
34
|
body.put("name",authentication.getUserInfo().getName());
|
35
|
|
36
|
body.put("email",authentication.getUserInfo().getEmail());
|
37
|
List<String> roles = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
|
38
|
body.put("role",roles);
|
39
|
|
40
|
return new ResponseEntity<>(body, HttpStatus.OK);
|
41
|
}
|
42
|
}
|