Project

General

Profile

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
    @Override
23
    public ResponseEntity<Object> login() {
24
        OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
25
        LOGGER.debug("User authentication : " + authentication);
26
        Map<String,Object> body = new HashMap<>();
27
        body.put("sub",authentication.getSub());
28
        if(authentication.getUserInfo().getName() == null || authentication.getUserInfo().getName().equals(""))
29
            body.put("name",authentication.getUserInfo().getGivenName() + " " + authentication.getUserInfo().getFamilyName());
30
        else
31
            body.put("name",authentication.getUserInfo().getName());
32

    
33
        body.put("email",authentication.getUserInfo().getEmail());
34
        List<String> roles = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
35
        body.put("role",roles);
36

    
37
        return new ResponseEntity<>(body, HttpStatus.OK);
38
    }
39
}
(18-18/20)