-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
116 lines (103 loc) · 3.05 KB
/
index.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* ApiKey Strategy.
*
* Configure the keys to use in localSettings.js within secure: {}.
* Configure: api_keys: [{name: 'NAME of client', apikey: 'THE API key', scope: ['Name of scope']}
* Sample: api_keys: [{name: 'pontus', apikey: 'AAAA', scope: ['write']}, {name: 'ove', apikey: 'BBBB', scope: ['read']}, {name: 'jon', apikey: '1234', scope: ['write', 'read']}],
* client "pontus" has a key with scope 'write', client "ove" has a key with scope 'read' and finally client "jon" has a key with scopes 'read' and 'write'
*/
var passport = require('passport')
var util = require('util')
var log = { debug: console.log, info: console.log, warn: console.log, error: console.log }
/**
* Creates an instance of `Strategy` checking api keys.
*/
function Strategy(options, verify) {
if (typeof options === 'function') {
verify = options
options = {}
} else {
if (options && options.log) {
log = options.log
}
}
if (!verify) {
throw new Error('apikey authentication strategy requires a verify function')
}
passport.Strategy.call(this)
this._apiKeyHeader = options.apiKeyHeader || 'api_key'
this.name = 'apikey'
this._verify = verify
this._passReqToCallback = true
}
/**
* Inherit from `passport.Strategy`.
*/
util.inherits(Strategy, passport.Strategy)
/**
* Authenticate request.
*
* @param req The request to authenticate.
* @param options Strategy-specific options.
*/
Strategy.prototype.authenticate = function (req, options) {
options = options || {}
var apikey = req.header(this._apiKeyHeader)
if (!apikey) {
return this.fail(new BadRequestError('Missing API Key'))
}
var self = this
/*
* Verifies the user login add set error, fail or success depending on the result.
*/
var verified = function (err, user, info) {
if (err) {
return self.error(err)
}
if (!user) {
return self.fail(info)
}
self.success(user, info)
}
this._verify(req, apikey, verified)
}
/**
* `BadRequestError` error.
* @api public
*/
function BadRequestError(message) {
this.name = 'BadRequestError'
this.message = message
this.stack = new Error().stack
}
// inherit from Error
BadRequestError.prototype = Object.create(Error.prototype)
BadRequestError.prototype.constructor = BadRequestError
function verifyApiKey(req, apikey, configuredApiKeys, done) {
try {
for (var i = 0; i < configuredApiKeys.length; i++) {
var client = configuredApiKeys[i]
if (client.apikey === apikey) {
log.debug('Authenticate ' + client.name)
for (var s = 0; s < client.scope.length; s++) {
var assignedScope = client.scope[s]
if (req.scope.indexOf(assignedScope) >= 0) {
req.apiClient = {}
req.apiClient.name = client.name
req.apiClient.scope = client.scope
return done(null, client.name)
}
}
}
}
return done(null, null, '401')
} catch (err) {
done(err)
}
}
/**
* Expose `Strategy`.
* And verify function.
*/
module.exports.Strategy = Strategy
module.exports.verifyApiKey = verifyApiKey