Skip to content

Commit efd1ec0

Browse files
authored
Merge pull request #22 from asmclean/IPS-1135
[IPS-1135] Execute the authorization code and refresh token exchange from a backend call
2 parents 4ec463d + 147b325 commit efd1ec0

File tree

7 files changed

+7462
-912
lines changed

7 files changed

+7462
-912
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [2.0.3] - 2020-04-03
8+
9+
### Changed
10+
- Refresh Token Exchange and Authorization Code exchange will be performed via the backend server.
11+
712
## [2.0.2] - 2019-06-13
813

914
### Changed

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "auth0-authentication-api-debugger-extension",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"description": "My extension for ..",
55
"main": "index.js",
66
"scripts": {
@@ -17,6 +17,7 @@
1717
"license": "MIT",
1818
"auth0-extension": {
1919
"externals": [
20+
"auth0@2.17.0",
2021
"auth0-extension-tools@1.3.2",
2122
"auth0-extension-express-tools@1.1.6",
2223
"auth0-oauth2-express@1.2.0",
@@ -34,6 +35,7 @@
3435
]
3536
},
3637
"dependencies": {
38+
"auth0": "2.17.0",
3739
"auth0-extension-express-tools": "^1.1.9",
3840
"auth0-extension-tools": "^1.3.3",
3941
"auth0-oauth2-express": "1.2.0",
@@ -50,7 +52,7 @@
5052
"winston": "^2.2.0"
5153
},
5254
"devDependencies": {
53-
"nodemon": "^1.11.0",
54-
"auth0-extensions-cli": "^1.3.1"
55+
"auth0-extensions-cli": "^1.3.1",
56+
"nodemon": "^1.11.0"
5557
}
5658
}

server/index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const config = require('./lib/config');
99
const utils = require('./lib/utils');
1010
const metadata = require('../webtask.json');
1111
const dashboardAdmins = require('./middleware/dashboardAdmins');
12+
const AuthenticationClient = require('auth0').AuthenticationClient;
1213

1314
module.exports = (configProvider) => {
1415
config.setProvider(configProvider);
@@ -50,6 +51,45 @@ module.exports = (configProvider) => {
5051
}));
5152
});
5253

54+
app.post('/request/code', function(req, res) {
55+
const data = {
56+
code: req.body.code,
57+
redirect_uri: req.body.redirect_uri
58+
};
59+
const auth0 = new AuthenticationClient({
60+
domain: config('AUTH0_DOMAIN'),
61+
clientId: req.body.client_id,
62+
clientSecret: req.body.client_secret,
63+
__bypassIdTokenValidation: true
64+
});
65+
66+
auth0.oauth.authorizationCodeGrant(data, function (err, response) {
67+
if (err) {
68+
const data = utils.tryParseJSON(err.message);
69+
return res.status(err.statusCode).json(data);
70+
}
71+
res.json(response);
72+
});
73+
});
74+
75+
app.post('/request/token', function(req, res) {
76+
const auth0 = new AuthenticationClient({
77+
domain: config('AUTH0_DOMAIN'),
78+
clientId: req.body.client_id,
79+
clientSecret: req.body.client_secret,
80+
__bypassIdTokenValidation: true
81+
});
82+
83+
const data = { refresh_token: req.body.refresh_token, client_secret: req.body.client_secret };
84+
auth0.oauth.refreshToken(data, function (err, response) {
85+
if (err) {
86+
const data = utils.tryParseJSON(err.message);
87+
return res.status(err.statusCode).json(data);
88+
}
89+
res.json(response);
90+
});
91+
});
92+
5393
app.get('/meta', cors(), function (req, res) {
5494
res.status(200).send(metadata);
5595
});

server/lib/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,12 @@ module.exports.syntaxHighlight = function(obj) {
116116
} catch(e) {
117117
return JSON.stringify(obj, null, 2);
118118
}
119+
}
120+
121+
module.exports.tryParseJSON = function(string) {
122+
try {
123+
return JSON.parse(string);
124+
} catch (e) {
125+
return;
126+
}
119127
}

server/views/index.js

Lines changed: 78 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -515,51 +515,92 @@ function setSelectedClientSecrets() {
515515
$('#client_secret').val('');
516516
}
517517
}
518+
function setSelectedClientSecrets() {
519+
selectedClient = _.find(clients, { 'client_id': $('#client').val() });
520+
521+
if (selectedClient) {
522+
$('#client_id').val(selectedClient.client_id);
523+
$('#client_secret').val(selectedClient.client_secret);
524+
} else {
525+
$('#client_id').val('');
526+
$('#client_secret').val('');
527+
}
528+
}
529+
function handleSuccessRequest(url, opt, data) {
530+
data.request = opt;
531+
if (data.refresh_token) {
532+
$('#refresh_token').val(data.refresh_token);
533+
}
534+
if (data.request.password) {
535+
data.request.password = '*****************';
536+
}
537+
if (data.request.client_secret) {
538+
data.request.client_secret = '*****************';
539+
}
540+
$.ajax({ type: "POST", url: '{{baseUrl}}/request', data: JSON.stringify(data), contentType: 'application/json' })
541+
.done(function (data) {
542+
$('#modal-body').html(data);
543+
$('#modal-body').prepend($('<pre/>', { 'class': 'json-object', 'html': 'POST ' + url }));
544+
})
545+
.fail(function (err) {
546+
$('#modal-body').html('<p>Error decoding the response.</p>');
547+
$('<pre/>', { 'class': 'json-object', 'html': err.responseText || err.name || err.text || err.body || err.status }).appendTo('#modal-body');
548+
});
549+
}
550+
function handleErrorRequest(url, opt, err) {
551+
if (opt.password) {
552+
opt.password = '*****************';
553+
}
554+
if (opt.client_secret) {
555+
opt.client_secret = '*****************';
556+
}
557+
$.ajax({ type: "POST", url: '{{baseUrl}}/request', data: JSON.stringify({ request: opt, err: err }), contentType: 'application/json' })
558+
.done(function (data) {
559+
$('#modal-body').html(data);
560+
$('#modal-body').prepend($('<pre/>', { 'class': 'json-object', 'html': 'POST ' + url }));
561+
})
562+
.fail(function (err) {
563+
$('#modal-body').html('<p>Error decoding the response.</p>');
564+
$('<pre/>', { 'class': 'json-object', 'html': err.responseText || err.name || err.text || err.body || err.status }).appendTo('#modal-body');
565+
});
566+
}
518567
function executeRequest(title, url, opt) {
519568
save();
520569
$('#modal-title').html(title);
521570
$('#modal-body').html('Loading...');
522571
$('#modal-dialog').modal({ show: true });
523572
$.post(url, opt)
524-
.done(function(data) {
573+
.done(function (data) {
574+
handleSuccessRequest(url, opt, data);
575+
})
576+
.fail(function (err) {
577+
handleErrorRequest(url, opt, err);
578+
});
579+
}
580+
function executeBackendRequest(title, url, backendUrl, opt) {
581+
save();
582+
$('#modal-title').html(title);
583+
$('#modal-body').html('Loading...');
584+
$('#modal-dialog').modal({ show: true });
585+
$.post(backendUrl, opt)
586+
.done(function (data) {
525587
data.request = opt;
526-
if (data.refresh_token) {
527-
localStorage.setItem('auth_debugger_refresh_token', data.refresh_token);
528-
}
529-
if (data.request.password) {
530-
data.request.password = '*****************';
531-
}
532-
if (data.request.client_secret) {
533-
data.request.client_secret = '*****************';
534-
}
535-
$.ajax({ type: "POST", url: '{{baseUrl}}/request', data: JSON.stringify(data), contentType: 'application/json' })
536-
.done(function(data) {
537-
$('#modal-body').html(data);
538-
$('#modal-body').prepend($('<pre/>', { 'class':'json-object', 'html': 'POST ' + url }));
539-
})
540-
.fail(function(err) {
541-
$('#modal-body').html('<p>Error decoding the response.</p>');
542-
$('<pre/>', { 'class':'json-object', 'html': err.responseText || err.name || err.text || err.body || err.status }).appendTo('#modal-body');
543-
});
588+
handleSuccessRequest(url, opt, data);
544589
})
545-
.fail(function(err) {
546-
if (opt.password) {
547-
opt.password = '*****************';
548-
}
549-
if (opt.client_secret) {
550-
opt.client_secret = '*****************';
551-
}
552-
$.ajax({ type: "POST", url: '{{baseUrl}}/request', data: JSON.stringify({ request: opt, err: err }), contentType: 'application/json' })
553-
.done(function(data) {
554-
$('#modal-body').html(data);
555-
$('#modal-body').prepend($('<pre/>', { 'class':'json-object', 'html': 'POST ' + url }));
556-
})
557-
.fail(function(err) {
558-
$('#modal-body').html('<p>Error decoding the response.</p>');
559-
$('<pre/>', { 'class':'json-object', 'html': err.responseText || err.name || err.text || err.body || err.status }).appendTo('#modal-body');
560-
});
590+
.fail(function (err) {
591+
handleErrorRequest(url, opt, err);
561592
});
562593
}
594+
function executeTokenExchange(title, opt) {
595+
var url = 'https://' + $('#domain').val() + '/oauth/token';
596+
var backendUrl = '{{baseUrl}}/request/token';
597+
executeBackendRequest(title, url, backendUrl, opt);
598+
}
599+
function executeCodeExchange(title, opt) {
600+
var url = 'https://' + $('#domain').val() + '/oauth/token';
601+
var backendUrl = '{{baseUrl}}/request/code';
602+
executeBackendRequest(title, url, backendUrl, opt);
603+
}
563604
if (!window.location.origin) {
564605
window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port : '');
565606
}
@@ -659,7 +700,7 @@ $(function () {
659700
} else {
660701
opt.client_secret = $('#client_secret').val();
661702
}
662-
executeRequest('OAuth2 - Authorization Code Exchange', 'https://' + $('#domain').val() + '/oauth/token', opt);
703+
executeCodeExchange('OAuth2 - Authorization Code Exchange', opt);
663704
});
664705
$('#oauth2_refresh_token_exchange').click(function(e) {
665706
e.preventDefault();
@@ -676,7 +717,7 @@ $(function () {
676717
} else {
677718
opt.client_secret = $('#client_secret').val();
678719
}
679-
executeRequest('OAuth2 - Refresh Token Exchange', 'https://' + $('#domain').val() + '/oauth/token', opt);
720+
executeTokenExchange('OAuth2 - Refresh Token Exchange', opt);
680721
});
681722
$('#oauth2_password_grant').click(function(e) {
682723
e.preventDefault();

webtask.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"title": "Auth0 Authentication API Debugger",
33
"name": "auth0-authentication-api-debugger",
4-
"version": "2.0.2",
4+
"version": "2.0.3",
55
"author": "auth0",
66
"useHashName": false,
77
"description": "This extension allows you to test and debug the various Authentication API endpoints",

0 commit comments

Comments
 (0)