authmi 1.0.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 +21 -0
- package/README.md +425 -0
- package/dist/chunk-UNKK6FP4.mjs +609 -0
- package/dist/client-D6JiIhlU.d.mts +332 -0
- package/dist/client-D6JiIhlU.d.ts +332 -0
- package/dist/index.d.mts +31 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +609 -0
- package/dist/index.mjs +18 -0
- package/dist/next.d.mts +109 -0
- package/dist/next.d.ts +109 -0
- package/dist/next.js +5148 -0
- package/dist/next.mjs +4545 -0
- package/dist/react.d.mts +68 -0
- package/dist/react.d.ts +68 -0
- package/dist/react.js +812 -0
- package/dist/react.mjs +220 -0
- package/package.json +97 -0
package/dist/next.d.mts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
3
|
+
import { b as AuthMiConfig } from './client-D6JiIhlU.mjs';
|
|
4
|
+
export { A as ApiKey, a as AuthMiClient, c as AuthMiError, d as AuthToken, C as CreateApiKeyRequest, e as CreateApiKeyResponse, f as CreateScopeRequest, g as CreateUserRequest, I as IntrospectRequest, h as IntrospectResponse, L as LocalStorageTokenStorage, i as LoginCredentials, M as MemoryTokenStorage, O as OAuthCallbackResult, j as OAuthInitiateResponse, k as OAuthProvider, l as OnboardRequest, m as OnboardResponse, R as RegisterWebhookRequest, S as Scope, n as ScopeError, o as Service, T as TokenStorage, U as User, p as UserListResponse, V as ValidationResult, W as Webhook } from './client-D6JiIhlU.mjs';
|
|
5
|
+
|
|
6
|
+
interface MiddlewareOptions {
|
|
7
|
+
/** Base URL of Auth Mi service */
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
/** Service API Key */
|
|
10
|
+
apiKey: string;
|
|
11
|
+
/** Service ID */
|
|
12
|
+
serviceId: string;
|
|
13
|
+
/** Paths that require authentication */
|
|
14
|
+
protectedPaths?: string[];
|
|
15
|
+
/** Paths that require specific scopes: path -> scopes[] */
|
|
16
|
+
scopedPaths?: Record<string, string[]>;
|
|
17
|
+
/** Paths that are public (no auth required) */
|
|
18
|
+
publicPaths?: string[];
|
|
19
|
+
/** Login page path */
|
|
20
|
+
loginPath?: string;
|
|
21
|
+
/** Forbidden page path (insufficient scopes) */
|
|
22
|
+
forbiddenPath?: string;
|
|
23
|
+
}
|
|
24
|
+
interface MiddlewareContext {
|
|
25
|
+
valid: boolean;
|
|
26
|
+
userId?: string;
|
|
27
|
+
email?: string;
|
|
28
|
+
scopes?: string[];
|
|
29
|
+
error?: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Create a Next.js middleware with scope-based protection
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* // middleware.ts
|
|
36
|
+
* import { createAuthMiMiddleware } from "authmi/next";
|
|
37
|
+
*
|
|
38
|
+
* export const middleware = createAuthMiMiddleware({
|
|
39
|
+
* baseUrl: process.env.AUTHMI_URL!,
|
|
40
|
+
* apiKey: process.env.AUTHMI_API_KEY!,
|
|
41
|
+
* protectedPaths: ["/dashboard", "/api/protected"],
|
|
42
|
+
* scopedPaths: {
|
|
43
|
+
* "/api/users": ["users:read"],
|
|
44
|
+
* "/api/users/create": ["users:write"],
|
|
45
|
+
* "/api/billing": ["billing:read"],
|
|
46
|
+
* "/api/admin": ["admin"],
|
|
47
|
+
* },
|
|
48
|
+
* publicPaths: ["/", "/login", "/register"],
|
|
49
|
+
* });
|
|
50
|
+
*/
|
|
51
|
+
declare function createAuthMiMiddleware(options: MiddlewareOptions): (request: NextRequest) => Promise<NextResponse<unknown>>;
|
|
52
|
+
/**
|
|
53
|
+
* Higher-order component to protect a page with required scopes
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* // pages/admin.tsx
|
|
57
|
+
* import { withScope } from "authmi/next";
|
|
58
|
+
*
|
|
59
|
+
* function AdminPage() {
|
|
60
|
+
* return <div>Admin Only</div>;
|
|
61
|
+
* }
|
|
62
|
+
*
|
|
63
|
+
* export default withScope(AdminPage, ["admin"], {
|
|
64
|
+
* baseUrl: process.env.NEXT_PUBLIC_AUTHMI_URL!,
|
|
65
|
+
* apiKey: process.env.NEXT_PUBLIC_AUTHMI_API_KEY!,
|
|
66
|
+
* });
|
|
67
|
+
*/
|
|
68
|
+
declare function withScope<P extends object>(Component: React.ComponentType<P>, _requiredScopes: string[], _config: AuthMiConfig): (props: P) => react_jsx_runtime.JSX.Element;
|
|
69
|
+
/**
|
|
70
|
+
* Server-side helper to validate scopes in API routes
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* // app/api/users/route.ts
|
|
74
|
+
* import { validateScopes } from "authmi/next";
|
|
75
|
+
*
|
|
76
|
+
* export async function GET(request: Request) {
|
|
77
|
+
* const result = await validateScopes(request, {
|
|
78
|
+
* baseUrl: process.env.AUTHMI_URL!,
|
|
79
|
+
* apiKey: process.env.AUTHMI_API_KEY!,
|
|
80
|
+
* requiredScopes: ["users:read"],
|
|
81
|
+
* });
|
|
82
|
+
*
|
|
83
|
+
* if (!result.valid) {
|
|
84
|
+
* return new Response(JSON.stringify({ error: result.error }), {
|
|
85
|
+
* status: result.status || 401,
|
|
86
|
+
* });
|
|
87
|
+
* }
|
|
88
|
+
*
|
|
89
|
+
* // Proceed with the request
|
|
90
|
+
* return Response.json({ users: [] });
|
|
91
|
+
* }
|
|
92
|
+
*/
|
|
93
|
+
declare function validateScopes(request: Request, options: {
|
|
94
|
+
baseUrl: string;
|
|
95
|
+
apiKey: string;
|
|
96
|
+
serviceId: string;
|
|
97
|
+
requiredScopes: string[];
|
|
98
|
+
}): Promise<{
|
|
99
|
+
valid: true;
|
|
100
|
+
userId: string;
|
|
101
|
+
email: string;
|
|
102
|
+
scopes: string[];
|
|
103
|
+
} | {
|
|
104
|
+
valid: false;
|
|
105
|
+
error: string;
|
|
106
|
+
status: number;
|
|
107
|
+
}>;
|
|
108
|
+
|
|
109
|
+
export { AuthMiConfig, type MiddlewareContext, type MiddlewareOptions, createAuthMiMiddleware, validateScopes, withScope };
|
package/dist/next.d.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
3
|
+
import { b as AuthMiConfig } from './client-D6JiIhlU.js';
|
|
4
|
+
export { A as ApiKey, a as AuthMiClient, c as AuthMiError, d as AuthToken, C as CreateApiKeyRequest, e as CreateApiKeyResponse, f as CreateScopeRequest, g as CreateUserRequest, I as IntrospectRequest, h as IntrospectResponse, L as LocalStorageTokenStorage, i as LoginCredentials, M as MemoryTokenStorage, O as OAuthCallbackResult, j as OAuthInitiateResponse, k as OAuthProvider, l as OnboardRequest, m as OnboardResponse, R as RegisterWebhookRequest, S as Scope, n as ScopeError, o as Service, T as TokenStorage, U as User, p as UserListResponse, V as ValidationResult, W as Webhook } from './client-D6JiIhlU.js';
|
|
5
|
+
|
|
6
|
+
interface MiddlewareOptions {
|
|
7
|
+
/** Base URL of Auth Mi service */
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
/** Service API Key */
|
|
10
|
+
apiKey: string;
|
|
11
|
+
/** Service ID */
|
|
12
|
+
serviceId: string;
|
|
13
|
+
/** Paths that require authentication */
|
|
14
|
+
protectedPaths?: string[];
|
|
15
|
+
/** Paths that require specific scopes: path -> scopes[] */
|
|
16
|
+
scopedPaths?: Record<string, string[]>;
|
|
17
|
+
/** Paths that are public (no auth required) */
|
|
18
|
+
publicPaths?: string[];
|
|
19
|
+
/** Login page path */
|
|
20
|
+
loginPath?: string;
|
|
21
|
+
/** Forbidden page path (insufficient scopes) */
|
|
22
|
+
forbiddenPath?: string;
|
|
23
|
+
}
|
|
24
|
+
interface MiddlewareContext {
|
|
25
|
+
valid: boolean;
|
|
26
|
+
userId?: string;
|
|
27
|
+
email?: string;
|
|
28
|
+
scopes?: string[];
|
|
29
|
+
error?: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Create a Next.js middleware with scope-based protection
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* // middleware.ts
|
|
36
|
+
* import { createAuthMiMiddleware } from "authmi/next";
|
|
37
|
+
*
|
|
38
|
+
* export const middleware = createAuthMiMiddleware({
|
|
39
|
+
* baseUrl: process.env.AUTHMI_URL!,
|
|
40
|
+
* apiKey: process.env.AUTHMI_API_KEY!,
|
|
41
|
+
* protectedPaths: ["/dashboard", "/api/protected"],
|
|
42
|
+
* scopedPaths: {
|
|
43
|
+
* "/api/users": ["users:read"],
|
|
44
|
+
* "/api/users/create": ["users:write"],
|
|
45
|
+
* "/api/billing": ["billing:read"],
|
|
46
|
+
* "/api/admin": ["admin"],
|
|
47
|
+
* },
|
|
48
|
+
* publicPaths: ["/", "/login", "/register"],
|
|
49
|
+
* });
|
|
50
|
+
*/
|
|
51
|
+
declare function createAuthMiMiddleware(options: MiddlewareOptions): (request: NextRequest) => Promise<NextResponse<unknown>>;
|
|
52
|
+
/**
|
|
53
|
+
* Higher-order component to protect a page with required scopes
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* // pages/admin.tsx
|
|
57
|
+
* import { withScope } from "authmi/next";
|
|
58
|
+
*
|
|
59
|
+
* function AdminPage() {
|
|
60
|
+
* return <div>Admin Only</div>;
|
|
61
|
+
* }
|
|
62
|
+
*
|
|
63
|
+
* export default withScope(AdminPage, ["admin"], {
|
|
64
|
+
* baseUrl: process.env.NEXT_PUBLIC_AUTHMI_URL!,
|
|
65
|
+
* apiKey: process.env.NEXT_PUBLIC_AUTHMI_API_KEY!,
|
|
66
|
+
* });
|
|
67
|
+
*/
|
|
68
|
+
declare function withScope<P extends object>(Component: React.ComponentType<P>, _requiredScopes: string[], _config: AuthMiConfig): (props: P) => react_jsx_runtime.JSX.Element;
|
|
69
|
+
/**
|
|
70
|
+
* Server-side helper to validate scopes in API routes
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* // app/api/users/route.ts
|
|
74
|
+
* import { validateScopes } from "authmi/next";
|
|
75
|
+
*
|
|
76
|
+
* export async function GET(request: Request) {
|
|
77
|
+
* const result = await validateScopes(request, {
|
|
78
|
+
* baseUrl: process.env.AUTHMI_URL!,
|
|
79
|
+
* apiKey: process.env.AUTHMI_API_KEY!,
|
|
80
|
+
* requiredScopes: ["users:read"],
|
|
81
|
+
* });
|
|
82
|
+
*
|
|
83
|
+
* if (!result.valid) {
|
|
84
|
+
* return new Response(JSON.stringify({ error: result.error }), {
|
|
85
|
+
* status: result.status || 401,
|
|
86
|
+
* });
|
|
87
|
+
* }
|
|
88
|
+
*
|
|
89
|
+
* // Proceed with the request
|
|
90
|
+
* return Response.json({ users: [] });
|
|
91
|
+
* }
|
|
92
|
+
*/
|
|
93
|
+
declare function validateScopes(request: Request, options: {
|
|
94
|
+
baseUrl: string;
|
|
95
|
+
apiKey: string;
|
|
96
|
+
serviceId: string;
|
|
97
|
+
requiredScopes: string[];
|
|
98
|
+
}): Promise<{
|
|
99
|
+
valid: true;
|
|
100
|
+
userId: string;
|
|
101
|
+
email: string;
|
|
102
|
+
scopes: string[];
|
|
103
|
+
} | {
|
|
104
|
+
valid: false;
|
|
105
|
+
error: string;
|
|
106
|
+
status: number;
|
|
107
|
+
}>;
|
|
108
|
+
|
|
109
|
+
export { AuthMiConfig, type MiddlewareContext, type MiddlewareOptions, createAuthMiMiddleware, validateScopes, withScope };
|