@stacksjs/auth 0.70.87 → 0.70.88

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stacksjs/auth",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "0.70.87",
5
+ "version": "0.70.88",
6
6
  "description": "A more simplistic way to authenticate.",
7
7
  "author": "Chris Breuer",
8
8
  "contributors": [
@@ -55,7 +55,7 @@
55
55
  },
56
56
  "devDependencies": {
57
57
  "better-dx": "^0.2.16",
58
- "@stacksjs/error-handling": "0.70.87",
59
- "@stacksjs/router": "0.70.87"
58
+ "@stacksjs/error-handling": "0.70.88",
59
+ "@stacksjs/router": "0.70.88"
60
60
  }
61
61
  }
@@ -1,43 +0,0 @@
1
- import { User } from '@stacksjs/orm';
2
- import type { AuthCredentials, AuthToken, NewAccessToken, PersonalAccessToken, TokenCreateOptions } from '@stacksjs/types';
3
- declare type UserModel = NonNullable<Awaited<ReturnType<typeof User.find>>>;
4
- export declare class Auth {
5
- static attempt(credentials: AuthCredentials): Promise<boolean>;
6
- static validate(credentials: AuthCredentials): Promise<boolean>;
7
- static login(credentials: AuthCredentials, options?: TokenCreateOptions): Promise<
8
- { user: UserModel, token: AuthToken, refreshToken?: string, expiresIn?: number } | null
9
- >;
10
- static loginUsingId(userId: number, options?: TokenCreateOptions): Promise<
11
- { user: UserModel, token: AuthToken, refreshToken?: string, expiresIn?: number } | null
12
- >;
13
- static logout(): Promise<void>;
14
- static user(): Promise<UserModel | undefined>;
15
- static check(): Promise<boolean>;
16
- static guest(): Promise<boolean>;
17
- static id(): Promise<number | undefined>;
18
- static setUser(user: UserModel): void;
19
- static createTokenForUser(user: UserModel, options?: TokenCreateOptions): Promise<NewAccessToken>;
20
- static createToken(user: UserModel, name?: string, abilities?: string[]): Promise<AuthToken>;
21
- static requestToken(credentials: AuthCredentials, clientId: number, clientSecret: string): Promise<{ token: AuthToken } | null>;
22
- static validateToken(token: string): Promise<boolean>;
23
- static getUserFromToken(token: string): Promise<UserModel | undefined>;
24
- static currentAccessToken(): Promise<PersonalAccessToken | undefined>;
25
- static tokenCan(ability: string): Promise<boolean>;
26
- static tokenCant(ability: string): Promise<boolean>;
27
- static tokenAbilities(): Promise<string[]>;
28
- static tokenCanAll(abilities: string[]): Promise<boolean>;
29
- static tokenCanAny(abilities: string[]): Promise<boolean>;
30
- static tokens(userId?: number): Promise<PersonalAccessToken[]>;
31
- static revokeToken(token: string): Promise<void>;
32
- static revokeTokenById(tokenId: number): Promise<void>;
33
- static revokeAllTokens(userId?: number): Promise<void>;
34
- static revokeOtherTokens(userId?: number): Promise<void>;
35
- static pruneExpiredTokens(): Promise<number>;
36
- static pruneRevokedTokens(): Promise<number>;
37
- static rotateToken(oldToken: string): Promise<AuthToken | null>;
38
- static findToken(tokenId: number): Promise<PersonalAccessToken | null>;
39
- static once(credentials: AuthCredentials): Promise<boolean>;
40
- static guard(_name?: string): typeof Auth;
41
- static viaRemember(): boolean;
42
- static clearState(): void;
43
- }
@@ -1,17 +0,0 @@
1
- export declare function generateTwoFactorSecret(): string;
2
- export declare function generateTwoFactorToken(secret: Secret): Promise<Token>;
3
- export declare function verifyTwoFactorCode(token: Token, secret: Secret): Promise<boolean>;
4
- /**
5
- * Generate an otpauth:// URI for two-factor authentication
6
- *
7
- * This URI can be used with any QR code library to generate a scannable
8
- * QR code for authenticator apps.
9
- *
10
- * @param user - User identifier (email or username)
11
- * @param service - Service name (e.g., 'StacksJS 2FA')
12
- * @param secret - Optional secret (will be generated if not provided)
13
- * @returns The otpauth:// URI string
14
- */
15
- export declare function generateTwoFactorUri(user?: string, service?: string, secret?: Secret): string;
16
- export type Token = string;
17
- export type Secret = string;
@@ -1,77 +0,0 @@
1
- import { any, can, cannot } from './gate';
2
- import type { AuthorizationResponse } from './gate';
3
- import type { UserModel as OrmUserModel } from '@stacksjs/orm';
4
- /**
5
- * Check if a user can perform an ability
6
- *
7
- * @example
8
- * if (await userCan(user, 'edit-settings')) { ... }
9
- * if (await userCan(user, 'update', post)) { ... }
10
- */
11
- export declare function userCan(user: UserModel | null, ability: string, ...args: any[]): Promise<boolean>;
12
- /**
13
- * Check if a user cannot perform an ability
14
- *
15
- * @example
16
- * if (await userCannot(user, 'delete', post)) { ... }
17
- */
18
- export declare function userCannot(user: UserModel | null, ability: string, ...args: any[]): Promise<boolean>;
19
- /**
20
- * Check if a user can perform any of the given abilities
21
- *
22
- * @example
23
- * if (await userCanAny(user, ['update', 'delete'], post)) { ... }
24
- */
25
- export declare function userCanAny(user: UserModel | null, abilities: string[], ...args: any[]): Promise<boolean>;
26
- /**
27
- * Check if a user can perform all of the given abilities
28
- *
29
- * @example
30
- * if (await userCanAll(user, ['view', 'update'], post)) { ... }
31
- */
32
- export declare function userCanAll(user: UserModel | null, abilities: string[], ...args: any[]): Promise<boolean>;
33
- /**
34
- * Authorize a user or throw an exception
35
- *
36
- * @example
37
- * await authorizeUser(user, 'update', post) // Throws if not allowed
38
- */
39
- export declare function authorizeUser(user: UserModel | null, ability: string, ...args: any[]): Promise<AuthorizationResponse>;
40
- /**
41
- * Get detailed authorization result
42
- *
43
- * @example
44
- * const result = await inspectUser(user, 'update', post)
45
- * if (result.denied()) {
46
- * console.log(result.message)
47
- * }
48
- */
49
- export declare function inspectUser(user: UserModel | null, ability: string, ...args: any[]): Promise<AuthorizationResponse>;
50
- /**
51
- * Authorizable trait for user models
52
- *
53
- * Add authorization methods to a user object
54
- *
55
- * @example
56
- * const authorizedUser = withAuthorization(user)
57
- * if (await authorizedUser.can('update', post)) { ... }
58
- */
59
- export declare function withAuthorization<T extends UserModel>(user: T): T & AuthorizableMethods;
60
- /**
61
- * Authorization methods interface
62
- */
63
- export declare interface AuthorizableMethods {
64
- can(ability: string, ...args: any[]): Promise<boolean>
65
- cannot(ability: string, ...args: any[]): Promise<boolean>
66
- canAny(abilities: string[], ...args: any[]): Promise<boolean>
67
- canAll(abilities: string[], ...args: any[]): Promise<boolean>
68
- authorize(ability: string, ...args: any[]): Promise<AuthorizationResponse>
69
- }
70
- // Alias the ORM-derived UserModel under the name the rest of this module
71
- // expects. Using the row/instance shape (rather than `typeof User`) lets
72
- // callers pass plain authenticated user objects to the gate helpers.
73
- declare type UserModel = OrmUserModel;
74
- /**
75
- * Type for a user with authorization methods
76
- */
77
- export type AuthorizableUser<T extends UserModel> = T & AuthorizableMethods;
package/dist/client.d.ts DELETED
@@ -1,22 +0,0 @@
1
- import type { Result } from '@stacksjs/error-handling';
2
- /**
3
- * Create a personal access OAuth client.
4
- *
5
- * Idempotent: returns an `err({ code: 'already-exists' })` when a
6
- * non-revoked personal access client already exists in the
7
- * `oauth_clients` table, rather than inserting a duplicate. The
8
- * previous behaviour silently created a second row, which made
9
- * `getPersonalAccessClient()` (which `LIMIT 1`s with no `ORDER BY`)
10
- * return whichever the DB happened to surface first — racy with
11
- * subsequent token mints. See stacksjs/stacks#1860 M-7.
12
- *
13
- * The plaintext secret is what callers (e.g., `./buddy auth:token`)
14
- * surface to the operator — they store it themselves. The DB holds
15
- * only the bcrypt hash so a DB compromise doesn't leak usable client
16
- * credentials (stacksjs/stacks#1861 M-1).
17
- */
18
- export declare function createPersonalAccessClient(): Promise<Result<string, CreatePersonalAccessClientError>>;
19
- export declare interface CreatePersonalAccessClientError {
20
- code: 'already-exists'
21
- message: string
22
- }
@@ -1,29 +0,0 @@
1
- /**
2
- * Check if a user's email is verified
3
- */
4
- export declare function isEmailVerified(user: { email_verified_at?: string | Date | null }): boolean;
5
- /**
6
- * Send a verification email to the user
7
- */
8
- export declare function sendVerificationEmail(user: { id: number, email: string, name?: string }): Promise<void>;
9
- /**
10
- * Verify a user's email with the provided token
11
- */
12
- export declare function verifyEmail(userId: number, token: string): Promise<EmailVerificationResult>;
13
- /**
14
- * Resend verification email with rate limiting
15
- */
16
- export declare function resendVerificationEmail(user: { id: number, email: string, name?: string, email_verified_at?: string | Date | null }): Promise<EmailVerificationResult>;
17
- /**
18
- * Email verification facade
19
- */
20
- export declare const EmailVerification: {
21
- isVerified: unknown;
22
- send: unknown;
23
- verify: unknown;
24
- resend: unknown
25
- };
26
- export declare interface EmailVerificationResult {
27
- success: boolean
28
- message?: string
29
- }
package/dist/gate.d.ts DELETED
@@ -1,180 +0,0 @@
1
- import type { UserModel as OrmUserModel } from '@stacksjs/orm';
2
- /**
3
- * Define a new authorization gate
4
- *
5
- * @example
6
- * define('edit-settings', (user) => user?.isAdmin)
7
- * define('update-post', (user, post) => user?.id === post.userId)
8
- */
9
- export declare function define<T = any>(ability: string, callback: GateCallback<T>): void;
10
- /**
11
- * Register a policy for a model
12
- *
13
- * @example
14
- * policy('Post', PostPolicy)
15
- * policy(Post, PostPolicy)
16
- */
17
- export declare function policy(model: string | { name: string }, policyClass: new () => Policy): void;
18
- /**
19
- * Register a callback to run before all gate checks
20
- *
21
- * @example
22
- * before((user, _ability) => {
23
- * if (user?.isSuperAdmin) return true // Super admins can do anything
24
- * return null // Continue to normal checks
25
- * })
26
- */
27
- export declare function before(callback: (user: UserModel | null, ability: string, args: any[]) => boolean | null | Promise<boolean | null>): void;
28
- /**
29
- * Register a callback to run after all gate checks
30
- */
31
- export declare function after(callback: (user: UserModel | null, ability: string, result: boolean, args: any[]) => boolean | void | Promise<boolean | void>): void;
32
- /**
33
- * Check if the user is allowed to perform an ability
34
- *
35
- * @example
36
- * if (await allows('edit-settings', user)) { ... }
37
- * if (await allows('update', user, post)) { ... }
38
- */
39
- export declare function allows(ability: string, user: UserModel | null, ...args: any[]): Promise<boolean>;
40
- /**
41
- * Check if the user is denied from performing an ability
42
- *
43
- * @example
44
- * if (await denies('delete', user, post)) { ... }
45
- */
46
- export declare function denies(ability: string, user: UserModel | null, ...args: any[]): Promise<boolean>;
47
- /**
48
- * Check if the user can perform an ability (alias for allows)
49
- */
50
- export declare function can(ability: string, user: UserModel | null, ...args: any[]): Promise<boolean>;
51
- /**
52
- * Check if the user cannot perform an ability (alias for denies)
53
- */
54
- export declare function cannot(ability: string, user: UserModel | null, ...args: any[]): Promise<boolean>;
55
- /**
56
- * Check if the user can perform any of the given abilities
57
- *
58
- * @example
59
- * if (await any(['update', 'delete'], user, post)) { ... }
60
- */
61
- export declare function any(abilities: string[], user: UserModel | null, ...args: any[]): Promise<boolean>;
62
- /**
63
- * Check if the user can perform all of the given abilities
64
- *
65
- * @example
66
- * if (await all(['view', 'update'], user, post)) { ... }
67
- */
68
- export declare function all(abilities: string[], user: UserModel | null, ...args: any[]): Promise<boolean>;
69
- /**
70
- * Check if the user can perform none of the given abilities
71
- */
72
- export declare function none(abilities: string[], user: UserModel | null, ...args: any[]): Promise<boolean>;
73
- /**
74
- * Authorize an ability or throw an exception
75
- *
76
- * @example
77
- * await authorize('update', user, post) // Throws if not allowed
78
- */
79
- export declare function authorize(ability: string, user: UserModel | null, ...args: any[]): Promise<AuthorizationResponse>;
80
- /**
81
- * Get detailed inspection result for an ability check
82
- */
83
- export declare function inspect(ability: string, user: UserModel | null, ...args: any[]): Promise<AuthorizationResponse>;
84
- /**
85
- * Get a policy instance for a model
86
- */
87
- export declare function getPolicyFor<T = any>(model: T): Policy<T> | null;
88
- /**
89
- * Check if a gate is defined
90
- */
91
- export declare function has(ability: string): boolean;
92
- /**
93
- * Check if a policy is registered for a model
94
- */
95
- export declare function hasPolicy(model: string | { name: string }): boolean;
96
- /**
97
- * Get all defined gate names
98
- */
99
- export declare function abilities(): string[];
100
- /**
101
- * Clear all gates and policies (useful for testing)
102
- */
103
- export declare function flush(): void;
104
- /**
105
- * Gate facade for convenient access
106
- */
107
- export declare const Gate: {
108
- define: typeof define;
109
- policy: typeof policy;
110
- before: typeof before;
111
- after: typeof after;
112
- allows: typeof allows;
113
- denies: typeof denies;
114
- can: typeof can;
115
- cannot: typeof cannot;
116
- any: typeof any;
117
- all: typeof all;
118
- none: typeof none;
119
- authorize: typeof authorize;
120
- inspect: typeof inspect;
121
- has: typeof has;
122
- hasPolicy: typeof hasPolicy;
123
- abilities: typeof abilities;
124
- getPolicyFor: typeof getPolicyFor;
125
- flush: typeof flush;
126
- AuthorizationResponse: typeof AuthorizationResponse;
127
- AuthorizationException: typeof AuthorizationException
128
- };
129
- /**
130
- * Policy class interface
131
- */
132
- export declare interface Policy<T = any> {
133
- before?(user: UserModel | null, ability: string): boolean | null | Promise<boolean | null>
134
- viewAny?(user: UserModel | null): boolean | Promise<boolean> | AuthorizationResponse
135
- view?(user: UserModel | null, model: T): boolean | Promise<boolean> | AuthorizationResponse
136
- create?(user: UserModel | null): boolean | Promise<boolean> | AuthorizationResponse
137
- update?(user: UserModel | null, model: T): boolean | Promise<boolean> | AuthorizationResponse
138
- delete?(user: UserModel | null, model: T): boolean | Promise<boolean> | AuthorizationResponse
139
- restore?(user: UserModel | null, model: T): boolean | Promise<boolean> | AuthorizationResponse
140
- forceDelete?(user: UserModel | null, model: T): boolean | Promise<boolean> | AuthorizationResponse
141
- [key: string]: PolicyMethod | undefined
142
- }
143
- // Alias the ORM-derived UserModel under the name this module uses internally.
144
- // The gate API receives authenticated user objects (rows / instances),
145
- // not the User class constructor.
146
- declare type UserModel = OrmUserModel;
147
- /**
148
- * Gate callback function type
149
- */
150
- export type GateCallback<T = any> = (_user: UserModel | null, ..._args: T[]) => boolean | Promise<boolean> | AuthorizationResponse;
151
- /**
152
- * Policy method type. The return type intentionally allows `null` so that
153
- * a policy's `before()` hook (which returns `null` to delegate to the
154
- * underlying ability check) is index-compatible with the catch-all
155
- * `[key: string]: PolicyMethod | undefined` signature on `Policy`.
156
- */
157
- export type PolicyMethod<T = any> = (_user: UserModel | null, _model?: T, ..._args: any[]) => boolean | null | Promise<boolean | null> | AuthorizationResponse;
158
- /**
159
- * Authorization response for detailed allow/deny
160
- */
161
- export declare class AuthorizationResponse {
162
- readonly isAllowed: boolean;
163
- readonly message?: string;
164
- readonly code?: string;
165
- constructor(allowed: boolean, message?: string, code?: string);
166
- static allow(message?: string): AuthorizationResponse;
167
- static deny(message?: string, code?: string): AuthorizationResponse;
168
- allowed(): boolean;
169
- denied(): boolean;
170
- authorize(): void;
171
- }
172
- /**
173
- * Authorization exception
174
- */
175
- export declare class AuthorizationException extends Error {
176
- public readonly code?: string;
177
- public readonly status?: number;
178
- constructor(message?: string, code?: string, status?: number);
179
- }
180
- export default Gate;
package/dist/index.d.ts DELETED
@@ -1,36 +0,0 @@
1
- export type { SeedDefaultRolesResult } from './rbac-seed';
2
- export * from './authentication';
3
- export * from './authenticator';
4
- export * from './client';
5
- export * from './middleware';
6
- export * from './rate-limiter';
7
- // WebAuthn/Passkey support (now using ts-auth - no external dependencies)
8
- export * from './passkey';
9
- export * from './password/reset';
10
- export * from './register';
11
- export * from './user';
12
- // Token management (Laravel Passport-style)
13
- export * from './tokens';
14
- // Authorization Gates & Policies (Laravel-style)
15
- export * from './gate';
16
- export * from './policy';
17
- export * from './authorizable';
18
- // Role-Based Access Control (RBAC)
19
- export * from './rbac';
20
- export { createBqbRbacStore } from './rbac-store-bqb';
21
- export { DEFAULT_ROLE_PACKS, seedDefaultRoles } from './rbac-seed';
22
- // Email Verification
23
- export * from './email-verification';
24
- // Session-based Authentication (SPA Cookie Auth)
25
- export * from './session-auth';
26
- // TOTP (Two-Factor Authentication) - re-export from ts-auth
27
- export {
28
- generateTOTP,
29
- verifyTOTP,
30
- generateTOTPSecret,
31
- totpKeyUri,
32
- } from '@stacksjs/ts-auth';
33
- // TOTP setup/enable/disable + login-challenge persistence
34
- export * from './two-factor';
35
- // Team resolution from auth credentials (dashboard-form scoping)
36
- export * from './team';