forked from senecajs/seneca-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
99 lines (77 loc) · 2.89 KB
/
auth.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
/* Copyright (c) 2012-2014 Richard Rodger, MIT License */
'use strict'
// External modules.
var _ = require('lodash')
var AuthUrlmatcher = require('auth-urlmatcher')
var AuthToken = require('auth-token-cookie')
// Internal modules
var UserManagement = require('./lib/user-management')
var AuthRedirect = require('auth-redirect')
var LocalStrategy = require('seneca-local-auth')
var Utility = require('./lib/common-utility')
var ExpressAuth = require('./lib/express-auth')
var HapiAuth = require('./lib/hapi-auth')
// Load configuration
var DefaultOptions = require('./default-options.js')
var error = require('eraro')({
package: 'auth'
})
module.exports = function auth (opts) {
var seneca = this
var internals = {}
internals.accepted_framworks = [
'express',
'hapi'
]
// using seneca.util.deepextend here, as there are sub properties
internals.options = seneca.util.deepextend(DefaultOptions, opts)
internals.load_common_plugins = function () {
seneca.use(AuthToken, internals.options)
seneca.use(AuthUrlmatcher, internals.options)
seneca.use(Utility, internals.options)
seneca.use(UserManagement, internals.options)
seneca.use(AuthRedirect, internals.options.redirect || {})
}
internals.load_default_express_plugins = function () {
internals.load_common_plugins()
seneca.use(ExpressAuth, internals.options)
seneca.use(LocalStrategy, internals.options)
}
internals.load_default_hapi_plugins = function () {
internals.load_common_plugins()
seneca.use(HapiAuth, internals.options)
seneca.use(LocalStrategy, internals.options)
}
internals.choose_framework = function () {
if (internals.options.framework === 'express') {
internals.load_default_express_plugins()
}
else {
internals.load_default_hapi_plugins()
}
}
internals.migrate_options = function () {
if (internals.options.service || internals.options.sendemail || internals.options.email) {
throw error('<' + (internals.options.service ? 'service' : (internals.options.sendemail ? 'sendemail' : 'email')) +
'> option is no longer supported, please check seneca-auth documentation for migrating to new version of seneca-auth')
}
if (internals.options.tokenkey) {
seneca.log('<tokenkey> option is deprecated, please check seneca-auth documentation for migrating to new version of seneca-auth')
}
if (seneca.options().plugin.web && seneca.options().plugin.web.framework) {
internals.options.framework = seneca.options().plugin.web.framework
}
if (_.indexOf(internals.accepted_framworks, internals.options.framework) === -1) {
throw error('Framework type <' + internals.options.framework + '> not supported.')
}
}
internals.migrate_options()
var m = internals.options.prefix.match(/^(.*)\/+$/)
if (m) {
internals.options.prefix = m[1]
}
internals.choose_framework()
return {
name: 'auth'
}
}