@shipfox/api-auth 9.1.0 → 9.3.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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +41 -0
- package/README.md +44 -2
- package/dist/config.d.ts +5 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +26 -0
- package/dist/config.js.map +1 -1
- package/dist/core/admin-role-model.d.ts +5 -0
- package/dist/core/admin-role-model.d.ts.map +1 -0
- package/dist/core/admin-role-model.js +18 -0
- package/dist/core/admin-role-model.js.map +1 -0
- package/dist/core/admin-role.d.ts +13 -0
- package/dist/core/admin-role.d.ts.map +1 -0
- package/dist/core/admin-role.js +21 -0
- package/dist/core/admin-role.js.map +1 -0
- package/dist/core/administration.d.ts +27 -0
- package/dist/core/administration.d.ts.map +1 -0
- package/dist/core/administration.js +122 -0
- package/dist/core/administration.js.map +1 -0
- package/dist/core/auth.d.ts +9 -0
- package/dist/core/auth.d.ts.map +1 -1
- package/dist/core/auth.js +68 -20
- package/dist/core/auth.js.map +1 -1
- package/dist/core/entities/admin-grant.d.ts +10 -0
- package/dist/core/entities/admin-grant.d.ts.map +1 -0
- package/dist/core/entities/admin-grant.js +3 -0
- package/dist/core/entities/admin-grant.js.map +1 -0
- package/dist/core/errors.d.ts +25 -0
- package/dist/core/errors.d.ts.map +1 -1
- package/dist/core/errors.js +49 -0
- package/dist/core/errors.js.map +1 -1
- package/dist/core/ports.d.ts +13 -0
- package/dist/core/ports.d.ts.map +1 -0
- package/dist/core/ports.js +3 -0
- package/dist/core/ports.js.map +1 -0
- package/dist/core/signup-policy.d.ts +10 -0
- package/dist/core/signup-policy.d.ts.map +1 -0
- package/dist/core/signup-policy.js +39 -0
- package/dist/core/signup-policy.js.map +1 -0
- package/dist/db/admin-grants.d.ts +33 -0
- package/dist/db/admin-grants.d.ts.map +1 -0
- package/dist/db/admin-grants.js +193 -0
- package/dist/db/admin-grants.js.map +1 -0
- package/dist/db/db.d.ts +474 -0
- package/dist/db/db.d.ts.map +1 -1
- package/dist/db/db.js +4 -0
- package/dist/db/db.js.map +1 -1
- package/dist/db/schema/admin-command-results.d.ts +142 -0
- package/dist/db/schema/admin-command-results.d.ts.map +1 -0
- package/dist/db/schema/admin-command-results.js +21 -0
- package/dist/db/schema/admin-command-results.js.map +1 -0
- package/dist/db/schema/admin-grants.d.ts +115 -0
- package/dist/db/schema/admin-grants.d.ts.map +1 -0
- package/dist/db/schema/admin-grants.js +42 -0
- package/dist/db/schema/admin-grants.js.map +1 -0
- package/dist/index.d.ts +12 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -4
- package/dist/index.js.map +1 -1
- package/dist/metrics/instance.d.ts +1 -1
- package/dist/metrics/instance.d.ts.map +1 -1
- package/dist/metrics/instance.js.map +1 -1
- package/dist/presentation/dto/user.d.ts +2 -1
- package/dist/presentation/dto/user.d.ts.map +1 -1
- package/dist/presentation/dto/user.js +2 -1
- package/dist/presentation/dto/user.js.map +1 -1
- package/dist/presentation/inter-module.d.ts.map +1 -1
- package/dist/presentation/inter-module.js +26 -2
- package/dist/presentation/inter-module.js.map +1 -1
- package/dist/presentation/routes/administration.d.ts +3 -0
- package/dist/presentation/routes/administration.d.ts.map +1 -0
- package/dist/presentation/routes/administration.js +186 -0
- package/dist/presentation/routes/administration.js.map +1 -0
- package/dist/presentation/routes/index.d.ts +2 -1
- package/dist/presentation/routes/index.d.ts.map +1 -1
- package/dist/presentation/routes/index.js +2 -2
- package/dist/presentation/routes/index.js.map +1 -1
- package/dist/presentation/routes/rate-limit.d.ts +1 -0
- package/dist/presentation/routes/rate-limit.d.ts.map +1 -1
- package/dist/presentation/routes/rate-limit.js +20 -1
- package/dist/presentation/routes/rate-limit.js.map +1 -1
- package/dist/presentation/routes/registration/signup.d.ts +2 -1
- package/dist/presentation/routes/registration/signup.d.ts.map +1 -1
- package/dist/presentation/routes/registration/signup.js +14 -3
- package/dist/presentation/routes/registration/signup.js.map +1 -1
- package/dist/presentation/routes/session/me.d.ts.map +1 -1
- package/dist/presentation/routes/session/me.js +5 -1
- package/dist/presentation/routes/session/me.js.map +1 -1
- package/dist/tsconfig.test.tsbuildinfo +1 -1
- package/drizzle/0001_brainy_shriek.sql +14 -0
- package/drizzle/0002_burly_whizzer.sql +12 -0
- package/drizzle/meta/0001_snapshot.json +702 -0
- package/drizzle/meta/0002_snapshot.json +791 -0
- package/drizzle/meta/_journal.json +14 -0
- package/package.json +10 -9
- package/src/config.test.ts +39 -0
- package/src/config.ts +34 -0
- package/src/core/admin-role-model.ts +25 -0
- package/src/core/admin-role.test.ts +39 -0
- package/src/core/admin-role.ts +25 -0
- package/src/core/administration.ts +172 -0
- package/src/core/auth-default-signup-policy.test.ts +65 -0
- package/src/core/auth.test.ts +115 -0
- package/src/core/auth.ts +77 -21
- package/src/core/entities/admin-grant.ts +10 -0
- package/src/core/errors.ts +59 -0
- package/src/core/jwt.test.ts +1 -0
- package/src/core/ports.ts +7 -0
- package/src/core/signup-policy.test.ts +78 -0
- package/src/core/signup-policy.ts +42 -0
- package/src/db/admin-grants.test.ts +57 -0
- package/src/db/admin-grants.ts +340 -0
- package/src/db/db.ts +4 -0
- package/src/db/schema/admin-command-results.ts +41 -0
- package/src/db/schema/admin-grants.ts +49 -0
- package/src/index.test.ts +58 -1
- package/src/index.ts +47 -6
- package/src/metrics/instance.ts +1 -1
- package/src/presentation/auth/refresh-cookie.test.ts +4 -0
- package/src/presentation/dto/user.ts +7 -2
- package/src/presentation/e2eRoutes/index.test.ts +1 -0
- package/src/presentation/inter-module.test.ts +47 -0
- package/src/presentation/inter-module.ts +22 -1
- package/src/presentation/routes/administration.test.ts +253 -0
- package/src/presentation/routes/administration.ts +195 -0
- package/src/presentation/routes/index.test.ts +1 -0
- package/src/presentation/routes/index.ts +3 -1
- package/src/presentation/routes/rate-limit.ts +23 -2
- package/src/presentation/routes/registration/signup.test.ts +26 -0
- package/src/presentation/routes/registration/signup.ts +22 -2
- package/src/presentation/routes/session/login.test.ts +1 -0
- package/src/presentation/routes/session/me.test.ts +18 -2
- package/src/presentation/routes/session/me.ts +5 -1
- package/test/globalSetup.ts +6 -2
- package/test/routes.ts +7 -1
- package/tsconfig.build.tsbuildinfo +1 -1
|
@@ -6,14 +6,21 @@ import {signup, signupWithInvitation} from '#core/auth.js';
|
|
|
6
6
|
import {
|
|
7
7
|
EmailTakenError,
|
|
8
8
|
InvitationEmailMismatchError,
|
|
9
|
+
SignupNotAllowedError,
|
|
9
10
|
TokenAlreadyUsedError,
|
|
10
11
|
TokenExpiredError,
|
|
11
12
|
TokenInvalidError,
|
|
12
13
|
} from '#core/errors.js';
|
|
14
|
+
import type {SignupPolicy} from '#core/ports.js';
|
|
13
15
|
import {setRefreshTokenCookie} from '#presentation/auth/refresh-cookie.js';
|
|
14
16
|
import {toUserDto} from '#presentation/dto/user.js';
|
|
15
17
|
|
|
16
|
-
|
|
18
|
+
const SIGNUP_NOT_ALLOWED_MESSAGE_MAX_LENGTH = 500;
|
|
19
|
+
|
|
20
|
+
export function createSignupRoute(
|
|
21
|
+
workspaces: WorkspacesInterModuleClient,
|
|
22
|
+
signupPolicy?: SignupPolicy,
|
|
23
|
+
) {
|
|
17
24
|
return defineRoute({
|
|
18
25
|
method: 'POST',
|
|
19
26
|
path: '/signup',
|
|
@@ -35,6 +42,13 @@ export function createSignupRoute(workspaces: WorkspacesInterModuleClient) {
|
|
|
35
42
|
if (error instanceof EmailTakenError) {
|
|
36
43
|
throw new ClientError('Email already registered', 'email-taken', {status: 409});
|
|
37
44
|
}
|
|
45
|
+
if (error instanceof SignupNotAllowedError) {
|
|
46
|
+
const message = error.message.slice(0, SIGNUP_NOT_ALLOWED_MESSAGE_MAX_LENGTH);
|
|
47
|
+
throw new ClientError(message, 'signup-not-allowed', {
|
|
48
|
+
status: 403,
|
|
49
|
+
details: {message},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
38
52
|
if (error instanceof TokenInvalidError) {
|
|
39
53
|
throw new ClientError('Invitation token is invalid', 'invitation-token-invalid', {
|
|
40
54
|
status: 410,
|
|
@@ -63,7 +77,13 @@ export function createSignupRoute(workspaces: WorkspacesInterModuleClient) {
|
|
|
63
77
|
const {email, password, name, invitation_token} = request.body;
|
|
64
78
|
|
|
65
79
|
if (invitation_token === undefined) {
|
|
66
|
-
const result = await signup({
|
|
80
|
+
const result = await signup({
|
|
81
|
+
email,
|
|
82
|
+
password,
|
|
83
|
+
name,
|
|
84
|
+
sourceIp: request.ip,
|
|
85
|
+
signupPolicy,
|
|
86
|
+
});
|
|
67
87
|
reply.code(201);
|
|
68
88
|
return {
|
|
69
89
|
user: toUserDto(result),
|
|
@@ -35,6 +35,7 @@ describe('POST /auth/login', () => {
|
|
|
35
35
|
expect(res.statusCode).toBe(200);
|
|
36
36
|
expect(res.json().token).toBeDefined();
|
|
37
37
|
expect(res.json().user.email).toBe(email);
|
|
38
|
+
expect(res.json().admin_role).toBeNull();
|
|
38
39
|
expect(res.headers['set-cookie']).toContain('shipfox_refresh_token=');
|
|
39
40
|
expect(res.headers['set-cookie']).toContain('HttpOnly');
|
|
40
41
|
expect(res.headers['set-cookie']).toContain('Secure');
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type {FastifyInstance} from 'fastify';
|
|
2
2
|
import {signUserToken} from '#core/jwt.js';
|
|
3
|
+
import {createAdminGrant} from '#db/admin-grants.js';
|
|
3
4
|
import {
|
|
4
5
|
createAuthTestApp,
|
|
6
|
+
createVerifiedSession,
|
|
5
7
|
ROUTE_TEST_SECRET,
|
|
6
8
|
resetCapturedMail,
|
|
7
|
-
signupVerifyLogin,
|
|
8
9
|
} from '#test/routes.js';
|
|
9
10
|
|
|
10
11
|
describe('GET /auth/me', () => {
|
|
@@ -23,7 +24,7 @@ describe('GET /auth/me', () => {
|
|
|
23
24
|
});
|
|
24
25
|
|
|
25
26
|
test('returns the signed-in user', async () => {
|
|
26
|
-
const account = await
|
|
27
|
+
const account = await createVerifiedSession('me');
|
|
27
28
|
|
|
28
29
|
const res = await app.inject({
|
|
29
30
|
method: 'GET',
|
|
@@ -33,6 +34,21 @@ describe('GET /auth/me', () => {
|
|
|
33
34
|
|
|
34
35
|
expect(res.statusCode).toBe(200);
|
|
35
36
|
expect(res.json().user.email).toBe(account.email);
|
|
37
|
+
expect(res.json().admin_role).toBeNull();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('returns the current Auth-owned role for dashboard presentation', async () => {
|
|
41
|
+
const account = await createVerifiedSession('me-admin');
|
|
42
|
+
await createAdminGrant({userId: account.userId, role: 'admin-observer'});
|
|
43
|
+
|
|
44
|
+
const res = await app.inject({
|
|
45
|
+
method: 'GET',
|
|
46
|
+
url: '/auth/me',
|
|
47
|
+
headers: {authorization: `Bearer ${account.token}`},
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
expect(res.statusCode).toBe(200);
|
|
51
|
+
expect(res.json().admin_role).toBe('admin-observer');
|
|
36
52
|
});
|
|
37
53
|
|
|
38
54
|
test('transforms a token for a missing user into 404', async () => {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {AUTH_USER} from '@shipfox/api-auth-context';
|
|
2
2
|
import {meResponseSchema} from '@shipfox/api-auth-dto';
|
|
3
3
|
import {ClientError, defineRoute} from '@shipfox/node-fastify';
|
|
4
|
+
import {getCurrentAdminRole} from '#core/admin-role.js';
|
|
4
5
|
import {getCurrentUser} from '#core/auth.js';
|
|
5
6
|
import {UserNotFoundError} from '#core/errors.js';
|
|
6
7
|
import {getClientContext} from '#presentation/auth/jwt-auth.js';
|
|
@@ -30,6 +31,9 @@ export const meRoute = defineRoute({
|
|
|
30
31
|
|
|
31
32
|
const result = await getCurrentUser({userId: client.userId});
|
|
32
33
|
|
|
33
|
-
return {
|
|
34
|
+
return {
|
|
35
|
+
user: toUserDto(result.user),
|
|
36
|
+
admin_role: await getCurrentAdminRole({userId: client.userId}),
|
|
37
|
+
};
|
|
34
38
|
},
|
|
35
39
|
});
|
package/test/globalSetup.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import './env.js';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
initializeEmailChallengesForTests,
|
|
4
|
+
resetEmailChallengesForTests,
|
|
5
|
+
} from '@shipfox/api-email-challenges/test';
|
|
3
6
|
import {runMigrations} from '@shipfox/node-drizzle';
|
|
4
7
|
import {closePostgresClient, createPostgresClient} from '@shipfox/node-postgres';
|
|
5
8
|
import {sql} from 'drizzle-orm';
|
|
@@ -11,8 +14,9 @@ export async function setup() {
|
|
|
11
14
|
|
|
12
15
|
await runMigrations(db(), migrationsPath, '__drizzle_migrations_auth');
|
|
13
16
|
await initializeEmailChallengesForTests();
|
|
17
|
+
await resetEmailChallengesForTests();
|
|
14
18
|
await db().execute(
|
|
15
|
-
sql`TRUNCATE auth_outbox, auth_password_resets, auth_rate_limits, auth_refresh_tokens, auth_users CASCADE`,
|
|
19
|
+
sql`TRUNCATE auth_admin_command_results, auth_admin_grants, auth_outbox, auth_password_resets, auth_rate_limits, auth_refresh_tokens, auth_users CASCADE`,
|
|
16
20
|
);
|
|
17
21
|
|
|
18
22
|
closeDb();
|
package/test/routes.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {and, desc, eq, sql} from 'drizzle-orm';
|
|
|
8
8
|
import {db} from '#db/db.js';
|
|
9
9
|
import {authOutbox} from '#db/schema/outbox.js';
|
|
10
10
|
import {createJwtAuthMethod} from '#presentation/auth/jwt-auth.js';
|
|
11
|
+
import {administrationRoutes} from '#presentation/routes/administration.js';
|
|
11
12
|
import {buildAuthRoutes} from '#presentation/routes/index.js';
|
|
12
13
|
|
|
13
14
|
const testConfig = vi.hoisted(
|
|
@@ -48,11 +49,16 @@ const workspaces = workspaceTestDoubles as unknown as WorkspacesInterModuleClien
|
|
|
48
49
|
|
|
49
50
|
vi.mock('#config.js', () => ({
|
|
50
51
|
config: {
|
|
52
|
+
ADMIN_BOOTSTRAP_TOKEN: 'test-bootstrap-token',
|
|
51
53
|
AUTH_JWT_EXPIRES_IN: '15m',
|
|
52
54
|
AUTH_REFRESH_TOKEN_EXPIRES_IN_DAYS: 14,
|
|
53
55
|
AUTH_REFRESH_ROTATION_GRACE_SECONDS: 30,
|
|
54
56
|
AUTH_REFRESH_COOKIE_NAME: 'shipfox_refresh_token',
|
|
55
57
|
AUTH_PASSWORD_ENABLED: true,
|
|
58
|
+
AUTH_SIGNUP_GATE_ENABLED: false,
|
|
59
|
+
AUTH_SIGNUP_ALLOWED_EMAIL_DOMAINS: '',
|
|
60
|
+
AUTH_SIGNUP_ALLOWED_EMAILS: '',
|
|
61
|
+
AUTH_SIGNUP_NOT_ALLOWED_MESSAGE: undefined,
|
|
56
62
|
CLIENT_BASE_URL: testConfig.clientBaseUrl,
|
|
57
63
|
},
|
|
58
64
|
mailer: testConfig.mailer,
|
|
@@ -147,7 +153,7 @@ export async function createAuthTestApp(params?: {
|
|
|
147
153
|
}): Promise<FastifyInstance> {
|
|
148
154
|
const appConfig: AppConfig = {
|
|
149
155
|
auth: [createJwtAuthMethod()],
|
|
150
|
-
routes: [buildAuthRoutes(true, workspaces)],
|
|
156
|
+
routes: [buildAuthRoutes(true, workspaces), administrationRoutes],
|
|
151
157
|
swagger: false,
|
|
152
158
|
};
|
|
153
159
|
if (params?.fastifyOptions) appConfig.fastifyOptions = params.fastifyOptions;
|