very-express 0.6.3-alpha → 0.6.5-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/dist/index.js +134 -112
- package/dist/templates/_services/MongooseRepositoryAdapter.ts +16 -3
- package/dist/templates/_services/TypeOrmRepositoryAdapter.ts +16 -3
- package/dist/templates/_services/auth/JWTService.ts +9 -37
- package/dist/templates/_services/oauth/OAuthStrategyService.ts +13 -15
- package/dist/templates/_types/IVexRepository.ts +3 -3
- package/dist/templates/jsonSchemaRBAC/UserRole.json +42 -0
- package/package.json +1 -1
- package/vex.config.json +1 -1
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { Model, Document } from "mongoose";
|
|
3
3
|
import { IVexRepository } from "../_types/IVexRepository.gen";
|
|
4
4
|
import { Select, Filter, Join } from "../_types/VexRequest.gen";
|
|
5
|
+
import utils from "../_utils";
|
|
5
6
|
|
|
6
7
|
export class MongooseRepositoryAdapter<T extends Document> implements IVexRepository<T> {
|
|
7
8
|
constructor(private model: Model<T>) {}
|
|
@@ -30,15 +31,27 @@ export class MongooseRepositoryAdapter<T extends Document> implements IVexReposi
|
|
|
30
31
|
return doc.save();
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
replace(id: string |
|
|
34
|
+
replace(id: string | undefined, data: Partial<T>): Promise<T | null> {
|
|
35
|
+
if(!id) {
|
|
36
|
+
utils.log.error("MongooseRepositoryAdapter.replace called without id — refuse replace document");
|
|
37
|
+
return Promise.resolve(null);
|
|
38
|
+
}
|
|
34
39
|
return this.model.findByIdAndUpdate(id, data, { new: true, overwrite: true }).exec();
|
|
35
40
|
}
|
|
36
41
|
|
|
37
|
-
update(id: string |
|
|
42
|
+
update(id: string | undefined, data: Partial<T>): Promise<T | null> {
|
|
43
|
+
if(!id) {
|
|
44
|
+
utils.log.error("MongooseRepositoryAdapter.update called without id — refuse update document");
|
|
45
|
+
return Promise.resolve(null);
|
|
46
|
+
}
|
|
38
47
|
return this.model.findByIdAndUpdate(id, { $set: data }, { new: true }).exec();
|
|
39
48
|
}
|
|
40
49
|
|
|
41
|
-
async delete(id: string |
|
|
50
|
+
async delete(id: string | undefined): Promise<void> {
|
|
51
|
+
if(!id) {
|
|
52
|
+
utils.log.error("MongooseRepositoryAdapter.delete called without id — refuse delete document");
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
42
55
|
await this.model.findByIdAndDelete(id).exec();
|
|
43
56
|
}
|
|
44
57
|
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { Repository, ObjectLiteral, FindOptionsWhere, DeepPartial } from "typeorm";
|
|
3
3
|
import { IVexRepository } from "../_types/IVexRepository.gen";
|
|
4
4
|
import { Select, Filter, Join } from "../_types/VexRequest.gen";
|
|
5
|
+
import utils from "../_utils";
|
|
5
6
|
|
|
6
7
|
export class TypeOrmRepositoryAdapter<T extends ObjectLiteral> implements IVexRepository<T> {
|
|
7
8
|
constructor(private repo: Repository<T>) {}
|
|
@@ -38,18 +39,30 @@ export class TypeOrmRepositoryAdapter<T extends ObjectLiteral> implements IVexRe
|
|
|
38
39
|
return this.repo.save(this.repo.create(data as DeepPartial<T>));
|
|
39
40
|
}
|
|
40
41
|
|
|
41
|
-
async replace(id: string |
|
|
42
|
+
async replace(id: string | undefined, data: Partial<T>): Promise<T | null> {
|
|
43
|
+
if (!id) {
|
|
44
|
+
utils.log.error("TypeOrmRepositoryAdapter.replace called without id — refuse replace entity");
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
42
47
|
const existing = await this.findOne({ _id: id });
|
|
43
48
|
if (!existing) return null;
|
|
44
49
|
return this.repo.save(this.repo.merge(existing, data as DeepPartial<T>));
|
|
45
50
|
}
|
|
46
51
|
|
|
47
|
-
async update(id: string |
|
|
52
|
+
async update(id: string | undefined, data: Partial<T>): Promise<T | null> {
|
|
53
|
+
if (!id) {
|
|
54
|
+
utils.log.error("TypeOrmRepositoryAdapter.update called without id — refuse update entity");
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
48
57
|
await this.repo.update({ _id: id } as unknown as FindOptionsWhere<T>, data as any);
|
|
49
58
|
return this.findOne({ _id: id });
|
|
50
59
|
}
|
|
51
60
|
|
|
52
|
-
async delete(id: string |
|
|
61
|
+
async delete(id: string | undefined): Promise<void> {
|
|
62
|
+
if (!id) {
|
|
63
|
+
utils.log.error("TypeOrmRepositoryAdapter.delete called without id — refuse delete entity");
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
53
66
|
await this.repo.delete({ _id: id } as unknown as FindOptionsWhere<T>);
|
|
54
67
|
}
|
|
55
68
|
|
|
@@ -10,9 +10,8 @@ import VexResponseError from "../../_types/VexResponseError.gen";
|
|
|
10
10
|
import { IVexRepository } from "../../_types/IVexRepository.gen";
|
|
11
11
|
|
|
12
12
|
import VexDb from "../VexDb.gen";
|
|
13
|
-
import { UserEntity } from "../../_models/UserModel.gen";
|
|
14
|
-
import { SessionEntity } from "../../_models/SessionModel.gen";
|
|
15
|
-
import { User } from "../../_types/User.gen";
|
|
13
|
+
import { UserEntity, User } from "../../_models/UserModel.gen";
|
|
14
|
+
import { SessionEntity, Session } from "../../_models/SessionModel.gen";
|
|
16
15
|
|
|
17
16
|
interface tokenObj {
|
|
18
17
|
token: string,
|
|
@@ -23,11 +22,11 @@ interface tokenObj {
|
|
|
23
22
|
export default class JWTService {
|
|
24
23
|
private keyStore = new JWTKeyStore();
|
|
25
24
|
|
|
26
|
-
private get userRepo(): IVexRepository<
|
|
27
|
-
return VexDb.getRepository(UserEntity);
|
|
25
|
+
private get userRepo(): IVexRepository<User> {
|
|
26
|
+
return VexDb.getRepository<User>(UserEntity);
|
|
28
27
|
}
|
|
29
|
-
private get sessionRepo(): IVexRepository<
|
|
30
|
-
return VexDb.getRepository(SessionEntity);
|
|
28
|
+
private get sessionRepo(): IVexRepository<Session> {
|
|
29
|
+
return VexDb.getRepository<Session>(SessionEntity);
|
|
31
30
|
}
|
|
32
31
|
|
|
33
32
|
constructor() {}
|
|
@@ -71,29 +70,6 @@ export default class JWTService {
|
|
|
71
70
|
throw new VexResponseError(500, null, "User Data Error, User._id missing.");
|
|
72
71
|
}
|
|
73
72
|
|
|
74
|
-
// const accessToken = await this.generateAccessToken(user._id);
|
|
75
|
-
// const refreshToken = await this.generateRefreshToken({ _id: user._id });
|
|
76
|
-
|
|
77
|
-
// const accessTokenOptions: CookieOptions = {
|
|
78
|
-
// maxAge: ms(process.env.ACCESS_TOKEN_EXPIRE_TIME as ms.StringValue),
|
|
79
|
-
// httpOnly: true,
|
|
80
|
-
// secure: true,
|
|
81
|
-
// sameSite: "strict",
|
|
82
|
-
// };
|
|
83
|
-
|
|
84
|
-
// res.cookie("authorization", accessToken.token, accessTokenOptions);
|
|
85
|
-
// res.cookie("x-auth-index", accessToken.clientIndex, accessTokenOptions);
|
|
86
|
-
|
|
87
|
-
// res.cookie("vex-refresh-token", {
|
|
88
|
-
// refreshToken: refreshToken.token,
|
|
89
|
-
// refreshTokenIndex: refreshToken.clientIndex,
|
|
90
|
-
// }, {
|
|
91
|
-
// maxAge: ms(process.env.REFRESH_TOKEN_EXPIRE_TIME as ms.StringValue),
|
|
92
|
-
// httpOnly: true,
|
|
93
|
-
// secure: true,
|
|
94
|
-
// sameSite: "strict",
|
|
95
|
-
// });
|
|
96
|
-
|
|
97
73
|
const sessionCode = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
98
74
|
|
|
99
75
|
if (this.sessionRepo) {
|
|
@@ -133,7 +109,7 @@ export default class JWTService {
|
|
|
133
109
|
email: user.email,
|
|
134
110
|
name: user.name,
|
|
135
111
|
locale: user.locale,
|
|
136
|
-
roles: user.
|
|
112
|
+
roles: user.userRole?.map((r: any) => r.role) || [],
|
|
137
113
|
profileErrors: user.profileErrors,
|
|
138
114
|
active: user.active
|
|
139
115
|
};
|
|
@@ -162,13 +138,9 @@ export default class JWTService {
|
|
|
162
138
|
};
|
|
163
139
|
}
|
|
164
140
|
|
|
165
|
-
public async generateAccessToken(
|
|
141
|
+
public async generateAccessToken(user: User, index?: number): Promise<tokenObj> {
|
|
166
142
|
|
|
167
|
-
const
|
|
168
|
-
if (!userDoc) {
|
|
169
|
-
throw new VexResponseError(404, null, "Invalid User Id");
|
|
170
|
-
}
|
|
171
|
-
const userInfo = this.sanitizeUser(userDoc);
|
|
143
|
+
const userInfo = this.sanitizeUser(user);
|
|
172
144
|
|
|
173
145
|
return this.generateToken(userInfo, index, process.env.ACCESS_TOKEN_EXPIRE_TIME);
|
|
174
146
|
}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
// {{headerComment}}
|
|
2
|
-
import { UserEntity } from "./../../_models/UserModel.gen";
|
|
3
|
-
import { UserAuthProfilesEntity } from "./../../_models/UserAuthProfilesModel.gen";
|
|
4
|
-
import { User } from "./../../_types/User.gen";
|
|
5
|
-
import { UserAuthProfiles } from "./../../_types/UserAuthProfiles.gen";
|
|
2
|
+
import { UserEntity, User } from "./../../_models/UserModel.gen";
|
|
3
|
+
import { UserAuthProfilesEntity, UserAuthProfiles } from "./../../_models/UserAuthProfilesModel.gen";
|
|
6
4
|
import { IVexRepository } from "./../../_types/IVexRepository.gen";
|
|
7
5
|
import JWTService from "./../auth/JWTService.gen";
|
|
8
6
|
import OAuthProfileMap, { IProfile } from "./OAuthProfileMap.gen";
|
|
@@ -11,12 +9,12 @@ import utils from "../../_utils";
|
|
|
11
9
|
|
|
12
10
|
export default class OAuthStrategyService {
|
|
13
11
|
|
|
14
|
-
private get userRepo(): IVexRepository<
|
|
15
|
-
return VexDb.getRepository(UserEntity);
|
|
12
|
+
private get userRepo(): IVexRepository<User> {
|
|
13
|
+
return VexDb.getRepository<User>(UserEntity);
|
|
16
14
|
}
|
|
17
15
|
|
|
18
|
-
private get uapRepo(): IVexRepository<
|
|
19
|
-
return VexDb.getRepository(UserAuthProfilesEntity);
|
|
16
|
+
private get uapRepo(): IVexRepository<UserAuthProfiles> {
|
|
17
|
+
return VexDb.getRepository<UserAuthProfiles>(UserAuthProfilesEntity);
|
|
20
18
|
}
|
|
21
19
|
|
|
22
20
|
constructor() {}
|
|
@@ -24,12 +22,12 @@ export default class OAuthStrategyService {
|
|
|
24
22
|
public async verify(accessToken: string, refreshToken: string, profile: IProfile, done: (error: any, user?: any) => void): Promise<void> {
|
|
25
23
|
try {
|
|
26
24
|
|
|
27
|
-
let user:
|
|
25
|
+
let user: User;
|
|
28
26
|
const authUser = new OAuthProfileMap().map(profile);
|
|
29
27
|
const authProfile = authUser.userAuthProfiles?.[0];
|
|
30
28
|
|
|
31
29
|
// find user by oauthId + provider, or fall back to email
|
|
32
|
-
let existingUser:
|
|
30
|
+
let existingUser: User | null = null;
|
|
33
31
|
if (authProfile?.oauthId && authProfile?.provider) {
|
|
34
32
|
const matchedProfile = await this.uapRepo.findOneWhere(
|
|
35
33
|
{ oauthId: authProfile.oauthId, provider: authProfile.provider },
|
|
@@ -63,22 +61,22 @@ export default class OAuthStrategyService {
|
|
|
63
61
|
}
|
|
64
62
|
}
|
|
65
63
|
|
|
66
|
-
private async createNewUser(authProfile: User): Promise<
|
|
64
|
+
private async createNewUser(authProfile: User): Promise<User> {
|
|
67
65
|
utils.log.info("OAuthVerify > createNewUser");
|
|
68
|
-
const user = await this.userRepo.create(authProfile
|
|
66
|
+
const user = await this.userRepo.create(authProfile);
|
|
69
67
|
|
|
70
68
|
// create auth profile row linked to the new user
|
|
71
69
|
if (authProfile.userAuthProfiles?.[0]) {
|
|
72
70
|
await this.uapRepo.create({
|
|
73
71
|
...authProfile.userAuthProfiles[0],
|
|
74
72
|
userId: user._id,
|
|
75
|
-
} as Partial<
|
|
73
|
+
} as Partial<UserAuthProfiles>);
|
|
76
74
|
}
|
|
77
75
|
|
|
78
76
|
return user;
|
|
79
77
|
}
|
|
80
78
|
|
|
81
|
-
private async processExistingUser(incomingProfile: UserAuthProfiles | undefined, existingUser:
|
|
79
|
+
private async processExistingUser(incomingProfile: UserAuthProfiles | undefined, existingUser: User): Promise<User> {
|
|
82
80
|
utils.log.info("OAuthVerify > processExistingUser");
|
|
83
81
|
|
|
84
82
|
if (!incomingProfile) {
|
|
@@ -97,7 +95,7 @@ export default class OAuthStrategyService {
|
|
|
97
95
|
await this.uapRepo.create({
|
|
98
96
|
...incomingProfile,
|
|
99
97
|
userId: existingUser._id,
|
|
100
|
-
} as Partial<
|
|
98
|
+
} as Partial<UserAuthProfiles>);
|
|
101
99
|
}
|
|
102
100
|
else if (existing.username !== incomingProfile.username) {
|
|
103
101
|
// B.3. update username if changed
|
|
@@ -11,8 +11,8 @@ export interface IVexRepository<T> {
|
|
|
11
11
|
findOne(filter: Filter, join?: Join, select?: Select): Promise<T | null>;
|
|
12
12
|
findOneWhere(filter: Filter, join?: Join, select?: Select): Promise<T | null>;
|
|
13
13
|
create(data: Partial<T>): Promise<T>;
|
|
14
|
-
replace(id: string |
|
|
15
|
-
update(id: string |
|
|
16
|
-
delete(id: string |
|
|
14
|
+
replace(id: string | undefined, data: Partial<T>): Promise<T | null>;
|
|
15
|
+
update(id: string | undefined, data: Partial<T>): Promise<T | null>;
|
|
16
|
+
delete(id: string | undefined): Promise<void>;
|
|
17
17
|
deleteWhere(filter: Filter): Promise<void>;
|
|
18
18
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "object",
|
|
3
|
+
"x-documentConfig": {
|
|
4
|
+
"documentName": "UserRole",
|
|
5
|
+
"methods": [
|
|
6
|
+
"get",
|
|
7
|
+
"getList",
|
|
8
|
+
"post",
|
|
9
|
+
"delete"
|
|
10
|
+
]
|
|
11
|
+
},
|
|
12
|
+
"properties": {
|
|
13
|
+
"_id": {
|
|
14
|
+
"type": "string",
|
|
15
|
+
"x-format": "PrimaryUUID"
|
|
16
|
+
},
|
|
17
|
+
"userId": {
|
|
18
|
+
"type": "string",
|
|
19
|
+
"index": true,
|
|
20
|
+
"x-foreignKey": {
|
|
21
|
+
"schemaName": "User",
|
|
22
|
+
"fieldName": "_id",
|
|
23
|
+
"relationType": "one-to-many"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"role": {
|
|
27
|
+
"type": "string",
|
|
28
|
+
"x-vexData": "role",
|
|
29
|
+
"index": true,
|
|
30
|
+
"enum": [
|
|
31
|
+
"visitor",
|
|
32
|
+
"member",
|
|
33
|
+
"admin"
|
|
34
|
+
],
|
|
35
|
+
"x-format": "enum"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"required": [
|
|
39
|
+
"userId",
|
|
40
|
+
"role"
|
|
41
|
+
]
|
|
42
|
+
}
|
package/package.json
CHANGED