-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial jwt auth setup * refactor JWTConverter * adds test for jwt auth * use existing object id * remove commented code * make requested formating changes and add retry template * add more and clean up JWTSecurityTests * make requested changes, update variables and fix typo
- Loading branch information
jaserud
authored
Aug 24, 2020
1 parent
1268b8f
commit 12863cd
Showing
17 changed files
with
937 additions
and
55 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
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
75 changes: 75 additions & 0 deletions
75
score-server/src/main/java/bio/overture/score/server/config/RetryConfig.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,75 @@ | ||
/* | ||
* Copyright (c) 2018. Ontario Institute for Cancer Research | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package bio.overture.score.server.config; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import lombok.val; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.retry.backoff.BackOffPolicy; | ||
import org.springframework.retry.backoff.ExponentialBackOffPolicy; | ||
import org.springframework.retry.policy.SimpleRetryPolicy; | ||
import org.springframework.retry.support.RetryTemplate; | ||
import org.springframework.web.client.HttpServerErrorException; | ||
import org.springframework.web.client.ResourceAccessException; | ||
|
||
import java.util.Map; | ||
|
||
import static java.lang.Boolean.TRUE; | ||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.springframework.retry.backoff.ExponentialBackOffPolicy.DEFAULT_MULTIPLIER; | ||
|
||
@Configuration | ||
public class RetryConfig { | ||
|
||
private static final int DEFAULT_MAX_RETRIES = 5; | ||
private static final long DEFAULT_INITIAL_BACKOFF_INTERVAL = SECONDS.toMillis(15L); | ||
|
||
@Value("${auth.connection.maxRetries}") | ||
private int maxRetries = DEFAULT_MAX_RETRIES; | ||
|
||
@Value("${auth.connection.initialBackoff}") | ||
private long initialBackoff = DEFAULT_INITIAL_BACKOFF_INTERVAL; | ||
|
||
@Value("${auth.connection.multiplier}") | ||
private double multiplier = DEFAULT_MULTIPLIER; | ||
|
||
@Bean | ||
public RetryTemplate retryTemplate() { | ||
val result = new RetryTemplate(); | ||
result.setBackOffPolicy(defineBackOffPolicy()); | ||
|
||
result.setRetryPolicy( | ||
new SimpleRetryPolicy(maxRetries, getRetryableExceptions(), true)); | ||
return result; | ||
} | ||
|
||
private BackOffPolicy defineBackOffPolicy() { | ||
val backOffPolicy = new ExponentialBackOffPolicy(); | ||
backOffPolicy.setInitialInterval(initialBackoff); | ||
backOffPolicy.setMultiplier(multiplier); | ||
|
||
return backOffPolicy; | ||
} | ||
|
||
private static Map<Class<? extends Throwable>, Boolean> getRetryableExceptions() { | ||
return ImmutableMap.of( | ||
ResourceAccessException.class, TRUE, | ||
HttpServerErrorException.class, TRUE); | ||
} | ||
} |
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
80 changes: 80 additions & 0 deletions
80
score-server/src/main/java/bio/overture/score/server/config/TokenServicesConfig.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,80 @@ | ||
package bio.overture.score.server.config; | ||
|
||
import bio.overture.score.server.security.*; | ||
import lombok.NonNull; | ||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.retry.support.RetryTemplate; | ||
import org.springframework.security.oauth2.provider.token.AccessTokenConverter; | ||
import org.springframework.security.oauth2.provider.token.DefaultTokenServices; | ||
import org.springframework.security.oauth2.provider.token.RemoteTokenServices; | ||
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
@Slf4j | ||
@Profile("secure") | ||
public class TokenServicesConfig { | ||
|
||
@Value("${auth.server.url}") private String checkTokenUrl; | ||
@Value("${auth.server.tokenName:token}") private String tokenName; | ||
@Value("${auth.server.clientId}") private String clientId; | ||
@Value("${auth.server.clientSecret}") private String clientSecret; | ||
|
||
@Bean | ||
@Profile("!jwt") | ||
public RemoteTokenServices remoteTokenServices() { | ||
return createRemoteTokenServices(); | ||
} | ||
|
||
@Bean | ||
@Autowired | ||
@Profile("jwt") | ||
public MergedServerTokenServices mergedServerTokenServices( | ||
@NonNull PublicKeyFetcher publicKeyFetcher, | ||
@NonNull RetryTemplate retryTemplate | ||
) { | ||
val jwtTokenServices = createJwtTokenServices(publicKeyFetcher.getPublicKey()); | ||
val remoteTokenServices = createRemoteTokenServices(); | ||
return new MergedServerTokenServices(jwtTokenServices, remoteTokenServices, retryTemplate); | ||
} | ||
|
||
@Bean | ||
@Autowired | ||
@Profile("jwt") | ||
public PublicKeyFetcher publicKeyFetcher( | ||
@Value("${auth.jwt.publicKeyUrl}") @NonNull String publicKeyUrl, | ||
@NonNull RetryTemplate retryTemplate) { | ||
return new DefaultPublicKeyFetcher(publicKeyUrl, new RestTemplate(), retryTemplate); | ||
} | ||
|
||
private AccessTokenConverter accessTokenConverter() { | ||
return new AccessTokenConverterWithExpiry(); | ||
} | ||
|
||
private RemoteTokenServices createRemoteTokenServices() { | ||
val remoteTokenServices = new CachingRemoteTokenServices(); | ||
remoteTokenServices.setCheckTokenEndpointUrl(checkTokenUrl); | ||
remoteTokenServices.setClientId(clientId); | ||
remoteTokenServices.setClientSecret(clientSecret); | ||
remoteTokenServices.setTokenName(tokenName); | ||
remoteTokenServices.setAccessTokenConverter(accessTokenConverter()); | ||
|
||
log.debug("using auth server: " + checkTokenUrl); | ||
|
||
return remoteTokenServices; | ||
} | ||
|
||
private DefaultTokenServices createJwtTokenServices(String publicKey) { | ||
val tokenStore = new JwtTokenStore(new JWTConverter(publicKey)); | ||
val defaultTokenServices = new DefaultTokenServices(); | ||
defaultTokenServices.setTokenStore(tokenStore); | ||
return defaultTokenServices; | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
score-server/src/main/java/bio/overture/score/server/security/DefaultPublicKeyFetcher.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,21 @@ | ||
package bio.overture.score.server.security; | ||
|
||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.val; | ||
import org.springframework.retry.support.RetryTemplate; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@RequiredArgsConstructor | ||
public class DefaultPublicKeyFetcher implements PublicKeyFetcher { | ||
|
||
@NonNull private final String url; | ||
@NonNull private final RestTemplate restTemplate; | ||
@NonNull private final RetryTemplate retryTemplate; | ||
|
||
@Override | ||
public String getPublicKey() { | ||
val resp = retryTemplate.execute(x -> restTemplate.getForEntity(url, String.class)); | ||
return resp.hasBody() ? resp.getBody() : null; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
score-server/src/main/java/bio/overture/score/server/security/JWTConverter.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,52 @@ | ||
package bio.overture.score.server.security; | ||
|
||
import lombok.NonNull; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
import org.springframework.security.oauth2.provider.OAuth2Authentication; | ||
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; | ||
|
||
import java.util.*; | ||
|
||
@Slf4j | ||
public class JWTConverter extends JwtAccessTokenConverter { | ||
|
||
private final static String CONTEXT = "context"; | ||
private final static String SCOPE = "scope"; | ||
|
||
@SneakyThrows | ||
public JWTConverter(String publicKey) { | ||
super(); | ||
this.setVerifierKey(publicKey); | ||
this.afterPropertiesSet(); | ||
} | ||
|
||
@Override | ||
public OAuth2Authentication extractAuthentication(@NonNull Map<String, ?> map) { | ||
// Currently EGO's JWT spec places scopes in map at 'context.scope' | ||
// but extractAuthentication expects them in map's root at 'scope' | ||
// so put all scopes into root level for spring security processing | ||
val allScopes = getRootAndContextScopes(map); | ||
HashMap<String, Object> updatedMap = new HashMap<>(map); | ||
updatedMap.put(SCOPE, allScopes); | ||
|
||
return super.extractAuthentication(updatedMap); | ||
} | ||
|
||
private Collection<String> getRootAndContextScopes(Map<String, ?> map) { | ||
List<String> extractedScopes = new ArrayList<>(Collections.emptyList()); | ||
try { | ||
if (map.containsKey(CONTEXT)) { | ||
val context = (Map<String, Object>) map.get(CONTEXT); | ||
extractedScopes.addAll((Collection<String>) context.get(SCOPE)); | ||
} | ||
if (map.containsKey(SCOPE)) { | ||
extractedScopes.addAll((Collection<String>) map.get(SCOPE)); | ||
} | ||
} catch (Exception e) { | ||
log.error("Failed to extract scopes from JWT"); | ||
} | ||
return extractedScopes; | ||
} | ||
} |
Oops, something went wrong.