Skip to content

Commit

Permalink
✨(keycloak) add ralph client for generating access tokens
Browse files Browse the repository at this point in the history
Add a `ralph` client to `fun-mooc` realm, with the creation of two users.
  • Loading branch information
wilbrdt authored and quitterie-lcs committed Dec 15, 2023
1 parent b26d74a commit 681e8b4
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ jobs:
- run:
name: Create external potsie network
command: docker network create potsie
- run:
name: Create external ralph network
command: docker network create ralph
- run:
name: Bootstrap project
command: make bootstrap
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,21 @@ started with the project's `make run`, it can be accessed at
[http://localhost:8080](http://localhost:8080). Administrator credentials are:
`admin:pass`.

For now only the `potsie` client has been configured to login to grafana (see
The `potsie` client has been configured to login to grafana (see
the [openfun/potsie](https://github.com/openfun/potsie) project) using a
Keycloak account (it should have been created by the `make bootstrap` command).
You can login to grafana using the following credentials: `grafana:funfunfun`.

The `ralph` client has been configured with the specific audience `http://localhost:8100` but it can be changed through the Keycloak interface.
Two users have been created for this client:
- `ralph_admin:funfunfun` with the scope `all`
- `ralph_learner:moocmooc` with the scope `statements/read/mine`

To get the access token, you can use the following command:
```
curl -X POST -d "grant_type=password" -d "client_id=ralph" -d "client_secret=bcef3562-730d-4575-9e39-63e185f99bca" -d "username=ralph_admin" -d "password=funfunfun" http://localhost:8080/auth/realms/fun-mooc/protocol/openid-connect/token
```

## License

This work is released under the MIT license (see [LICENSE](./LICENSE)).
54 changes: 47 additions & 7 deletions bin/realm
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ set -eo pipefail
export PATH="/opt/jboss/keycloak/bin/:${PATH}"

declare realm="${DEFAULT_REALM_NAME}"
declare user="${TEST_USER_NAME}"
declare password="${TEST_USER_PASSWORD}"
declare email="${TEST_USER_EMAIL}"
declare potsie_user="${POTSIE_TEST_USER_NAME}"
declare potsie_password="${POTSIE_TEST_USER_PASSWORD}"
declare potsie_email="${POTSIE_TEST_USER_EMAIL}"
declare client_id="${GRAFANA_CLIENT_ID}"
declare client_uuid
declare client_secret="${GRAFANA_CLIENT_SECRET}"
declare ralph_admin_user="${RALPH_ADMIN_TEST_USER_NAME}"
declare ralph_admin_password="${RALPH_ADMIN_TEST_USER_PASSWORD}"
declare ralph_admin_email="${RALPH_ADMIN_TEST_USER_EMAIL}"
declare ralph_learner_user="${RALPH_LEARNER_TEST_USER_NAME}"
declare ralph_learner_password="${RALPH_LEARNER_TEST_USER_PASSWORD}"
declare ralph_learner_email="${RALPH_LEARNER_TEST_USER_EMAIL}"

# Server login
kcadm.sh config credentials \
Expand All @@ -28,7 +34,7 @@ fi
# And (re-)create it
kcadm.sh create realms -s realm="${realm}" -s enabled=true

# Create a client along with its roles
# Create a potsie client along with its roles
echo "Will create potsie client..."
kcadm.sh create clients -r "${realm}" -f - < /tmp/config/clients/potsie.json
client_uuid=$(kcadm.sh get clients -r fun-mooc --fields id -q clientId=potsie --format csv | sed 's/"//g')
Expand All @@ -37,7 +43,41 @@ kcadm.sh create "clients/${client_uuid}/roles" -r "${realm}" -s name=editor
kcadm.sh create "clients/${client_uuid}/roles" -r "${realm}" -s name=viewer

# Create a new user
kcadm.sh create users -r "${realm}" -s username="${user}" -s email="${email}" -s enabled=true
kcadm.sh set-password -r "${realm}" --username "${user}" --new-password "${password}"
kcadm.sh create users -r "${realm}" -s username="${potsie_user}" -s email="${potsie_email}" -s enabled=true
kcadm.sh set-password -r "${realm}" --username "${potsie_user}" --new-password "${potsie_password}"
# Add role for potsie client
kcadm.sh add-roles -r "${realm}" --uusername "${user}" --cclientid "potsie" --rolename "viewer"
kcadm.sh add-roles -r "${realm}" --uusername "${potsie_user}" --cclientid "potsie" --rolename "viewer"


# Create a ralph client along with its roles
echo "Will create ralph client..."
kcadm.sh create clients -r "${realm}" -f - < /tmp/config/clients/ralph.json
client_uuid=$(kcadm.sh get clients -r fun-mooc --fields id -q clientId=ralph --format csv | sed 's/"//g')
kcadm.sh create "clients/${client_uuid}/roles" -r "${realm}" -s name=all
kcadm.sh create "clients/${client_uuid}/roles" -r "${realm}" -s name=all/read
kcadm.sh create "clients/${client_uuid}/roles" -r "${realm}" -s name=state
kcadm.sh create "clients/${client_uuid}/roles" -r "${realm}" -s name=statements/read
kcadm.sh create "clients/${client_uuid}/roles" -r "${realm}" -s name=statements/read/mine
kcadm.sh create "clients/${client_uuid}/roles" -r "${realm}" -s name=statements/write

# Create an audience for this specific client
kcadm.sh create "clients/${client_uuid}/protocol-mappers/models" -r "${realm}" \
-s name=audience-mapping \
-s protocol=openid-connect \
-s protocolMapper=oidc-audience-mapper \
-s 'config."included.custom.audience"=http://localhost:8100' \
-s 'config."access.token.claim"=true' \
-s 'config."id.token.claim"=false'


# Create an admin user
kcadm.sh create users -r "${realm}" -s username="${ralph_admin_user}" -s email="${ralph_admin_email}" -s enabled=true
kcadm.sh set-password -r "${realm}" --username "${ralph_admin_user}" --new-password "${ralph_admin_password}"
# Add role for ralph user
kcadm.sh add-roles -r "${realm}" --uusername "${ralph_admin_user}" --cclientid "ralph" --rolename "all"

# Create a user that can only read its statements
kcadm.sh create users -r "${realm}" -s username="${ralph_learner_user}" -s email="${ralph_learner_email}" -s enabled=true
kcadm.sh set-password -r "${realm}" --username "${ralph_learner_user}" --new-password "${ralph_learner_password}"
# Add role for ralph user
kcadm.sh add-roles -r "${realm}" --uusername "${ralph_learner_user}" --cclientid "ralph" --rolename "statements/read/mine"
34 changes: 34 additions & 0 deletions data/keycloak/clients/ralph.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"clientId": "ralph",
"name": "Ralph",
"description": "The ultimate toolbox for your learning analytics",
"enabled": true,
"rootUrl": "http://localhost:8100",
"adminUrl": "http://localhost:8100",
"baseUrl": "/",
"clientAuthenticatorType": "client-secret",
"secret": "bcef3562-730d-4575-9e39-63e185f99bca",
"redirectUris": ["http://localhost:8100/whoami"],
"webOrigins": ["http://localhost:8100"],
"standardFlowEnabled": true,
"directAccessGrantsEnabled": true,
"fullScopeAllowed": false,
"protocol": "openid-connect",
"publicClient": false,
"protocolMappers": [
{
"name": "Roles",
"protocol": "openid-connect",
"protocolMapper": "oidc-usermodel-client-role-mapper",
"config": {
"claim.name": "roles",
"jsonType.label": "String",
"usermodel.clientRoleMapping.clientId": "ralph",
"userinfo.token.claim": "true",
"id.token.claim": "true",
"access.token.claim": "true",
"multivalued": "true"
}
}
]
}
5 changes: 5 additions & 0 deletions docker-compose.keycloak.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ services:
potsie:
aliases:
- keycloak
ralph:
aliases:
- keycloak
default:

keycloak_postgres:
Expand All @@ -26,3 +29,5 @@ services:
networks:
potsie:
external: true
ralph:
external: true
17 changes: 13 additions & 4 deletions env.d/keycloak
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@ DB_PASSWORD=password
KEYCLOAK_USER=admin
KEYCLOAK_PASSWORD=pass

# Potsie
DEFAULT_REALM_NAME=fun-mooc
TEST_USER_NAME=grafana
TEST_USER_PASSWORD=funfunfun
[email protected]

# Potsie
POTSIE_TEST_USER_NAME=grafana
POTSIE_TEST_USER_PASSWORD=funfunfun
[email protected]
GRAFANA_CLIENT_ID=potsie
GRAFANA_CLIENT_SECRET=fa9e98ee-61a1-4092-8dac-1597da0c1bb0

# Ralph
RALPH_ADMIN_TEST_USER_NAME=ralph_admin
RALPH_ADMIN_TEST_USER_PASSWORD=funfunfun
[email protected]
RALPH_LEARNER_TEST_USER_NAME=ralph_learner
RALPH_LEARNER_TEST_USER_PASSWORD=moocmooc
[email protected]

0 comments on commit 681e8b4

Please sign in to comment.