@ejfdelgado/ejflab-back 1.23.0 → 1.24.1
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/package.json +1 -1
- package/srv/AuthorizationSrv.mjs +52 -0
- package/srv/common/FirebasConfig.mjs +66 -11
- package/srv/common/Usuario.mjs +30 -8
package/package.json
CHANGED
package/srv/AuthorizationSrv.mjs
CHANGED
@@ -140,6 +140,58 @@ export class AuthorizationSrv {
|
|
140
140
|
});
|
141
141
|
}
|
142
142
|
|
143
|
+
static isUserInGroupInternal(user, groups, and) {
|
144
|
+
if (!user) {
|
145
|
+
return false;
|
146
|
+
}
|
147
|
+
const currentGroups = user.groups;
|
148
|
+
const notMeet = groups.filter((group) => {
|
149
|
+
if (currentGroups.indexOf(group) >= 0) {
|
150
|
+
return false;
|
151
|
+
}
|
152
|
+
return true;
|
153
|
+
});
|
154
|
+
if (and) {
|
155
|
+
//All must have
|
156
|
+
return notMeet.length == 0;
|
157
|
+
} else {
|
158
|
+
// At least one
|
159
|
+
return notMeet.length < groups.length;
|
160
|
+
}
|
161
|
+
}
|
162
|
+
|
163
|
+
static isUserInAllGroup(groups) {
|
164
|
+
return async (req, res, next) => {
|
165
|
+
if (res.locals.user) {
|
166
|
+
// Hay usuario
|
167
|
+
const cumple = this.isUserInGroupInternal(res.locals.user, groups, true);
|
168
|
+
if (cumple) {
|
169
|
+
next();
|
170
|
+
} else {
|
171
|
+
res.status(403).send({ message: `User not allowed` });
|
172
|
+
}
|
173
|
+
} else {
|
174
|
+
res.status(403).send({ message: `User not authenticated` });
|
175
|
+
}
|
176
|
+
}
|
177
|
+
}
|
178
|
+
|
179
|
+
static isUserInSomeGroup(groups) {
|
180
|
+
return async (req, res, next) => {
|
181
|
+
if (res.locals.user) {
|
182
|
+
// Hay usuario
|
183
|
+
const cumple = this.isUserInGroupInternal(res.locals.user, groups, false);
|
184
|
+
if (cumple) {
|
185
|
+
next();
|
186
|
+
} else {
|
187
|
+
res.status(403).send({ message: `User not allowed` });
|
188
|
+
}
|
189
|
+
} else {
|
190
|
+
res.status(403).send({ message: `User not authenticated` });
|
191
|
+
}
|
192
|
+
}
|
193
|
+
}
|
194
|
+
|
143
195
|
static hasPagePermisions(listaOr) {
|
144
196
|
return async (req, res, next) => {
|
145
197
|
if (listaOr.length == 0) {
|
@@ -5,6 +5,48 @@ import { Usuario } from './Usuario.mjs';
|
|
5
5
|
import fs from "fs";
|
6
6
|
import { General } from './General.mjs';
|
7
7
|
import { MyConstants } from '@ejfdelgado/ejflab-common/src/MyConstants.js';
|
8
|
+
import jwt from 'jsonwebtoken';
|
9
|
+
import jwksClient from 'jwks-rsa';
|
10
|
+
|
11
|
+
const AUTH_PROVIDER = process.env.AUTH_PROVIDER;
|
12
|
+
const MICROSOFT_CLIENT_ID = process.env.MICROSOFT_CLIENT_ID;
|
13
|
+
const MICROSOFT_TENANT = process.env.MICROSOFT_TENANT;
|
14
|
+
|
15
|
+
const microsoftClient = jwksClient({
|
16
|
+
jwksUri: `https://login.microsoftonline.com/${MICROSOFT_TENANT}/discovery/v2.0/keys`,
|
17
|
+
});
|
18
|
+
|
19
|
+
// Helper to get the signing key
|
20
|
+
function getMicrosoftKey(header, callback) {
|
21
|
+
microsoftClient.getSigningKey(header.kid, (err, key) => {
|
22
|
+
if (err) {
|
23
|
+
return callback(err);
|
24
|
+
}
|
25
|
+
const signingKey = key.getPublicKey();
|
26
|
+
callback(null, signingKey);
|
27
|
+
});
|
28
|
+
}
|
29
|
+
|
30
|
+
function verifyMicrosoftToken(token) {
|
31
|
+
return new Promise((resolve, reject) => {
|
32
|
+
jwt.verify(
|
33
|
+
token,
|
34
|
+
getMicrosoftKey,
|
35
|
+
{
|
36
|
+
algorithms: ['RS256'], // Tokens are typically signed with RS256
|
37
|
+
audience: MICROSOFT_CLIENT_ID, // Replace with your application's client ID
|
38
|
+
issuer: `https://login.microsoftonline.com/${MICROSOFT_TENANT}/v2.0`, // Replace with your tenant's issuer
|
39
|
+
//issuer: `https://sts.windows.net/${MICROSOFT_TENANT}/`
|
40
|
+
},
|
41
|
+
(err, decoded) => {
|
42
|
+
if (err) {
|
43
|
+
return reject(err);
|
44
|
+
}
|
45
|
+
resolve(decoded);
|
46
|
+
}
|
47
|
+
);
|
48
|
+
});
|
49
|
+
}
|
8
50
|
|
9
51
|
function getFirebaseConfig() {
|
10
52
|
const firebaseJson = fs.readFileSync(MyConstants.FIREBASE_CONFIG_FILE, { encoding: "utf8" });
|
@@ -54,17 +96,30 @@ async function checkAutenticated(req) {
|
|
54
96
|
reject(new MyError("Missing Authorization header.", 403));
|
55
97
|
return;
|
56
98
|
}
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
99
|
+
if (AUTH_PROVIDER == "microsoft") {
|
100
|
+
verifyMicrosoftToken(sessionToken)
|
101
|
+
.then((decodedToken) => {
|
102
|
+
resolve({
|
103
|
+
decodedToken,
|
104
|
+
sessionToken,
|
105
|
+
});
|
106
|
+
})
|
107
|
+
.catch((error) => {
|
108
|
+
reject(new MyError(error.message, 403));
|
63
109
|
});
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
110
|
+
} else if (AUTH_PROVIDER == "google") {
|
111
|
+
getAuth()
|
112
|
+
.verifyIdToken(sessionToken)
|
113
|
+
.then((decodedToken) => {
|
114
|
+
resolve({
|
115
|
+
decodedToken,
|
116
|
+
sessionToken,
|
117
|
+
});
|
118
|
+
})
|
119
|
+
.catch((error) => {
|
120
|
+
reject(new MyError(error.message, 403));
|
121
|
+
});
|
122
|
+
}
|
68
123
|
});
|
69
124
|
}
|
70
125
|
|
@@ -107,7 +162,7 @@ async function checkAuthenticatedSilent(req, res, next) {
|
|
107
162
|
res.locals.user = new Usuario(res.locals.token);
|
108
163
|
await next();
|
109
164
|
} catch (err) {
|
110
|
-
|
165
|
+
console.log(err);
|
111
166
|
res.locals.token = null;
|
112
167
|
res.locals.user = null;
|
113
168
|
await next();
|
package/srv/common/Usuario.mjs
CHANGED
@@ -4,7 +4,10 @@ import { MyConstants } from "@ejfdelgado/ejflab-common/src/MyConstants.js";
|
|
4
4
|
import { MyStore } from "./MyStore.mjs";
|
5
5
|
import { General } from "./General.mjs";
|
6
6
|
import { MalaPeticionException } from "../MyError.mjs";
|
7
|
+
import { Buffer } from 'buffer';
|
7
8
|
|
9
|
+
const AUTH_PROVIDER = process.env.AUTH_PROVIDER;
|
10
|
+
const groupIdMap = JSON.parse("AUTH_GROUP_ID_MAP" in process.env ? Buffer.from(process.env.AUTH_GROUP_ID_MAP, 'base64').toString("utf8") : "{}");
|
8
11
|
const USER_TYPE = "user";
|
9
12
|
|
10
13
|
export class Usuario {
|
@@ -12,20 +15,39 @@ export class Usuario {
|
|
12
15
|
id = null;
|
13
16
|
email = null;
|
14
17
|
phone = null;
|
18
|
+
groups = [];
|
15
19
|
constructor(token) {
|
16
20
|
this.metadatos = token;
|
17
21
|
if (this.metadatos != null) {
|
18
22
|
if (this.metadatos.email) {
|
19
23
|
this.email = this.metadatos.email;
|
20
24
|
}
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
if (AUTH_PROVIDER == "microsoft") {
|
26
|
+
this.id = token.oid;
|
27
|
+
this.email = token.preferred_username;
|
28
|
+
if (token.groups instanceof Array) {
|
29
|
+
this.groups = token.groups.map((idGroup) => {
|
30
|
+
if (idGroup in groupIdMap) {
|
31
|
+
return groupIdMap[idGroup];
|
32
|
+
}
|
33
|
+
return idGroup;
|
34
|
+
});
|
35
|
+
}
|
36
|
+
//console.log(`id: ${this.id}`);
|
37
|
+
//console.log(`email: ${this.email}`);
|
38
|
+
//console.log(`groups: ${JSON.stringify(this.groups)}`);
|
39
|
+
} else {
|
40
|
+
if ("firebase" in this.metadatos) {
|
41
|
+
const contenedor = this.metadatos["firebase"];
|
42
|
+
const identidades = contenedor["identities"];
|
43
|
+
if ("email" in identidades) {
|
44
|
+
this.id = identidades["email"][0];
|
45
|
+
this.email = this.id;
|
46
|
+
} else if ("phone" in identidades) {
|
47
|
+
this.id = identidades["phone"][0];
|
48
|
+
this.phone = this.id;
|
49
|
+
}
|
50
|
+
}
|
29
51
|
}
|
30
52
|
}
|
31
53
|
}
|