Skip to content

Commit

Permalink
Merge pull request #474 from navikt/with-any-claim-values
Browse files Browse the repository at this point in the history
Støtt bruk av asterisk som claims-verdi i ProtectedWithClaims
  • Loading branch information
jan-olaveide authored Feb 19, 2022
2 parents a308ad5 + be16418 commit 527c472
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ new ResourceConfig()
This example shows

- First method - An unprotected endpoint. No token is required to use this endpoint.
- Second method - A protected endpoint. This endpoint will require a valid token from the "employee" issuer.
- Third method - A protected endpoint. This endpoint will require a valid token from one of the configured issuers.
- Fourth method - A non-annotated endpoint. This endpoint will not be accessible from outside the server (will return a 501 NOT_IMPLEMENTED).

- Second method - A protected endpoint. This endpoint will require a valid token from one of the configured issuers.
- Third method - A protected endpoint. This endpoint will require a valid token from the "employee" or "manager" issuer.
- Fourth method - A protected endpoint. This endpoint will require a valid token from the "manager" issuer and a claim where key is "acr" and value is "Level4".
- Fifth method - A non-annotated endpoint. This endpoint will not be accessible from outside the server (will return a 501 NOT_IMPLEMENTED).
```java
@Path("/rest")
public class ProductResource {
Expand All @@ -181,6 +181,16 @@ public class ProductResource {
public Product add(Product product) {
return service.create(product);
}

@PUT
@PATH("/product")
@RequiredIssuers(value = {
ProtectedWithClaims(issuer = "employee"),
ProtectedWithClaims(issuer = "manager")
})
public Product add(Product product) {
return service.update(product);
}

@DELETE
@PATH("/product/{id}")
Expand All @@ -189,8 +199,17 @@ public class ProductResource {
return service.delete(id);
}

@GET
@PATH("/product/{id}")
public void add(String id) {
return service.get(id);
}
}
```

The claimMap in **`@ProtectedWithClaims`** can contain entries where the expected value is an asterisk, e.g.: **`"acr=*"`**. This will require that the claim is present in the token, without regards to its value.


### token-validation-ktor

See demo application in **`token-validation-ktor-demo`** for example configurations and setups.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
@Target({ TYPE, METHOD })
@Protected
public @interface ProtectedWithClaims {

String issuer();
/**
* Required claims in token in key=value format
* Required claims in token in key=value format.
* If the value is an asterisk (*), it checks that the required key is present.
* @return array containing claims as key=value
*/
String[] claimMap() default {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public boolean containsClaim(String name, String value) {
if (claim == null) {
return false;
}
if (value.equals("*")) {
return true;
}
if (claim instanceof String) {
String claimAsString = (String) claim;
return claimAsString.equals(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,32 @@ class JwtTokenClaimsTest {
@Test
void containsClaimShouldHandleBothStringAndListClaim() {
assertThat(
withClaim("arrayClaim", List.of("1","2")).containsClaim("arrayClaim", "1")
withClaim("arrayClaim", List.of("1", "2")).containsClaim("arrayClaim", "1")
).isTrue();
assertThat(
withClaim("stringClaim", "1").containsClaim("stringClaim", "1")
).isTrue();
}

@Test
void containsClaimShouldHandleAsterisk() {
assertThat(
withClaim("stringClaim", "1").containsClaim("stringClaim", "*")
).isTrue();
assertThat(
withClaim("emptyStringClaim", "").containsClaim("emptyStringClaim", "*")
).isTrue();
assertThat(
withClaim("nullStringClaim", null).containsClaim("nullStringClaim", "*")
).isFalse();
assertThat(
withClaim("arrayClaim", List.of("1", "2")).containsClaim("arrayClaim", "*")
).isTrue();
assertThat(
withClaim("emptyArrayClaim", List.of()).containsClaim("emptyArrayClaim", "*")
).isTrue();
}

private JwtTokenClaims withClaim(String name, Object value) {
var claims = new JWTClaimsSet.Builder().claim(name, value).build();
//do json parsing to simulate usage when creating from token
Expand Down

0 comments on commit 527c472

Please sign in to comment.