Skip to content

Commit

Permalink
Migrate from Auth0 rule to action
Browse files Browse the repository at this point in the history
  • Loading branch information
ch4mpy committed Jun 2, 2023
1 parent 748df2e commit 6387375
Show file tree
Hide file tree
Showing 29 changed files with 87 additions and 91 deletions.
Binary file removed .readme_resources/auth0-user-data-rule.png
Binary file not shown.
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Using such libs is dead simple: just declare depedency on one of those libs and
jsonObjectClaims = { @JsonObjectClaim(name = "obj1", value = obj1), @JsonObjectClaim(name = "obj2", value = obj2)},
jsonObjectArrayClaims = @JsonObjectArrayClaim(name = "objArr1", value = { obj3, obj4}),
nestedClaims = { @NestedClaims(
name = "https://c4-soft.com/spring-addons",
name = "https://c4-soft.com/user",
intClaims = { @IntClaim(name = "nested_int1", value = 42), @IntClaim(name = "nested_int2", value = 51) },
longClaims = { @LongClaim(name = "nested_long1", value = 42), @LongClaim(name = "nested_long2", value = 51) },
doubleClaims = { @DoubleClaim(name = "nested_double1", value = 4.2), @DoubleClaim(name = "nested_double2", value = 5.1) },
Expand Down
36 changes: 16 additions & 20 deletions samples/tutorials/auth0.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,26 @@ The issuer to configure in tutorials is `https://{Domain}/`. The "Domain" placeh

![Application details](https://github.com/ch4mpy/spring-addons/blob/master/.readme_resources/auth0-application-details.png)

Next, create a rule to enrich the access tokens with user data:
- browse to "Auth Pipeline -> Rules"
- click "+ Create" and then "<> Empty rule"
- enter `Add user data to access and ID tokens` as "Name"
- set the following rule script:
Next, create an action to enrich the access tokens with user data:
- browse to "Actions -> Flows -> Login"
- click "+ Add Action" and then "Build Custom"
- enter `Add user data to access and ID tokens` as "Name" and keep "Login / Post Login" as well as default Runtime version
- script body:
```typescript
function addUserData(user, context, callback) {
context.accessToken['https://c4-soft.com/spring-addons'] = user;
context.idToken['https://c4-soft.com/spring-addons'] = user;
return callback(null, user, context);
}
exports.onExecutePostLogin = async (event, api) => {
const namespace = 'https://c4-soft.com';
const user = Object.assign({}, event.user);
user.roles = event.authorization?.roles || [];
api.accessToken.setCustomClaim(`${namespace}/user`, user);
api.idToken.setCustomClaim(`${namespace}/user`, user);
return; // success
};
```
![Rule to add user data to access tokens](https://github.com/ch4mpy/spring-addons/blob/master/.readme_resources/auth0-user-data-rule.png)

From the left menu, select "User Management -> Users" and add at least a user for yourself.

Select "Extensions" from the left menu and:
- install `Auth0 Authorization`
- click "Auth0 Authorization" to navigate to "Authorization Extension" details
- click "Go To Configuration"
- enable `Groups`, `Roles` and `Permissions` toggles
- click "ROTATE"
- click "PUBLISH RULE"
- from the left menu, click "Roles" and add a `NICE` role
- from the left menu, click "Users", open one of the users details, browse to "Roles" tab, click "+ ADD ROLE TO USER", and assign the `NICE` role
From the left menu, click "Roles" and add a `NICE` role

From the left menu, click "Users", open one of the users details, browse to "Roles" tab, click "+ ADD ROLE TO USER", and assign the `NICE` role

You're all set to update tutorials configuration with your own Auth0 instance & confidential client
8 changes: 4 additions & 4 deletions samples/tutorials/bff/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@ com:
authorities:
- path: cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
client:
client-uri: ${gateway-uri}
security-matchers: /**
Expand Down Expand Up @@ -427,7 +427,7 @@ com:
authorities:
- path: cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ com:
authorities:
- path: cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
client:
client-uri: ${gateway-uri}
security-matchers: /**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ com:
authorities:
- path: cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
permit-all:
- "/public/**"
- "/actuator/health/readiness"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ com:
authorities:
- path: cognito:groups
- location: https://dev-ch4mpy.eu.auth0.com
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
---
scheme: https
keycloak-port: 8443
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ com:
authorities:
- path: cognito:groups
- location: https://dev-ch4mpy.eu.auth0.com
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
permit-all:
- "/actuator/health/readiness"
- "/actuator/health/liveness"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ com:
authorities:
- path: cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions

---
scheme: https
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ com:
authorities:
- path: cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
permit-all:
- "/actuator/health/readiness"
- "/actuator/health/liveness"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ com:
security:
issuers:
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: $['https://c4-soft.com/spring-addons']['roles']
- path: $['https://c4-soft.com/spring-addons']['permissions']
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
spring:
security:
oauth2:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ com:
security:
issuers:
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: $['https://c4-soft.com/spring-addons']['roles']
- path: $['https://c4-soft.com/spring-addons']['permissions']
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
spring:
security:
oauth2:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ com:
authorities:
- path: cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ com:
authorities:
- path: cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ com:
authorities:
- path: cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions

---
scheme: https
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ com:
authorities:
- path: cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
permit-all:
- "/actuator/health/readiness"
- "/actuator/health/liveness"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ com:
authorities:
- path: cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
permit-all:
- "/greet/public"
- "/actuator/health/readiness"
Expand Down
4 changes: 2 additions & 2 deletions samples/tutorials/resource-server_with_ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ com:
authorities:
- path: $.cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: $.roles
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
permit-all:
- /actuator/health/readiness
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ com:
authorities:
- path: $.cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: $.roles
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
permit-all:
- /actuator/health/readiness
Expand Down
6 changes: 3 additions & 3 deletions samples/webflux-jwt-default/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ com:
authorities:
- path: cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
permit-all:
- "/greet/public"
- "/actuator/health/readiness"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ com:
authorities:
- path: cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
permit-all:
- "/greet/public"
- "/actuator/health/readiness"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ com:
authorities:
- path: cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
permit-all:
- "/greet/public"
- "/actuator/health/readiness"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ com:
authorities:
- path: cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
permit-all:
- "/greet/public"
- "/actuator/health/readiness"
Expand Down
6 changes: 3 additions & 3 deletions samples/webmvc-jwt-default/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ com:
authorities:
- path: cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
permit-all:
- "/greet/public"
- "/actuator/health/readiness"
Expand Down
6 changes: 3 additions & 3 deletions samples/webmvc-jwt-default/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ com:
authorities:
- path: cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
permit-all:
- "/greet/public"
- "/actuator/health/readiness"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void givenUserIsGrantedWithAuthorizedPersonnel_whenGetSecuredMethod_thenOk() thr
jsonObjectClaims = { @JsonObjectClaim(name = "obj1", value = obj1), @JsonObjectClaim(name = "obj2", value = obj2)},
jsonObjectArrayClaims = @JsonObjectArrayClaim(name = "objArr1", value = { obj3, obj4}),
nestedClaims = { @NestedClaims(
name = "https://c4-soft.com/spring-addons",
name = "https://c4-soft.com/user",
intClaims = { @IntClaim(name = "nested_int1", value = 42), @IntClaim(name = "nested_int2", value = 51) },
longClaims = { @LongClaim(name = "nested_long1", value = 42), @LongClaim(name = "nested_long2", value = 51) },
doubleClaims = { @DoubleClaim(name = "nested_double1", value = 4.2), @DoubleClaim(name = "nested_double2", value = 5.1) },
Expand All @@ -187,6 +187,6 @@ void givenUserIsGrantedWithAuthorizedPersonnel_whenGetSecuredMethod_thenOk() thr
void givenUserIsAuthenticated_whenGetClaims_thenOk() throws Exception {
api.get("/claims").andExpect(status().isOk()).andExpect(
content().string(
"{\"sub\":\"Ch4mpy\",\"objArr1\":[{\"prop1_1\":{\"nested1_1_1\":\"value1\"},\"prop1_2\":{\"nested1_2_1\":221}},{\"prop2_2\":{\"nested2_2_1\":221},\"prop2_1\":{\"nested2_1_1\":\"value2\"}}],\"strArr1\":[\"a\",\"b\",\"c\"],\"strArr2\":[\"D\",\"E\",\"F\"],\"preferred_username\":\"user\",\"long2\":51,\"int2\":51,\"int1\":42,\"long1\":42,\"url1\":\"https://localhost:8080/greet\",\"url2\":\"https://localhost:4200/home\",\"str1\":\"String 1\",\"str2\":\"String 2\",\"address\":{},\"email_verified\":false,\"obj2\":{\"prop2_1\":{\"nested2_1_1\":{\"nested2_1_1_1\":2111}}},\"obj1\":{\"prop1_1\":{\"nested1_1_1\":\"value1\"},\"prop1_2\":{\"nested1_2_1\":121}},\"phone_number_verified\":false,\"date1\":\"2023-04-04T00:42:00.000+00:00\",\"https://c4-soft.com/spring-addons\":{\"nested_int1\":42,\"nested_int2\":51,\"nested_str2\":\"String 2\",\"nested_str1\":\"String 1\",\"nested_objArr1\":[{\"prop1_1\":{\"nested1_1_1\":\"value1\"},\"prop1_2\":{\"nested1_2_1\":221}},{\"prop2_2\":{\"nested2_2_1\":221},\"prop2_1\":{\"nested2_1_1\":\"value2\"}}],\"nested_strArr1\":[\"a\",\"b\",\"c\"],\"nested_obj2\":{\"prop2_1\":{\"nested2_1_1\":{\"nested2_1_1_1\":2111}}},\"nested_strArr2\":[\"D\",\"E\",\"F\"],\"nested_obj1\":{\"prop1_1\":{\"nested1_1_1\":\"value1\"},\"prop1_2\":{\"nested1_2_1\":121}},\"nested_double2\":5.1,\"nested_double1\":4.2,\"nested_epoch2\":\"2023-04-04T22:42:52.000+00:00\",\"nested_epoch1\":\"2022-12-14T00:40:00.000+00:00\",\"nested_long2\":51,\"nested_long1\":42,\"nested_url1\":\"https://localhost:8080/greet\",\"nested_url2\":\"https://localhost:4200/home\",\"nested_date1\":\"2023-04-04T00:42:00.000+00:00\",\"nested_uri1\":\"https://localhost:8080/greet\",\"nested_uri2\":\"https://localhost:4200/home#greet\"},\"uri2\":\"https://localhost:4200/home#greet\",\"uri1\":\"https://localhost:8080/greet\",\"double2\":5.1,\"double1\":4.2,\"epoch2\":\"2023-04-04T22:42:52.000+00:00\",\"epoch1\":\"2022-12-14T00:40:00.000+00:00\"}"));
"{\"sub\":\"Ch4mpy\",\"objArr1\":[{\"prop1_1\":{\"nested1_1_1\":\"value1\"},\"prop1_2\":{\"nested1_2_1\":221}},{\"prop2_2\":{\"nested2_2_1\":221},\"prop2_1\":{\"nested2_1_1\":\"value2\"}}],\"strArr1\":[\"a\",\"b\",\"c\"],\"strArr2\":[\"D\",\"E\",\"F\"],\"preferred_username\":\"user\",\"long2\":51,\"int2\":51,\"int1\":42,\"long1\":42,\"url1\":\"https://localhost:8080/greet\",\"url2\":\"https://localhost:4200/home\",\"str1\":\"String 1\",\"str2\":\"String 2\",\"address\":{},\"email_verified\":false,\"obj2\":{\"prop2_1\":{\"nested2_1_1\":{\"nested2_1_1_1\":2111}}},\"obj1\":{\"prop1_1\":{\"nested1_1_1\":\"value1\"},\"prop1_2\":{\"nested1_2_1\":121}},\"phone_number_verified\":false,\"date1\":\"2023-04-04T00:42:00.000+00:00\",\"uri2\":\"https://localhost:4200/home#greet\",\"uri1\":\"https://localhost:8080/greet\",\"double2\":5.1,\"https://c4-soft.com/user\":{\"nested_int1\":42,\"nested_int2\":51,\"nested_str2\":\"String 2\",\"nested_str1\":\"String 1\",\"nested_objArr1\":[{\"prop1_1\":{\"nested1_1_1\":\"value1\"},\"prop1_2\":{\"nested1_2_1\":221}},{\"prop2_2\":{\"nested2_2_1\":221},\"prop2_1\":{\"nested2_1_1\":\"value2\"}}],\"nested_strArr1\":[\"a\",\"b\",\"c\"],\"nested_obj2\":{\"prop2_1\":{\"nested2_1_1\":{\"nested2_1_1_1\":2111}}},\"nested_strArr2\":[\"D\",\"E\",\"F\"],\"nested_obj1\":{\"prop1_1\":{\"nested1_1_1\":\"value1\"},\"prop1_2\":{\"nested1_2_1\":121}},\"nested_double2\":5.1,\"nested_double1\":4.2,\"nested_epoch2\":\"2023-04-04T22:42:52.000+00:00\",\"nested_epoch1\":\"2022-12-14T00:40:00.000+00:00\",\"nested_long2\":51,\"nested_long1\":42,\"nested_url1\":\"https://localhost:8080/greet\",\"nested_url2\":\"https://localhost:4200/home\",\"nested_date1\":\"2023-04-04T00:42:00.000+00:00\",\"nested_uri1\":\"https://localhost:8080/greet\",\"nested_uri2\":\"https://localhost:4200/home#greet\"},\"double1\":4.2,\"epoch2\":\"2023-04-04T22:42:52.000+00:00\",\"epoch1\":\"2022-12-14T00:40:00.000+00:00\"}"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ com:
authorities:
- path: cognito:groups
- location: ${auth0-issuer}
username-claim: $['https://c4-soft.com/spring-addons']['name']
username-claim: $['https://c4-soft.com/user']['name']
authorities:
- path: roles
- path: permissions
- path: $['https://c4-soft.com/user']['roles']
- path: $.permissions
permit-all:
- "/greet/public"
- "/actuator/health/readiness"
Expand Down
Loading

0 comments on commit 6387375

Please sign in to comment.