-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathoauth.js
68 lines (59 loc) · 2.08 KB
/
oauth.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
/////////////////////////////////////////////////////////////////////
// Copyright (c) Autodesk, Inc. All rights reserved
// Written by Forge Partner Development
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
/////////////////////////////////////////////////////////////////////
const express = require('express');
const config = require('../config');
const { OAuth } = require('./common/oauth');
let router = express.Router();
router.get('/callback/oauth', async (req, res, next) => {
const { code } = req.query;
const oauth = new OAuth(req.session);
try {
await oauth.setCode(code);
res.redirect('/');
} catch(err) {
next(err);
}
});
router.get('/oauth/url', (req, res) => {
const url =
'https://developer.api.autodesk.com' +
'/authentication/v1/authorize?response_type=code' +
'&client_id=' + config.credentials.client_id +
'&redirect_uri=' + config.credentials.callback_url +
'&scope=' + config.scopes.internal.join(' ');
res.end(url);
});
router.get('/oauth/signout', (req, res) => {
req.session = null;
res.redirect('/');
});
// Endpoint to return a 2-legged access token
router.get('/oauth/token', async (req, res, next) => {
const oauth = new OAuth(req.session);
if (!oauth.isAuthorized()) {
res.status(401).end();
return;
}
try {
const accessToken = await oauth.getPublicToken();
res.json(accessToken);
} catch(err) {
next(err);
}
});
module.exports = router;