Skip to content

Commit

Permalink
Add http request options in js adapter
Browse files Browse the repository at this point in the history
This adds httpRequestHeaders and httpRequestWillSend init options so end user can send aditionnal headers or perform custom tweaks on http requests before they are sent.

Closes keycloak#10312
  • Loading branch information
Toilal committed Feb 28, 2022
1 parent f2ed799 commit afdd412
Show file tree
Hide file tree
Showing 2 changed files with 60 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,11 @@ export interface KeycloakInitOptions {
* @default 10000
*/
messageReceiveTimeout?: number

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

export interface KeycloakLoginOptions {
Expand Down Expand Up @@ -506,6 +512,11 @@ declare class Keycloak {
*/
onActionUpdate?(status: 'success'|'cancelled'|'error'): void;

/**
* Called when HTTP request is sent to allow customization.
*/
onHttpRequestSend?(req: XMLHttpRequest, type: KeycloakHttpRequestType): void;

/**
* Called to initialize the adapter.
* @param initOptions Initialization options.
Expand Down
49 changes: 49 additions & 0 deletions adapters/oidc/js/src/keycloak.js
Expand Up @@ -157,6 +157,12 @@ function Keycloak (config) {
} else {
kc.messageReceiveTimeout = 10000;
}

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

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

applyHttpRequestHeaders(req)
if (kc.onHttpRequestSend) {
kc.onHttpRequestSend(req, 'load_user_profile')
}

req.send();

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

applyHttpRequestHeaders(req)
if (kc.onHttpRequestSend) {
kc.onHttpRequestSend(req, 'load_user_info')
}

req.send();

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

applyHttpRequestHeaders(req)
if (kc.onHttpRequestSend) {
kc.onHttpRequestSend(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,11 @@ function Keycloak (config) {
}
};

applyHttpRequestHeaders(req)
if (kc.onHttpRequestSend) {
kc.onHttpRequestSend(req, 'authorization_code')
}

req.send(params);
}

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

applyHttpRequestHeaders(req)
if (kc.onHttpRequestSend) {
kc.onHttpRequestSend(req, 'load_config')
}

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

applyHttpRequestHeaders(req)
if (kc.onHttpRequestSend) {
kc.onHttpRequestSend(req, 'openid_configuration')
}

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

0 comments on commit afdd412

Please sign in to comment.