use of org.springframework.security.oauth2.provider.OAuth2Request in project ORCID-Source by ORCID.
the class OrcidTokenStoreServiceTest method testReadAuthentication.
@Test
@Transactional
public void testReadAuthentication() throws Exception {
OAuth2Authentication oAuth2Authentication = orcidTokenStoreService.readAuthentication("persistent-token-2");
assertNotNull(oAuth2Authentication);
OAuth2Request oAuth2Request = oAuth2Authentication.getOAuth2Request();
assertNotNull(oAuth2Request);
Object principal = oAuth2Authentication.getPrincipal();
assertNotNull(principal);
assertTrue(!oAuth2Authentication.isClientOnly());
}
use of org.springframework.security.oauth2.provider.OAuth2Request in project ORCID-Source by ORCID.
the class OrcidSecurityManagerImpl method checkScopes.
@Override
public void checkScopes(ScopePathType requiredScope) {
//Verify the client is not a public client
checkClientType();
OAuth2Authentication oAuth2Authentication = getOAuth2Authentication();
OAuth2Request authorizationRequest = oAuth2Authentication.getOAuth2Request();
Set<ScopePathType> requestedScopes = ScopePathType.getScopesFromStrings(authorizationRequest.getScope());
for (ScopePathType scope : requestedScopes) {
if (scope.hasScope(requiredScope)) {
return;
}
}
throw new OrcidAccessControlException();
}
use of org.springframework.security.oauth2.provider.OAuth2Request in project ORCID-Source by ORCID.
the class OrcidSecurityManagerImpl method isNonClientCredentialScope.
private boolean isNonClientCredentialScope(OAuth2Authentication oAuth2Authentication) {
OAuth2Request authorizationRequest = oAuth2Authentication.getOAuth2Request();
Set<String> requestedScopes = ScopePathType.getCombinedScopesFromStringsAsStrings(authorizationRequest.getScope());
for (String scopeName : requestedScopes) {
ScopePathType scopePathType = ScopePathType.fromValue(scopeName);
if (!scopePathType.isClientCreditalScope()) {
return true;
}
}
return false;
}
use of org.springframework.security.oauth2.provider.OAuth2Request in project ORCID-Source by ORCID.
the class OrcidSecurityManagerImpl method getClientIdFromAPIRequest.
@Override
public String getClientIdFromAPIRequest() {
SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
if (authentication != null && OAuth2Authentication.class.isAssignableFrom(authentication.getClass())) {
OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) authentication;
OAuth2Request request = oAuth2Authentication.getOAuth2Request();
return request.getClientId();
}
return null;
}
use of org.springframework.security.oauth2.provider.OAuth2Request in project ORCID-Source by ORCID.
the class SecurityContextTestUtils method setUpSecurityContext.
public static void setUpSecurityContext(String userOrcid, String clientId, ScopePathType... scopePathTypes) {
SecurityContextImpl securityContext = new SecurityContextImpl();
OrcidOAuth2Authentication mockedAuthentication = mock(OrcidOAuth2Authentication.class);
securityContext.setAuthentication(mockedAuthentication);
SecurityContextHolder.setContext(securityContext);
ProfileEntity userProfileEntity = new ProfileEntity(userOrcid);
when(mockedAuthentication.getPrincipal()).thenReturn(userProfileEntity);
Authentication userAuthentication = mock(Authentication.class);
when(userAuthentication.getPrincipal()).thenReturn(userProfileEntity);
when(mockedAuthentication.getUserAuthentication()).thenReturn(userAuthentication);
Set<String> scopes = new HashSet<String>();
if (scopePathTypes != null) {
for (ScopePathType scopePathType : scopePathTypes) {
scopes.add(scopePathType.value());
}
}
OAuth2Request authorizationRequest = new OAuth2Request(Collections.<String, String>emptyMap(), clientId, Collections.<GrantedAuthority>emptyList(), true, scopes, Collections.<String>emptySet(), null, Collections.<String>emptySet(), Collections.<String, Serializable>emptyMap());
when(mockedAuthentication.getOAuth2Request()).thenReturn(authorizationRequest);
when(mockedAuthentication.isAuthenticated()).thenReturn(true);
}
Aggregations