Skip to content

Commit 729b861

Browse files
authored
Merge pull request #141 from ibm-cloud-security/taimorSSO
add logoutSSO
2 parents f09e7ac + 2d5dbac commit 729b861

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

lib/strategies/webapp-strategy.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,4 +528,28 @@ function logAction(req, url, activity) {
528528
});
529529
}
530530

531+
/**
532+
* This method will trigger a REST HTTP call to the AppID server logoutSSO endpoint.
533+
* Bare in mind that SSO feature must be activated in the server side.
534+
* Calling the server SSO APIs will do nothing if you forgot to enable SSO in the server (tenant) configuration.
535+
* Once you enabled SSO login in the server side you can try the logout in the client side:
536+
* If you use the sample application , add a logout SSO UI widget , add to the app.js an express routing such as
537+
* app.get("/logoutSSO", function(req, res, next) {
538+
* res.clearCookie("refreshToken");
539+
* webAppStrategy.logoutSSO(req,res, { redirect_uri: "http://localhost:3000/niceGoodbyePage" , all_sessions: false });
540+
* });
541+
* @param req - the HTTP request
542+
* @param res - the HTTP response object (will be used to redirect)
543+
* @param options - need to contain at least the redirect_uri. all_sessions can logout all the users sessions at once.
544+
*/
545+
WebAppStrategy.prototype.logoutSSO = function (req, res, options = {}) {
546+
options.client_id = this.serviceConfig.getClientId();
547+
let queryParams = Object.keys(options).map(e => encodeURIComponent(e) + '=' + encodeURIComponent(options[e])).join('&');
548+
let oauthServerUrl = this.serviceConfig.getOAuthServerUrl();
549+
let url = `${oauthServerUrl}/cloud_directory/sso/logout?${queryParams}`;
550+
logger.debug('cleaning client session and calling server url:'+url);
551+
WebAppStrategy.logout(req); // without logging to activity tracker since the server will be reporting it .
552+
res.redirect(url);
553+
};
554+
531555
module.exports = WebAppStrategy;

test/webapp-strategy-test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,36 @@ describe("/lib/strategies/webapp-strategy", function () {
3737
redirectUri: "https://redirectUri"
3838
});
3939
});
40+
41+
describe("#SSO ", () => {
42+
let resultRedirect='';
43+
const redirectURL = "http://localhost:3000/somethingElse";
44+
45+
beforeEach( () => {
46+
resultRedirect='';
47+
});
48+
49+
it("good callback" , () => {
50+
let req = {
51+
session: { returnTo : 'ssss'},
52+
logout : function(req) {}
53+
};
54+
let res = {
55+
redirect : function (url) {
56+
resultRedirect = url;
57+
}
58+
};
59+
60+
let options = { redirect_uri: redirectURL};
61+
webAppStrategy.logoutSSO(req,res, options);
62+
const uriEncodedCallBack = encodeURIComponent(redirectURL);
63+
const excpected = `https://oauthServerUrlMock/cloud_directory/sso/logout?redirect_uri=${uriEncodedCallBack}&client_id=clientId`;
64+
assert.equal(resultRedirect, excpected);
65+
assert.equal(req.session.returnTo , undefined); // expect session to be cleaned.
66+
});
67+
68+
});
69+
4070

4171
describe("#setPreferredLocale", function () {
4272
it("Should fail if request doesn't have session", function (done) {

0 commit comments

Comments
 (0)