Skip to content

Commit

Permalink
Merge pull request IdentityModel#1060 from waldo2188/dev
Browse files Browse the repository at this point in the history
Add client_secret_basic auth exchangeCode Method. IdentityModel#892
  • Loading branch information
brockallen authored Jan 28, 2021
2 parents 058ab22 + 7bb91c7 commit deed90c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/JsonService.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class JsonService {
});
}

postForm(url, payload) {
postForm(url, payload, basicAuth) {
if (!url){
Log.error("JsonService.postForm: No url passed");
throw new Error("url");
Expand Down Expand Up @@ -197,6 +197,12 @@ export class JsonService {
}

req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

if (basicAuth !== undefined)
{
req.setRequestHeader("Authorization", "Basic " + btoa(basicAuth));
}

req.send(body);
});
}
Expand Down
8 changes: 7 additions & 1 deletion src/OidcClientSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const OidcMetadataUrlPath = '.well-known/openid-configuration';

const DefaultResponseType = "id_token";
const DefaultScope = "openid";
const DefaultClientAuthentication = "client_secret_post" // The default value must be client_secret_basic, as explained in https://openid.net/specs/openid-connect-core-1_0.html#ClientAuthentication
const DefaultStaleStateAge = 60 * 15; // seconds
const DefaultClockSkewInSeconds = 60 * 5;

Expand All @@ -21,6 +22,7 @@ export class OidcClientSettings {
// client related
client_id, client_secret, response_type = DefaultResponseType, scope = DefaultScope,
redirect_uri, post_logout_redirect_uri,
client_authentication = DefaultClientAuthentication,
// optional protocol
prompt, display, max_age, ui_locales, acr_values, resource, response_mode,
// behavior flags
Expand Down Expand Up @@ -49,6 +51,7 @@ export class OidcClientSettings {
this._scope = scope;
this._redirect_uri = redirect_uri;
this._post_logout_redirect_uri = post_logout_redirect_uri;
this._client_authentication = client_authentication;

this._prompt = prompt;
this._display = display;
Expand Down Expand Up @@ -102,7 +105,10 @@ export class OidcClientSettings {
get post_logout_redirect_uri() {
return this._post_logout_redirect_uri;
}

get client_authentication() {
return this._client_authentication;
}


// optional protocol params
get prompt() {
Expand Down
26 changes: 24 additions & 2 deletions src/TokenClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ export class TokenClient {
exchangeCode(args = {}) {
args = Object.assign({}, args);

var basicAuth = undefined;
var urlQuery = "";

args.grant_type = args.grant_type || "authorization_code";
args.client_id = args.client_id || this._settings.client_id;
args.client_secret = args.client_secret || this._settings.client_secret;
args.redirect_uri = args.redirect_uri || this._settings.redirect_uri;

var client_authentication = args._client_authentication || this._settings._client_authentication;
delete args._client_authentication;

if (!args.code) {
Log.error("TokenClient.exchangeCode: No code passed");
return Promise.reject(new Error("A code is required"));
Expand All @@ -40,11 +47,26 @@ export class TokenClient {
Log.error("TokenClient.exchangeCode: No client_id passed");
return Promise.reject(new Error("A client_id is required"));
}
if (!args.client_secret && client_authentication == "client_secret_basic") {
Log.error("TokenClient.exchangeCode: No client_secret passed");
return Promise.reject(new Error("A client_secret is required"));
}


// Sending the client credentials using the Basic Auth method
if(client_authentication == "client_secret_basic")
{
basicAuth = args.client_id + ':' + args.client_secret;
urlQuery = "?grant_type=" + encodeURIComponent(args.grant_type) +
"&redirect_uri="+ encodeURIComponent(args.redirect_uri) +
"&code="+ encodeURIComponent(args.code);

args = {};
}

return this._metadataService.getTokenEndpoint(false).then(url => {
Log.debug("TokenClient.exchangeCode: Received token endpoint");

return this._jsonService.postForm(url, args).then(response => {
return this._jsonService.postForm(url + urlQuery, args, basicAuth).then(response => {
Log.debug("TokenClient.exchangeCode: response received");
return response;
});
Expand Down

0 comments on commit deed90c

Please sign in to comment.