pangea-server 3.3.53 → 3.3.54
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/dist/authentication/access-token.class.d.ts +0 -13
- package/dist/authentication/access-token.class.js +0 -28
- package/dist/authentication/authentication.helpers.d.ts +0 -17
- package/dist/authentication/authentication.helpers.js +0 -73
- package/dist/authentication/authentication.types.d.ts +0 -11
- package/dist/authentication/authentication.types.js +0 -2
- package/dist/authentication/base-auth.class.d.ts +0 -11
- package/dist/authentication/base-auth.class.js +0 -35
- package/dist/authentication/index.d.ts +0 -4
- package/dist/authentication/index.js +0 -20
- package/dist/helpers/file.helpers.d.ts +0 -21
- package/dist/helpers/file.helpers.js +0 -73
- package/dist/helpers/hashing.helpers.d.ts +0 -2
- package/dist/helpers/hashing.helpers.js +0 -16
- package/dist/helpers/password.helpers.d.ts +0 -4
- package/dist/helpers/password.helpers.js +0 -16
- package/dist/types/error.types.d.ts +0 -4
- package/dist/types/error.types.js +0 -2
package/package.json
CHANGED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { User } from '../database';
|
|
2
|
-
import type { UserCtor } from './authentication.types';
|
|
3
|
-
export declare class AccessToken {
|
|
4
|
-
private __privateKey;
|
|
5
|
-
constructor(privateKey: string);
|
|
6
|
-
getKey(userCtor: UserCtor): string;
|
|
7
|
-
createToken(userCtor: UserCtor, id: ModelId): string;
|
|
8
|
-
verifyToken(token: string): Record<string, number>;
|
|
9
|
-
getTokenData(userCtor: UserCtor, user: User): {
|
|
10
|
-
accessToken: string;
|
|
11
|
-
user: User;
|
|
12
|
-
};
|
|
13
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.AccessToken = void 0;
|
|
7
|
-
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
-
// helpers
|
|
9
|
-
const pangea_helpers_1 = require("pangea-helpers");
|
|
10
|
-
class AccessToken {
|
|
11
|
-
constructor(privateKey) {
|
|
12
|
-
this.__privateKey = privateKey;
|
|
13
|
-
}
|
|
14
|
-
getKey(userCtor) {
|
|
15
|
-
return `${(0, pangea_helpers_1.getFirstChartInLowercase)(userCtor.name)}Id`;
|
|
16
|
-
}
|
|
17
|
-
createToken(userCtor, id) {
|
|
18
|
-
const key = this.getKey(userCtor);
|
|
19
|
-
return jsonwebtoken_1.default.sign({ [key]: id }, this.__privateKey, { expiresIn: 60 * 60 * 24 * 365 });
|
|
20
|
-
}
|
|
21
|
-
verifyToken(token) {
|
|
22
|
-
return jsonwebtoken_1.default.verify(token, this.__privateKey);
|
|
23
|
-
}
|
|
24
|
-
getTokenData(userCtor, user) {
|
|
25
|
-
return { accessToken: this.createToken(userCtor, user.id), user };
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
exports.AccessToken = AccessToken;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { AccessToken } from './access-token.class';
|
|
2
|
-
import { User } from '../database';
|
|
3
|
-
import type { UserCtor, AuthMap } from './authentication.types';
|
|
4
|
-
type AuthHeader = string | undefined;
|
|
5
|
-
export declare function login<U extends User>(authHeader: AuthHeader, db: Db, accessToken: AccessToken, userCtor: BaseModelCtor<U>, extraWhere?: Where<U>): Promise<{
|
|
6
|
-
accessToken: string;
|
|
7
|
-
user: User;
|
|
8
|
-
}>;
|
|
9
|
-
export declare function validateAccessToken(authHeader: AuthHeader, db: Db, accessToken: AccessToken, userCtor: UserCtor): Promise<{
|
|
10
|
-
accessToken: string;
|
|
11
|
-
user: User;
|
|
12
|
-
}>;
|
|
13
|
-
export declare function getUserFromToken(authHeader: AuthHeader, db: Db, accessToken: AccessToken, userCtor: UserCtor): Promise<User | null> | null;
|
|
14
|
-
export declare function getUsersFromToken(authHeader: AuthHeader, db: Db, accessToken: AccessToken, authMap: AuthMap): Promise<{
|
|
15
|
-
[k: string]: User | null;
|
|
16
|
-
}>;
|
|
17
|
-
export {};
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getUsersFromToken = exports.getUserFromToken = exports.validateAccessToken = exports.login = void 0;
|
|
4
|
-
// helpers
|
|
5
|
-
const helpers_1 = require("../helpers");
|
|
6
|
-
async function login(authHeader, db, accessToken, userCtor, extraWhere = {}) {
|
|
7
|
-
if (!authHeader?.startsWith('Basic '))
|
|
8
|
-
helpers_1.AppError.ThrowInvalidCredentials();
|
|
9
|
-
const base64 = authHeader.slice(6);
|
|
10
|
-
const decoded = Buffer.from(base64, 'base64').toString('utf-8');
|
|
11
|
-
const [username, password] = decoded.split(':');
|
|
12
|
-
if (!username || !password)
|
|
13
|
-
helpers_1.AppError.ThrowInvalidCredentials();
|
|
14
|
-
const where = { ...extraWhere, username };
|
|
15
|
-
const user = await db.findOneOrNull(userCtor, where, { scopes: ['withHiddenAttributes'] });
|
|
16
|
-
if (!user)
|
|
17
|
-
helpers_1.AppError.ThrowInvalidCredentials();
|
|
18
|
-
const isPasswordValid = await (0, helpers_1.comparePasswords)(password, user.password);
|
|
19
|
-
if (!isPasswordValid)
|
|
20
|
-
helpers_1.AppError.ThrowInvalidCredentials();
|
|
21
|
-
return accessToken.getTokenData(userCtor, user);
|
|
22
|
-
}
|
|
23
|
-
exports.login = login;
|
|
24
|
-
async function validateAccessToken(authHeader, db, accessToken, userCtor) {
|
|
25
|
-
const user = await getUserFromToken(authHeader, db, accessToken, userCtor);
|
|
26
|
-
if (!user)
|
|
27
|
-
helpers_1.AppError.ThrowUnauthorized();
|
|
28
|
-
return accessToken.getTokenData(userCtor, user);
|
|
29
|
-
}
|
|
30
|
-
exports.validateAccessToken = validateAccessToken;
|
|
31
|
-
function getUserFromToken(authHeader, db, accessToken, userCtor) {
|
|
32
|
-
try {
|
|
33
|
-
const token = getBearerToken(authHeader);
|
|
34
|
-
const payload = accessToken.verifyToken(token);
|
|
35
|
-
const key = accessToken.getKey(userCtor);
|
|
36
|
-
const id = payload[key];
|
|
37
|
-
if (!id)
|
|
38
|
-
return null;
|
|
39
|
-
return db.findOneOrNull(userCtor, id);
|
|
40
|
-
}
|
|
41
|
-
catch {
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
exports.getUserFromToken = getUserFromToken;
|
|
46
|
-
async function getUsersFromToken(authHeader, db, accessToken, authMap) {
|
|
47
|
-
try {
|
|
48
|
-
const token = getBearerToken(authHeader);
|
|
49
|
-
const payload = accessToken.verifyToken(token);
|
|
50
|
-
const results = await Promise.all(Object.entries(authMap).map(async ([type, userCtor]) => {
|
|
51
|
-
const key = accessToken.getKey(userCtor);
|
|
52
|
-
const id = payload[key];
|
|
53
|
-
if (!id)
|
|
54
|
-
return [type, null];
|
|
55
|
-
const user = await db.findOneOrNull(userCtor, id);
|
|
56
|
-
return [type, user];
|
|
57
|
-
}));
|
|
58
|
-
return Object.fromEntries(results);
|
|
59
|
-
}
|
|
60
|
-
catch {
|
|
61
|
-
return Object.fromEntries(Object.keys(authMap).map((type) => [type, null]));
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
exports.getUsersFromToken = getUsersFromToken;
|
|
65
|
-
// internal functions
|
|
66
|
-
function getBearerToken(authHeader) {
|
|
67
|
-
if (!authHeader)
|
|
68
|
-
throw new Error('Missing authorization header');
|
|
69
|
-
const [type, token] = authHeader.split(' ');
|
|
70
|
-
if (type !== 'Bearer' || !token)
|
|
71
|
-
throw new Error('Invalid authorization format');
|
|
72
|
-
return token;
|
|
73
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { BaseAuth } from './base-auth.class';
|
|
2
|
-
import { User } from '../database';
|
|
3
|
-
export type UserCtor = BaseModelCtor<User>;
|
|
4
|
-
export type AuthMap = Record<string, UserCtor>;
|
|
5
|
-
export type UserType<AM extends AuthMap> = keyof AM;
|
|
6
|
-
export type AuthUsers<AM extends AuthMap> = {
|
|
7
|
-
[K in keyof AM]: InstanceType<AM[K]> | null;
|
|
8
|
-
};
|
|
9
|
-
export interface AuthCtor<AM extends AuthMap, AU extends AuthUsers<AM>, BA extends BaseAuth<AM>> {
|
|
10
|
-
new (authUsers: AU): BA;
|
|
11
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { AuthMap, UserType, AuthUsers } from './authentication.types';
|
|
2
|
-
export declare abstract class BaseAuth<AM extends AuthMap> {
|
|
3
|
-
private __authUsers;
|
|
4
|
-
constructor(authUsers: AuthUsers<AM>);
|
|
5
|
-
free(): void;
|
|
6
|
-
notSetYet(): void;
|
|
7
|
-
notAllowed(): void;
|
|
8
|
-
isUserAuth<T extends UserType<AM>>(type: T): boolean;
|
|
9
|
-
getUserAuth<T extends UserType<AM>>(type: T): NonNullable<AuthUsers<AM>[T]>;
|
|
10
|
-
getUsersAuth<const T extends readonly UserType<AM>[]>(types: T): { [I in keyof T]: InstanceType<AM[T[I]]> | null; };
|
|
11
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BaseAuth = void 0;
|
|
4
|
-
// helpers
|
|
5
|
-
const helpers_1 = require("../helpers");
|
|
6
|
-
class BaseAuth {
|
|
7
|
-
constructor(authUsers) {
|
|
8
|
-
this.__authUsers = authUsers;
|
|
9
|
-
}
|
|
10
|
-
free() {
|
|
11
|
-
(0, helpers_1.printWarning)('authentication', 'free access');
|
|
12
|
-
}
|
|
13
|
-
notSetYet() {
|
|
14
|
-
(0, helpers_1.printWarning)('authentication', 'auth not set yet');
|
|
15
|
-
}
|
|
16
|
-
notAllowed() {
|
|
17
|
-
helpers_1.AppError.ThrowUnauthorized();
|
|
18
|
-
}
|
|
19
|
-
isUserAuth(type) {
|
|
20
|
-
return !!this.__authUsers[type];
|
|
21
|
-
}
|
|
22
|
-
getUserAuth(type) {
|
|
23
|
-
const user = this.__authUsers[type];
|
|
24
|
-
if (!user)
|
|
25
|
-
helpers_1.AppError.ThrowUnauthorized();
|
|
26
|
-
return user;
|
|
27
|
-
}
|
|
28
|
-
getUsersAuth(types) {
|
|
29
|
-
const users = types.map((type) => this.__authUsers[type]);
|
|
30
|
-
if (users.every((user) => !user))
|
|
31
|
-
helpers_1.AppError.ThrowUnauthorized();
|
|
32
|
-
return users;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
exports.BaseAuth = BaseAuth;
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./access-token.class"), exports);
|
|
18
|
-
__exportStar(require("./authentication.helpers"), exports);
|
|
19
|
-
__exportStar(require("./authentication.types"), exports);
|
|
20
|
-
__exportStar(require("./base-auth.class"), exports);
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
type GeneralUploadParamsConfig = {
|
|
2
|
-
folder: string;
|
|
3
|
-
fileType: string;
|
|
4
|
-
};
|
|
5
|
-
export declare abstract class File {
|
|
6
|
-
UploadImage(image: UploadableImage, folder: string, previousFile?: string | null): Promise<string>;
|
|
7
|
-
UploadImages(images: UploadableImage[], folder: string): Promise<string[]>;
|
|
8
|
-
UploadVideo(file: MulterFile, folder: string): Promise<string>;
|
|
9
|
-
UploadVideos(files: MulterFile[], folder: string): Promise<string[]>;
|
|
10
|
-
GenerateUploadUrl(config: GeneralUploadParamsConfig): Promise<{
|
|
11
|
-
url: string;
|
|
12
|
-
fileName: string;
|
|
13
|
-
}>;
|
|
14
|
-
GenerateUploadUrls(folder: string, fileTypes: string[]): Promise<{
|
|
15
|
-
url: string;
|
|
16
|
-
fileName: string;
|
|
17
|
-
}[]>;
|
|
18
|
-
DeleteFile(fileName: string): Promise<void>;
|
|
19
|
-
DeleteFiles(fileNames: string[]): Promise<void[]>;
|
|
20
|
-
}
|
|
21
|
-
export {};
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.File = void 0;
|
|
7
|
-
const path_1 = __importDefault(require("path"));
|
|
8
|
-
const promises_1 = __importDefault(require("fs/promises"));
|
|
9
|
-
const fs_1 = __importDefault(require("fs"));
|
|
10
|
-
const aws_sdk_1 = __importDefault(require("aws-sdk"));
|
|
11
|
-
// helpers
|
|
12
|
-
const print_helpers_1 = require("./print.helpers");
|
|
13
|
-
const env_helpers_1 = require("./env.helpers");
|
|
14
|
-
const random_helpers_1 = require("./random.helpers");
|
|
15
|
-
const bucketName = (0, env_helpers_1.getEnvStr)('DIGITAL_OCEAN_SPACES_BUCKET');
|
|
16
|
-
const endpointUrl = 'nyc3.digitaloceanspaces.com';
|
|
17
|
-
const s3 = new aws_sdk_1.default.S3({
|
|
18
|
-
endpoint: new aws_sdk_1.default.Endpoint(endpointUrl),
|
|
19
|
-
accessKeyId: (0, env_helpers_1.getEnvStr)('DIGITAL_OCEAN_SPACES_ACCESS_KEY'),
|
|
20
|
-
secretAccessKey: (0, env_helpers_1.getEnvStr)('DIGITAL_OCEAN_SPACES_SECRET_KEY'),
|
|
21
|
-
});
|
|
22
|
-
class File {
|
|
23
|
-
async UploadImage(image, folder, previousFile) {
|
|
24
|
-
if (previousFile)
|
|
25
|
-
await this.DeleteFile(previousFile);
|
|
26
|
-
const isString = typeof image === 'string';
|
|
27
|
-
const buffer = isString ? await promises_1.default.readFile(path_1.default.join(process.cwd(), image)) : image.buffer;
|
|
28
|
-
const fileType = isString ? 'image/png' : image.mimetype;
|
|
29
|
-
return uploadFile(buffer, { folder, fileType });
|
|
30
|
-
}
|
|
31
|
-
UploadImages(images, folder) {
|
|
32
|
-
return Promise.all(images.map((image) => this.UploadImage(image, folder)));
|
|
33
|
-
}
|
|
34
|
-
async UploadVideo(file, folder) {
|
|
35
|
-
const stream = fs_1.default.createReadStream(file.path);
|
|
36
|
-
try {
|
|
37
|
-
return await uploadFile(stream, { folder, fileType: file.mimetype });
|
|
38
|
-
}
|
|
39
|
-
finally {
|
|
40
|
-
promises_1.default.unlink(file.path);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
UploadVideos(files, folder) {
|
|
44
|
-
return Promise.all(files.map((file) => this.UploadVideo(file, folder)));
|
|
45
|
-
}
|
|
46
|
-
async GenerateUploadUrl(config) {
|
|
47
|
-
const params = { ...getGeneralUploadParams(config), Expires: 60 };
|
|
48
|
-
const url = await s3.getSignedUrlPromise('putObject', params);
|
|
49
|
-
return { url, fileName: params.Key };
|
|
50
|
-
}
|
|
51
|
-
async GenerateUploadUrls(folder, fileTypes) {
|
|
52
|
-
return Promise.all(fileTypes.map((fileType) => this.GenerateUploadUrl({ folder, fileType })));
|
|
53
|
-
}
|
|
54
|
-
async DeleteFile(fileName) {
|
|
55
|
-
await s3.deleteObject({ Bucket: bucketName, Key: fileName }).promise();
|
|
56
|
-
(0, print_helpers_1.printInfo)('file', `file deleted from ${fileName}`);
|
|
57
|
-
}
|
|
58
|
-
DeleteFiles(fileNames) {
|
|
59
|
-
return Promise.all(fileNames.map((fileName) => this.DeleteFile(fileName)));
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
exports.File = File;
|
|
63
|
-
// internal functions
|
|
64
|
-
function getGeneralUploadParams(config) {
|
|
65
|
-
const fileName = `${config.folder}/${(0, random_helpers_1.getRandomString)('short')}`;
|
|
66
|
-
return { Bucket: bucketName, Key: fileName, ContentType: config.fileType, ACL: 'public-read' };
|
|
67
|
-
}
|
|
68
|
-
async function uploadFile(body, config) {
|
|
69
|
-
const params = { ...getGeneralUploadParams(config), Body: body };
|
|
70
|
-
const res = await s3.upload(params).promise();
|
|
71
|
-
(0, print_helpers_1.printInfo)('file', `file uploaded to ${res.Location}`);
|
|
72
|
-
return params.Key;
|
|
73
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.comparePasswords = exports.hashPassword = void 0;
|
|
7
|
-
const bcrypt_1 = __importDefault(require("bcrypt"));
|
|
8
|
-
async function hashPassword(pass) {
|
|
9
|
-
const saltRounds = 10;
|
|
10
|
-
return await bcrypt_1.default.hash(pass, saltRounds);
|
|
11
|
-
}
|
|
12
|
-
exports.hashPassword = hashPassword;
|
|
13
|
-
async function comparePasswords(plainPass, encryptedPass) {
|
|
14
|
-
return await bcrypt_1.default.compare(plainPass, encryptedPass);
|
|
15
|
-
}
|
|
16
|
-
exports.comparePasswords = comparePasswords;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.Password = void 0;
|
|
7
|
-
const bcrypt_1 = __importDefault(require("bcrypt"));
|
|
8
|
-
class Password {
|
|
9
|
-
static Hash(data) {
|
|
10
|
-
return bcrypt_1.default.hash(data, 10);
|
|
11
|
-
}
|
|
12
|
-
static Compare(data, encrypted) {
|
|
13
|
-
return bcrypt_1.default.compare(data, encrypted);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
exports.Password = Password;
|