Skip to content

Commit d81d26c

Browse files
add pkce auth
1 parent e1b00ba commit d81d26c

File tree

8 files changed

+2043
-1159
lines changed

8 files changed

+2043
-1159
lines changed

index.js

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -344,28 +344,34 @@ const WEBFORMS_SCOPES = [
344344
const scope = [...ROOM_SCOPES, ...CLICK_SCOPES, ...MONITOR_SCOPES, ...ADMIN_SCOPES, ...SCOPES, ...WEBFORMS_SCOPES, ...MAESTRO_SCOPES];
345345

346346
// Configure passport for DocusignStrategy
347-
let docusignStrategy = new DocusignStrategy({
348-
production: dsConfig.production,
349-
clientID: dsConfig.dsClientId,
350-
scope: scope.join(' '),
351-
clientSecret: dsConfig.dsClientSecret,
352-
callbackURL: hostUrl + '/ds/callback',
353-
state: true // automatic CSRF protection.
354-
// See https://github.com/jaredhanson/passport-oauth2/blob/master/lib/state/session.js
347+
const docusignStrategyOptions = {
348+
production: dsConfig.production,
349+
clientID: dsConfig.dsClientId,
350+
scope: scope.join(' '),
351+
clientSecret: dsConfig.dsClientSecret,
352+
callbackURL: hostUrl + '/ds/callback',
353+
state: true // automatic CSRF protection.
354+
// See https://github.com/jaredhanson/passport-oauth2/blob/master/lib/state/session.js
355+
};
356+
function processDsResult(accessToken, refreshToken, params, profile, done) {
357+
// The params arg will be passed additional parameters of the grant.
358+
// See https://github.com/jaredhanson/passport-oauth2/pull/84
359+
//
360+
// Here we're just assigning the tokens to the account object
361+
// We store the data in DSAuthCodeGrant.getDefaultAccountInfo
362+
let user = profile;
363+
user.accessToken = accessToken;
364+
user.refreshToken = refreshToken;
365+
user.expiresIn = params.expires_in;
366+
user.tokenExpirationTimestamp = moment().add(user.expiresIn, 's'); // The dateTime when the access token will expire
367+
return done(null, user);
368+
}
369+
const docusignStrategy = new DocusignStrategy(docusignStrategyOptions, processDsResult);
370+
const docusignStrategyPKCE = new DocusignStrategy({
371+
...docusignStrategyOptions,
372+
pkce: true
355373
},
356-
function _processDsResult(accessToken, refreshToken, params, profile, done) {
357-
// The params arg will be passed additional parameters of the grant.
358-
// See https://github.com/jaredhanson/passport-oauth2/pull/84
359-
//
360-
// Here we're just assigning the tokens to the account object
361-
// We store the data in DSAuthCodeGrant.getDefaultAccountInfo
362-
let user = profile;
363-
user.accessToken = accessToken;
364-
user.refreshToken = refreshToken;
365-
user.expiresIn = params.expires_in;
366-
user.tokenExpirationTimestamp = moment().add(user.expiresIn, 's'); // The dateTime when the access token will expire
367-
return done(null, user);
368-
}
374+
processDsResult
369375
);
370376

371377
/**
@@ -378,4 +384,5 @@ if (!dsConfig.allowSilentAuthentication) {
378384
return { prompt: 'login' };
379385
};
380386
}
381-
passport.use(docusignStrategy);
387+
passport.use('docusign', docusignStrategy);
388+
passport.use('docusign_pkce', docusignStrategyPKCE);

jwt_console_project/package-lock.json

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jwt_console_project/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"author": "DocuSign, Inc",
1313
"license": "ISC",
1414
"dependencies": {
15-
"docusign-esign": "^8.0.0",
15+
"docusign-esign": "^8.0.1",
1616
"fs": "^0.0.1-security",
1717
"fs-extra": "^11.2.0",
1818
"path": "^0.12.7",

lib/DSAuthCodeGrant.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,35 @@ DSAuthCodeGrant.prototype.login = function(req, res, next) {
6060
// Reset
6161
this.internalLogout(req, res);
6262
req.session.authMethod = 'grand-auth';
63-
passport.authenticate('docusign')(req, res, next);
63+
64+
if (req.session?.pkceFailed) {
65+
passport.authenticate('docusign')(req, res, next);
66+
} else {
67+
passport.authenticate('docusign_pkce')(req, res, next);
68+
}
6469
};
6570

6671
DSAuthCodeGrant.prototype.oauth_callback1 = (req, res, next) => {
6772
// This callback URL is used for the login flow
68-
passport.authenticate('docusign', { failureRedirect: '/ds/login' })(req, res, next);
73+
if (req.session?.pkceFailed) {
74+
passport.authenticate('docusign', { failureRedirect: '/ds/login' })(req, res, next);
75+
} else {
76+
passport.authenticate('docusign_pkce', { failureRedirect: '/ds/login' }, (err, user, _info) => {
77+
if (err || !user) { return next(); }
78+
79+
req.logIn(user, function(err) {
80+
if (err) { return next(err); }
81+
return next();
82+
});
83+
})(req, res, next);
84+
}
6985
};
7086
DSAuthCodeGrant.prototype.oauth_callback2 = function _oauth_callback2(req, res, next) {
87+
if (!req.session.pkceFailed && !req?.user?.accessToken) {
88+
req.session.pkceFailed = true;
89+
return res.redirect('/ds/login');
90+
}
91+
7192
this._accessToken = req.user.accessToken;
7293
console.log(`Received access_token: |${req.user.accessToken}|`);
7394
console.log(`Expires at ${req.user.tokenExpirationTimestamp.format('dddd, MMMM Do YYYY, h:mm:ss a')}`);

0 commit comments

Comments
 (0)