This repository was archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.js
More file actions
53 lines (45 loc) · 1.44 KB
/
serve.js
File metadata and controls
53 lines (45 loc) · 1.44 KB
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
// created by austin brown - 15 July 2017
const express = require('express'),
morgan = require('morgan'),
bodyParser = require('body-parser'),
oauthserver = require('oauth2-server');
const Model = require('./lib/Model');
const app = express();
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const _validResponse = function(req, res) {
res.send("Auth is valid");
};
// password grant auth
const pModel = new Model();
const pGrant = oauthserver({
model: pModel,
grants: ['password'],
accessTokenLifetime: 60, // in seconds
debug: true
});
app.post('/oauth/p/token', pGrant.grant());
app.get('/p', pGrant.authorise(), _validResponse);
app.use('/p', pGrant.errorHandler());
// client_credentials + refresh_token grant auth
const ccrtModel = new Model();
const ccrtGrant = oauthserver({
model: ccrtModel,
grants: ['client_credentials', 'refresh_token'],
accessTokenLifetime: 300, // in seconds
refreshTokenLifetime: 3600, // in seconds
debug: true
});
app.post('/oauth/ccrt/token', ccrtGrant.grant());
app.delete('/oauth/ccrt/token', function(req, res) {
ccrtModel.expireAccessToken(req.body.access_token, function(err, _res) {
res.end();
});
});
app.get('/ccrt', ccrtGrant.authorise(), _validResponse);
app.use('/ccrt', ccrtGrant.errorHandler());
// listen
const port = 4001; // forty-hundred and one, best I could do...
app.listen(port);
console.log(`Open sesame on port ${port}!`);