-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
74 lines (60 loc) · 1.66 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
'use strict'
const async = require('async')
const Base = require('bfx-facs-base')
const FastifyAuth = require('@fastify/oauth2')
const SUPPORTED_AUTHS = ['google']
class HttpdAuthFacility extends Base {
constructor (caller, opts, ctx) {
super(caller, opts, ctx)
this.name = 'httpd-oauth2'
this._hasConf = true
this.init()
}
getSpecs (method) {
const specs = {}
switch (method) {
case 'google':
specs.name = 'googleOAuth2'
specs.auth = FastifyAuth.GOOGLE_CONFIGURATION
specs.startRedirectPath = this.conf.startRedirectPath || '/login/google'
specs.callbackUri = this.conf.callbackUri || '/login/google/callback'
specs.callbackUriParams = {
access_type: 'offline'
}
break
}
return specs
}
injection () {
const creds = this.conf.credentials
const specs = this.getSpecs(this.conf.method)
return [FastifyAuth, {
name: specs.name,
scope: ['profile', 'email'],
credentials: {
client: creds.client,
auth: specs.auth
},
startRedirectPath: specs.startRedirectPath,
callbackUri: specs.callbackUri,
callbackUriParams: specs.callbackUriParams
}]
}
resolveUserAccess (user, idField = 'email') {
return this.conf.users.find(u => u[idField] === user)
}
callbackUriUI () {
return this.conf.callbackUriUI
}
_start (cb) {
async.series([
next => { super._start(next) },
async () => {
if (!SUPPORTED_AUTHS.includes(this.conf.method)) {
throw new Error('ERR_FACS_HTTPD_OAUTH2_METHOD_INVALID')
}
}
], cb)
}
}
module.exports = HttpdAuthFacility