@flink-app/generic-auth-plugin 0.3.0 → 0.3.3

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.
@@ -1,68 +1,56 @@
1
- import {
2
- badRequest,
3
- conflict,
4
- FlinkContext,
5
- Handler,
6
- internalServerError,
7
- } from "@flink-app/flink";
1
+ import { badRequest, conflict, FlinkContext, Handler, internalServerError } from "@flink-app/flink";
8
2
  import { JwtAuthPlugin } from "@flink-app/jwt-auth-plugin";
9
3
  import { genericAuthContext } from "../genericAuthContext";
10
4
  import { UserCreateReq } from "../schemas/UserCreateReq";
11
5
  import { UserCreateRes } from "../schemas/UserCreateRes";
12
6
 
13
- const userCreateHandler: Handler<
14
- FlinkContext<genericAuthContext>,
15
- UserCreateReq,
16
- UserCreateRes
17
- > = async ({ ctx, req, origin }) => {
18
- let { password, username, authentificationMethod, profile } = req.body;
19
- if (authentificationMethod == null) {
20
- authentificationMethod = "password";
21
- }
22
- if (authentificationMethod == "password" && password == null) {
23
- return internalServerError(
24
- "When using password as authentification method password must be supplied"
25
- );
26
- }
27
- if (password == null) {
28
- password = "";
29
- }
30
- let roles: string[] = [];
31
- if (profile == null) profile = {};
7
+ const userCreateHandler: Handler<FlinkContext<genericAuthContext>, UserCreateReq, UserCreateRes> = async ({ ctx, req, origin }) => {
8
+ let { password, username, authentificationMethod, profile } = req.body;
9
+ if (authentificationMethod == null) {
10
+ authentificationMethod = "password";
11
+ }
12
+ if (authentificationMethod == "password" && password == null) {
13
+ return internalServerError("When using password as authentification method password must be supplied");
14
+ }
15
+ if (password == null) {
16
+ password = "";
17
+ }
18
+ let roles: string[] = [];
19
+ if (profile == null) profile = {};
32
20
 
33
- let pluginName = origin || "genericAuthPlugin";
34
- let repo = ctx.repos[(<any>ctx.plugins)[pluginName].repoName];
21
+ let pluginName = origin || "genericAuthPlugin";
22
+ let repo = ctx.repos[(<any>ctx.plugins)[pluginName].repoName];
35
23
 
36
- var re = <RegExp>(<any>ctx.plugins)[pluginName].usernameFormat;
24
+ var re = <RegExp>(<any>ctx.plugins)[pluginName].usernameFormat;
37
25
 
38
- if (!re.test(username)) {
39
- return badRequest("Username does not meet requirements", "usernameError");
40
- }
26
+ if (!re.test(username)) {
27
+ return badRequest("Username does not meet requirements", "usernameError");
28
+ }
41
29
 
42
- const createUserResponse = await ctx.plugins.genericAuthPlugin.createUser(
43
- repo,
44
- <JwtAuthPlugin>ctx.auth,
45
- username,
46
- password,
47
- authentificationMethod,
48
- roles,
49
- profile,
50
- ctx.plugins.genericAuthPlugin.createPasswordHashAndSaltMethod
51
- );
52
- if (createUserResponse.status != "success") {
53
- switch (createUserResponse.status) {
54
- case "error":
55
- return internalServerError("Unknown error", createUserResponse.status);
56
- case "passwordError":
57
- return badRequest("Invalid password", createUserResponse.status);
58
- case "userExists":
59
- return conflict("User already exists", createUserResponse.status);
30
+ const createUserResponse = await ctx.plugins.genericAuthPlugin.createUser(
31
+ repo,
32
+ <JwtAuthPlugin>ctx.auth,
33
+ username,
34
+ password,
35
+ authentificationMethod,
36
+ roles,
37
+ profile,
38
+ ctx.plugins.genericAuthPlugin.createPasswordHashAndSaltMethod
39
+ );
40
+ if (createUserResponse.status != "success") {
41
+ switch (createUserResponse.status) {
42
+ case "error":
43
+ return internalServerError("Unknown error", createUserResponse.status);
44
+ case "passwordError":
45
+ return badRequest("Invalid password", createUserResponse.status);
46
+ case "userExists":
47
+ return conflict("User already exists", createUserResponse.status);
48
+ }
60
49
  }
61
- }
62
- return {
63
- data: createUserResponse,
64
- status: 200,
65
- };
50
+ return {
51
+ data: createUserResponse,
52
+ status: 200,
53
+ };
66
54
  };
67
55
 
68
56
  export default userCreateHandler;
@@ -16,8 +16,14 @@ const putUserProfileHandler: Handler<
16
16
  return notFound();
17
17
  }
18
18
 
19
- user.profile = req.body;
20
- await repo.updateOne(userId, { profile: req.body });
19
+
20
+ const updatedProfile = {
21
+ ...user.profile,
22
+ ...req.body,
23
+ }
24
+
25
+
26
+ await repo.updateOne(userId, { profile: updatedProfile });
21
27
 
22
28
  user = await repo.getById(userId);
23
29
 
package/src/management.ts CHANGED
@@ -1,7 +1,4 @@
1
- import {
2
- ManagementApiModule,
3
- ManagementApiType,
4
- } from "@flink-app/management-api-plugin";
1
+ import { ManagementApiModule, ManagementApiType } from "@flink-app/management-api-plugin";
5
2
  import { HttpMethod } from "@flink-app/flink";
6
3
  import * as userCreateHandler from "./handlers/UserCreate";
7
4
  import * as GetManagementUser from "./handlers/Management/GetUser";
@@ -11,145 +8,164 @@ import * as PutManagementUserProfileByUserid from "./handlers/Management/PutUser
11
8
  import * as PutManagementUserUsernameByUserid from "./handlers/Management/PutUserUsernameByUserid";
12
9
  import * as PutManagementUserRolesByUserid from "./handlers/Management/PutUserRolesByUserid";
13
10
  import * as DeleteManagementUserByUserid from "./handlers/Management/DeleteUserByUserid";
11
+ import * as GetUserViewByUserid from "./handlers/Management/GetUserViewByUserid";
14
12
 
15
13
  import * as PutUserProfileByUseridAppend from "./handlers/Management/PutUserProfileByUseridAppend";
16
14
  import * as GetSchema from "./handlers/Management/GetSchema";
15
+ import { User } from ".";
16
+ import { GetManagementUserViewByUseridRes } from "./schemas/Management/GetUserViewByUseridRes";
17
17
 
18
18
  export interface GetManagementModuleConfig {
19
- pluginId?: string;
20
- profileSchema?: any;
21
- ui: boolean;
22
- uiSettings?: {
23
- title: string;
24
- enableUserDelete?: boolean;
25
- enableUserCreate?: boolean;
26
- enableUserEdit?: boolean;
27
- };
19
+ pluginId?: string;
20
+ profileSchema?: any;
21
+ ui: boolean;
22
+ userView?: {
23
+ getData(user: User): GetManagementUserViewByUseridRes;
24
+ };
25
+ uiSettings?: {
26
+ title: string;
27
+ enableUserDelete?: boolean;
28
+ enableUserCreate?: boolean;
29
+ enableUserEdit?: boolean;
30
+ enableUserView?: boolean;
31
+ };
28
32
  }
29
33
 
30
- export const GetManagementModule = (
31
- config: GetManagementModuleConfig
32
- ): ManagementApiModule => {
33
- if (config.pluginId == null) config.pluginId = "genericAuthPlugin";
34
-
35
- let endpoints: ManagementApiModule["endpoints"] = [];
36
- endpoints.push({
37
- routeProps: {
38
- path: "",
39
- method: HttpMethod.post,
40
- origin: config.pluginId,
41
- },
42
- handler: userCreateHandler,
43
- });
44
-
45
- endpoints.push({
46
- routeProps: {
47
- path: "",
48
- method: HttpMethod.get,
49
- origin: config.pluginId,
50
- },
51
- handler: GetManagementUser,
52
- });
53
-
54
- endpoints.push({
55
- routeProps: {
56
- path: "/profile/schema",
57
- method: HttpMethod.get,
58
- origin: config.pluginId || "genericAuthPlugin",
59
- },
60
- handler: GetSchema,
61
- });
62
-
63
- endpoints.push({
64
- routeProps: {
65
- path: "/:userid",
66
- method: HttpMethod.get,
67
- origin: config.pluginId,
68
- },
69
-
70
- handler: GetManagementUserByUserid,
71
- });
72
-
73
- endpoints.push({
74
- routeProps: {
75
- path: "/:userid",
76
- method: HttpMethod.delete,
77
- origin: config.pluginId,
78
- },
79
- handler: DeleteManagementUserByUserid,
80
- });
81
-
82
- endpoints.push({
83
- routeProps: {
84
- path: "/password/:userid",
85
- method: HttpMethod.put,
86
- origin: config.pluginId,
87
- },
88
- handler: PutManagementUserPasswordByUserid,
89
- });
90
-
91
- endpoints.push({
92
- routeProps: {
93
- path: "/username/:userid",
94
- method: HttpMethod.put,
95
- origin: config.pluginId,
96
- },
97
- handler: PutManagementUserUsernameByUserid,
98
- });
99
-
100
- endpoints.push({
101
- routeProps: {
102
- path: "/profile/:userid",
103
- method: HttpMethod.put,
104
- origin: config.pluginId,
105
- },
106
- handler: PutManagementUserProfileByUserid,
107
- });
108
-
109
- endpoints.push({
110
- routeProps: {
111
- path: "/profile/:userid/append",
112
- method: HttpMethod.put,
113
- origin: config.pluginId,
114
- },
115
- handler: PutUserProfileByUseridAppend,
116
- });
117
-
118
- endpoints.push({
119
- routeProps: {
120
- path: "/roles/:userid",
121
- method: HttpMethod.put,
122
- origin: config.pluginId,
123
- },
124
- handler: PutManagementUserRolesByUserid,
125
- });
126
-
127
- let features: string[] = [];
128
-
129
- if (config.uiSettings?.enableUserDelete == true) {
130
- features.push("delete");
131
- }
132
- if (config.uiSettings?.enableUserCreate == true) {
133
- features.push("create");
134
- }
135
- if (config.uiSettings?.enableUserEdit == true) {
136
- features.push("edit");
137
- }
138
-
139
- let module: ManagementApiModule = {
140
- id: config.pluginId || "user",
141
- uiSettings: {
142
- title: config.uiSettings?.title || "Users",
143
- icon: "",
144
- features,
145
- },
146
- ui: config.ui,
147
- type: ManagementApiType.user,
148
- endpoints: endpoints,
149
- data : {
150
- profileSchema : config.profileSchema
34
+ export const GetManagementModule = (config: GetManagementModuleConfig): ManagementApiModule => {
35
+ if (config.pluginId == null) config.pluginId = "genericAuthPlugin";
36
+
37
+ let endpoints: ManagementApiModule["endpoints"] = [];
38
+ endpoints.push({
39
+ routeProps: {
40
+ path: "",
41
+ method: HttpMethod.post,
42
+ origin: config.pluginId,
43
+ },
44
+ handler: userCreateHandler,
45
+ });
46
+
47
+ endpoints.push({
48
+ routeProps: {
49
+ path: "",
50
+ method: HttpMethod.get,
51
+ origin: config.pluginId,
52
+ },
53
+ handler: GetManagementUser,
54
+ });
55
+
56
+ endpoints.push({
57
+ routeProps: {
58
+ path: "/profile/schema",
59
+ method: HttpMethod.get,
60
+ origin: config.pluginId || "genericAuthPlugin",
61
+ },
62
+ handler: GetSchema,
63
+ });
64
+
65
+ endpoints.push({
66
+ routeProps: {
67
+ path: "/:userid",
68
+ method: HttpMethod.get,
69
+ origin: config.pluginId,
70
+ },
71
+
72
+ handler: GetManagementUserByUserid,
73
+ });
74
+
75
+ endpoints.push({
76
+ routeProps: {
77
+ path: "/:userid/view",
78
+ method: HttpMethod.get,
79
+ origin: config.pluginId,
80
+ },
81
+
82
+ handler: GetUserViewByUserid,
83
+ });
84
+
85
+ endpoints.push({
86
+ routeProps: {
87
+ path: "/:userid",
88
+ method: HttpMethod.delete,
89
+ origin: config.pluginId,
90
+ },
91
+ handler: DeleteManagementUserByUserid,
92
+ });
93
+
94
+ endpoints.push({
95
+ routeProps: {
96
+ path: "/password/:userid",
97
+ method: HttpMethod.put,
98
+ origin: config.pluginId,
99
+ },
100
+ handler: PutManagementUserPasswordByUserid,
101
+ });
102
+
103
+ endpoints.push({
104
+ routeProps: {
105
+ path: "/username/:userid",
106
+ method: HttpMethod.put,
107
+ origin: config.pluginId,
108
+ },
109
+ handler: PutManagementUserUsernameByUserid,
110
+ });
111
+
112
+ endpoints.push({
113
+ routeProps: {
114
+ path: "/profile/:userid",
115
+ method: HttpMethod.put,
116
+ origin: config.pluginId,
117
+ },
118
+ handler: PutManagementUserProfileByUserid,
119
+ });
120
+
121
+ endpoints.push({
122
+ routeProps: {
123
+ path: "/profile/:userid/append",
124
+ method: HttpMethod.put,
125
+ origin: config.pluginId,
126
+ },
127
+ handler: PutUserProfileByUseridAppend,
128
+ });
129
+
130
+ endpoints.push({
131
+ routeProps: {
132
+ path: "/roles/:userid",
133
+ method: HttpMethod.put,
134
+ origin: config.pluginId,
135
+ },
136
+ handler: PutManagementUserRolesByUserid,
137
+ });
138
+
139
+ let features: string[] = [];
140
+
141
+ if (config.uiSettings?.enableUserDelete == true) {
142
+ features.push("delete");
143
+ }
144
+ if (config.uiSettings?.enableUserCreate == true) {
145
+ features.push("create");
146
+ }
147
+ if (config.uiSettings?.enableUserEdit == true) {
148
+ features.push("edit");
149
+ }
150
+ if (config.uiSettings?.enableUserView == true) {
151
+ features.push("view");
151
152
  }
152
- };
153
153
 
154
- return module;
154
+ let module: ManagementApiModule = {
155
+ id: config.pluginId || "user",
156
+ uiSettings: {
157
+ title: config.uiSettings?.title || "Users",
158
+ icon: "",
159
+ features,
160
+ },
161
+ ui: config.ui,
162
+ type: ManagementApiType.user,
163
+ endpoints: endpoints,
164
+ data: {
165
+ profileSchema: config.profileSchema,
166
+ userViewGetData: config.userView?.getData,
167
+ },
168
+ };
169
+
170
+ return module;
155
171
  };
@@ -0,0 +1,3 @@
1
+ export interface GetManagementUserViewByUseridReq{
2
+
3
+ }
@@ -0,0 +1,4 @@
1
+ export interface GetManagementUserViewByUseridRes {
2
+ buttons: { text: string; url: string }[];
3
+ data: { [key: string]: string };
4
+ }