-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
record JTI when creating access tokens
- Loading branch information
1 parent
3845b4a
commit 146e4f6
Showing
6 changed files
with
114 additions
and
22 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
...ed/src/main/java/org/eclipse/edc/iam/identitytrust/sts/embedded/AccessTokenDecorator.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 @@ | ||
/* | ||
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.iam.identitytrust.sts.embedded; | ||
|
||
import org.eclipse.edc.spi.iam.TokenParameters; | ||
import org.eclipse.edc.token.spi.TokenDecorator; | ||
|
||
import java.time.Instant; | ||
import java.util.Date; | ||
import java.util.Map; | ||
|
||
import static org.eclipse.edc.jwt.spi.JwtRegisteredClaimNames.EXPIRATION_TIME; | ||
import static org.eclipse.edc.jwt.spi.JwtRegisteredClaimNames.ISSUED_AT; | ||
import static org.eclipse.edc.jwt.spi.JwtRegisteredClaimNames.JWT_ID; | ||
import static org.eclipse.edc.jwt.spi.JwtRegisteredClaimNames.NOT_BEFORE; | ||
|
||
public class AccessTokenDecorator implements TokenDecorator { | ||
|
||
private final String jti; | ||
private final Instant now; | ||
private final Instant expiration; | ||
private final Map<String, String> claims; | ||
|
||
public AccessTokenDecorator(String jti, Instant now, Instant expiration, Map<String, String> claims) { | ||
this.jti = jti; | ||
this.now = now; | ||
this.expiration = expiration; | ||
this.claims = claims; | ||
} | ||
|
||
@Override | ||
public TokenParameters.Builder decorate(TokenParameters.Builder tokenParameters) { | ||
this.claims.forEach(tokenParameters::claims); | ||
return tokenParameters | ||
.claims(ISSUED_AT, Date.from(now)) | ||
.claims(NOT_BEFORE, Date.from(now)) | ||
.claims(EXPIRATION_TIME, Date.from(expiration)) | ||
.claims(JWT_ID, jti); | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
...rc/test/java/org/eclipse/edc/iam/identitytrust/sts/embedded/AccessTokenDecoratorTest.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,44 @@ | ||
/* | ||
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.iam.identitytrust.sts.embedded; | ||
|
||
import org.eclipse.edc.spi.iam.TokenParameters; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.Instant; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.eclipse.edc.jwt.spi.JwtRegisteredClaimNames.EXPIRATION_TIME; | ||
import static org.eclipse.edc.jwt.spi.JwtRegisteredClaimNames.ISSUED_AT; | ||
import static org.eclipse.edc.jwt.spi.JwtRegisteredClaimNames.JWT_ID; | ||
import static org.eclipse.edc.jwt.spi.JwtRegisteredClaimNames.NOT_BEFORE; | ||
|
||
class AccessTokenDecoratorTest { | ||
|
||
@Test | ||
void verifyExpectedClaims() { | ||
var builder = TokenParameters.Builder.newInstance(); | ||
var now = Instant.now(); | ||
var decorator = new AccessTokenDecorator("test-id", now, now.plusSeconds(5), Map.of("claim1", "value1")); | ||
decorator.decorate(builder); | ||
|
||
var tokenParams = builder.build(); | ||
assertThat(tokenParams.getClaims()) | ||
.containsEntry("claim1", "value1") | ||
.containsEntry(JWT_ID, "test-id") | ||
.containsKeys(ISSUED_AT, EXPIRATION_TIME, NOT_BEFORE); | ||
} | ||
} |
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