Skip to content

Commit 04220a2

Browse files
committed
Add IdP logout
1 parent 967728c commit 04220a2

5 files changed

Lines changed: 71 additions & 28 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"version": "0.1.0",
44
"license": "MIT",
55
"description": "React code example with OAuth Implicit grant and DocuSign API",
6-
"homepage": "https://github.com/docusign/react-oauth-docusign",
6+
"homepage_comment": "<homepage> is used by the build process as the build target URL path",
7+
"homepage": "react-oauth-docusign/build",
78
"private": true,
89
"dependencies": {
910
"@testing-library/jest-dom": "^5.11.4",

public/config_example.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var config = {};
2+
3+
// Configuration file example.
4+
//
5+
// Development: Add to the public directory as config.js.
6+
//
7+
// Production: Add to the build directory after the build is complete as config.js.
8+
9+
config.DS_CLIENT_ID='xxxx-xxxx-xxxx-xxxx-xxxxxxx';
10+
config.IMPLICIT_SCOPES='signature';
11+
12+
// Your DocuSign eSignature REST API private CORS proxy
13+
config.DS_API_CORS_PROXY='https://your_private_proxy.xxx.xxx';
14+
config.DS_API_CORS_PROXY_FOR='https://demo.docusign.net';
15+
16+
// Your app's URL
17+
config.DS_APP_URL='http://localhost:3000';
18+
config.DS_DEBUG=true;
19+
config.DS_IDP='https://account-d.docusign.com';
20+
config.DS_AUTHENTICATION='https://account-d.docusign.com';

src/App.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ class App extends React.Component {
9494
this.clearAuth();
9595
this.clearState();
9696
this.setState({ page: 'welcome' });
97-
toast.success('You have logged out.', { autoClose: 1000 });
97+
toast.success('You have logged out.', { autoClose: 5000 });
98+
this.oAuthImplicit.logout();
9899
}
99100

100101
/**

src/DocuSign.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class DocuSign {
7272
},
7373
documents: [
7474
{
75-
name: 'Example document.pdf',
75+
name: docName,
7676
fileExtension: 'pdf',
7777
documentId: '1',
7878
documentBase64: base64File,

src/OAuthImplicit.js

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,9 @@ class OAuthImplicit {
8585
// This API method is common for many IdP systems.
8686
// But the exact format of the response tends to vary.
8787
// The following works for the DocuSign IdP.
88-
let userInfoResponse;
89-
try {
90-
userInfoResponse = await this.fetchUserInfo();
91-
} catch (e) {
92-
const msg = `Problem while completing login.\nPlease retry.\nError: ${e.toString()}`;
93-
log(msg);
94-
toast.error(msg, { autoClose: 10000 });
95-
return;
96-
}
97-
if (!userInfoResponse || !userInfoResponse.ok) {
98-
const msg = `Problem while completing login.\nPlease retry.\nError: ${userInfoResponse.statusText}`;
99-
log(msg);
100-
toast.error(msg, { autoClose: 10000 });
101-
return;
102-
}
103-
const userInfo = await userInfoResponse.json();
104-
const defaultAccount = userInfo.accounts.filter((acc) => acc.is_default)[0];
88+
const userInfo = await this.fetchUserInfo();
89+
const defaultAccountArray = userInfo.accounts.filter((acc) => acc.is_default);
90+
const defaultAccount = defaultAccountArray.length > 0 && defaultAccountArray[0];
10591
if (!defaultAccount) {
10692
const msg = `Problem: the user does not have a default account. Contact DocuSign Customer Service to fix.`;
10793
log(msg);
@@ -149,23 +135,58 @@ class OAuthImplicit {
149135
`scope=${window.config.IMPLICIT_SCOPES}&` +
150136
`client_id=${window.config.DS_CLIENT_ID}&` +
151137
`state=${oauthStateValue}&` +
152-
`redirect_uri=${window.config.DS_APP_URL}`;
138+
`redirect_uri=${encodeURIComponent(window.config.DS_APP_URL)}`;
139+
140+
window.location = url;
141+
}
142+
143+
/**
144+
* logout of the DocuSign IdP.
145+
* If SSO is used, the upstream IdP may not redirect the
146+
* browser back to this app
147+
*/
148+
logout () {
149+
const url =
150+
`${window.config.DS_IDP}/logout?` +
151+
//`response_type=code&` +
152+
`response_type=token&` +
153+
`scope=${window.config.IMPLICIT_SCOPES}&` +
154+
`client_id=${window.config.DS_CLIENT_ID}&` +
155+
`redirect_uri=${encodeURIComponent(window.config.DS_APP_URL)}&` +
156+
`response_mode=logout_redirect`;
153157

154158
window.location = url;
155159
}
156160

157161
/**
158162
* A relatively common OAuth API endpoint for obtaining information
159163
* on the user associated with the accessToken
164+
* @returns userInfoResponse JSON
160165
*/
161166
async fetchUserInfo() {
162-
return fetch(`${window.config.DS_AUTHENTICATION}/oauth/userinfo`, {
163-
headers: new Headers({
164-
Authorization: `Bearer ${this.accessToken}`,
165-
Accept: `application/json`,
166-
'X-DocuSign-SDK': sdkString,
167-
}),
168-
})
167+
let userInfoResponse
168+
try {
169+
userInfoResponse = await fetch(
170+
`${window.config.DS_AUTHENTICATION}/oauth/userinfo`, {
171+
headers: new Headers({
172+
Authorization: `Bearer ${this.accessToken}`,
173+
Accept: `application/json`,
174+
'X-DocuSign-SDK': sdkString,
175+
}),
176+
})
177+
} catch (e) {
178+
const msg = `Problem while completing login.\nPlease retry.\nError: ${e.toString()}`;
179+
log(msg);
180+
toast.error(msg, { autoClose: 10000 });
181+
return null;
182+
}
183+
if (!userInfoResponse || !userInfoResponse.ok) {
184+
const msg = `Problem while completing login.\nPlease retry.\nError: ${userInfoResponse.statusText}`;
185+
log(msg);
186+
toast.error(msg, { autoClose: 10000 });
187+
return null;
188+
}
189+
return await userInfoResponse.json();
169190
}
170191

171192
/**

0 commit comments

Comments
 (0)