-
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.
Oauth using EGA AAI and custom Role Based authority
- Loading branch information
Showing
16 changed files
with
477 additions
and
7 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
31 changes: 31 additions & 0 deletions
31
src/main/java/uk/ac/ebi/ega/accession/configuration/AccessioningUserConfiguration.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,31 @@ | ||
/* | ||
* | ||
* 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; | ||
|
||
import org.springframework.boot.autoconfigure.domain.EntityScan; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import uk.ac.ebi.ega.accession.user.AccessioningUser; | ||
import uk.ac.ebi.ega.accession.user.AccessioningUserRepository; | ||
|
||
@Configuration | ||
@EntityScan(basePackageClasses = AccessioningUser.class) | ||
@EnableJpaRepositories(basePackageClasses = AccessioningUserRepository.class) | ||
public class AccessioningUserConfiguration { | ||
} |
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
76 changes: 76 additions & 0 deletions
76
...ava/uk/ac/ebi/ega/accession/configuration/security/CustomUserAuthenticationConverter.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,76 @@ | ||
/* | ||
* | ||
* 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.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.AuthorityUtils; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.provider.token.UserAuthenticationConverter; | ||
import uk.ac.ebi.ega.accession.user.AccessioningUser; | ||
import uk.ac.ebi.ega.accession.user.AccessioningUserRepository; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* This class provides custom authorities for the EGA AAI authenticated user. | ||
*/ | ||
public class CustomUserAuthenticationConverter implements UserAuthenticationConverter { | ||
|
||
private final static String USER_ID = "user_id"; | ||
|
||
private AccessioningUserRepository accessioningUserRepository; | ||
|
||
public CustomUserAuthenticationConverter(AccessioningUserRepository accessioningUserRepository) { | ||
this.accessioningUserRepository = accessioningUserRepository; | ||
} | ||
|
||
public Map<String, ?> convertUserAuthentication(Authentication authentication) { | ||
Map<String, Object> response = new LinkedHashMap<String, Object>(); | ||
response.put(USER_ID, authentication.getName()); | ||
if (authentication.getAuthorities() != null && !authentication.getAuthorities().isEmpty()) { | ||
response.put(AUTHORITIES, AuthorityUtils.authorityListToSet(authentication.getAuthorities())); | ||
} | ||
return response; | ||
} | ||
|
||
public Authentication extractAuthentication(Map<String, ?> map) { | ||
if (map.containsKey(USER_ID)) { | ||
Object principal = map.get(USER_ID); | ||
Collection<? extends GrantedAuthority> authorities = getAuthorities(map); | ||
return new UsernamePasswordAuthenticationToken(principal, "N/A", authorities); | ||
} | ||
return null; | ||
} | ||
|
||
private Collection<? extends GrantedAuthority> getAuthorities(Map<String, ?> map) { | ||
String userId = (String) map.get(USER_ID); | ||
AccessioningUser accessioningUser = accessioningUserRepository.findByUserId(userId); | ||
if (accessioningUser == null) { | ||
accessioningUser = new AccessioningUser(userId, 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())); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/uk/ac/ebi/ega/accession/configuration/security/DisableSecurityConfig.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,35 @@ | ||
/* | ||
* | ||
* 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.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; | ||
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; | ||
|
||
@ConditionalOnProperty(value = "security.enabled", havingValue = "false") | ||
@Configuration | ||
@EnableResourceServer | ||
public class DisableSecurityConfig extends ResourceServerConfigurerAdapter { | ||
|
||
@Override | ||
public void configure(HttpSecurity http) throws Exception { | ||
http.authorizeRequests().anyRequest().permitAll(); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/uk/ac/ebi/ega/accession/configuration/security/EnableSecurityConfig.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,72 @@ | ||
/* | ||
* | ||
* 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.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; | ||
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; | ||
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer; | ||
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter; | ||
import org.springframework.security.oauth2.provider.token.RemoteTokenServices; | ||
import uk.ac.ebi.ega.accession.user.AccessioningUserRepository; | ||
|
||
@ConditionalOnProperty(value = "security.enabled", havingValue = "true") | ||
@Configuration | ||
@EnableResourceServer | ||
public class EnableSecurityConfig extends ResourceServerConfigurerAdapter { | ||
|
||
private static final String[] AUTH_WHITELIST = { | ||
// -- swagger ui | ||
"/v2/api-docs", | ||
"/swagger-resources", | ||
"/swagger-resources/**", | ||
"/swagger-ui.html", | ||
"/webjars/**", | ||
"/" | ||
}; | ||
|
||
@Autowired | ||
private RemoteTokenServices remoteTokenServices; | ||
|
||
@Autowired | ||
private AccessioningUserRepository accessioningUserRepository; | ||
|
||
@Override | ||
public void configure(ResourceServerSecurityConfigurer resources) throws Exception { | ||
DefaultAccessTokenConverter defaultAccessTokenConverter = new DefaultAccessTokenConverter(); | ||
defaultAccessTokenConverter.setUserTokenConverter(new CustomUserAuthenticationConverter(accessioningUserRepository)); | ||
remoteTokenServices.setAccessTokenConverter(defaultAccessTokenConverter); | ||
} | ||
|
||
@Override | ||
public void configure(HttpSecurity http) throws Exception { | ||
http.authorizeRequests() | ||
.antMatchers(AUTH_WHITELIST).permitAll() | ||
.antMatchers("/users/**").hasRole("ADMIN") | ||
.antMatchers(HttpMethod.POST).hasAnyRole("EDITOR", "ADMIN") | ||
.antMatchers(HttpMethod.PUT).hasAnyRole("EDITOR", "ADMIN") | ||
.antMatchers(HttpMethod.PATCH).hasAnyRole("EDITOR", "ADMIN") | ||
.antMatchers(HttpMethod.DELETE).hasAnyRole("EDITOR", "ADMIN") | ||
.anyRequest().authenticated(); | ||
} | ||
} |
Oops, something went wrong.