-
Notifications
You must be signed in to change notification settings - Fork 1
notes-vci-token #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: notes-vci
Are you sure you want to change the base?
notes-vci-token #112
Changes from all commits
2b1600d
1ddecd8
cd85b4c
dc34b6a
314f774
b43d11b
ac815b9
5a0c345
7bce997
f2eb895
cea7df7
7772311
c0d5072
8685a1b
9eecb40
57027e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ plugins { | |
| } | ||
|
|
||
| group = 'es.in2' | ||
| version = '1.7.0' | ||
| version = '3.0.0' | ||
|
|
||
| java { | ||
| sourceCompatibility = '17' | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package es.in2.issuer.backend.oidc4vci.domain.service.impl; | ||
|
|
||
| import com.nimbusds.jose.JWSObject; | ||
| import com.nimbusds.jose.Payload; | ||
| import es.in2.issuer.backend.oidc4vci.domain.model.TokenResponse; | ||
| import es.in2.issuer.backend.oidc4vci.domain.service.TokenWorkflow; | ||
| import es.in2.issuer.backend.shared.domain.service.CredentialIssuanceRecordService; | ||
| import es.in2.issuer.backend.shared.domain.service.JWTService; | ||
| import es.in2.issuer.backend.shared.domain.util.JwtUtils; | ||
| import es.in2.issuer.backend.shared.infrastructure.config.AppConfig; | ||
| import es.in2.issuer.backend.shared.infrastructure.repository.CacheStore; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Service; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| import java.time.Instant; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.Map; | ||
| import java.util.NoSuchElementException; | ||
|
|
||
| import static es.in2.issuer.backend.oidc4vci.domain.util.Constants.ACCESS_TOKEN_EXPIRATION_TIME_MINUTES; | ||
| import static es.in2.issuer.backend.oidc4vci.domain.util.Constants.REFRESH_TOKEN_EXPIRATION_TIME_DAYS; | ||
| import static es.in2.issuer.backend.shared.domain.util.Constants.GRANT_TYPE; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class TokenWorkflowImpl implements TokenWorkflow { | ||
|
|
||
| private final CredentialIssuanceRecordService credentialIssuanceRecordService; | ||
| private final CacheStore<String> txCodeByPreAuthorizedCodeCacheStore; | ||
| private final JWTService jwtService; | ||
| private final AppConfig appConfig; | ||
| private final JwtUtils jwtUtils; | ||
|
|
||
| @Override | ||
| public Mono<TokenResponse> generateTokenResponse( | ||
| String grantType, | ||
| String preAuthorizedCode, | ||
| String txCode) { | ||
|
|
||
| return ensureGrantTypeIsPreAuthorizedCodeAndTxCodeAreCorrect(grantType, preAuthorizedCode, txCode) | ||
| .then(buildTokenResponse(preAuthorizedCode)); | ||
| } | ||
|
|
||
| private Mono<TokenResponse> buildTokenResponse(String preAuthorizedCode) { | ||
| Instant issueTime = Instant.now(); | ||
| long issueTimeEpochSeconds = issueTime.getEpochSecond(); | ||
| long accessTokenExpirationTimeEpochSeconds = generateAccessTokenExpirationTime(issueTime); | ||
| String accessToken = generateAccessToken(preAuthorizedCode, issueTimeEpochSeconds, accessTokenExpirationTimeEpochSeconds); | ||
| String tokenType = "bearer"; | ||
| long expiresIn = accessTokenExpirationTimeEpochSeconds - Instant.now().getEpochSecond(); | ||
| long refreshTokenExpirationTimeEpochSeconds = generateRefreshTokenExpirationTime(issueTime); | ||
| String refreshToken = generateRefreshToken(issueTimeEpochSeconds, refreshTokenExpirationTimeEpochSeconds); | ||
|
|
||
| return credentialIssuanceRecordService.getIdByPreAuthorizedCode(preAuthorizedCode) | ||
| .flatMap(credentialIssuanceRecordId -> { | ||
| String accessTokenJti = jwtUtils.getJti(accessToken); | ||
| String refreshTokenJti = jwtUtils.getJti(refreshToken); | ||
| return credentialIssuanceRecordService.setJtis( | ||
| credentialIssuanceRecordId, | ||
| accessTokenJti, | ||
| refreshTokenJti) | ||
| .thenReturn(TokenResponse.builder() | ||
| .accessToken(accessToken) | ||
| .tokenType(tokenType) | ||
| .expiresIn(expiresIn) | ||
| .refreshToken(refreshToken) | ||
| .build()); | ||
| }); | ||
| } | ||
|
|
||
| private String generateRefreshToken(long issueTimeEpochSeconds, long expirationTimeEpochSeconds) { | ||
| Payload payload = new Payload(Map.of( | ||
| "iss", appConfig.getIssuerBackendUrl(), | ||
| "iat", issueTimeEpochSeconds, | ||
| "exp", expirationTimeEpochSeconds | ||
| )); | ||
| return jwtService.generateJWT(payload.toString()); | ||
| } | ||
|
|
||
| private long generateAccessTokenExpirationTime(Instant issueTime) { | ||
| return issueTime.plus( | ||
| ACCESS_TOKEN_EXPIRATION_TIME_MINUTES, | ||
| ChronoUnit.MINUTES) | ||
| .getEpochSecond(); | ||
| } | ||
|
|
||
| private long generateRefreshTokenExpirationTime(Instant issueTime) { | ||
albertrodriguezin2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return issueTime.plus( | ||
| REFRESH_TOKEN_EXPIRATION_TIME_DAYS, | ||
| ChronoUnit.DAYS) | ||
| .getEpochSecond(); | ||
| } | ||
|
|
||
| private String generateAccessToken(String preAuthorizedCode, long issueTimeEpochSeconds, long expirationTimeEpochSeconds) { | ||
| Payload payload = new Payload(Map.of( | ||
| "iss", appConfig.getIssuerBackendUrl(), | ||
| "iat", issueTimeEpochSeconds, | ||
| "exp", expirationTimeEpochSeconds, | ||
| "jti", preAuthorizedCode | ||
| )); | ||
| return jwtService.generateJWT(payload.toString()); | ||
| } | ||
|
|
||
| private Mono<Void> ensureGrantTypeIsPreAuthorizedCodeAndTxCodeAreCorrect(String grantType, String preAuthorizedCode, String txCode) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. creus que es pot separar la reponsabilitat de ensureGrantTypeIsPreAuthorizedCodeAndTxCodeAreCorrect()? Per un costat el grant_type i per altre el pre-authorized_code y el tx_code?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Abans ho teníem per separat (a main està així) i em vas demanar que ho unís en una sola funció.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Separar-ho |
||
| if (!GRANT_TYPE.equals(grantType)) { | ||
| return Mono.error(new IllegalArgumentException("Invalid grant type")); | ||
| } | ||
|
|
||
| return txCodeByPreAuthorizedCodeCacheStore | ||
| .get(preAuthorizedCode) | ||
| .onErrorMap(NoSuchElementException.class, ex -> new IllegalArgumentException("Invalid pre-authorized code")) | ||
| .flatMap(cacheTxCode -> | ||
| cacheTxCode.equals(txCode) | ||
| ? Mono.empty() | ||
| : Mono.error(new IllegalArgumentException("Invalid tx code")) | ||
| ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.