@dotpress/auth 1.0.1 → 1.1.0

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.
@@ -14,6 +14,7 @@ export const registerHandlers = (plugin) => {
14
14
  username: z.string(),
15
15
  password: z.string(),
16
16
  client: z.string().optional(),
17
+ aud: settings.aud?.required ? z.string() : z.string().optional(),
17
18
  }),
18
19
  response: z.object({
19
20
  userId: z.string(),
@@ -22,7 +23,7 @@ export const registerHandlers = (plugin) => {
22
23
  }),
23
24
  }),
24
25
  handler: async ({ req }) => {
25
- const { username, password, client } = req.body;
26
+ const { username, password, client, aud } = req.body;
26
27
  const ipAddress = getRequestIp(req);
27
28
  const user = await settings.dataProvider.findUserIdentifiers(username);
28
29
  if (!user) {
@@ -48,6 +49,20 @@ export const registerHandlers = (plugin) => {
48
49
  }
49
50
  return unauthorizedError('INVALID_LOGIN');
50
51
  }
52
+ if (aud && typeof settings.aud?.isUserRoleAllowed === 'function') {
53
+ const isAllowed = settings.aud.isUserRoleAllowed(user.role || '', aud);
54
+ if (!isAllowed) {
55
+ if (settings.onLoginFailed) {
56
+ await settings.onLoginFailed({
57
+ username,
58
+ errorType: 'invalid_role',
59
+ client: client || '',
60
+ ipAddress: ipAddress || '',
61
+ });
62
+ }
63
+ return unauthorizedError('INVALID_LOGIN');
64
+ }
65
+ }
51
66
  const { accessToken, refreshToken } = await generateTokensForUser({
52
67
  username,
53
68
  userId: user.userId,
@@ -16,6 +16,10 @@ export type AuthSettings = {
16
16
  issuer: string;
17
17
  onLoginFailed?: (e: LoginFailedEvent) => Promise<void>;
18
18
  onLoginSuccess?: (e: LoginSuccessEvent) => Promise<void>;
19
+ aud?: {
20
+ required?: boolean;
21
+ isUserRoleAllowed: (role: string, aud: string) => boolean;
22
+ };
19
23
  };
20
24
  type LoginFailedEvent = {
21
25
  username: string;
@@ -69,6 +73,7 @@ export type DataProvider = {
69
73
  findUserIdentifiers: (username: string) => Promise<{
70
74
  userId: string;
71
75
  passwordHash: string;
76
+ role?: string;
72
77
  } | undefined>;
73
78
  };
74
79
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotpress/auth",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "JWT Authentication plugin for Dotpress library",
5
5
  "author": "Renaud Guyon",
6
6
  "license": "MIT",