-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…-config # Conflicts: # src/main/java/com/faforever/api/data/domain/package-info.java
- Loading branch information
Showing
10 changed files
with
200 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
123 changes: 123 additions & 0 deletions
123
src/main/java/com/faforever/api/security/FafMultiTokenStore.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,123 @@ | ||
package com.faforever.api.security; | ||
|
||
import com.faforever.api.config.FafApiProperties; | ||
import org.springframework.security.jwt.Jwt; | ||
import org.springframework.security.jwt.JwtHelper; | ||
import org.springframework.security.oauth2.common.OAuth2AccessToken; | ||
import org.springframework.security.oauth2.common.OAuth2RefreshToken; | ||
import org.springframework.security.oauth2.common.util.JsonParser; | ||
import org.springframework.security.oauth2.common.util.JsonParserFactory; | ||
import org.springframework.security.oauth2.provider.OAuth2Authentication; | ||
import org.springframework.security.oauth2.provider.token.TokenStore; | ||
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; | ||
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; | ||
import org.springframework.security.oauth2.provider.token.store.jwk.JwkTokenStore; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
@Component | ||
public class FafMultiTokenStore implements TokenStore { | ||
private JsonParser objectMapper = JsonParserFactory.create(); | ||
|
||
/** | ||
* The token store for token created by this API (using custom OAuth 2.0 without OpenID Connect) | ||
*/ | ||
private final JwtTokenStore classicTokenStore; | ||
|
||
/** | ||
* The token store for tokens created by Ory Hydra (OpenID Connect token customized for FAF) | ||
*/ | ||
private final JwkTokenStore hydraTokenStore; | ||
|
||
private final FafApiProperties fafApiProperties; | ||
|
||
public FafMultiTokenStore(FafApiProperties fafApiProperties, JwtAccessTokenConverter jwtAccessTokenConverter) { | ||
this.fafApiProperties = fafApiProperties; | ||
|
||
classicTokenStore = new JwtTokenStore(jwtAccessTokenConverter); | ||
hydraTokenStore = new JwkTokenStore(fafApiProperties.getJwt().getFafHydraJwksUrl(), jwtAccessTokenConverter); | ||
} | ||
|
||
@Override | ||
public OAuth2Authentication readAuthentication(OAuth2AccessToken token) { | ||
return readAuthentication(token.getValue()); | ||
} | ||
|
||
@Override | ||
public OAuth2Authentication readAuthentication(String token) { | ||
Jwt unverifiedJwt = JwtHelper.decode(token); | ||
Map<String, Object> claims = objectMapper.parseMap(unverifiedJwt.getClaims()); | ||
|
||
if (Objects.equals(claims.get("iss"), fafApiProperties.getJwt().getFafHydraIssuer())) { | ||
return hydraTokenStore.readAuthentication(token); | ||
} else { | ||
return classicTokenStore.readAuthentication(token); | ||
} | ||
} | ||
|
||
@Override | ||
public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) { | ||
// no implementation, equal to JwtTokenStore | ||
} | ||
|
||
@Override | ||
public OAuth2AccessToken readAccessToken(String tokenValue) { | ||
try { | ||
return classicTokenStore.readAccessToken(tokenValue); | ||
} catch (Exception e) { | ||
return hydraTokenStore.readAccessToken(tokenValue); | ||
} | ||
} | ||
|
||
@Override | ||
public void removeAccessToken(OAuth2AccessToken token) { | ||
// no implementation, equal to JwtTokenStore | ||
} | ||
|
||
@Override | ||
public void storeRefreshToken(OAuth2RefreshToken refreshToken, OAuth2Authentication authentication) { | ||
// no implementation, equal to JwtTokenStore | ||
} | ||
|
||
@Override | ||
public OAuth2RefreshToken readRefreshToken(String tokenValue) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public OAuth2Authentication readAuthenticationForRefreshToken(OAuth2RefreshToken token) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void removeRefreshToken(OAuth2RefreshToken token) { | ||
// no implementation, equal to JwtTokenStore | ||
} | ||
|
||
@Override | ||
public void removeAccessTokenUsingRefreshToken(OAuth2RefreshToken refreshToken) { | ||
// no implementation, equal to JwtTokenStore | ||
} | ||
|
||
@Override | ||
public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) { | ||
// equal to JwtTokenStore | ||
return null; | ||
} | ||
|
||
@Override | ||
public Collection<OAuth2AccessToken> findTokensByClientIdAndUserName(String clientId, String userName) { | ||
// equal to JwtTokenStore | ||
return Collections.emptySet(); | ||
} | ||
|
||
@Override | ||
public Collection<OAuth2AccessToken> findTokensByClientId(String clientId) { | ||
// equal to JwtTokenStore | ||
return Collections.emptySet(); | ||
} | ||
} |
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