-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_code_with_backend.js
70 lines (62 loc) · 2.06 KB
/
auth_code_with_backend.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
This example demonstrates how to get an OAuth2 access token using a simple node
backend server using the code flow.
Once the server is started, navigate to https://localhost:3000 in the browser. You
should be redirected to the OAuth2 URL after which the server will be given the
authorization_code which will be exchanged for the access token.
*/
const express = require('express');
const app = express();
const https = require('https');
const fs = require('fs');
const { AsperaOnCloud } = require('../../../dist/cjs/index');
const port = 3000;
/**
* Insert your API client integration settings here as specified
* in the Aspera On Cloud Admin app.
*
* id - client id of your API client
* secret - client secret of your API client
* org - the organization or subdomain that your API client belongs to
* scope - desired grant access for the access token
*/
const CLIENT = {
id: 'CLIENT_ID',
secret: 'CLIENT_SECRET',
org: 'ORG',
scope: ['admin-user:all', 'user:all']
};
const options = {
clientId: CLIENT.id,
clientSecret: CLIENT.secret,
org: CLIENT.org,
redirectUri: 'https://localhost:3000'
};
const aoc = new AsperaOnCloud(options);
app.get('/', async (req, res) => {
const { code } = req.query;
if (code) {
try {
const tokenResponse = await aoc.auth.getAccessTokenWithCode(code, CLIENT.scope)
const { access_token } = tokenResponse.data;
console.log('Access Token:', access_token);
aoc.auth.setAccessToken(access_token);
const self = await aoc.getSelf();
console.log('Current user:', self.data);
} catch (err) {
console.error(err);
}
} else {
const url = aoc.auth.getOauthUrl(undefined, CLIENT.scope)
// Specify the URL to redirect the page to
res.writeHead(302, { Location: url });
res.end();
}
});
const httpsOptions = {
key: fs.readFileSync('./examples/localhost-key.pem'),
cert: fs.readFileSync('./examples/localhost.pem')
};
const server = https.createServer(httpsOptions, app).listen(port, () => {
console.log('Express server running at https://localhost:' + port);
});