forked from IOsonoTAN/koa-firebase-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (128 loc) · 3.24 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const {
merge
} = require('lodash')
const admin = require('firebase-admin')
const bluebird = require('bluebird')
const moment = require('moment')
const redis = require('redis')
const {
sprintf
} = require('sprintf-js')
const {
MongoClient
} = require('mongodb')
const options = require('./options')
const {
getAccessToken,
getFID,
throwError,
lower
} = require('./helpers')(options)
bluebird.promisifyAll(redis.RedisClient.prototype)
bluebird.promisifyAll(redis.Multi.prototype)
const datetimeFormat = 'MM/DD/YYYY HH:mm:ss'
let redisClient
let User
let userInfo
const handleError = (ctx, e) => {
const status = (!e.statusCode ? 401 : e.statusCode)
ctx.status = status
ctx.body = {
error: {
status,
message: e.message
}
}
return true
}
const init = (_options = {}) => {
merge(options, _options)
admin.initializeApp({
credential: admin.credential.cert(options.credential),
databaseURL: options.databaseURL
})
}
const connectRedis = async () => {
redisClient = await redis.createClient({
url: options.redis.url
})
return redisClient
}
const verifyAccessToken = async (ctx, next) => {
try {
const accessToken = getAccessToken(ctx)
const fid = getFID(ctx)
redisClient = await connectRedis()
const getRedisKey = sprintf(options.redis.storeKey, {
fid
})
const redisValue = await redisClient.getAsync(getRedisKey)
if (redisValue) {
ctx.user = JSON.parse(redisValue)
return next()
}
const db = await MongoClient.connect(options.mongo.url)
User = db.collection(options.mongo.userCollection)
const authData = await admin.auth()
.verifyIdToken(accessToken)
if (authData.uid !== fid) {
throwError(lower('Unauthorized.'), 401)
}
const redisKey = sprintf(options.redis.storeKey, {
fid: authData.uid
})
const nowTime = moment(moment()
.format(datetimeFormat), datetimeFormat)
const expTime = moment.unix(authData.exp, datetimeFormat)
const redisTTL = Math.round(((expTime.diff(nowTime) / 1000) / 60) * 60)
userInfo = await User.findOne({
[`${options.mongo.fields.authFirebase}.${options.mongo.fields.fid}`]: fid
})
if (!userInfo) {
userInfo = await User.insert({
[options.mongo.fields.authFirebase]: {
[options.mongo.fields.fid]: fid
},
[options.mongo.fields.createdAt]: new Date()
})
userInfo = userInfo.ops[0]
}
const dataContext = {
accessToken,
fid,
...userInfo
}
redisClient.set(redisKey, JSON.stringify(dataContext), 'EX', redisTTL)
ctx.user = dataContext
return next()
} catch (e) {
return handleError(ctx, e)
}
}
const passUserContext = async (ctx, next) => {
try {
const fid = getFID(ctx, {
skipThrowError: true
})
if (!fid) {
return next()
}
redisClient = await connectRedis()
const redisStoreKey = sprintf(options.redis.storeKey, {
fid
})
const redisValue = await redisClient.getAsync(redisStoreKey)
if (redisValue) {
ctx.user = JSON.parse(redisValue)
}
return next()
} catch (e) {
return handleError(ctx, e)
}
}
const moduleExports = {
init,
verifyAccessToken,
passUserContext
}
module.exports = moduleExports