Skip to content

Commit

Permalink
Update and rename bash.sh to create_users_and_roles.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasean authored Dec 18, 2023
1 parent a96d2f4 commit 28b958d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
11 changes: 0 additions & 11 deletions docker/bash.sh

This file was deleted.

25 changes: 25 additions & 0 deletions docker/create_users_and_roles.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

# Start Keycloak
/opt/jboss/tools/docker-entrypoint.sh -b 0.0.0.0 &

# Wait for Keycloak to start
sleep 20

# Get admin access token
TOKEN=$(curl -s -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'username=admin' -d 'password=admin' -d 'grant_type=password' -d 'client_id=admin-cli' "http://localhost:9990/auth/realms/master/protocol/openid-connect/token" | jq -r .access_token)

# Create roles
curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d '{"name":"SUPERADMIN"}' "http://localhost:9990/auth/admin/realms/master/roles"
curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d '{"name":"ADMIN"}' "http://localhost:9990/auth/admin/realms/master/roles"

# Create users
curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d '{"username":"SUPERADMIN","enabled":true,"credentials":[{"type":"password","value":"superadminpassword","temporary":false}]}' "http://localhost:9990/auth/admin/realms/master/users"
curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d '{"username":"ADMIN","enabled":true,"credentials":[{"type":"password","value":"adminpassword","temporary":false}]}' "http://localhost:9990/auth/admin/realms/master/users"

# Assign roles to users
SUPERADMIN_ID=$(curl -s -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" "http://localhost:9990/auth/admin/realms/master/users?username=SUPERADMIN" | jq -r .[0].id)
ADMIN_ID=$(curl -s -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" "http://localhost:9990/auth/admin/realms/master/users?username=ADMIN" | jq -r .[0].id)

curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d '[{"id":"","name":"SUPERADMIN"}]' "http://localhost:9990/auth/admin/realms/master/users/$SUPERADMIN_ID/role-mappings/realm"
curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d '[{"id":"","name":"ADMIN"}]' "http://localhost:9990/auth/admin/realms/master/users/$ADMIN_ID/role-mappings/realm"

0 comments on commit 28b958d

Please sign in to comment.