-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
dmitrys
committed
Jul 18, 2024
1 parent
15b5db8
commit 05597d3
Showing
6 changed files
with
110 additions
and
61 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
52 changes: 52 additions & 0 deletions
52
datanode/src/main/java/com/odysseusinc/arachne/datanode/controller/Authenticator.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 com.odysseusinc.arachne.datanode.controller; | ||
|
||
import lombok.Getter; | ||
import org.ohdsi.authenticator.converter.TokenInfoToTokenConverter; | ||
import org.ohdsi.authenticator.converter.TokenInfoToUserInfoConverter; | ||
import org.ohdsi.authenticator.exception.MethodNotSupportedAuthenticationException; | ||
import org.ohdsi.authenticator.model.TokenInfo; | ||
import org.ohdsi.authenticator.model.UserInfo; | ||
import org.ohdsi.authenticator.service.AuthService; | ||
import org.ohdsi.authenticator.service.authentication.AuthServiceProvider; | ||
import org.ohdsi.authenticator.service.authentication.TokenProvider; | ||
import org.pac4j.core.credentials.Credentials; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Getter | ||
@Service | ||
public class Authenticator { | ||
private final TokenProvider tokenProvider; | ||
private final AuthServiceProvider authServiceProvider; | ||
private final TokenInfoToTokenConverter tokenInfoToTokenConverter; | ||
private final TokenInfoToUserInfoConverter tokenInfoToUserInfoConverter; | ||
|
||
public Authenticator(TokenProvider tokenProvider, AuthServiceProvider authServiceProvider) { | ||
this.tokenProvider = tokenProvider; | ||
this.authServiceProvider = authServiceProvider; | ||
this.tokenInfoToTokenConverter = new TokenInfoToTokenConverter(tokenProvider); | ||
this.tokenInfoToUserInfoConverter = new TokenInfoToUserInfoConverter(); | ||
} | ||
|
||
public UserInfo authenticate(String method, Credentials request) { | ||
AuthService authService = authServiceProvider.getByMethod(method).orElseThrow(MethodNotSupportedAuthenticationException::new); | ||
TokenInfo authentication = authService.authenticate(request); | ||
String token = tokenInfoToTokenConverter.toToken(authentication); | ||
return tokenInfoToUserInfoConverter.toUserInfo(authentication, token); | ||
} | ||
|
||
public String resolveUsername(String token) { | ||
return tokenProvider.resolveValue(token, "sub", String.class); | ||
} | ||
|
||
public UserInfo refreshToken(String token) { | ||
TokenInfo tokenInfo = tokenInfoToTokenConverter.toTokenInfo(token); | ||
AuthService authService = authServiceProvider.getByMethod(tokenInfo.getAuthMethod()).orElseThrow(MethodNotSupportedAuthenticationException::new); | ||
TokenInfo newAuthentication = authService.refreshToken(tokenInfo); | ||
String newToken = tokenInfoToTokenConverter.toToken(newAuthentication); | ||
return tokenInfoToUserInfoConverter.toUserInfo(newAuthentication, newToken); | ||
} | ||
|
||
public void invalidateToken(String token) { | ||
this.tokenProvider.invalidateToken(token); | ||
} | ||
} |
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