Skip to content

Commit ba66e7c

Browse files
author
Eric Koleda
committed
Add IdentityServer4 sample.
1 parent 528d0be commit ba66e7c

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

samples/IdentityServer4.gs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Demonstrates how to authorize access to the IdentityServer4 Demo API using
3+
* the Authorization Code grant type.
4+
* @see https://demo.identityserver.io/
5+
* http://docs.identityserver.io/en/release/topics/grant_types.html#authorization-code
6+
* @see http://docs.identityserver.io/en/release/endpoints/authorize.html
7+
* @see http://docs.identityserver.io/en/release/endpoints/token.html
8+
*/
9+
10+
// Test credentials for the Demo API.
11+
var CLIENT_ID = 'server.code';
12+
var CLIENT_SECRET = 'secret';
13+
14+
/**
15+
* Authorizes and makes a request to the IdentityServer4 Demo API.
16+
*/
17+
function run() {
18+
var service = getService();
19+
if (service.hasAccess()) {
20+
var url = 'https://demo.identityserver.io/api/test';
21+
var response = UrlFetchApp.fetch(url, {
22+
headers: {
23+
Authorization: 'Bearer ' + service.getAccessToken()
24+
}
25+
});
26+
var result = JSON.parse(response.getContentText());
27+
Logger.log(JSON.stringify(result, null, 2));
28+
} else {
29+
var authorizationUrl = service.getAuthorizationUrl();
30+
Logger.log('Open the following URL and re-run the script: %s',
31+
authorizationUrl);
32+
}
33+
}
34+
35+
/**
36+
* Reset the authorization state, so that it can be re-tested.
37+
*/
38+
function reset() {
39+
getService().reset();
40+
}
41+
42+
/**
43+
* Configures the service.
44+
*/
45+
function getService() {
46+
return OAuth2.createService('IdentityServer4')
47+
// Set the endpoint URLs.
48+
.setAuthorizationBaseUrl('https://demo.identityserver.io/connect/authorize')
49+
.setTokenUrl('https://demo.identityserver.io/connect/token')
50+
51+
// Set the client ID and secret.
52+
.setClientId(CLIENT_ID)
53+
.setClientSecret(CLIENT_SECRET)
54+
55+
// Set the name of the callback function that should be invoked to
56+
// complete the OAuth flow.
57+
.setCallbackFunction('authCallback')
58+
59+
// Set the property store where authorized tokens should be persisted.
60+
.setPropertyStore(PropertiesService.getUserProperties())
61+
62+
// Set the scope and additional Google-specific parameters.
63+
.setScope('openid api offline_access');
64+
}
65+
66+
/**
67+
* Handles the OAuth callback.
68+
*/
69+
function authCallback(request) {
70+
var service = getService();
71+
var authorized = service.handleCallback(request);
72+
if (authorized) {
73+
return HtmlService.createHtmlOutput('Success!');
74+
} else {
75+
return HtmlService.createHtmlOutput('Denied');
76+
}
77+
}

0 commit comments

Comments
 (0)