-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebaseAuth.ts
152 lines (143 loc) · 4.4 KB
/
firebaseAuth.ts
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
152
import { KeystoneContext } from '@keystone-6/core/types';
import * as Keystone from '.keystone/types';
import firebaseAdmin from 'firebase-admin';
import { UserRecord } from 'firebase-admin/auth'
import _ from 'lodash';
type Writeable<T> = { -readonly [P in keyof T]: T[P] };
type DeepWriteable<T> = { -readonly [P in keyof T]: DeepWriteable<T[P]> };
export const getUser = async (accessToken: string, context: KeystoneContext) => {
context = context.sudo();
try {
if (accessToken) {
let decodedToken;
try {
decodedToken = await firebaseAdmin
.auth()
.verifyIdToken(accessToken);
} catch (error) {
throw error;
}
let firebaseId = decodedToken.uid;
// Find an existing user that matches the given validated service id
let user: Record<string, any>;
try {
user = await context.query.User.findOne({
where: { firebaseId },
query: 'id name email firebaseId lastUpdated roles { value }'
}) as any;
} catch (error) {
throw error;
}
let operation: string | undefined = "create";
if (user) {
const lastUpdated = new Date(user.lastUpdated || '');
if (Math.abs(Date.now() - lastUpdated.getTime()) > 604800000) {
operation = "update";
} else {
operation = undefined;
}
}
let userRecord: UserRecord;
try {
userRecord = await firebaseAdmin.auth().getUser(firebaseId);
} catch (error) {
throw error;
}
const count = await context.db.User.count();
if (operation === 'create') {
const data: DeepWriteable<Keystone.UserCreateInput> = {
name: userRecord.displayName || '',
email: userRecord.email || '',
phoneNumbers: userRecord.phoneNumber ? { create: [{ type: 'mobile', value: userRecord.phoneNumber }] } : undefined,
firebaseId
};
if (count === 0) {
data.roles = { create: { value: 'Admin' } }
await context.query.Role.createMany({
data: [
{
value: 'Booker',
},
{
value: 'Editor',
},
]
})
}
user = await context.query.User.createOne({
data,
query: 'id name email firebaseId lastUpdated roles { value }'
}) as Keystone.Lists.User.Item;
} else if (operation === 'update') {
user = await context.query.User.updateOne({
where: { id: user.id },
data: {
name: userRecord.displayName,
email: userRecord.email,
firebaseId
},
query: 'id name email firebaseId lastUpdated roles { value }'
}) as Keystone.Lists.User.Item;
}
return {
firebaseUser: userRecord,
keystoneUser: user
};
}
} catch (e) {
console.error(e);
} finally {
context = context.exitSudo();
}
}
export type Context = {
keystoneUser?: Record<string, any>,
firebaseUser?: UserRecord,
isAdmin?: boolean,
} & KeystoneContext
export const accessSetup = async (context: Context): Promise<Context> => {
const accessToken = context.req?.headers.authorization;
if (accessToken && !context.keystoneUser) {
context = { ... (await getUser(accessToken, context)), ...context };
}
context.isAdmin = _.some(context.keystoneUser?.roles, { 'value': 'Admin' });
return context;
}
export type AccessArgs = {
context?: KeystoneContext
item?: any;
};
export const access = {
isAdmin: async ({ context }: AccessArgs) => {
const ctx = await accessSetup(context as Context);
return ctx?.isAdmin || false;
},
isEditor: async ({ context }: AccessArgs) => {
const ctx = await accessSetup(context as Context);
return ctx?.isAdmin || _.some(ctx.keystoneUser?.roles, { 'value': 'Editor' });
},
isBooker: async ({ context }: AccessArgs) => {
const ctx = await accessSetup(context as Context);
return ctx?.isAdmin || _.some(ctx.keystoneUser?.roles, { 'value': 'Booker' });
},
isOwner: async ({ context, item }: AccessArgs) => {
const ctx = await accessSetup(context as Context);
return (item as Record<string, any>).author.id === ctx.keystoneUser?.id || false;
},
isOwnerFilter: async ({ context }: AccessArgs, filter = { firebaseId: { equals: '' } }, path = 'firebaseId.equals') => {
const ctx = await accessSetup(context as Context);
if (ctx.isAdmin) {
return true;
}
_.set(filter, path, ctx.keystoneUser?.firebaseId);
return filter;
},
combineRules: async (rules: Promise<boolean>[], operand: '&&' | '||' = '||') => {
const results = await Promise.all(rules);
if (operand === '&&') {
return results.every(Boolean);
} else if (operand === '||') {
return results.some(Boolean);
}
}
};