Last active
December 27, 2022 03:25
-
-
Save maitrungduc1410/b20d72be66419e9cccbd08979b29012e to your computer and use it in GitHub Desktop.
Import users from OpenEdx to Keycloak with NodeJS
This file contains hidden or 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
import KcAdminClient from "@keycloak/keycloak-admin-client"; | |
const kcAdminClient = new KcAdminClient({ | |
baseUrl: "http://localhost:8080", | |
realmName: "myrealm", | |
}); | |
const credentials = { | |
grantType: "password", | |
username: "superuser", | |
password: "xxxxxx", | |
clientId: "myclient", | |
clientSecret: "myclientsecret", | |
}; | |
await kcAdminClient.auth(credentials); | |
const user = await kcAdminClient.users.create({ | |
username: "openedxuser1", | |
email: '[email protected]', | |
emailVerified: true, | |
firstName: "AAAAA", | |
lastName: "BBBBB", | |
enabled: true, | |
credentials: [ | |
{ | |
type: 'password', | |
credentialData: "{\"hashIterations\": 150000,\"algorithm\": \"pbkdf2-sha256\"}", | |
secretData: "{\"salt\": \"eGl5VFUzTDVHbFlI\",\"value\": \"Y+tlU1BH10IDYMycH5+4S8J3IoeakcGKjKS51jDxcEQ=\"}", | |
} | |
] | |
}) | |
console.log(user) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Notes
superuser
must have rolemanage-users
in order to create users. From admin console -> select your realm -> Users -> select the superuser -> Role Mapping -> Assign Role -> Filter By ClientsPassword of an account from OpenEdx is in this format
pbkdf2_sha256$150000$xiyTU3L5GlYH$Y+tlU1BH10IDYMycH5+4S8J3IoeakcGKjKS51jDxcEQ=
Breakdown the hashed password:
pbkdf2_sha256
150000
xiyTU3L5GlYH
Y+tlU1BH10IDYMycH5+4S8J3IoeakcGKjKS51jDxcEQ=
When we import to Keycloak, in
credentials
we need to put same information, exceptsalt
, we need to encodesalt
to base64 and ONLY take the first 16 chars of the encoded stringIn our case, salt is
xiyTU3L5GlYH
----> base64:eGl5VFUzTDVHbFlICg==
--> first 16 chars:eGl5VFUzTDVHbFlI
After you have successfully created the user, you should be able to login to keycloak with same credentials as in OpenEdx
This solution works in latest version of Keycloak
20.0.0