-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoauth.js
30 lines (25 loc) · 1013 Bytes
/
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
var oauth2orize = require('oauth2orize')
, passport = require('passport')
, db = require('./db').db()
, crypto = require('crypto')
, utils = require("./utils")
, bcrypt = require('bcrypt')
// create OAuth 2.0 server
var server = oauth2orize.createServer();
//Client Credentials
server.exchange(oauth2orize.exchange.clientCredentials(function(client, scope, done) {
var token = utils.uid(256)
var tokenHash = crypto.createHash('sha1').update(token).digest('hex')
var expiresIn = 1800
var expirationDate = new Date(new Date().getTime() + (expiresIn * 1000))
db.collection('accessTokens').save({token: tokenHash, expirationDate: expirationDate, clientId: client.clienId, scope: scope}, function(err) {
if (err) return done(err)
return done(null, token, {expires_in: expiresIn})
})
}))
// token endpoint
exports.token = [
passport.authenticate(['clientBasic', 'clientPassword'], { session: false }),
server.token(),
server.errorHandler()
]