@thzero/library_server_firebase 0.14.1 → 0.15.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/auth/index.js CHANGED
@@ -1,3 +1,6 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+
1
4
  import admin from 'firebase-admin';
2
5
 
3
6
  import LibraryConstants from '@thzero/library_server/constants';
@@ -174,7 +177,16 @@ class FirebaseAuthAdminService extends Service {
174
177
  }
175
178
 
176
179
  _initConfig() {
177
- throw new NotImplementedError();
180
+ const filePath = path.join(process.cwd(), 'config', 'serviceAccountKey.json');
181
+ const file = fs.readFileSync(filePath, 'utf8');
182
+ if (String.isNullOrEmpty(file))
183
+ throw Error('Invalid serviceAccountKey.json configuration file for Firebase; expected in the <app root>/config folder.');
184
+
185
+ const config = JSON.parse(file);
186
+ if (!config)
187
+ throw Error('Invalid serviceAccountKey.json file for Firebase config.');
188
+
189
+ return config;
178
190
  }
179
191
  }
180
192
 
@@ -0,0 +1,181 @@
1
+ import admin from 'firebase-admin';
2
+
3
+ import LibraryConstants from '@thzero/library_server/constants';
4
+
5
+ import NotImplementedError from '@thzero/library_common/errors/notImplemented';
6
+
7
+ import Service from '@thzero/library_server/service/index';
8
+
9
+ import TokenExpiredError from '@thzero/library_server/errors/tokenExpired';
10
+
11
+ class FirebaseAuthAdminService extends Service {
12
+ constructor() {
13
+ super();
14
+
15
+ this._serviceUsers = null;
16
+ }
17
+
18
+ async init(injector) {
19
+ await super.init(injector);
20
+
21
+ let serviceAccount = process.env.SERVICE_ACCOUNT_KEY;
22
+ if (serviceAccount)
23
+ serviceAccount = JSON.parse(serviceAccount);
24
+ if (!serviceAccount)
25
+ serviceAccount = this._initConfig();
26
+ admin.initializeApp({
27
+ credential: admin.credential.cert(serviceAccount),
28
+ databaseURL: serviceAccount.database_url
29
+ });
30
+
31
+ this._serviceUsers = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
32
+ }
33
+
34
+ async deleteUser(correlationId, uid) {
35
+ try {
36
+ if (String.isNullOrEmpty(uid))
37
+ return null;
38
+
39
+ const user = await admin.auth().getUser(uid);
40
+ if (!user)
41
+ return null;
42
+
43
+ const results = await admin.auth().deleteUser(uid);
44
+ if (!results)
45
+ return this._error('FirebaseAuthAdminService', 'deleteUser', 'Unable to delete user.', null, null, null, correlationId);
46
+
47
+ return this._success(correlationId);
48
+ }
49
+ catch(err) {
50
+ if (err.code && err.code === 'auth/user-not-found') {
51
+ this._logger.warn('FirebaseAuthAdminService', 'deleteUser', 'user not found', err, correlationId);
52
+ return this._error('FirebaseAuthAdminService', 'deleteUser', 'user-not-found', err, correlationId);
53
+ }
54
+ this._logger.exception('FirebaseAuthAdminService', 'deleteUser', err, correlationId);
55
+ }
56
+
57
+ return this._error('FirebaseAuthAdminService', 'deleteUser', null, null, null, correlationId);
58
+ }
59
+
60
+ async getUser(correlationId, uid) {
61
+ try {
62
+ if (String.isNullOrEmpty(uid))
63
+ return null;
64
+
65
+ const user = await admin.auth().getUser(uid);
66
+ if (!user)
67
+ return null;
68
+
69
+ return this._convert(user);
70
+ }
71
+ catch(err) {
72
+ this._logger.exception('FirebaseAuthAdminService', 'getUser', err, correlationId);
73
+ }
74
+
75
+ return null
76
+ }
77
+
78
+ async setClaims(correlationId, uid, claims, replace) {
79
+ try {
80
+ this._enforceNotEmpty('FirebaseAuthAdminService', 'deleteUser', uid, 'uid', correlationId);
81
+
82
+ // Lookup the user associated with the specified uid.
83
+ const user = await admin.auth().getUser(uid);
84
+ if (!user)
85
+ return this._error('FirebaseAuthAdminService', 'deleteUser', 'Unable to get user', null, null, null, correlationId);
86
+
87
+ let updatedClaims = claims ? { ...claims } : null;
88
+ if (!replace) {
89
+ const customClaims = user.customClaims;
90
+ // merge new claims into existing
91
+ updatedClaims = { ...customClaims, ...claims };
92
+ }
93
+
94
+ // The new custom claims will propagate to the user's ID token the
95
+ // next time a new one is issued.
96
+ await admin.auth().setCustomUserClaims(uid, updatedClaims);
97
+
98
+ return this._success(correlationId);
99
+ }
100
+ catch(err) {
101
+ this._logger.exception('FirebaseAuthAdminService', 'setClaims', err, correlationId);
102
+ return this._error('FirebaseAuthAdminService', 'setClaims', err, null, null, null, correlationId);
103
+ }
104
+ }
105
+
106
+ async verifyToken(correlationId, token) {
107
+ try {
108
+ const results = {
109
+ user: null,
110
+ claims: null,
111
+ success: false
112
+ }
113
+
114
+ if (String.isNullOrEmpty(token))
115
+ return results;
116
+
117
+ const decodedToken = await admin.auth().verifyIdToken(token);
118
+ if (!decodedToken)
119
+ return results;
120
+
121
+ this._logger.debug('FirebaseAuthAdminService', 'verifyToken', 'decodedToken', decodedToken, correlationId);
122
+
123
+ const uid = decodedToken.uid;
124
+ if (!uid)
125
+ return results;
126
+
127
+ // Getting user from database, which has the claims already, plus plan, etc.
128
+ // Lookup the user associated with the specified uid.
129
+ // const user = await admin.auth().getUser(uid);
130
+ // const claims = user.customClaims;
131
+
132
+ const userResponse = await this._serviceUsers.fetchByExternalId(correlationId, uid);
133
+ if (this._hasFailed(userResponse) || (this._hasSucceeded(userResponse) && !userResponse.results)) {
134
+ const userUpdateResponse = this._serviceUsers.update(correlationId, {
135
+ id: uid
136
+ });
137
+ if (this._hasFailed(userUpdateResponse) || (this._hasSucceeded(userUpdateResponse) && !userUpdateResponse.results))
138
+ return results;
139
+ }
140
+
141
+ results.user = userResponse.results;
142
+
143
+ results.claims = userResponse.results.claims;
144
+ const configAuth = this._config.get('auth');
145
+ if (configAuth.claims && configAuth.claims.useDefault && !results.claims)
146
+ results.claims = [ this._defaultClaims() ];
147
+
148
+ results.success = true;
149
+ return results;
150
+ }
151
+ catch(err) {
152
+ this._logger.exception('FirebaseAuthAdminService', 'verifyToken', err, correlationId);
153
+ if (err.code === "auth/id-token-expired")
154
+ throw new TokenExpiredError();
155
+ }
156
+
157
+ return null;
158
+ }
159
+
160
+ _convert(requestedUser) {
161
+ if (!requestedUser)
162
+ return null;
163
+
164
+ const user = {};
165
+ user.id = requestedUser.uid;
166
+ user.name = requestedUser.displayName;
167
+ user.picture = requestedUser.photoURL;
168
+ user.email = requestedUser.email;
169
+ return user;
170
+ }
171
+
172
+ _defaultClaims() {
173
+ throw new NotImplementedError();
174
+ }
175
+
176
+ _initConfig() {
177
+ throw new NotImplementedError();
178
+ }
179
+ }
180
+
181
+ export default FirebaseAuthAdminService;
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@thzero/library_server_firebase",
3
- "version": "0.14.1",
3
+ "type": "module",
4
+ "version": "0.15.2",
4
5
  "version_major": 0,
5
- "version_minor": 14,
6
- "version_patch": 1,
7
- "version_date": "10/02/2021",
6
+ "version_minor": 15,
7
+ "version_patch": 2,
8
+ "version_date": "10/20/2021",
8
9
  "author": "thZero",
9
10
  "license": "MIT",
10
11
  "repository": {
@@ -24,14 +25,14 @@
24
25
  "test": "echo \"Error: no test specified\" && exit 1"
25
26
  },
26
27
  "dependencies": {
27
- "firebase-admin": "^9.12.0"
28
+ "firebase-admin": "^10.0.0"
28
29
  },
29
30
  "peerDependencies": {
30
- "@thzero/library_common": "^0.14",
31
- "@thzero/library_common_service": "^0.14",
32
- "@thzero/library_server": "^0.14"
31
+ "@thzero/library_common": "^0.15",
32
+ "@thzero/library_common_service": "^0.15",
33
+ "@thzero/library_server": "^0.15"
33
34
  },
34
35
  "devDependencies": {
35
- "@thzero/library_cli": "^0.13.19"
36
+ "@thzero/library_cli": "^0.13.21"
36
37
  }
37
38
  }
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@thzero/library_server_firebase",
3
+ "version": "0.14.3",
4
+ "version_major": 0,
5
+ "version_minor": 14,
6
+ "version_patch": 3,
7
+ "version_date": "10/15/2021",
8
+ "author": "thZero",
9
+ "license": "MIT",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/thzero/library_server_firebase.git"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/thzero/library_server_firebase/issues"
16
+ },
17
+ "homepage": "https://github.com/thzero/library_server_firebase#readme",
18
+ "engines": {
19
+ "node": ">=12.8.3"
20
+ },
21
+ "scripts": {
22
+ "cli-update": "./node_modules/.bin/library-cli --updateversion --pi",
23
+ "@thzero/library_common": "^0.13",
24
+ "test": "echo \"Error: no test specified\" && exit 1"
25
+ },
26
+ "dependencies": {
27
+ "firebase-admin": "^10.0.0"
28
+ },
29
+ "peerDependencies": {
30
+ "@thzero/library_common": "^0.14",
31
+ "@thzero/library_common_service": "^0.14",
32
+ "@thzero/library_server": "^0.14"
33
+ },
34
+ "devDependencies": {
35
+ "@thzero/library_cli": "^0.13.21"
36
+ }
37
+ }