Project

General

Profile

« Previous | Next » 

Revision 61752

Create OpenAIREAuthentication object. Get user Info supports new login service user Info implementation

View differences:

AuthorizationService.java
1 1
package eu.dnetlib.uoaauthorizationlibrary.security;
2 2

  
3 3
import org.apache.log4j.Logger;
4
import org.springframework.security.core.Authentication;
5 4
import org.springframework.security.core.GrantedAuthority;
6 5
import org.springframework.security.core.context.SecurityContextHolder;
7
import org.springframework.security.core.userdetails.User;
8 6
import org.springframework.stereotype.Component;
9 7

  
10 8
import java.util.ArrayList;
......
71 69
    }
72 70

  
73 71
    public List<String> getRoles() {
74
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
75
        if (authentication != null) {
72
        OpenAIREAuthentication authentication = (OpenAIREAuthentication) SecurityContextHolder.getContext().getAuthentication();
73
        if (authentication != null && authentication.isAuthenticated()) {
76 74
            return authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
77 75
        }
78 76
        return new ArrayList<>();
79 77
    }
80 78

  
81 79
    public String getAaiId() {
82
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
83
        if (authentication != null && (authentication.getPrincipal() instanceof User)) {
84
            User user = (User) authentication.getPrincipal();
85
            return user.getPassword();
80
        OpenAIREAuthentication authentication = (OpenAIREAuthentication) SecurityContextHolder.getContext().getAuthentication();
81
        if (authentication != null && authentication.isAuthenticated()) {
82
            return authentication.getUser().getSub();
86 83
        }
87 84
        return null;
88 85
    }
89 86

  
90 87
    public String getEmail() {
91
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
92
        if (authentication != null && (authentication.getPrincipal() instanceof User)) {
93
            User user = (User) authentication.getPrincipal();
94
            return user.getUsername();
88
        OpenAIREAuthentication authentication = (OpenAIREAuthentication) SecurityContextHolder.getContext().getAuthentication();
89
        if (authentication != null && authentication.isAuthenticated()) {
90
            return authentication.getUser().getEmail();
95 91
        }
96 92
        return null;
97 93
    }

Also available in: Unified diff