Project

General

Profile

« Previous | Next » 

Revision 61787

Fix authentication cast to OpenAIREAuthentication while it is not a instance of. Check cookie existance to avoid extra calls for get user info.

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;
4 5
import org.springframework.security.core.GrantedAuthority;
5 6
import org.springframework.security.core.context.SecurityContextHolder;
6 7
import org.springframework.stereotype.Component;
......
69 70
    }
70 71

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

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

  
87 88
    public String getEmail() {
88
        OpenAIREAuthentication authentication = (OpenAIREAuthentication) SecurityContextHolder.getContext().getAuthentication();
89
        OpenAIREAuthentication authentication = getAuthentication();
89 90
        if (authentication != null && authentication.isAuthenticated()) {
90 91
            return authentication.getUser().getEmail();
91 92
        }
92 93
        return null;
93 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
    }
94 104
}

Also available in: Unified diff