@dotpress/auth 1.0.0 → 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 renoguyon
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import type { Plugin } from 'dotpress';
2
2
  import { AuthSettings, DataProvider, KeyPairSettings } from './types/types.js';
3
+ import { generateAccessToken } from './lib/tokens.js';
3
4
  type Settings = Partial<AuthSettings> & {
4
5
  keys: KeyPairSettings;
5
6
  dataProvider: DataProvider;
6
7
  };
7
8
  export declare const configurePlugin: (settings: Settings) => Plugin;
8
9
  export declare const generatePasswordHash: (password: string, salt?: number) => Promise<string>;
9
- export {};
10
+ export { generateAccessToken };
package/dist/index.js CHANGED
@@ -1,7 +1,8 @@
1
+ import bcrypt from 'bcryptjs';
1
2
  import { setSettings } from './lib/settings.js';
2
3
  import { registerHandlers } from './lib/handlers.js';
3
4
  import { attachAuthenticatedUser } from './lib/middleware.js';
4
- import bcrypt from 'bcryptjs';
5
+ import { generateAccessToken } from './lib/tokens.js';
5
6
  export const configurePlugin = (settings) => {
6
7
  setSettings(settings);
7
8
  return (plugin) => {
@@ -14,3 +15,4 @@ export const configurePlugin = (settings) => {
14
15
  export const generatePasswordHash = (password, salt = 10) => {
15
16
  return bcrypt.hash(password, salt);
16
17
  };
18
+ export { generateAccessToken };
@@ -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,
@@ -22,6 +22,7 @@ export declare const generateRefreshToken: () => CreatedRefreshToken;
22
22
  * Generate a pair of access and refresh tokens for a given user
23
23
  */
24
24
  export declare const generateTokensForUser: ({ userId, username, }: UserIdentifiers) => Promise<TokenPair>;
25
+ export declare const generateAccessToken: ({ userId, username, }: UserIdentifiers) => CreatedAccessToken;
25
26
  /**
26
27
  * Regenerate a pair of token using a refresh token
27
28
  * Expired access token must be provided in order to validate userId and jwtId
@@ -29,11 +29,7 @@ export const generateRefreshToken = () => {
29
29
  */
30
30
  export const generateTokensForUser = async ({ userId, username, }) => {
31
31
  const settings = getSettings();
32
- const { jwtId, accessToken } = createAccessTokenFromPayload({
33
- sub: userId,
34
- username,
35
- auth_time: Math.floor(Date.now() / 1000),
36
- });
32
+ const { jwtId, accessToken } = generateAccessToken({ userId, username });
37
33
  const { token: refreshToken, tokenHash: refreshTokenHash } = generateRefreshToken();
38
34
  await settings.dataProvider.storeRefreshToken({
39
35
  token: refreshToken,
@@ -47,6 +43,13 @@ export const generateTokensForUser = async ({ userId, username, }) => {
47
43
  refreshToken,
48
44
  };
49
45
  };
46
+ export const generateAccessToken = ({ userId, username, }) => {
47
+ return createAccessTokenFromPayload({
48
+ sub: userId,
49
+ username,
50
+ auth_time: Math.floor(Date.now() / 1000),
51
+ });
52
+ };
50
53
  const addDaysFromNow = (days) => {
51
54
  const now = new Date();
52
55
  now.setDate(now.getDate() + days);
@@ -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.0",
3
+ "version": "1.1.0",
4
4
  "description": "JWT Authentication plugin for Dotpress library",
5
5
  "author": "Renaud Guyon",
6
6
  "license": "MIT",