forked from hotello/meteor-keycloak-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keycloak_client.js
42 lines (37 loc) · 1.62 KB
/
keycloak_client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Keycloak = {};
// Request Keycloak credentials for the user
//
// @param options {optional}
// @param credentialRequestCompleteCallback {Function} Callback function to call on
// completion. Takes one argument, credentialToken on success, or Error on
// error.
Keycloak.requestCredential = function (options, credentialRequestCompleteCallback) {
// support both (options, callback) and (callback).
if (!credentialRequestCompleteCallback && typeof options === 'function') {
credentialRequestCompleteCallback = options;
options = {};
}
var config = ServiceConfiguration.configurations.findOne({service: 'keycloak'});
if (!config) {
credentialRequestCompleteCallback && credentialRequestCompleteCallback(
new ServiceConfiguration.ConfigError());
return;
}
var credentialToken = Random.secret();
var loginStyle = OAuth._loginStyle('keycloak', config, options);
var idpHint = options && options.idpHint ? '&kc_idp_hint=' + options.idpHint : '';
var loginUrl =
`${config.serverUrl}/realms/${config.realm}/protocol/openid-connect/auth` +
'?client_id=' + encodeURIComponent(config.clientId) +
'&state=' + OAuth._stateParam(loginStyle, credentialToken, options && options.redirectUrl) +
'&redirect_uri=' + OAuth._redirectUri('keycloak', config) +
'&scope=' + encodeURIComponent(config.scope ? 'openid ' + config.scope : 'openid') +
'&response_type=code' + idpHint;
OAuth.launchLogin({
loginService: "keycloak",
loginStyle: loginStyle,
loginUrl: loginUrl,
credentialRequestCompleteCallback: credentialRequestCompleteCallback,
credentialToken: credentialToken
});
};