-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
custom authorities extractor using UserInfo Url
- Loading branch information
Showing
8 changed files
with
104 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/uk/ac/ebi/ega/accession/configuration/security/CustomAuthoritiesExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* | ||
* Copyright 2019 EMBL - European Bioinformatics Institute | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package uk.ac.ebi.ega.accession.configuration.security; | ||
|
||
import org.springframework.boot.autoconfigure.security.oauth2.resource.AuthoritiesExtractor; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import uk.ac.ebi.ega.accession.user.AccessioningUser; | ||
import uk.ac.ebi.ega.accession.user.AccessioningUserRepository; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* This class provides custom authorities for the EGA AAI authenticated user. | ||
*/ | ||
public class CustomAuthoritiesExtractor implements AuthoritiesExtractor { | ||
|
||
private AccessioningUserRepository accessioningUserRepository; | ||
|
||
public CustomAuthoritiesExtractor(AccessioningUserRepository accessioningUserRepository) { | ||
this.accessioningUserRepository = accessioningUserRepository; | ||
} | ||
|
||
@Override | ||
public List<GrantedAuthority> extractAuthorities(Map<String, Object> map) { | ||
String email = (String) map.get("email"); | ||
AccessioningUser accessioningUser = accessioningUserRepository.findByUserId(email); | ||
if (accessioningUser == null) { | ||
accessioningUser = new AccessioningUser(email, AccessioningUser.Role.ROLE_USER); | ||
accessioningUserRepository.save(accessioningUser); | ||
return Arrays.asList(new SimpleGrantedAuthority(AccessioningUser.Role.ROLE_USER.name())); | ||
} | ||
return Arrays.asList(new SimpleGrantedAuthority(accessioningUser.getRole().toString())); | ||
} | ||
} |
76 changes: 0 additions & 76 deletions
76
...ava/uk/ac/ebi/ega/accession/configuration/security/CustomUserAuthenticationConverter.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
insert into accessioning_user VALUES ('[email protected]','ROLE_ADMIN'); | ||
INSERT INTO accessioning_user | ||
SELECT | ||
'[email protected]', | ||
'ROLE_ADMIN' | ||
WHERE | ||
NOT EXISTS( | ||
SELECT user_id | ||
FROM accessioning_user | ||
WHERE user_id = '[email protected]' | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,48 +23,48 @@ | |
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import uk.ac.ebi.ega.accession.configuration.AccessioningUserConfiguration; | ||
import uk.ac.ebi.ega.accession.user.AccessioningUserRepository; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@RunWith(SpringRunner.class) | ||
@ContextConfiguration(classes = AccessioningUserConfiguration.class) | ||
@DataJpaTest | ||
public class CustomUserAuthenticationConverterTest { | ||
public class CustomAuthoritiesExtractorTest { | ||
|
||
@Autowired | ||
private AccessioningUserRepository accessioningUserRepository; | ||
|
||
@Test | ||
public void testRoles() throws Exception { | ||
CustomUserAuthenticationConverter customUserAuthenticationConverter = new | ||
CustomUserAuthenticationConverter(accessioningUserRepository); | ||
Map<String, Object> userIdMap = new HashMap<>(); | ||
userIdMap.put("user_id", "[email protected]"); | ||
Authentication authentication = customUserAuthenticationConverter.extractAuthentication(userIdMap); | ||
Assert.assertEquals("ROLE_EDITOR", getAuthority(authentication)); | ||
CustomAuthoritiesExtractor customAuthoritiesExtractor = new | ||
CustomAuthoritiesExtractor(accessioningUserRepository); | ||
Map<String, Object> userInfoMap = new HashMap<>(); | ||
userInfoMap.put("email", "[email protected]"); | ||
List<GrantedAuthority> authorities = customAuthoritiesExtractor.extractAuthorities(userInfoMap); | ||
Assert.assertEquals("ROLE_EDITOR", getAuthority(authorities)); | ||
|
||
userIdMap.put("user_id", "[email protected]"); | ||
authentication = customUserAuthenticationConverter.extractAuthentication(userIdMap); | ||
Assert.assertEquals("ROLE_USER", getAuthority(authentication)); | ||
userInfoMap.put("email", "[email protected]"); | ||
authorities = customAuthoritiesExtractor.extractAuthorities(userInfoMap); | ||
Assert.assertEquals("ROLE_USER", getAuthority(authorities)); | ||
|
||
userIdMap.put("user_id", "[email protected]"); | ||
authentication = customUserAuthenticationConverter.extractAuthentication(userIdMap); | ||
Assert.assertEquals("ROLE_ADMIN", getAuthority(authentication)); | ||
userInfoMap.put("email", "[email protected]"); | ||
authorities = customAuthoritiesExtractor.extractAuthorities(userInfoMap); | ||
Assert.assertEquals("ROLE_ADMIN", getAuthority(authorities)); | ||
|
||
userIdMap.put("user_id", "[email protected]"); | ||
authentication = customUserAuthenticationConverter.extractAuthentication(userIdMap); | ||
Assert.assertEquals("ROLE_USER", getAuthority(authentication)); | ||
userInfoMap.put("email", "[email protected]"); | ||
authorities = customAuthoritiesExtractor.extractAuthorities(userInfoMap); | ||
Assert.assertEquals("ROLE_USER", getAuthority(authorities)); | ||
} | ||
|
||
private String getAuthority(Authentication authentication) { | ||
return ((SimpleGrantedAuthority) authentication.getAuthorities().toArray()[0]).getAuthority(); | ||
private String getAuthority(List<GrantedAuthority> authorities) { | ||
return authorities.get(0).getAuthority(); | ||
} | ||
|
||
} |