very-express 0.6.9-alpha → 0.6.11-alpha
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/.github/copilot-instructions.md +0 -1
- package/dist/index.js +103 -93
- package/dist/templates/_middlewares/tsoaAuthentication.ts +3 -3
- package/dist/templates/_routes/oauth/OAuthRouteFactory.ts +1 -1
- package/dist/templates/_services/MongooseRepositoryAdapter.ts +2 -3
- package/dist/templates/_services/TypeOrmRepositoryAdapter.ts +75 -14
- package/dist/templates/_services/VexDbConnector.ts +10 -13
- package/dist/templates/_services/VexSystem.ts +5 -5
- package/dist/templates/_services/auth/JWTService.ts +20 -21
- package/dist/templates/_services/oauth/OAuthProfileMap.ts +6 -6
- package/dist/templates/_services/oauth/OAuthStrategyService.ts +8 -8
- package/dist/templates/_types/{IVexRepository.ts → vex/VexRepository.ts} +10 -6
- package/dist/templates/_types/vex/VexRequest.ts +34 -0
- package/dist/templates/_types/{VexResponseError.ts → vex/VexResErr.ts} +9 -5
- package/dist/templates/_types/{VexResponse.ts → vex/VexResOk.ts} +9 -5
- package/dist/templates/_types/vex/VexResponse.ts +12 -0
- package/dist/templates/_types/vex/index.ts +6 -0
- package/dist/templates/_utils/common.ts +4 -4
- package/dist/templates/_utils/hash.ts +2 -2
- package/dist/templates/jsonSchema/User.json +0 -6
- package/dist/templates/root/public/js/login.js +2 -2
- package/dist/templates/root/public/js/mytokens.js +7 -7
- package/dist/templates/root/public/js/refreshtokens.js +20 -20
- package/dist/templates/root/scripts/oacg-fix.js +83 -0
- package/package.json +3 -4
- package/vex.config.json +1 -1
- package/dist/templates/_types/VexRequest.ts +0 -11
- package/dist/templates/_types/responseObject.ts +0 -7
- package/scripts/bump-version.js +0 -87
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// {{headerComment}}
|
|
2
2
|
import { Request } from "express";
|
|
3
|
-
import
|
|
3
|
+
import { VexResErr } from "../_types/vex";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Called by tsoa-generated routes for every @Security decorator.
|
|
@@ -13,11 +13,11 @@ export async function expressAuthentication(
|
|
|
13
13
|
): Promise<unknown> {
|
|
14
14
|
|
|
15
15
|
if (securityName === "BearerAuth" && !request.headers["authorization"]) {
|
|
16
|
-
throw new
|
|
16
|
+
throw new VexResErr(401, null, "Missing Authorization header");
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
if (securityName === "AuthIndex" && !request.headers["x-auth-index"]) {
|
|
20
|
-
throw new
|
|
20
|
+
throw new VexResErr(401, null, "Missing X-Auth-Index header");
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
return {};
|
|
@@ -72,7 +72,7 @@ export default class OAuthRouteFactory {
|
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
// return appAuthCode to client
|
|
75
|
-
const redirectUrl = await this.JWTService.assignTokens(req.user as User);
|
|
75
|
+
const redirectUrl = await this.JWTService.assignTokens(req.user as User, this.config.strategyName);
|
|
76
76
|
return res.redirect(redirectUrl);
|
|
77
77
|
|
|
78
78
|
}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
// {{headerComment}}
|
|
2
2
|
import { Model, Document } from "mongoose";
|
|
3
|
-
import {
|
|
4
|
-
import { Select, Filter, Join } from "../_types/VexRequest.gen";
|
|
3
|
+
import { VexRepository, Select, Filter, Join } from "../_types/vex";
|
|
5
4
|
import utils from "../_utils";
|
|
6
5
|
|
|
7
|
-
export class MongooseRepositoryAdapter<T extends Document> implements
|
|
6
|
+
export class MongooseRepositoryAdapter<T extends Document> implements VexRepository<T> {
|
|
8
7
|
constructor(private model: Model<T>) {}
|
|
9
8
|
|
|
10
9
|
public get native(): Model<T> {
|
|
@@ -1,36 +1,97 @@
|
|
|
1
1
|
// {{headerComment}}
|
|
2
|
-
import { Repository, ObjectLiteral, FindOptionsWhere, DeepPartial } from "typeorm";
|
|
3
|
-
import {
|
|
4
|
-
import { Select, Filter, Join } from "../_types/VexRequest.gen";
|
|
2
|
+
import { Repository, ObjectLiteral, FindOptionsWhere, DeepPartial, In, Not, Like, MoreThan, LessThan, MoreThanOrEqual, LessThanOrEqual, Raw } from "typeorm";
|
|
3
|
+
import { VexRepository, Select, Filter, Join, FieldOperators } from "../_types/vex";
|
|
5
4
|
import utils from "../_utils";
|
|
6
5
|
|
|
7
|
-
export class TypeOrmRepositoryAdapter<T extends ObjectLiteral> implements
|
|
6
|
+
export class TypeOrmRepositoryAdapter<T extends ObjectLiteral> implements VexRepository<T> {
|
|
8
7
|
constructor(private repo: Repository<T>) {}
|
|
9
8
|
|
|
9
|
+
private mapOperators(filter: Filter<T>): Record<string, unknown> {
|
|
10
|
+
if (!filter || typeof filter !== "object") return {};
|
|
11
|
+
// if (Array.isArray(filter)) return filter.map(f => this.mapOperators(f));
|
|
12
|
+
|
|
13
|
+
const out: Record<string, unknown> = {};
|
|
14
|
+
|
|
15
|
+
for (const [key, val] of Object.entries(filter)) {
|
|
16
|
+
if (key === "$and" && Array.isArray(val)) {
|
|
17
|
+
out["$and"] = val.map(v => this.mapOperators(v));
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
else if (key === "$or" && Array.isArray(val)) {
|
|
21
|
+
out["$or"] = val.map(v => this.mapOperators(v));
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
else if (val && typeof val === "object" && !Array.isArray(val)) {
|
|
25
|
+
// Common mongo-like operators supported in JSON payloads
|
|
26
|
+
const oval = val as FieldOperators;
|
|
27
|
+
|
|
28
|
+
if (oval?.$in && Array.isArray(oval.$in) && oval.$in.every((v: unknown) => typeof v === "string")) {
|
|
29
|
+
out[key] = In(oval.$in);
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (oval?.$nin && Array.isArray(oval.$nin) && oval.$nin.every((v: unknown) => typeof v === "string")) {
|
|
33
|
+
out[key] = Not(In(oval.$nin));
|
|
34
|
+
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if (oval?.$gt) {
|
|
38
|
+
out[key] = MoreThan(oval.$gt);
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (oval?.$gte) {
|
|
42
|
+
out[key] = MoreThanOrEqual(oval.$gte);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (oval?.$lt) {
|
|
46
|
+
out[key] = LessThan(oval.$lt);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (oval?.$lte) {
|
|
50
|
+
out[key] = LessThanOrEqual(oval.$lte);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (oval?.$like) {
|
|
54
|
+
out[key] = Like(oval.$like as string);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
// if (val?.$raw) {
|
|
58
|
+
// out[key] = Raw(val.$raw as string);
|
|
59
|
+
// continue;
|
|
60
|
+
// }
|
|
61
|
+
} else {
|
|
62
|
+
out[key] = val;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
|
|
10
68
|
public get native(): Repository<T> {
|
|
11
69
|
return this.repo;
|
|
12
70
|
}
|
|
13
71
|
|
|
14
|
-
find(filter: Filter
|
|
72
|
+
find(filter: Filter<T>, join?: Join, select?: Select): Promise<T[]> {
|
|
73
|
+
const where = this.mapOperators(filter) as FindOptionsWhere<T>;
|
|
15
74
|
return this.repo.find({
|
|
16
|
-
select,
|
|
17
|
-
where
|
|
75
|
+
select,
|
|
76
|
+
where,
|
|
18
77
|
relations: join
|
|
19
78
|
});
|
|
20
79
|
}
|
|
21
80
|
|
|
22
|
-
findOne(filter: Filter
|
|
81
|
+
findOne(filter: Filter<T>, join?: Join, select?: Select): Promise<T | null> {
|
|
82
|
+
const where = this.mapOperators(filter) as FindOptionsWhere<T>;
|
|
23
83
|
return this.repo.findOne({
|
|
24
84
|
select,
|
|
25
|
-
where
|
|
85
|
+
where,
|
|
26
86
|
relations: join
|
|
27
87
|
});
|
|
28
88
|
}
|
|
29
89
|
|
|
30
|
-
findOneWhere(filter: Filter
|
|
90
|
+
findOneWhere(filter: Filter<T>, join?: Join, select?: Select): Promise<T | null> {
|
|
91
|
+
const where = this.mapOperators(filter) as FindOptionsWhere<T>;
|
|
31
92
|
return this.repo.findOne({
|
|
32
93
|
select,
|
|
33
|
-
where
|
|
94
|
+
where,
|
|
34
95
|
relations: join
|
|
35
96
|
});
|
|
36
97
|
}
|
|
@@ -44,7 +105,7 @@ export class TypeOrmRepositoryAdapter<T extends ObjectLiteral> implements IVexRe
|
|
|
44
105
|
utils.log.error("TypeOrmRepositoryAdapter.replace called without id — refuse replace entity");
|
|
45
106
|
return null;
|
|
46
107
|
}
|
|
47
|
-
const existing = await this.findOne({ _id: id });
|
|
108
|
+
const existing = await this.findOne({ _id: id } as unknown as Filter<T>);
|
|
48
109
|
if (!existing) return null;
|
|
49
110
|
return this.repo.save(this.repo.merge(existing, data as DeepPartial<T>));
|
|
50
111
|
}
|
|
@@ -54,8 +115,8 @@ export class TypeOrmRepositoryAdapter<T extends ObjectLiteral> implements IVexRe
|
|
|
54
115
|
utils.log.error("TypeOrmRepositoryAdapter.update called without id — refuse update entity");
|
|
55
116
|
return null;
|
|
56
117
|
}
|
|
57
|
-
await this.repo.update({ _id: id } as unknown as FindOptionsWhere<T>, data
|
|
58
|
-
return this.findOne({ _id: id });
|
|
118
|
+
await this.repo.update({ _id: id } as unknown as FindOptionsWhere<T>, data);
|
|
119
|
+
return this.findOne({ _id: id } as unknown as Filter<T>);
|
|
59
120
|
}
|
|
60
121
|
|
|
61
122
|
async delete(id: string | undefined): Promise<void> {
|
|
@@ -3,7 +3,7 @@ import { Request, Response, NextFunction } from "express";
|
|
|
3
3
|
import { DataSource, EntityTarget, ObjectLiteral } from "typeorm";
|
|
4
4
|
import mongoose, { Document } from "mongoose";
|
|
5
5
|
import { sqlMigrations } from "../_models/sqlMigration.gen";
|
|
6
|
-
import {
|
|
6
|
+
import { VexRepository } from "../_types/vex";
|
|
7
7
|
import { TypeOrmRepositoryAdapter } from "./TypeOrmRepositoryAdapter.gen";
|
|
8
8
|
import { MongooseRepositoryAdapter } from "./MongooseRepositoryAdapter.gen";
|
|
9
9
|
import utils from "./../_utils";
|
|
@@ -41,18 +41,15 @@ export class VexDbConnector {
|
|
|
41
41
|
|
|
42
42
|
connectSql(): void {
|
|
43
43
|
utils.log.infoSql("Connecting to SQL DB (TypeORM)...");
|
|
44
|
+
const sqlUrl = new URL(this.sqlUrl);
|
|
44
45
|
|
|
45
|
-
const
|
|
46
|
-
let ca: string | undefined;
|
|
47
|
-
if (caRaw) {
|
|
48
|
-
ca = caRaw.startsWith("-----BEGIN") ? caRaw : Buffer.from(caRaw, "base64").toString("utf8");
|
|
49
|
-
}
|
|
46
|
+
const ca = !this.sqlCa ? undefined : this.sqlCa.startsWith("-----BEGIN") ? this.sqlCa : Buffer.from(this.sqlCa, "base64").toString("utf8");
|
|
50
47
|
const insecure = (process.env.SQL_INSECURE_TLS || "").toLowerCase() === "true";
|
|
51
|
-
const ssl =
|
|
52
|
-
?
|
|
53
|
-
|
|
48
|
+
const ssl = {
|
|
49
|
+
rejectUnauthorized: insecure ? false : true,
|
|
50
|
+
ca,
|
|
51
|
+
};
|
|
54
52
|
|
|
55
|
-
const sqlUrl = new URL(this.sqlUrl);
|
|
56
53
|
const ds = new DataSource({
|
|
57
54
|
type: "postgres",
|
|
58
55
|
host: sqlUrl.hostname,
|
|
@@ -131,9 +128,9 @@ export class VexDbConnector {
|
|
|
131
128
|
.catch((err) => utils.log.errorMongo("Error disconnecting Mongoose", err));
|
|
132
129
|
}
|
|
133
130
|
|
|
134
|
-
// getRepository<T extends Document>(target: { schema: unknown; modelName: string }):
|
|
135
|
-
getRepository<T extends ObjectLiteral>(target: EntityTarget<T>):
|
|
136
|
-
getRepository<T>(target: EntityTarget<ObjectLiteral> | any):
|
|
131
|
+
// getRepository<T extends Document>(target: { schema: unknown; modelName: string }): VexRepository<T>;
|
|
132
|
+
getRepository<T extends ObjectLiteral>(target: EntityTarget<T>): VexRepository<T>;
|
|
133
|
+
getRepository<T>(target: EntityTarget<ObjectLiteral> | any): VexRepository<T> {
|
|
137
134
|
// TODO: complete mongoose support, handle join and select
|
|
138
135
|
if (target?.schema && target?.modelName) {
|
|
139
136
|
console.warn("VeryExpress unified db adapter (MongooseRepositoryAdapter) for Mongodb are under development, use with caution. Contribute to \"https://github.com/Justin26l/VeryExpress\"");
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// {{headerComment}}
|
|
2
2
|
import { Request, Response, NextFunction } from "express";
|
|
3
3
|
import utils from "./../_utils";
|
|
4
|
-
import
|
|
5
|
-
|
|
4
|
+
import { VexResErr, VexResOk } from "../_types/vex";
|
|
5
|
+
|
|
6
6
|
|
|
7
7
|
export default class VexSystem {
|
|
8
8
|
|
|
@@ -11,11 +11,11 @@ export default class VexSystem {
|
|
|
11
11
|
* Register AFTER all routes: app.use(VexSystem.responseHandler)
|
|
12
12
|
*/
|
|
13
13
|
static responseHandler(err: unknown, _req: Request, res: Response, _next: NextFunction): Response {
|
|
14
|
-
if (err instanceof
|
|
14
|
+
if (err instanceof VexResOk) {
|
|
15
15
|
return utils.response.send(res, err.status, err.body);
|
|
16
16
|
}
|
|
17
|
-
else if (err instanceof
|
|
18
|
-
const vexErr = err as
|
|
17
|
+
else if (err instanceof VexResErr) {
|
|
18
|
+
const vexErr = err as VexResErr;
|
|
19
19
|
return utils.response.send(res, vexErr.status, {
|
|
20
20
|
code: vexErr.ret_code,
|
|
21
21
|
message: vexErr.message,
|
|
@@ -6,11 +6,10 @@ import path from "path";
|
|
|
6
6
|
|
|
7
7
|
import JWTKeyStore from "./JWTKeyStore.gen";
|
|
8
8
|
|
|
9
|
-
import
|
|
10
|
-
import { IVexRepository } from "../../_types/IVexRepository.gen";
|
|
9
|
+
import { VexRepository, VexResErr } from "../../_types/vex";
|
|
11
10
|
|
|
12
11
|
import VexDb from "../VexDb.gen";
|
|
13
|
-
import { UserEntity,
|
|
12
|
+
import { UserEntity, UserWithRelations } from "../../_models/UserModel.gen";
|
|
14
13
|
import { SessionEntity, Session } from "../../_models/SessionModel.gen";
|
|
15
14
|
|
|
16
15
|
interface tokenObj {
|
|
@@ -22,10 +21,10 @@ interface tokenObj {
|
|
|
22
21
|
export default class JWTService {
|
|
23
22
|
private keyStore = new JWTKeyStore();
|
|
24
23
|
|
|
25
|
-
private get userRepo():
|
|
26
|
-
return VexDb.getRepository<
|
|
24
|
+
private get userRepo(): VexRepository<UserWithRelations> {
|
|
25
|
+
return VexDb.getRepository<UserWithRelations>(UserEntity);
|
|
27
26
|
}
|
|
28
|
-
private get sessionRepo():
|
|
27
|
+
private get sessionRepo(): VexRepository<Session> {
|
|
29
28
|
return VexDb.getRepository<Session>(SessionEntity);
|
|
30
29
|
}
|
|
31
30
|
|
|
@@ -51,10 +50,10 @@ export default class JWTService {
|
|
|
51
50
|
}
|
|
52
51
|
catch (error) {
|
|
53
52
|
if (error instanceof jwt.TokenExpiredError) {
|
|
54
|
-
throw new
|
|
53
|
+
throw new VexResErr(401, null, "Token expired");
|
|
55
54
|
}
|
|
56
55
|
else if (error instanceof jwt.JsonWebTokenError) {
|
|
57
|
-
throw new
|
|
56
|
+
throw new VexResErr(401, null, "jwt malformed");
|
|
58
57
|
}
|
|
59
58
|
else {
|
|
60
59
|
throw error;
|
|
@@ -65,21 +64,21 @@ export default class JWTService {
|
|
|
65
64
|
/**
|
|
66
65
|
* Process of login user, assign tokens and create session.
|
|
67
66
|
*/
|
|
68
|
-
public async assignTokens(user:
|
|
67
|
+
public async assignTokens(user: UserWithRelations, provider: string): Promise<string> {
|
|
69
68
|
if(!user._id) {
|
|
70
|
-
throw new
|
|
69
|
+
throw new VexResErr(500, null, "User Data Error, User._id missing.");
|
|
71
70
|
}
|
|
72
71
|
|
|
73
72
|
const sessionCode = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
74
73
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
74
|
+
// insert new unique session for user
|
|
75
|
+
await this.sessionRepo.deleteWhere({ userId: user._id });
|
|
76
|
+
await this.sessionRepo.create({
|
|
77
|
+
sessionCode,
|
|
78
|
+
userId: user._id,
|
|
79
|
+
provider: provider,
|
|
80
|
+
expired: Date.now() + 5000,
|
|
81
|
+
});
|
|
83
82
|
|
|
84
83
|
// return appAuthCode to client
|
|
85
84
|
const redirectPath = `${path.posix.join("/", process.env.LOGIN_SUCCESS_REDIRECT_PATH || "/logincallback")}?code=${sessionCode}`;
|
|
@@ -87,7 +86,7 @@ export default class JWTService {
|
|
|
87
86
|
return redirectPath;
|
|
88
87
|
}
|
|
89
88
|
|
|
90
|
-
public returnToken(userProfile:
|
|
89
|
+
public returnToken(userProfile: UserWithRelations) {
|
|
91
90
|
const sanitizedProfile = this.sanitizeUser(userProfile);
|
|
92
91
|
const accessToken = this.generateToken(sanitizedProfile, undefined, process.env.ACCESS_TOKEN_EXPIRE_TIME);
|
|
93
92
|
const refreshToken = this.generateToken({ _id: userProfile._id }, 0, process.env.REFRESH_TOKEN_EXPIRE_TIME);
|
|
@@ -103,7 +102,7 @@ export default class JWTService {
|
|
|
103
102
|
|
|
104
103
|
/** utils */
|
|
105
104
|
|
|
106
|
-
public sanitizeUser(user:
|
|
105
|
+
public sanitizeUser(user: UserWithRelations) {
|
|
107
106
|
return {
|
|
108
107
|
_id: user._id,
|
|
109
108
|
email: user.email,
|
|
@@ -138,7 +137,7 @@ export default class JWTService {
|
|
|
138
137
|
};
|
|
139
138
|
}
|
|
140
139
|
|
|
141
|
-
public async generateAccessToken(user:
|
|
140
|
+
public async generateAccessToken(user: UserWithRelations, index?: number): Promise<tokenObj> {
|
|
142
141
|
|
|
143
142
|
const userInfo = this.sanitizeUser(user);
|
|
144
143
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// {{headerComment}}
|
|
2
2
|
import { Profile } from "passport";
|
|
3
|
-
import { User } from "./../../_types/User.gen";
|
|
3
|
+
import { User, UserWithRelations } from "./../../_types/User.gen";
|
|
4
4
|
|
|
5
5
|
export interface IProfile extends Profile {
|
|
6
6
|
[key: string]: any;
|
|
@@ -9,7 +9,7 @@ export interface IProfile extends Profile {
|
|
|
9
9
|
export default class OAuthProfileMap {
|
|
10
10
|
|
|
11
11
|
public map(oauthProfile: Profile) {
|
|
12
|
-
let authProfile:
|
|
12
|
+
let authProfile: UserWithRelations;
|
|
13
13
|
switch (oauthProfile.provider) {
|
|
14
14
|
case "github":
|
|
15
15
|
authProfile = this.GithubProfileMapping(oauthProfile);
|
|
@@ -24,8 +24,8 @@ export default class OAuthProfileMap {
|
|
|
24
24
|
return authProfile;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
private GithubProfileMapping(oauthProfile: IProfile):
|
|
28
|
-
const user:
|
|
27
|
+
private GithubProfileMapping(oauthProfile: IProfile): UserWithRelations {
|
|
28
|
+
const user: UserWithRelations = {
|
|
29
29
|
active: true,
|
|
30
30
|
userAuthProfiles: [{
|
|
31
31
|
provider: oauthProfile.provider,
|
|
@@ -40,8 +40,8 @@ export default class OAuthProfileMap {
|
|
|
40
40
|
|
|
41
41
|
return user;
|
|
42
42
|
}
|
|
43
|
-
private GoogleProfileMapping(oauthProfile: IProfile):
|
|
44
|
-
const user:
|
|
43
|
+
private GoogleProfileMapping(oauthProfile: IProfile): UserWithRelations {
|
|
44
|
+
const user: UserWithRelations = {
|
|
45
45
|
active: true,
|
|
46
46
|
userAuthProfiles: [{
|
|
47
47
|
provider: oauthProfile.provider,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// {{headerComment}}
|
|
2
|
-
import { UserEntity, User } from "./../../_models/UserModel.gen";
|
|
2
|
+
import { UserEntity, User, UserWithRelations } from "./../../_models/UserModel.gen";
|
|
3
3
|
import { UserAuthProfilesEntity, UserAuthProfiles } from "./../../_models/UserAuthProfilesModel.gen";
|
|
4
|
-
import {
|
|
4
|
+
import { VexRepository } from "./../../_types/vex";
|
|
5
5
|
import JWTService from "./../auth/JWTService.gen";
|
|
6
6
|
import OAuthProfileMap, { IProfile } from "./OAuthProfileMap.gen";
|
|
7
7
|
import VexDb from "./../VexDb.gen";
|
|
@@ -9,11 +9,11 @@ import utils from "../../_utils";
|
|
|
9
9
|
|
|
10
10
|
export default class OAuthStrategyService {
|
|
11
11
|
|
|
12
|
-
private get userRepo():
|
|
13
|
-
return VexDb.getRepository<
|
|
12
|
+
private get userRepo(): VexRepository<UserWithRelations> {
|
|
13
|
+
return VexDb.getRepository<UserWithRelations>(UserEntity);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
private get uapRepo():
|
|
16
|
+
private get uapRepo(): VexRepository<UserAuthProfiles> {
|
|
17
17
|
return VexDb.getRepository<UserAuthProfiles>(UserAuthProfilesEntity);
|
|
18
18
|
}
|
|
19
19
|
|
|
@@ -22,12 +22,12 @@ export default class OAuthStrategyService {
|
|
|
22
22
|
public async verify(accessToken: string, refreshToken: string, profile: IProfile, done: (error: any, user?: any) => void): Promise<void> {
|
|
23
23
|
try {
|
|
24
24
|
|
|
25
|
-
let user:
|
|
25
|
+
let user: UserWithRelations;
|
|
26
26
|
const authUser = new OAuthProfileMap().map(profile);
|
|
27
27
|
const authProfile = authUser.userAuthProfiles?.[0];
|
|
28
28
|
|
|
29
29
|
// find user by oauthId + provider, or fall back to email
|
|
30
|
-
let existingUser:
|
|
30
|
+
let existingUser: UserWithRelations | null = null;
|
|
31
31
|
if (authProfile?.oauthId && authProfile?.provider) {
|
|
32
32
|
const matchedProfile = await this.uapRepo.findOneWhere(
|
|
33
33
|
{ oauthId: authProfile.oauthId, provider: authProfile.provider },
|
|
@@ -61,7 +61,7 @@ export default class OAuthStrategyService {
|
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
private async createNewUser(authProfile:
|
|
64
|
+
private async createNewUser(authProfile: UserWithRelations): Promise<UserWithRelations> {
|
|
65
65
|
utils.log.info("OAuthVerify > createNewUser");
|
|
66
66
|
const user = await this.userRepo.create(authProfile);
|
|
67
67
|
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Generated by veryExpress@0.6.5-alpha
|
|
3
|
+
* https://github.com/Justin26l/VeryExpress
|
|
4
|
+
*/
|
|
5
|
+
|
|
2
6
|
|
|
3
7
|
import { Select, Filter, Join } from "./VexRequest.gen";
|
|
4
8
|
|
|
@@ -6,13 +10,13 @@ import { Select, Filter, Join } from "./VexRequest.gen";
|
|
|
6
10
|
* DB-agnostic repository interface.
|
|
7
11
|
* Controllers and services talk only to this — the adapter layer (TypeORM/Mongoose) absorbs DB specifics.
|
|
8
12
|
*/
|
|
9
|
-
export interface
|
|
10
|
-
find(filter: Filter
|
|
11
|
-
findOne(filter: Filter
|
|
12
|
-
findOneWhere(filter: Filter
|
|
13
|
+
export interface VexRepository<T> {
|
|
14
|
+
find(filter: Filter<T>, join?: Join, select?: Select): Promise<T[]>;
|
|
15
|
+
findOne(filter: Filter<T>, join?: Join, select?: Select): Promise<T | null>;
|
|
16
|
+
findOneWhere(filter: Filter<T>, join?: Join, select?: Select): Promise<T | null>;
|
|
13
17
|
create(data: Partial<T>): Promise<T>;
|
|
14
18
|
replace(id: string | undefined, data: Partial<T>): Promise<T | null>;
|
|
15
19
|
update(id: string | undefined, data: Partial<T>): Promise<T | null>;
|
|
16
20
|
delete(id: string | undefined): Promise<void>;
|
|
17
|
-
deleteWhere(filter: Filter): Promise<void>;
|
|
21
|
+
deleteWhere(filter: Filter<T>): Promise<void>;
|
|
18
22
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generated by veryExpress@0.6.3-alpha
|
|
3
|
+
* https://github.com/Justin26l/VeryExpress
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export type FieldOperators = {
|
|
7
|
+
$in?: string[] | number[];
|
|
8
|
+
$nin?: string[] | number[];
|
|
9
|
+
$gt?: number | string;
|
|
10
|
+
$gte?: number | string;
|
|
11
|
+
$lt?: number | string;
|
|
12
|
+
$lte?: number | string;
|
|
13
|
+
$like?: string;
|
|
14
|
+
$raw?: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type FieldFilter<V> = V | FieldOperators;
|
|
18
|
+
|
|
19
|
+
export type Select = string[];
|
|
20
|
+
|
|
21
|
+
export type Filter<T = any> = {
|
|
22
|
+
[K in keyof T]?: FieldFilter<T[K]>;
|
|
23
|
+
} & {
|
|
24
|
+
$and?: Filter<T>[];
|
|
25
|
+
$or?: Filter<T>[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type Join = string[];
|
|
29
|
+
|
|
30
|
+
export interface VexRequest<T = any> {
|
|
31
|
+
select?: Select;
|
|
32
|
+
filter?: Filter<T>;
|
|
33
|
+
join?: Join;
|
|
34
|
+
};
|
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Generated by veryExpress@0.6.6-alpha
|
|
3
|
+
* https://github.com/Justin26l/VeryExpress
|
|
4
|
+
*/
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
import { ResponseCode } from "./../../_utils/response.gen";
|
|
7
|
+
import utils from "./../../_utils/index";
|
|
8
|
+
|
|
9
|
+
export class VexResErr extends Error {
|
|
6
10
|
public status: number;
|
|
7
11
|
public ret_code: ResponseCode | string;
|
|
8
12
|
public ret_msg: any;
|
|
@@ -13,7 +17,7 @@ export default class VexResponseError extends Error {
|
|
|
13
17
|
message?: string,
|
|
14
18
|
) {
|
|
15
19
|
super(message);
|
|
16
|
-
this.name = "
|
|
20
|
+
this.name = "VexResErr";
|
|
17
21
|
this.status = status;
|
|
18
22
|
this.ret_code = code || utils.response.statusCodeMap.get(status) || utils.response.code.SERVER_ERROR;
|
|
19
23
|
this.ret_msg = message || utils.response.msg.SERVER_ERROR;
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Generated by veryExpress@0.6.3-alpha
|
|
3
|
+
* https://github.com/Justin26l/VeryExpress
|
|
4
|
+
*/
|
|
3
5
|
|
|
4
|
-
|
|
6
|
+
import { ResponseCode } from "./../../_utils/response.gen";
|
|
7
|
+
|
|
8
|
+
export class VexResOk<T = unknown> extends Error {
|
|
5
9
|
public status: number;
|
|
6
10
|
public body?: {
|
|
7
11
|
code?: ResponseCode;
|
|
@@ -17,8 +21,8 @@ export default class VexResponse<T = unknown> extends Error {
|
|
|
17
21
|
result?: T;
|
|
18
22
|
}
|
|
19
23
|
) {
|
|
20
|
-
super("
|
|
21
|
-
this.name = "
|
|
24
|
+
super("VexResOk");
|
|
25
|
+
this.name = "VexResOk";
|
|
22
26
|
this.status = status;
|
|
23
27
|
this.body = body;
|
|
24
28
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generated by veryExpress@0.6.3-alpha
|
|
3
|
+
* https://github.com/Justin26l/VeryExpress
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { ResponseCode } from "./../../_utils/response.gen";
|
|
7
|
+
|
|
8
|
+
export interface VexResponse<T = unknown>{
|
|
9
|
+
code?: ResponseCode;
|
|
10
|
+
message?: string | null;
|
|
11
|
+
result?: T;
|
|
12
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import jsYaml from "js-yaml";
|
|
3
3
|
import fs from "fs";
|
|
4
4
|
import { Request } from "express";
|
|
5
|
-
import
|
|
5
|
+
import { VexResErr } from "../_types/vex";
|
|
6
6
|
import response from "./response.gen";
|
|
7
7
|
|
|
8
8
|
export function loadYaml(yamlFilePath: string) {
|
|
@@ -34,7 +34,7 @@ export function parseFieldsSelect(req: Request) : string[] | undefined {
|
|
|
34
34
|
_selectRaw = req.body._select;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
const ErrorDataType = new
|
|
37
|
+
const ErrorDataType = new VexResErr(400, response.code.err_validation, "Invalid field \"_select\", only json array of string accepted");
|
|
38
38
|
if (!Array.isArray(_selectRaw)) {
|
|
39
39
|
throw ErrorDataType;
|
|
40
40
|
}
|
|
@@ -67,7 +67,7 @@ export function parseCollectionJoin(req: Request, availablePopulateOptions?: {[k
|
|
|
67
67
|
_joinRaw = req.body._join;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
const ErrorDataType = new
|
|
70
|
+
const ErrorDataType = new VexResErr(400, response.code.err_validation, "Invalid field \"_join\", only json array of string accepted");
|
|
71
71
|
if (!Array.isArray(_joinRaw)) {
|
|
72
72
|
throw ErrorDataType;
|
|
73
73
|
}
|
|
@@ -119,7 +119,7 @@ export function parseRelations(req: Request, allowedRelations?: string[]): strin
|
|
|
119
119
|
_joinRaw = req.body._join;
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
-
const err = new
|
|
122
|
+
const err = new VexResErr(400, response.code.err_validation, "Invalid field \"_join\", only json array of relation names accepted");
|
|
123
123
|
if (!Array.isArray(_joinRaw)) throw err;
|
|
124
124
|
for (const item of _joinRaw) {
|
|
125
125
|
if (typeof item !== "string") throw err;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// {{headerComment}}
|
|
2
2
|
import crypto from "crypto";
|
|
3
3
|
// import log from "~/utils/logger";
|
|
4
|
-
import {
|
|
4
|
+
import { UserWithRelations } from "../_types/User.gen";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Hashes a password with a salt (e.g., user email).
|
|
@@ -25,7 +25,7 @@ export function hashPassword(
|
|
|
25
25
|
* @returns True if the password matches, false otherwise.
|
|
26
26
|
*/
|
|
27
27
|
export function verifyPassword(
|
|
28
|
-
user:
|
|
28
|
+
user: UserWithRelations,
|
|
29
29
|
plainPassword: string,
|
|
30
30
|
): boolean {
|
|
31
31
|
if(!user.email || !user.userAuthProfiles) {
|