@vivinkv28/strapi-2fa-admin-plugin 0.1.12 → 0.1.13

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.
@@ -74,6 +74,16 @@ const runtimeRequire = createRequire(__filename);
74
74
  var strapiSessionAuth = runtimeRequire(resolveSessionAuthPath());
75
75
  const sessionAuth$1 = strapiSessionAuth;
76
76
  const getService = () => strapi.plugin("admin-2fa").service("auth");
77
+ const APPLICATION_ERROR_STATUS = {
78
+ ApplicationError: 400,
79
+ ValidationError: 400,
80
+ UnauthorizedError: 401,
81
+ ForbiddenError: 403,
82
+ NotFoundError: 404,
83
+ PayloadTooLargeError: 413,
84
+ RateLimitError: 429,
85
+ NotImplementedError: 501
86
+ };
77
87
  const setRefreshCookie = (ctx, refreshToken, cookieOptions) => {
78
88
  ctx.cookies.set(sessionAuth$1.REFRESH_COOKIE_NAME, refreshToken, cookieOptions);
79
89
  };
@@ -87,32 +97,69 @@ const getClientIp = (ctx) => {
87
97
  }
88
98
  return String(ctx.request.ip ?? ctx.ip ?? "").trim();
89
99
  };
100
+ const sendApplicationError = (ctx, error2) => {
101
+ const derivedStatus = typeof error2?.status === "number" && error2.status >= 400 && error2.status < 500 ? error2.status : APPLICATION_ERROR_STATUS[error2?.name] ?? 400;
102
+ ctx.status = derivedStatus;
103
+ ctx.body = {
104
+ data: null,
105
+ error: {
106
+ status: derivedStatus,
107
+ name: error2?.name ?? "ApplicationError",
108
+ message: error2?.message ?? "Request failed",
109
+ details: error2?.details ?? {}
110
+ }
111
+ };
112
+ };
90
113
  var auth$3 = {
91
114
  async login(ctx) {
92
- const result = await getService().createChallenge(ctx.request.body ?? {}, {
93
- clientIp: getClientIp(ctx)
94
- });
95
- ctx.body = { data: result };
115
+ try {
116
+ const result = await getService().createChallenge(ctx.request.body ?? {}, {
117
+ clientIp: getClientIp(ctx)
118
+ });
119
+ ctx.body = { data: result };
120
+ } catch (error2) {
121
+ if (error2?.name && APPLICATION_ERROR_STATUS[error2.name]) {
122
+ sendApplicationError(ctx, error2);
123
+ return;
124
+ }
125
+ throw error2;
126
+ }
96
127
  },
97
128
  async resend(ctx) {
98
- const result = await getService().resendChallenge(ctx.request.body ?? {}, {
99
- clientIp: getClientIp(ctx)
100
- });
101
- ctx.body = { data: result };
129
+ try {
130
+ const result = await getService().resendChallenge(ctx.request.body ?? {}, {
131
+ clientIp: getClientIp(ctx)
132
+ });
133
+ ctx.body = { data: result };
134
+ } catch (error2) {
135
+ if (error2?.name && APPLICATION_ERROR_STATUS[error2.name]) {
136
+ sendApplicationError(ctx, error2);
137
+ return;
138
+ }
139
+ throw error2;
140
+ }
102
141
  },
103
142
  async verify(ctx) {
104
- const result = await getService().verifyChallenge(ctx.request.body ?? {}, {
105
- secureRequest: ctx.request.secure,
106
- clientIp: getClientIp(ctx)
107
- });
108
- setRefreshCookie(ctx, result.refreshToken, result.cookieOptions);
109
- ctx.body = {
110
- data: {
111
- token: result.accessToken,
112
- accessToken: result.accessToken,
113
- user: result.user
143
+ try {
144
+ const result = await getService().verifyChallenge(ctx.request.body ?? {}, {
145
+ secureRequest: ctx.request.secure,
146
+ clientIp: getClientIp(ctx)
147
+ });
148
+ setRefreshCookie(ctx, result.refreshToken, result.cookieOptions);
149
+ ctx.body = {
150
+ data: {
151
+ token: result.accessToken,
152
+ accessToken: result.accessToken,
153
+ user: result.user
154
+ }
155
+ };
156
+ } catch (error2) {
157
+ if (error2?.name && APPLICATION_ERROR_STATUS[error2.name]) {
158
+ sendApplicationError(ctx, error2);
159
+ return;
114
160
  }
115
- };
161
+ throw error2;
162
+ }
116
163
  }
117
164
  };
118
165
  const auth$2 = auth$3;
@@ -60,6 +60,16 @@ const runtimeRequire = createRequire(__filename);
60
60
  var strapiSessionAuth = runtimeRequire(resolveSessionAuthPath());
61
61
  const sessionAuth$1 = strapiSessionAuth;
62
62
  const getService = () => strapi.plugin("admin-2fa").service("auth");
63
+ const APPLICATION_ERROR_STATUS = {
64
+ ApplicationError: 400,
65
+ ValidationError: 400,
66
+ UnauthorizedError: 401,
67
+ ForbiddenError: 403,
68
+ NotFoundError: 404,
69
+ PayloadTooLargeError: 413,
70
+ RateLimitError: 429,
71
+ NotImplementedError: 501
72
+ };
63
73
  const setRefreshCookie = (ctx, refreshToken, cookieOptions) => {
64
74
  ctx.cookies.set(sessionAuth$1.REFRESH_COOKIE_NAME, refreshToken, cookieOptions);
65
75
  };
@@ -73,32 +83,69 @@ const getClientIp = (ctx) => {
73
83
  }
74
84
  return String(ctx.request.ip ?? ctx.ip ?? "").trim();
75
85
  };
86
+ const sendApplicationError = (ctx, error2) => {
87
+ const derivedStatus = typeof error2?.status === "number" && error2.status >= 400 && error2.status < 500 ? error2.status : APPLICATION_ERROR_STATUS[error2?.name] ?? 400;
88
+ ctx.status = derivedStatus;
89
+ ctx.body = {
90
+ data: null,
91
+ error: {
92
+ status: derivedStatus,
93
+ name: error2?.name ?? "ApplicationError",
94
+ message: error2?.message ?? "Request failed",
95
+ details: error2?.details ?? {}
96
+ }
97
+ };
98
+ };
76
99
  var auth$3 = {
77
100
  async login(ctx) {
78
- const result = await getService().createChallenge(ctx.request.body ?? {}, {
79
- clientIp: getClientIp(ctx)
80
- });
81
- ctx.body = { data: result };
101
+ try {
102
+ const result = await getService().createChallenge(ctx.request.body ?? {}, {
103
+ clientIp: getClientIp(ctx)
104
+ });
105
+ ctx.body = { data: result };
106
+ } catch (error2) {
107
+ if (error2?.name && APPLICATION_ERROR_STATUS[error2.name]) {
108
+ sendApplicationError(ctx, error2);
109
+ return;
110
+ }
111
+ throw error2;
112
+ }
82
113
  },
83
114
  async resend(ctx) {
84
- const result = await getService().resendChallenge(ctx.request.body ?? {}, {
85
- clientIp: getClientIp(ctx)
86
- });
87
- ctx.body = { data: result };
115
+ try {
116
+ const result = await getService().resendChallenge(ctx.request.body ?? {}, {
117
+ clientIp: getClientIp(ctx)
118
+ });
119
+ ctx.body = { data: result };
120
+ } catch (error2) {
121
+ if (error2?.name && APPLICATION_ERROR_STATUS[error2.name]) {
122
+ sendApplicationError(ctx, error2);
123
+ return;
124
+ }
125
+ throw error2;
126
+ }
88
127
  },
89
128
  async verify(ctx) {
90
- const result = await getService().verifyChallenge(ctx.request.body ?? {}, {
91
- secureRequest: ctx.request.secure,
92
- clientIp: getClientIp(ctx)
93
- });
94
- setRefreshCookie(ctx, result.refreshToken, result.cookieOptions);
95
- ctx.body = {
96
- data: {
97
- token: result.accessToken,
98
- accessToken: result.accessToken,
99
- user: result.user
129
+ try {
130
+ const result = await getService().verifyChallenge(ctx.request.body ?? {}, {
131
+ secureRequest: ctx.request.secure,
132
+ clientIp: getClientIp(ctx)
133
+ });
134
+ setRefreshCookie(ctx, result.refreshToken, result.cookieOptions);
135
+ ctx.body = {
136
+ data: {
137
+ token: result.accessToken,
138
+ accessToken: result.accessToken,
139
+ user: result.user
140
+ }
141
+ };
142
+ } catch (error2) {
143
+ if (error2?.name && APPLICATION_ERROR_STATUS[error2.name]) {
144
+ sendApplicationError(ctx, error2);
145
+ return;
100
146
  }
101
- };
147
+ throw error2;
148
+ }
102
149
  }
103
150
  };
104
151
  const auth$2 = auth$3;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vivinkv28/strapi-2fa-admin-plugin",
3
- "version": "0.1.12",
3
+ "version": "0.1.13",
4
4
  "description": "Reusable Strapi admin 2FA plugin",
5
5
  "type": "commonjs",
6
6
  "keywords": [