Skip to content

Commit

Permalink
add httpRequestHeaders and httpRequestWillSend init options (keycloak…
Browse files Browse the repository at this point in the history
  • Loading branch information
Toilal committed Feb 18, 2022
1 parent f2ed799 commit 0aa90ff
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
11 changes: 11 additions & 0 deletions adapters/oidc/js/dist/keycloak.d.ts
Expand Up @@ -23,6 +23,7 @@ export type KeycloakResponseMode = 'query'|'fragment';
export type KeycloakResponseType = 'code'|'id_token token'|'code id_token token';
export type KeycloakFlow = 'standard'|'implicit'|'hybrid';
export type KeycloakPkceMethod = 'S256';
export type KeycloakHttpRequestType = 'refresh_token'|'authorization_code'|'load_config'|'openid_configuration'|'load_user_profile'|'load_user_info';

export interface KeycloakConfig {
/**
Expand Down Expand Up @@ -175,6 +176,16 @@ export interface KeycloakInitOptions {
* @default 10000
*/
messageReceiveTimeout?: number

/**
* Set additional headers in each HTTP requests.
*/
httpRequestHeaders?: { [headerName: string]: string | string[] }

/**
* Function called before HTTP request is sent to allow customization.
*/
httpRequestWillSend?: (req: XMLHttpRequest, type: KeycloakHttpRequestType) => void
}

export interface KeycloakLoginOptions {
Expand Down
43 changes: 43 additions & 0 deletions adapters/oidc/js/src/keycloak.js
Expand Up @@ -157,6 +157,18 @@ function Keycloak (config) {
} else {
kc.messageReceiveTimeout = 10000;
}

if (typeof initOptions.httpRequestHeaders === 'object') {
kc.httpRequestHeaders = initOptions.httpRequestHeaders;
} else {
kc.httpRequestHeaders = {};
}

if (typeof initOptions.httpRequestWillSend === 'function') {
kc.httpRequestWillSend = initOptions.httpRequestWillSend;
} else {
kc.httpRequestWillSend = function() {};
}
}

if (!kc.responseMode) {
Expand Down Expand Up @@ -531,6 +543,9 @@ function Keycloak (config) {
}
}

applyHttpRequestHeaders(req)
kc.httpRequestWillSend(req, 'load_user_profile')

req.send();

return promise.promise;
Expand All @@ -556,6 +571,9 @@ function Keycloak (config) {
}
}

applyHttpRequestHeaders(req)
kc.httpRequestWillSend(req, 'load_user_info')

req.send();

return promise.promise;
Expand Down Expand Up @@ -649,6 +667,9 @@ function Keycloak (config) {
}
};

applyHttpRequestHeaders(req)
kc.httpRequestWillSend(req, 'refresh_token')

req.send(params);
}
}
Expand Down Expand Up @@ -678,6 +699,19 @@ function Keycloak (config) {
}
}

function applyHttpRequestHeaders(req) {
for (var headerName in kc.httpRequestHeaders) {
var headerValue = kc.httpRequestHeaders[headerName];
if (Array.isArray(headerValue)) {
for (var i = 0; i < headerValue.length; i++) {
req.setRequestHeader(headerName, headerValue[i]);
}
} else {
req.setRequestHeader(headerName, headerValue);
}
}
}

function getRealmUrl() {
if (typeof kc.authServerUrl !== 'undefined') {
if (kc.authServerUrl.charAt(kc.authServerUrl.length - 1) == '/') {
Expand Down Expand Up @@ -753,6 +787,9 @@ function Keycloak (config) {
}
};

applyHttpRequestHeaders(req)
kc.httpRequestWillSend(req, 'authorization_code')

req.send(params);
}

Expand Down Expand Up @@ -875,6 +912,9 @@ function Keycloak (config) {
}
};

applyHttpRequestHeaders(req)
kc.httpRequestWillSend(req, 'load_config')

req.send();
} else {
if (!config.clientId) {
Expand Down Expand Up @@ -926,6 +966,9 @@ function Keycloak (config) {
}
};

applyHttpRequestHeaders(req)
kc.httpRequestWillSend(req, 'openid_configuration')

req.send();
} else {
setupOidcEndoints(oidcProvider);
Expand Down

0 comments on commit 0aa90ff

Please sign in to comment.