@oauth42/next 0.4.8 → 0.5.1
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/README.md +164 -2
- package/dist/build-manifest.json +59 -0
- package/dist/client/index.d.mts +15 -6
- package/dist/client/index.d.ts +15 -6
- package/dist/client/index.js +23 -2
- package/dist/client/index.js.map +1 -1
- package/dist/client/index.mjs +23 -2
- package/dist/client/index.mjs.map +1 -1
- package/dist/index-BuZTiUds.d.mts +144 -0
- package/dist/index-BuZTiUds.d.ts +144 -0
- package/dist/index.d.mts +20 -2
- package/dist/index.d.ts +20 -2
- package/dist/index.js +617 -104
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +613 -103
- package/dist/index.mjs.map +1 -1
- package/dist/middleware/index.d.mts +3 -39
- package/dist/middleware/index.d.ts +3 -39
- package/dist/middleware/index.js +283 -34
- package/dist/middleware/index.js.map +1 -1
- package/dist/middleware/index.mjs +284 -34
- package/dist/middleware/index.mjs.map +1 -1
- package/dist/{auth-BThRQMzh.d.ts → par-Cyu6M1e6.d.mts} +36 -11
- package/dist/{auth-BThRQMzh.d.mts → par-DQNt8j-s.d.ts} +36 -11
- package/dist/server/index.d.mts +7 -3
- package/dist/server/index.d.ts +7 -3
- package/dist/server/index.js +657 -123
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs +650 -122
- package/dist/server/index.mjs.map +1 -1
- package/package.json +14 -5
- package/src/types/index.ts +2 -0
- package/src/types/next-auth.d.ts +2 -0
package/dist/client/index.mjs
CHANGED
|
@@ -186,7 +186,7 @@ function generateState() {
|
|
|
186
186
|
crypto.getRandomValues(array);
|
|
187
187
|
return Array.from(array, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
188
188
|
}
|
|
189
|
-
function redirectToHostedAuth(options) {
|
|
189
|
+
async function redirectToHostedAuth(options) {
|
|
190
190
|
const {
|
|
191
191
|
clientId,
|
|
192
192
|
redirectUri,
|
|
@@ -194,10 +194,22 @@ function redirectToHostedAuth(options) {
|
|
|
194
194
|
state = generateState(),
|
|
195
195
|
issuer = "https://api.oauth42.com"
|
|
196
196
|
} = options;
|
|
197
|
+
if (typeof window === "undefined") return;
|
|
198
|
+
if (state.length < 32) throw new Error("OAuth state must contain at least 32 random characters");
|
|
199
|
+
const verifier = generateState();
|
|
200
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(verifier));
|
|
201
|
+
const challenge = btoa(String.fromCharCode(...new Uint8Array(digest))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
202
|
+
const secure = window.location.protocol === "https:";
|
|
203
|
+
const prefix = secure ? "__Host-" : "";
|
|
204
|
+
const attributes = `; Path=/; SameSite=Lax; Max-Age=300${secure ? "; Secure" : ""}`;
|
|
205
|
+
document.cookie = `${prefix}oauth42-state=${encodeURIComponent(state)}${attributes}`;
|
|
206
|
+
document.cookie = `${prefix}oauth42-pkce=${verifier}${attributes}`;
|
|
197
207
|
const params = new URLSearchParams({
|
|
198
208
|
client_id: clientId,
|
|
199
209
|
redirect_uri: redirectUri,
|
|
200
210
|
response_type: "code",
|
|
211
|
+
code_challenge: challenge,
|
|
212
|
+
code_challenge_method: "S256",
|
|
201
213
|
scope,
|
|
202
214
|
state
|
|
203
215
|
});
|
|
@@ -253,7 +265,16 @@ async function loginWithPassword(options) {
|
|
|
253
265
|
success: false,
|
|
254
266
|
error: "mfa_required",
|
|
255
267
|
error_description: "MFA code required",
|
|
256
|
-
requires_mfa: true
|
|
268
|
+
requires_mfa: true,
|
|
269
|
+
mfa_methods: errorData?.mfa_methods,
|
|
270
|
+
email_otp_sent: errorData?.email_otp_sent,
|
|
271
|
+
email_otp_email: errorData?.email_otp_email,
|
|
272
|
+
email_otp_rate_limited: errorData?.email_otp_rate_limited,
|
|
273
|
+
phone_mfa_sent: errorData?.phone_mfa_sent,
|
|
274
|
+
phone_mfa_number: errorData?.phone_mfa_number,
|
|
275
|
+
phone_mfa_method: errorData?.phone_mfa_method,
|
|
276
|
+
phone_mfa_rate_limited: errorData?.phone_mfa_rate_limited,
|
|
277
|
+
phone_mfa_unavailable: errorData?.phone_mfa_unavailable
|
|
257
278
|
};
|
|
258
279
|
}
|
|
259
280
|
if (loginResponse.status === 401 && /invalid mfa code/i.test(desc)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/client/index.ts","../../src/client/hooks.ts","../../src/client/components.tsx","../../src/utils/hosted-auth.ts","../../src/client/auth.ts"],"sourcesContent":["// Client-side exports\n\n// Re-export commonly used next-auth/react functions\nexport { signIn, signOut, useSession, SessionProvider } from 'next-auth/react';\nexport type { Session } from 'next-auth';\nexport {\n useOAuth42Session,\n useOAuth42User,\n useOAuth42Tokens,\n useRequireAuth,\n} from './hooks';\n\nexport type {\n OAuth42Session,\n UseOAuth42SessionReturn,\n} from './hooks';\n\nexport {\n SignInButton,\n SignOutButton,\n UserProfile,\n AuthStatus,\n ProtectedComponent,\n} from './components';\n\nexport type {\n SignInButtonProps,\n SignOutButtonProps,\n UserProfileProps,\n AuthStatusProps,\n ProtectedComponentProps,\n} from './components';\n\n// Hosted auth utilities\nexport {\n redirectToHostedAuth,\n verifyState,\n DEFAULT_HOSTED_AUTH_CONFIG,\n} from '../utils/hosted-auth';\n\nexport type {\n HostedAuthOptions,\n HostedAuthConfig,\n} from '../utils/hosted-auth';\n\n// Custom authentication utilities\nexport {\n loginWithPassword,\n initiateAuthenticatorLogin,\n completeAuthenticatorLogin,\n simulateApproval,\n logout,\n logoutEverywhere,\n} from './auth';\n\nexport type {\n LoginWithPasswordOptions,\n LoginWithAuthenticatorOptions,\n AuthError,\n} from './auth';","import { useSession, signIn, signOut } from 'next-auth/react';\nimport { useCallback, useEffect, useState } from 'react';\n\nexport type OAuth42Session<E = {}> = ({\n user?: {\n email?: string | null;\n name?: string | null;\n image?: string | null;\n username?: string;\n emailVerified?: boolean;\n };\n accessToken?: string;\n idToken?: string;\n expires?: string;\n}) & E;\n\nexport interface UseOAuth42SessionReturn<E = {}> {\n session: OAuth42Session<E> | null;\n loading: boolean;\n error: Error | null;\n isAuthenticated: boolean;\n signIn: () => Promise<void>;\n signOut: () => Promise<void>;\n}\n\n/**\n * Hook to manage OAuth42 session with optional extra fields\n */\nexport function useOAuth42Session<E = {}>(): UseOAuth42SessionReturn<E> {\n const { data: session, status } = useSession();\n const [error, setError] = useState<Error | null>(null);\n \n const handleSignIn = useCallback(async () => {\n try {\n setError(null);\n await signIn('oauth42');\n } catch (err) {\n setError(err as Error);\n }\n }, []);\n \n const handleSignOut = useCallback(async () => {\n try {\n setError(null);\n await signOut();\n } catch (err) {\n setError(err as Error);\n }\n }, []);\n \n return {\n session: session as unknown as OAuth42Session<E> | null,\n loading: status === 'loading',\n error,\n isAuthenticated: status === 'authenticated',\n signIn: handleSignIn,\n signOut: handleSignOut,\n };\n}\n\n/**\n * Hook to get the current OAuth42 user\n */\nexport function useOAuth42User<E = {}>() {\n const { session, isAuthenticated } = useOAuth42Session<E>();\n \n return {\n user: isAuthenticated ? session?.user : null,\n isAuthenticated,\n };\n}\n\n/**\n * Hook to manage OAuth42 tokens\n */\nexport function useOAuth42Tokens<E = {}>() {\n const { session } = useOAuth42Session<E>();\n const [isExpired, setIsExpired] = useState(false);\n \n useEffect(() => {\n if (session?.expires) {\n const expiryTime = new Date(session.expires).getTime();\n const now = Date.now();\n setIsExpired(now >= expiryTime);\n \n // Set a timer to update expiry status\n const timeUntilExpiry = expiryTime - now;\n if (timeUntilExpiry > 0) {\n const timer = setTimeout(() => {\n setIsExpired(true);\n }, timeUntilExpiry);\n \n return () => clearTimeout(timer);\n }\n }\n }, [session?.expires]);\n \n return {\n accessToken: session?.accessToken,\n idToken: session?.idToken,\n isExpired,\n refreshToken: async () => {\n // Trigger a session refresh\n await signIn('oauth42');\n },\n };\n}\n\n/**\n * Hook for protected routes\n */\nexport function useRequireAuth(redirectTo: string = '/auth/signin') {\n const { isAuthenticated, loading } = useOAuth42Session();\n const [isRedirecting, setIsRedirecting] = useState(false);\n \n useEffect(() => {\n if (!loading && !isAuthenticated && !isRedirecting) {\n setIsRedirecting(true);\n if (typeof window !== 'undefined') {\n window.location.href = redirectTo;\n }\n }\n }, [isAuthenticated, loading, redirectTo, isRedirecting]);\n \n return {\n isAuthenticated,\n loading: loading || isRedirecting,\n };\n}\n","import React from 'react';\nimport { signIn, signOut } from 'next-auth/react';\nimport { useOAuth42Session, useOAuth42User } from './hooks';\n\nexport interface SignInButtonProps {\n children?: React.ReactNode;\n className?: string;\n callbackUrl?: string;\n onClick?: () => void;\n}\n\n/**\n * Sign in button component\n */\nexport function SignInButton({ \n children = 'Sign in with OAuth42', \n className = '',\n callbackUrl = '/',\n onClick\n}: SignInButtonProps) {\n const handleClick = async () => {\n if (onClick) onClick();\n await signIn('oauth42', { callbackUrl });\n };\n \n return (\n <button\n onClick={handleClick}\n className={className}\n type=\"button\"\n >\n {children}\n </button>\n );\n}\n\nexport interface SignOutButtonProps {\n children?: React.ReactNode;\n className?: string;\n callbackUrl?: string;\n onClick?: () => void;\n}\n\n/**\n * Sign out button component\n */\nexport function SignOutButton({ \n children = 'Sign out', \n className = '',\n callbackUrl = '/',\n onClick\n}: SignOutButtonProps) {\n const handleClick = async () => {\n if (onClick) onClick();\n await signOut({ callbackUrl });\n };\n \n return (\n <button\n onClick={handleClick}\n className={className}\n type=\"button\"\n >\n {children}\n </button>\n );\n}\n\nexport interface UserProfileProps {\n className?: string;\n showEmail?: boolean;\n showName?: boolean;\n showImage?: boolean;\n loadingComponent?: React.ReactNode;\n notAuthenticatedComponent?: React.ReactNode;\n}\n\n/**\n * User profile display component\n */\nexport function UserProfile({\n className = '',\n showEmail = true,\n showName = true,\n showImage = true,\n loadingComponent = <div>Loading...</div>,\n notAuthenticatedComponent = <div>Not authenticated</div>,\n}: UserProfileProps) {\n const { session, loading, isAuthenticated } = useOAuth42Session();\n \n if (loading) {\n return <>{loadingComponent}</>;\n }\n \n if (!isAuthenticated || !session?.user) {\n return <>{notAuthenticatedComponent}</>;\n }\n \n const { user } = session;\n \n return (\n <div className={className}>\n {showImage && user.image && (\n <img \n src={user.image} \n alt={user.name || 'User'} \n style={{ width: 50, height: 50, borderRadius: '50%' }}\n />\n )}\n {showName && user.name && <div>{user.name}</div>}\n {showEmail && user.email && <div>{user.email}</div>}\n </div>\n );\n}\n\nexport interface AuthStatusProps {\n authenticatedComponent?: React.ReactNode;\n unauthenticatedComponent?: React.ReactNode;\n loadingComponent?: React.ReactNode;\n}\n\n/**\n * Conditional rendering based on auth status\n */\nexport function AuthStatus({\n authenticatedComponent,\n unauthenticatedComponent,\n loadingComponent = <div>Loading...</div>,\n}: AuthStatusProps) {\n const { isAuthenticated, loading } = useOAuth42Session();\n \n if (loading) {\n return <>{loadingComponent}</>;\n }\n \n return <>{isAuthenticated ? authenticatedComponent : unauthenticatedComponent}</>;\n}\n\nexport interface ProtectedComponentProps {\n children: React.ReactNode;\n fallback?: React.ReactNode;\n loadingComponent?: React.ReactNode;\n}\n\n/**\n * Wrapper component for protected content\n */\nexport function ProtectedComponent({\n children,\n fallback = <SignInButton />,\n loadingComponent = <div>Loading...</div>,\n}: ProtectedComponentProps) {\n const { isAuthenticated, loading } = useOAuth42Session();\n \n if (loading) {\n return <>{loadingComponent}</>;\n }\n \n if (!isAuthenticated) {\n return <>{fallback}</>;\n }\n \n return <>{children}</>;\n}","/**\n * Utilities for OAuth42 Hosted Authentication\n */\n\nexport interface HostedAuthOptions {\n /** OAuth2 client ID */\n clientId: string;\n /** Redirect URI after authentication */\n redirectUri: string;\n /** OAuth2 scopes (space-separated) */\n scope?: string;\n /** OAuth2 state parameter for CSRF protection */\n state?: string;\n /** Base URL for OAuth42 issuer/API (defaults to production) */\n issuer?: string;\n}\n\n/**\n * Generate a random state parameter for CSRF protection\n */\nfunction generateState(): string {\n const array = new Uint8Array(32);\n crypto.getRandomValues(array);\n return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('');\n}\n\n/**\n * Redirect to OAuth42 hosted authentication pages\n *\n * @example\n * ```ts\n * import { redirectToHostedAuth } from '@oauth42/next/client';\n *\n * function LoginButton() {\n * return (\n * <button onClick={() => redirectToHostedAuth({\n * clientId: process.env.NEXT_PUBLIC_OAUTH42_CLIENT_ID!,\n * redirectUri: `${window.location.origin}/api/auth/callback`,\n * })}>\n * Sign in with OAuth42\n * </button>\n * );\n * }\n * ```\n */\nexport function redirectToHostedAuth(options: HostedAuthOptions): void {\n const {\n clientId,\n redirectUri,\n scope = 'openid profile email',\n state = generateState(),\n issuer = 'https://api.oauth42.com',\n } = options;\n\n const params = new URLSearchParams({\n client_id: clientId,\n redirect_uri: redirectUri,\n response_type: 'code',\n scope,\n state,\n });\n\n // Redirect to the backend's authorize endpoint, which handles the OAuth flow\n // and redirects to hosted auth login if user is not authenticated\n const authUrl = `${issuer}/oauth2/authorize?${params.toString()}`;\n\n // Store state in sessionStorage for verification on callback\n if (typeof window !== 'undefined') {\n sessionStorage.setItem('oauth42_state', state);\n window.location.href = authUrl;\n }\n}\n\n/**\n * Verify state parameter on OAuth2 callback\n * Call this in your callback page to verify the state matches\n *\n * @example\n * ```ts\n * import { verifyState } from '@oauth42/next/client';\n *\n * export default function CallbackPage() {\n * const searchParams = useSearchParams();\n * const state = searchParams.get('state');\n *\n * if (!verifyState(state)) {\n * return <div>Invalid state parameter</div>;\n * }\n *\n * // Continue with token exchange...\n * }\n * ```\n */\nexport function verifyState(state: string | null): boolean {\n if (typeof window === 'undefined') return false;\n if (!state) return false;\n\n const storedState = sessionStorage.getItem('oauth42_state');\n sessionStorage.removeItem('oauth42_state');\n\n return storedState === state;\n}\n\n/**\n * Configuration for hosted authentication\n */\nexport interface HostedAuthConfig {\n /** Enable hosted authentication */\n enabled: boolean;\n /** Base URL for hosted auth pages (optional, defaults to production) */\n baseUrl?: string;\n /** Feature flags for hosted auth */\n features?: {\n /** Allow user signup */\n signup?: boolean;\n /** Allow social login */\n socialLogin?: boolean;\n /** Allow password reset */\n passwordReset?: boolean;\n };\n}\n\n/**\n * Default hosted auth configuration\n */\nexport const DEFAULT_HOSTED_AUTH_CONFIG: HostedAuthConfig = {\n enabled: true,\n baseUrl: 'https://auth.oauth42.com',\n features: {\n signup: true,\n socialLogin: false,\n passwordReset: true,\n },\n};\n","/**\n * OAuth42 Custom Authentication Utilities\n *\n * Provides functions for implementing custom login UIs in customer apps\n * while properly handling OAuth2 PKCE flows and next-auth integration.\n */\n\nimport { signIn } from 'next-auth/react';\n\n// PKCE utilities\nfunction base64URLEncode(buffer: ArrayBuffer): string {\n const bytes = new Uint8Array(buffer);\n let binary = '';\n for (let i = 0; i < bytes.byteLength; i++) {\n binary += String.fromCharCode(bytes[i]);\n }\n return btoa(binary)\n .replace(/\\+/g, '-')\n .replace(/\\//g, '_')\n .replace(/=/g, '');\n}\n\nasync function generateCodeVerifier(): Promise<string> {\n const array = new Uint8Array(32);\n crypto.getRandomValues(array);\n return base64URLEncode(array.buffer);\n}\n\nasync function generateCodeChallenge(verifier: string): Promise<string> {\n const encoder = new TextEncoder();\n const data = encoder.encode(verifier);\n const hash = await crypto.subtle.digest('SHA-256', data);\n return base64URLEncode(hash);\n}\n\nfunction generateState(): string {\n const array = new Uint8Array(32);\n crypto.getRandomValues(array);\n return base64URLEncode(array.buffer);\n}\n\nexport interface LoginWithPasswordOptions {\n email: string;\n password: string;\n mfaCode?: string;\n /**\n * URL to redirect to after successful authentication.\n * If not provided, will redirect to '/'\n */\n callbackUrl?: string;\n}\n\nexport interface LoginWithAuthenticatorOptions {\n email: string;\n sessionId: string;\n /**\n * URL to redirect to after successful authentication.\n * If not provided, will redirect to '/'\n */\n callbackUrl?: string;\n}\n\nexport interface AuthError {\n error: string;\n error_description?: string;\n requires_enrollment?: boolean;\n enrollment_token?: string;\n requires_mfa?: boolean;\n}\n\n/**\n * Authenticate with email/password and complete OAuth PKCE flow\n *\n * This function handles the full authentication flow:\n * 1. Authenticates with the backend using credentials\n * 2. Uses the access token to authorize the OAuth client\n * 3. Exchanges authorization code for tokens via next-auth\n *\n * @example\n * ```tsx\n * const result = await loginWithPassword({\n * email: 'user@example.com',\n * password: 'password123',\n * callbackUrl: '/dashboard'\n * });\n *\n * if (result.success) {\n * // User is authenticated, next-auth session is set\n * } else if (result.requires_mfa) {\n * // Prompt for MFA code and call again with mfaCode\n * }\n * ```\n */\nexport async function loginWithPassword(\n options: LoginWithPasswordOptions\n): Promise<{ success: boolean; access_token?: string } & Partial<AuthError>> {\n try {\n // Step 1: Authenticate with backend to get access token\n const loginResponse = await fetch('/api/auth/login', {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n email: options.email,\n password: options.password,\n ...(options.mfaCode ? { mfa_code: options.mfaCode } : {}),\n }),\n });\n\n if (!loginResponse.ok) {\n const errorData = await loginResponse.json().catch(() => ({}));\n const desc: string = errorData?.error_description || errorData?.message || '';\n\n // Handle special cases\n if (loginResponse.status === 403 && errorData?.requires_enrollment) {\n return {\n success: false,\n error: 'enrollment_required',\n error_description: 'MFA enrollment required',\n requires_enrollment: true,\n enrollment_token: errorData.enrollment_token,\n };\n }\n\n if (loginResponse.status === 403 && /mfa required/i.test(desc)) {\n return {\n success: false,\n error: 'mfa_required',\n error_description: 'MFA code required',\n requires_mfa: true,\n };\n }\n\n if (loginResponse.status === 401 && /invalid mfa code/i.test(desc)) {\n return {\n success: false,\n error: 'invalid_mfa_code',\n error_description: 'Invalid MFA code',\n requires_mfa: true,\n };\n }\n\n return {\n success: false,\n error: 'authentication_failed',\n error_description: desc || 'Invalid credentials',\n };\n }\n\n const { access_token } = await loginResponse.json();\n\n // Return access token for the application to use in OAuth authorize flow\n return { success: true, access_token };\n } catch (error) {\n console.error('Login error:', error);\n return {\n success: false,\n error: 'network_error',\n error_description: 'Failed to connect to authentication server',\n };\n }\n}\n\n/**\n * Initiate authenticator (passwordless) login and return challenge code\n *\n * @example\n * ```tsx\n * const result = await initiateAuthenticatorLogin({\n * email: 'user@example.com',\n * sessionId: crypto.randomUUID()\n * });\n *\n * if (result.success) {\n * // Display result.challengeCode to user\n * // Wait for WebSocket approval or poll for completion\n * }\n * ```\n */\nexport async function initiateAuthenticatorLogin(\n options: LoginWithAuthenticatorOptions\n): Promise<{\n success: boolean;\n challengeCode?: string;\n challengeId?: string;\n} & Partial<AuthError>> {\n try {\n const response = await fetch('/api/auth/passwordless/initiate', {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n email: options.email,\n session_id: options.sessionId,\n }),\n });\n\n if (!response.ok) {\n const errorData = await response.json().catch(() => ({}));\n return {\n success: false,\n error: 'initiation_failed',\n error_description: errorData.error_description || 'Failed to create challenge',\n };\n }\n\n const data = await response.json();\n\n return {\n success: true,\n challengeCode: data.challenge_code,\n challengeId: data.challenge_id,\n };\n } catch (error) {\n console.error('Authenticator login error:', error);\n return {\n success: false,\n error: 'network_error',\n error_description: 'Failed to connect to authentication server',\n };\n }\n}\n\n/**\n * Complete authenticator login after approval\n *\n * Call this after receiving approval notification via WebSocket\n * or after the user has approved on their mobile device.\n * Returns the access token for the application to use in OAuth authorize flow.\n *\n * @param accessToken - Access token received from approval payload\n */\nexport async function completeAuthenticatorLogin(\n accessToken: string\n): Promise<{ success: boolean; access_token: string }> {\n return { success: true, access_token: accessToken };\n}\n\n/**\n * Simulate approval for testing (development only)\n *\n * @param challengeId - The challenge ID to approve\n * @param selectedCode - The code that was displayed to the user\n */\nexport async function simulateApproval(\n challengeId: string,\n selectedCode: string\n): Promise<{ success: boolean }> {\n try {\n await fetch('/api/auth/challenge/approve', {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n challenge_id: challengeId,\n selected_code: selectedCode,\n }),\n });\n\n return { success: true };\n } catch (error) {\n console.error('Simulated approval failed:', error);\n return { success: false };\n }\n}\n\n/**\n * Logout the current user from the app only (app-level logout)\n *\n * This clears the next-auth session for this app AND removes the app-user\n * mapping from the session registry. On next login, the user will see\n * the account picker to choose which account to use.\n *\n * For provider-level logout (sign out of all apps), use logoutEverywhere().\n *\n * @param options.callbackUrl - URL to redirect to after logout (default: '/')\n * @param options.clientId - OAuth client ID to clear from registry\n * @param options.issuer - OAuth42 issuer URL for the logout endpoint\n *\n * @example\n * ```tsx\n * import { logout } from '@oauth42/next/client';\n *\n * const handleLogout = async () => {\n * await logout({\n * callbackUrl: '/login',\n * clientId: process.env.NEXT_PUBLIC_OAUTH42_CLIENT_ID,\n * issuer: process.env.NEXT_PUBLIC_OAUTH_ISSUER || 'https://localhost:8443'\n * });\n * };\n * ```\n */\nexport async function logout(options?: {\n callbackUrl?: string;\n clientId?: string;\n issuer?: string;\n}): Promise<void> {\n // If clientId and issuer provided, call backend to clear app_user from registry\n if (options?.clientId && options?.issuer) {\n try {\n await fetch(`${options.issuer}/oauth2/logout?client_id=${encodeURIComponent(options.clientId)}`, {\n method: 'POST',\n credentials: 'include', // Include cookies (registry cookie)\n });\n } catch (err) {\n console.warn('[OAuth42] Failed to clear app user from registry:', err);\n // Continue with NextAuth signOut even if this fails\n }\n }\n\n // Import signOut from next-auth/react\n const { signOut } = await import('next-auth/react');\n\n // Clear next-auth session (app-level only)\n await signOut({\n callbackUrl: options?.callbackUrl || '/',\n redirect: true\n });\n}\n\n/**\n * Logout the current user from ALL apps using OAuth42 (provider-level logout)\n *\n * This redirects to the OAuth42 provider's logout endpoint to clear the\n * oauth42_session cookie, effectively logging the user out of all apps.\n *\n * Use this when testing fresh login flows or when the user explicitly\n * wants to sign out of everything.\n *\n * @param issuer - The OAuth42 issuer URL (e.g., 'https://localhost:8443')\n * @param callbackUrl - URL to redirect to after logout (default: current origin)\n *\n * @example\n * ```tsx\n * import { logoutEverywhere } from '@oauth42/next/client';\n *\n * const handleLogoutEverywhere = () => {\n * const issuer = process.env.NEXT_PUBLIC_OAUTH42_ISSUER || 'https://localhost:8443';\n * logoutEverywhere(issuer, '/auth/signin');\n * };\n * ```\n */\nexport async function logoutEverywhere(issuer: string, callbackUrl?: string): Promise<void> {\n const redirectUri = callbackUrl || window.location.origin;\n\n // Build the final redirect URL - backend will clear oauth42_session, then redirect\n // to NextAuth signout which will clear the app session cookie\n const backendLogoutUrl = `${issuer}/auth/logout?redirect_uri=${encodeURIComponent(redirectUri)}`;\n\n // First call NextAuth signout to clear the app session cookie\n // Then redirect to backend to clear the oauth42_session cookie\n try {\n // Get CSRF token from NextAuth\n const csrfResponse = await fetch('/api/auth/csrf');\n const { csrfToken } = await csrfResponse.json();\n\n // Call NextAuth signOut endpoint to clear session - wait for it to complete\n const signoutResponse = await fetch('/api/auth/signout', {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/x-www-form-urlencoded',\n },\n body: new URLSearchParams({\n csrfToken,\n json: 'true',\n }),\n credentials: 'include',\n });\n\n // Log response for debugging\n console.log('[OAuth42] NextAuth signout response:', signoutResponse.status);\n } catch (err) {\n console.warn('[OAuth42] Failed to call NextAuth signout:', err);\n }\n\n // Now redirect to OAuth42 provider logout endpoint\n // This clears the oauth42_session cookie and redirects back to the app\n console.log('[OAuth42] Redirecting to backend logout:', backendLogoutUrl);\n window.location.href = backendLogoutUrl;\n}\n"],"mappings":";AAGA,SAAS,UAAAA,SAAQ,WAAAC,UAAS,cAAAC,aAAY,uBAAuB;;;ACH7D,SAAS,YAAY,QAAQ,eAAe;AAC5C,SAAS,aAAa,WAAW,gBAAgB;AA2B1C,SAAS,oBAAwD;AACtE,QAAM,EAAE,MAAM,SAAS,OAAO,IAAI,WAAW;AAC7C,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAuB,IAAI;AAErD,QAAM,eAAe,YAAY,YAAY;AAC3C,QAAI;AACF,eAAS,IAAI;AACb,YAAM,OAAO,SAAS;AAAA,IACxB,SAAS,KAAK;AACZ,eAAS,GAAY;AAAA,IACvB;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,QAAM,gBAAgB,YAAY,YAAY;AAC5C,QAAI;AACF,eAAS,IAAI;AACb,YAAM,QAAQ;AAAA,IAChB,SAAS,KAAK;AACZ,eAAS,GAAY;AAAA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B,WAAW,CAAC;AAI9F,MAAI;AAEF,UAAM,eAAe,MAAM,MAAM,gBAAgB;AACjD,UAAM,EAAE,UAAU,IAAI,MAAM,aAAa,KAAK;AAG9C,UAAM,kBAAkB,MAAM,MAAM,qBAAqB;AAAA,MACvD,QAAQ;AAAA,MACR,SAAS;AAAA,QACP,gBAAgB;AAAA,MAClB;AAAA,MACA,MAAM,IAAI,gBAAgB;AAAA,QACxB;AAAA,QACA,MAAM;AAAA,MACR,CAAC;AAAA,MACD,aAAa;AAAA,IACf,CAAC;AAGD,YAAQ,IAAI,wCAAwC,gBAAgB,MAAM;AAAA,EAC5E,SAAS,KAAK;AACZ,YAAQ,KAAK,8CAA8C,GAAG;AAAA,EAChE;AAIA,UAAQ,IAAI,4CAA4C,gBAAgB;AACxE,SAAO,SAAS,OAAO;AACzB;","names":["signIn","signOut","useSession","signIn","signOut","signIn","signOut","signOut"]}
|
|
1
|
+
{"version":3,"sources":["../../src/client/index.ts","../../src/client/hooks.ts","../../src/client/components.tsx","../../src/utils/hosted-auth.ts","../../src/client/auth.ts"],"sourcesContent":["// Client-side exports\n\n// Re-export commonly used next-auth/react functions\nexport { signIn, signOut, useSession, SessionProvider } from 'next-auth/react';\nexport type { Session } from 'next-auth';\nexport {\n useOAuth42Session,\n useOAuth42User,\n useOAuth42Tokens,\n useRequireAuth,\n} from './hooks';\n\nexport type {\n OAuth42Session,\n UseOAuth42SessionReturn,\n} from './hooks';\n\nexport {\n SignInButton,\n SignOutButton,\n UserProfile,\n AuthStatus,\n ProtectedComponent,\n} from './components';\n\nexport type {\n SignInButtonProps,\n SignOutButtonProps,\n UserProfileProps,\n AuthStatusProps,\n ProtectedComponentProps,\n} from './components';\n\n// Hosted auth utilities\nexport {\n redirectToHostedAuth,\n verifyState,\n DEFAULT_HOSTED_AUTH_CONFIG,\n} from '../utils/hosted-auth';\n\nexport type {\n HostedAuthOptions,\n HostedAuthConfig,\n} from '../utils/hosted-auth';\n\n// Custom authentication utilities\nexport {\n loginWithPassword,\n initiateAuthenticatorLogin,\n completeAuthenticatorLogin,\n simulateApproval,\n logout,\n logoutEverywhere,\n} from './auth';\n\nexport type {\n LoginWithPasswordOptions,\n LoginWithAuthenticatorOptions,\n AuthError,\n} from './auth';","import { useSession, signIn, signOut } from 'next-auth/react';\nimport { useCallback, useEffect, useState } from 'react';\n\nexport type OAuth42Session<E extends object = Record<string, unknown>> = ({\n user?: {\n email?: string | null;\n name?: string | null;\n image?: string | null;\n username?: string;\n emailVerified?: boolean;\n };\n accessToken?: string;\n idToken?: string;\n expires?: string;\n}) & E;\n\nexport interface UseOAuth42SessionReturn<E extends object = Record<string, unknown>> {\n session: OAuth42Session<E> | null;\n loading: boolean;\n error: Error | null;\n isAuthenticated: boolean;\n signIn: () => Promise<void>;\n signOut: () => Promise<void>;\n}\n\n/**\n * Hook to manage OAuth42 session with optional extra fields\n */\nexport function useOAuth42Session<E extends object = Record<string, unknown>>(): UseOAuth42SessionReturn<E> {\n const { data: session, status } = useSession();\n const [error, setError] = useState<Error | null>(null);\n \n const handleSignIn = useCallback(async () => {\n try {\n setError(null);\n await signIn('oauth42');\n } catch (err) {\n setError(err as Error);\n }\n }, []);\n \n const handleSignOut = useCallback(async () => {\n try {\n setError(null);\n await signOut();\n } catch (err) {\n setError(err as Error);\n }\n }, []);\n \n return {\n session: session as unknown as OAuth42Session<E> | null,\n loading: status === 'loading',\n error,\n isAuthenticated: status === 'authenticated',\n signIn: handleSignIn,\n signOut: handleSignOut,\n };\n}\n\n/**\n * Hook to get the current OAuth42 user\n */\nexport function useOAuth42User<E extends object = Record<string, unknown>>() {\n const { session, isAuthenticated } = useOAuth42Session<E>();\n \n return {\n user: isAuthenticated ? session?.user : null,\n isAuthenticated,\n };\n}\n\n/**\n * Hook to manage OAuth42 tokens\n */\nexport function useOAuth42Tokens<E extends object = Record<string, unknown>>() {\n const { session } = useOAuth42Session<E>();\n const [isExpired, setIsExpired] = useState(false);\n \n useEffect(() => {\n if (session?.expires) {\n const expiryTime = new Date(session.expires).getTime();\n const now = Date.now();\n setIsExpired(now >= expiryTime);\n \n // Set a timer to update expiry status\n const timeUntilExpiry = expiryTime - now;\n if (timeUntilExpiry > 0) {\n const timer = setTimeout(() => {\n setIsExpired(true);\n }, timeUntilExpiry);\n \n return () => clearTimeout(timer);\n }\n }\n }, [session?.expires]);\n \n return {\n accessToken: session?.accessToken,\n idToken: session?.idToken,\n isExpired,\n refreshToken: async () => {\n // Trigger a session refresh\n await signIn('oauth42');\n },\n };\n}\n\n/**\n * Hook for protected routes\n */\nexport function useRequireAuth(redirectTo: string = '/auth/signin') {\n const { isAuthenticated, loading } = useOAuth42Session();\n const [isRedirecting, setIsRedirecting] = useState(false);\n \n useEffect(() => {\n if (!loading && !isAuthenticated && !isRedirecting) {\n setIsRedirecting(true);\n if (typeof window !== 'undefined') {\n window.location.href = redirectTo;\n }\n }\n }, [isAuthenticated, loading, redirectTo, isRedirecting]);\n \n return {\n isAuthenticated,\n loading: loading || isRedirecting,\n };\n}\n","import React from 'react';\nimport { signIn, signOut } from 'next-auth/react';\nimport { useOAuth42Session, useOAuth42User } from './hooks';\n\nexport interface SignInButtonProps {\n children?: React.ReactNode;\n className?: string;\n callbackUrl?: string;\n onClick?: () => void;\n}\n\n/**\n * Sign in button component\n */\nexport function SignInButton({ \n children = 'Sign in with OAuth42', \n className = '',\n callbackUrl = '/',\n onClick\n}: SignInButtonProps) {\n const handleClick = async () => {\n if (onClick) onClick();\n await signIn('oauth42', { callbackUrl });\n };\n \n return (\n <button\n onClick={handleClick}\n className={className}\n type=\"button\"\n >\n {children}\n </button>\n );\n}\n\nexport interface SignOutButtonProps {\n children?: React.ReactNode;\n className?: string;\n callbackUrl?: string;\n onClick?: () => void;\n}\n\n/**\n * Sign out button component\n */\nexport function SignOutButton({ \n children = 'Sign out', \n className = '',\n callbackUrl = '/',\n onClick\n}: SignOutButtonProps) {\n const handleClick = async () => {\n if (onClick) onClick();\n await signOut({ callbackUrl });\n };\n \n return (\n <button\n onClick={handleClick}\n className={className}\n type=\"button\"\n >\n {children}\n </button>\n );\n}\n\nexport interface UserProfileProps {\n className?: string;\n showEmail?: boolean;\n showName?: boolean;\n showImage?: boolean;\n loadingComponent?: React.ReactNode;\n notAuthenticatedComponent?: React.ReactNode;\n}\n\n/**\n * User profile display component\n */\nexport function UserProfile({\n className = '',\n showEmail = true,\n showName = true,\n showImage = true,\n loadingComponent = <div>Loading...</div>,\n notAuthenticatedComponent = <div>Not authenticated</div>,\n}: UserProfileProps) {\n const { session, loading, isAuthenticated } = useOAuth42Session();\n \n if (loading) {\n return <>{loadingComponent}</>;\n }\n \n if (!isAuthenticated || !session?.user) {\n return <>{notAuthenticatedComponent}</>;\n }\n \n const { user } = session;\n \n return (\n <div className={className}>\n {showImage && user.image && (\n <img \n src={user.image} \n alt={user.name || 'User'} \n style={{ width: 50, height: 50, borderRadius: '50%' }}\n />\n )}\n {showName && user.name && <div>{user.name}</div>}\n {showEmail && user.email && <div>{user.email}</div>}\n </div>\n );\n}\n\nexport interface AuthStatusProps {\n authenticatedComponent?: React.ReactNode;\n unauthenticatedComponent?: React.ReactNode;\n loadingComponent?: React.ReactNode;\n}\n\n/**\n * Conditional rendering based on auth status\n */\nexport function AuthStatus({\n authenticatedComponent,\n unauthenticatedComponent,\n loadingComponent = <div>Loading...</div>,\n}: AuthStatusProps) {\n const { isAuthenticated, loading } = useOAuth42Session();\n \n if (loading) {\n return <>{loadingComponent}</>;\n }\n \n return <>{isAuthenticated ? authenticatedComponent : unauthenticatedComponent}</>;\n}\n\nexport interface ProtectedComponentProps {\n children: React.ReactNode;\n fallback?: React.ReactNode;\n loadingComponent?: React.ReactNode;\n}\n\n/**\n * Wrapper component for protected content\n */\nexport function ProtectedComponent({\n children,\n fallback = <SignInButton />,\n loadingComponent = <div>Loading...</div>,\n}: ProtectedComponentProps) {\n const { isAuthenticated, loading } = useOAuth42Session();\n \n if (loading) {\n return <>{loadingComponent}</>;\n }\n \n if (!isAuthenticated) {\n return <>{fallback}</>;\n }\n \n return <>{children}</>;\n}","/**\n * Utilities for OAuth42 Hosted Authentication\n */\n\nexport interface HostedAuthOptions {\n /** OAuth2 client ID */\n clientId: string;\n /** Redirect URI after authentication */\n redirectUri: string;\n /** OAuth2 scopes (space-separated) */\n scope?: string;\n /** OAuth2 state parameter for CSRF protection */\n state?: string;\n /** Base URL for OAuth42 issuer/API (defaults to production) */\n issuer?: string;\n}\n\n/**\n * Generate a random state parameter for CSRF protection\n */\nfunction generateState(): string {\n const array = new Uint8Array(32);\n crypto.getRandomValues(array);\n return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('');\n}\n\n/**\n * Redirect to OAuth42 hosted authentication pages\n *\n * @example\n * ```ts\n * import { redirectToHostedAuth } from '@oauth42/next/client';\n *\n * function LoginButton() {\n * return (\n * <button onClick={() => redirectToHostedAuth({\n * clientId: process.env.NEXT_PUBLIC_OAUTH42_CLIENT_ID!,\n * redirectUri: `${window.location.origin}/api/auth/callback`,\n * })}>\n * Sign in with OAuth42\n * </button>\n * );\n * }\n * ```\n */\nexport async function redirectToHostedAuth(options: HostedAuthOptions): Promise<void> {\n const {\n clientId,\n redirectUri,\n scope = 'openid profile email',\n state = generateState(),\n issuer = 'https://api.oauth42.com',\n } = options;\n\n if (typeof window === 'undefined') return;\n if (state.length < 32) throw new Error('OAuth state must contain at least 32 random characters');\n const verifier = generateState();\n const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(verifier));\n const challenge = btoa(String.fromCharCode(...new Uint8Array(digest)))\n .replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '');\n const secure = window.location.protocol === 'https:';\n const prefix = secure ? '__Host-' : '';\n const attributes = `; Path=/; SameSite=Lax; Max-Age=300${secure ? '; Secure' : ''}`;\n document.cookie = `${prefix}oauth42-state=${encodeURIComponent(state)}${attributes}`;\n document.cookie = `${prefix}oauth42-pkce=${verifier}${attributes}`;\n const params = new URLSearchParams({\n client_id: clientId,\n redirect_uri: redirectUri,\n response_type: 'code',\n code_challenge: challenge,\n code_challenge_method: 'S256',\n scope,\n state,\n });\n\n // Redirect to the backend's authorize endpoint, which handles the OAuth flow\n // and redirects to hosted auth login if user is not authenticated\n const authUrl = `${issuer}/oauth2/authorize?${params.toString()}`;\n\n // Store state in sessionStorage for verification on callback\n if (typeof window !== 'undefined') {\n sessionStorage.setItem('oauth42_state', state);\n window.location.href = authUrl;\n }\n}\n\n/**\n * Verify state parameter on OAuth2 callback\n * Call this in your callback page to verify the state matches\n *\n * @example\n * ```ts\n * import { verifyState } from '@oauth42/next/client';\n *\n * export default function CallbackPage() {\n * const searchParams = useSearchParams();\n * const state = searchParams.get('state');\n *\n * if (!verifyState(state)) {\n * return <div>Invalid state parameter</div>;\n * }\n *\n * // Continue with token exchange...\n * }\n * ```\n */\nexport function verifyState(state: string | null): boolean {\n if (typeof window === 'undefined') return false;\n if (!state) return false;\n\n const storedState = sessionStorage.getItem('oauth42_state');\n sessionStorage.removeItem('oauth42_state');\n\n return storedState === state;\n}\n\n/**\n * Configuration for hosted authentication\n */\nexport interface HostedAuthConfig {\n /** Enable hosted authentication */\n enabled: boolean;\n /** Base URL for hosted auth pages (optional, defaults to production) */\n baseUrl?: string;\n /** Feature flags for hosted auth */\n features?: {\n /** Allow user signup */\n signup?: boolean;\n /** Allow social login */\n socialLogin?: boolean;\n /** Allow password reset */\n passwordReset?: boolean;\n };\n}\n\n/**\n * Default hosted auth configuration\n */\nexport const DEFAULT_HOSTED_AUTH_CONFIG: HostedAuthConfig = {\n enabled: true,\n baseUrl: 'https://auth.oauth42.com',\n features: {\n signup: true,\n socialLogin: false,\n passwordReset: true,\n },\n};\n","/**\n * OAuth42 Custom Authentication Utilities\n *\n * Provides functions for implementing custom login UIs in customer apps\n * while properly handling OAuth2 PKCE flows and next-auth integration.\n */\n\nimport { signIn } from 'next-auth/react';\n\n// PKCE utilities\nfunction base64URLEncode(buffer: ArrayBuffer): string {\n const bytes = new Uint8Array(buffer);\n let binary = '';\n for (let i = 0; i < bytes.byteLength; i++) {\n binary += String.fromCharCode(bytes[i]);\n }\n return btoa(binary)\n .replace(/\\+/g, '-')\n .replace(/\\//g, '_')\n .replace(/=/g, '');\n}\n\nasync function generateCodeVerifier(): Promise<string> {\n const array = new Uint8Array(32);\n crypto.getRandomValues(array);\n return base64URLEncode(array.buffer);\n}\n\nasync function generateCodeChallenge(verifier: string): Promise<string> {\n const encoder = new TextEncoder();\n const data = encoder.encode(verifier);\n const hash = await crypto.subtle.digest('SHA-256', data);\n return base64URLEncode(hash);\n}\n\nfunction generateState(): string {\n const array = new Uint8Array(32);\n crypto.getRandomValues(array);\n return base64URLEncode(array.buffer);\n}\n\nexport interface LoginWithPasswordOptions {\n email: string;\n password: string;\n mfaCode?: string;\n /**\n * URL to redirect to after successful authentication.\n * If not provided, will redirect to '/'\n */\n callbackUrl?: string;\n}\n\nexport interface LoginWithAuthenticatorOptions {\n email: string;\n sessionId: string;\n /**\n * URL to redirect to after successful authentication.\n * If not provided, will redirect to '/'\n */\n callbackUrl?: string;\n}\n\nexport interface AuthError {\n error: string;\n error_description?: string;\n requires_enrollment?: boolean;\n enrollment_token?: string;\n requires_mfa?: boolean;\n mfa_methods?: string[];\n email_otp_sent?: boolean;\n email_otp_email?: string;\n email_otp_rate_limited?: boolean;\n phone_mfa_sent?: boolean;\n phone_mfa_number?: string;\n phone_mfa_method?: 'sms' | 'voice';\n phone_mfa_rate_limited?: boolean;\n phone_mfa_unavailable?: boolean;\n}\n\n/**\n * Authenticate with email/password and complete OAuth PKCE flow\n *\n * This function handles the full authentication flow:\n * 1. Authenticates with the backend using credentials\n * 2. Uses the access token to authorize the OAuth client\n * 3. Exchanges authorization code for tokens via next-auth\n *\n * @example\n * ```tsx\n * const result = await loginWithPassword({\n * email: 'user@example.com',\n * password: 'password123',\n * callbackUrl: '/dashboard'\n * });\n *\n * if (result.success) {\n * // User is authenticated, next-auth session is set\n * } else if (result.requires_mfa) {\n * // Prompt for MFA code and call again with mfaCode\n * }\n * ```\n */\nexport async function loginWithPassword(\n options: LoginWithPasswordOptions\n): Promise<{ success: boolean; access_token?: string } & Partial<AuthError>> {\n try {\n // Step 1: Authenticate with backend to get access token\n const loginResponse = await fetch('/api/auth/login', {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n email: options.email,\n password: options.password,\n ...(options.mfaCode ? { mfa_code: options.mfaCode } : {}),\n }),\n });\n\n if (!loginResponse.ok) {\n const errorData = await loginResponse.json().catch(() => ({}));\n const desc: string = errorData?.error_description || errorData?.message || '';\n\n // Handle special cases\n if (loginResponse.status === 403 && errorData?.requires_enrollment) {\n return {\n success: false,\n error: 'enrollment_required',\n error_description: 'MFA enrollment required',\n requires_enrollment: true,\n enrollment_token: errorData.enrollment_token,\n };\n }\n\n if (loginResponse.status === 403 && /mfa required/i.test(desc)) {\n return {\n success: false,\n error: 'mfa_required',\n error_description: 'MFA code required',\n requires_mfa: true,\n mfa_methods: errorData?.mfa_methods,\n email_otp_sent: errorData?.email_otp_sent,\n email_otp_email: errorData?.email_otp_email,\n email_otp_rate_limited: errorData?.email_otp_rate_limited,\n phone_mfa_sent: errorData?.phone_mfa_sent,\n phone_mfa_number: errorData?.phone_mfa_number,\n phone_mfa_method: errorData?.phone_mfa_method,\n phone_mfa_rate_limited: errorData?.phone_mfa_rate_limited,\n phone_mfa_unavailable: errorData?.phone_mfa_unavailable,\n };\n }\n\n if (loginResponse.status === 401 && /invalid mfa code/i.test(desc)) {\n return {\n success: false,\n error: 'invalid_mfa_code',\n error_description: 'Invalid MFA code',\n requires_mfa: true,\n };\n }\n\n return {\n success: false,\n error: 'authentication_failed',\n error_description: desc || 'Invalid credentials',\n };\n }\n\n const { access_token } = await loginResponse.json();\n\n // Return access token for the application to use in OAuth authorize flow\n return { success: true, access_token };\n } catch (error) {\n console.error('Login error:', error);\n return {\n success: false,\n error: 'network_error',\n error_description: 'Failed to connect to authentication server',\n };\n }\n}\n\n/**\n * Initiate authenticator (passwordless) login and return challenge code\n *\n * @example\n * ```tsx\n * const result = await initiateAuthenticatorLogin({\n * email: 'user@example.com',\n * sessionId: crypto.randomUUID()\n * });\n *\n * if (result.success) {\n * // Display result.challengeCode to user\n * // Wait for WebSocket approval or poll for completion\n * }\n * ```\n */\nexport async function initiateAuthenticatorLogin(\n options: LoginWithAuthenticatorOptions\n): Promise<{\n success: boolean;\n challengeCode?: string;\n challengeId?: string;\n} & Partial<AuthError>> {\n try {\n const response = await fetch('/api/auth/passwordless/initiate', {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n email: options.email,\n session_id: options.sessionId,\n }),\n });\n\n if (!response.ok) {\n const errorData = await response.json().catch(() => ({}));\n return {\n success: false,\n error: 'initiation_failed',\n error_description: errorData.error_description || 'Failed to create challenge',\n };\n }\n\n const data = await response.json();\n\n return {\n success: true,\n challengeCode: data.challenge_code,\n challengeId: data.challenge_id,\n };\n } catch (error) {\n console.error('Authenticator login error:', error);\n return {\n success: false,\n error: 'network_error',\n error_description: 'Failed to connect to authentication server',\n };\n }\n}\n\n/**\n * Complete authenticator login after approval\n *\n * Call this after receiving approval notification via WebSocket\n * or after the user has approved on their mobile device.\n * Returns the access token for the application to use in OAuth authorize flow.\n *\n * @param accessToken - Access token received from approval payload\n */\nexport async function completeAuthenticatorLogin(\n accessToken: string\n): Promise<{ success: boolean; access_token: string }> {\n return { success: true, access_token: accessToken };\n}\n\n/**\n * Simulate approval for testing (development only)\n *\n * @param challengeId - The challenge ID to approve\n * @param selectedCode - The code that was displayed to the user\n */\nexport async function simulateApproval(\n challengeId: string,\n selectedCode: string\n): Promise<{ success: boolean }> {\n try {\n await fetch('/api/auth/challenge/approve', {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n challenge_id: challengeId,\n selected_code: selectedCode,\n }),\n });\n\n return { success: true };\n } catch (error) {\n console.error('Simulated approval failed:', error);\n return { success: false };\n }\n}\n\n/**\n * Logout the current user from the app only (app-level logout)\n *\n * This clears the next-auth session for this app AND removes the app-user\n * mapping from the session registry. On next login, the user will see\n * the account picker to choose which account to use.\n *\n * For provider-level logout (sign out of all apps), use logoutEverywhere().\n *\n * @param options.callbackUrl - URL to redirect to after logout (default: '/')\n * @param options.clientId - OAuth client ID to clear from registry\n * @param options.issuer - OAuth42 issuer URL for the logout endpoint\n *\n * @example\n * ```tsx\n * import { logout } from '@oauth42/next/client';\n *\n * const handleLogout = async () => {\n * await logout({\n * callbackUrl: '/login',\n * clientId: process.env.NEXT_PUBLIC_OAUTH42_CLIENT_ID,\n * issuer: process.env.NEXT_PUBLIC_OAUTH_ISSUER || 'https://localhost:8443'\n * });\n * };\n * ```\n */\nexport async function logout(options?: {\n callbackUrl?: string;\n clientId?: string;\n issuer?: string;\n}): Promise<void> {\n // If clientId and issuer provided, call backend to clear app_user from registry\n if (options?.clientId && options?.issuer) {\n try {\n await fetch(`${options.issuer}/oauth2/logout?client_id=${encodeURIComponent(options.clientId)}`, {\n method: 'POST',\n credentials: 'include', // Include cookies (registry cookie)\n });\n } catch (err) {\n console.warn('[OAuth42] Failed to clear app user from registry:', err);\n // Continue with NextAuth signOut even if this fails\n }\n }\n\n // Import signOut from next-auth/react\n const { signOut } = await import('next-auth/react');\n\n // Clear next-auth session (app-level only)\n await signOut({\n callbackUrl: options?.callbackUrl || '/',\n redirect: true\n });\n}\n\n/**\n * Logout the current user from ALL apps using OAuth42 (provider-level logout)\n *\n * This redirects to the OAuth42 provider's logout endpoint to clear the\n * oauth42_session cookie, effectively logging the user out of all apps.\n *\n * Use this when testing fresh login flows or when the user explicitly\n * wants to sign out of everything.\n *\n * @param issuer - The OAuth42 issuer URL (e.g., 'https://localhost:8443')\n * @param callbackUrl - URL to redirect to after logout (default: current origin)\n *\n * @example\n * ```tsx\n * import { logoutEverywhere } from '@oauth42/next/client';\n *\n * const handleLogoutEverywhere = () => {\n * const issuer = process.env.NEXT_PUBLIC_OAUTH42_ISSUER || 'https://localhost:8443';\n * logoutEverywhere(issuer, '/auth/signin');\n * };\n * ```\n */\nexport async function logoutEverywhere(issuer: string, callbackUrl?: string): Promise<void> {\n const redirectUri = callbackUrl || window.location.origin;\n\n // Build the final redirect URL - backend will clear oauth42_session, then redirect\n // to NextAuth signout which will clear the app session cookie\n const backendLogoutUrl = `${issuer}/auth/logout?redirect_uri=${encodeURIComponent(redirectUri)}`;\n\n // First call NextAuth signout to clear the app session cookie\n // Then redirect to backend to clear the oauth42_session cookie\n try {\n // Get CSRF token from NextAuth\n const csrfResponse = await fetch('/api/auth/csrf');\n const { csrfToken } = await csrfResponse.json();\n\n // Call NextAuth signOut endpoint to clear session - wait for it to complete\n const signoutResponse = await fetch('/api/auth/signout', {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/x-www-form-urlencoded',\n },\n body: new URLSearchParams({\n csrfToken,\n json: 'true',\n }),\n credentials: 'include',\n });\n\n // Log response for debugging\n console.log('[OAuth42] NextAuth signout response:', signoutResponse.status);\n } catch (err) {\n console.warn('[OAuth42] Failed to call NextAuth signout:', err);\n }\n\n // Now redirect to OAuth42 provider logout endpoint\n // This clears the oauth42_session cookie and redirects back to the app\n console.log('[OAuth42] Redirecting to backend logout:', backendLogoutUrl);\n window.location.href = backendLogoutUrl;\n}\n"],"mappings":";AAGA,SAAS,UAAAA,SAAQ,WAAAC,UAAS,cAAAC,aAAY,uBAAuB;;;ACH7D,SAAS,YAAY,QAAQ,eAAe;AAC5C,SAAS,aAAa,WAAW,gBAAgB;AA2B1C,SAAS,oBAA4F;AAC1G,QAAM,EAAE,MAAM,SAAS,OAAO,IAAI,WAAW;AAC7C,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAuB,IAAI;AAErD,QAAM,eAAe,YAAY,YAAY;AAC3C,QAAI;AACF,eAAS,IAAI;AACb,YAAM,OAAO,SAAS;AAAA,IACxB,SAAS,KAAK;AACZ,eAAS,GAAY;AAAA,IACvB;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,QAAM,gBAAgB,YAAY,YAAY;AAC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AAAA,QACA,MAAM;AAAA,MACR,CAAC;AAAA,MACD,aAAa;AAAA,IACf,CAAC;AAGD,YAAQ,IAAI,wCAAwC,gBAAgB,MAAM;AAAA,EAC5E,SAAS,KAAK;AACZ,YAAQ,KAAK,8CAA8C,GAAG;AAAA,EAChE;AAIA,UAAQ,IAAI,4CAA4C,gBAAgB;AACxE,SAAO,SAAS,OAAO;AACzB;","names":["signIn","signOut","useSession","signIn","signOut","signIn","signOut","signOut"]}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { KeyObject } from 'node:crypto';
|
|
2
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
3
|
+
|
|
4
|
+
/** A retained server-side proof key. It represents this backend, not a user's device. */
|
|
5
|
+
declare class OAuth42DPoP {
|
|
6
|
+
private readonly key;
|
|
7
|
+
private readonly jwk;
|
|
8
|
+
private readonly nonces;
|
|
9
|
+
readonly thumbprint: string;
|
|
10
|
+
constructor(key: KeyObject);
|
|
11
|
+
/** Persist keys in protected server storage; regeneration cannot recover a public refresh grant. */
|
|
12
|
+
static generate(): OAuth42DPoP;
|
|
13
|
+
/** Passed directly to openid-client so its callback keeps OIDC issuer/nonce/signature validation. */
|
|
14
|
+
get oidcKey(): KeyObject;
|
|
15
|
+
proof(method: string, target: string, nonce?: string, accessToken?: string): string;
|
|
16
|
+
assertTokenResponse(tokens: {
|
|
17
|
+
token_type?: unknown;
|
|
18
|
+
}): void;
|
|
19
|
+
fetch(target: string, init?: RequestInit, accessToken?: string, prepare?: () => RequestInit): Promise<Response>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Retained software signing key for a confidential backend. Never put this in a session. */
|
|
23
|
+
declare class OAuth42ClientAssertion {
|
|
24
|
+
#private;
|
|
25
|
+
readonly kid: string;
|
|
26
|
+
readonly thumbprint: string;
|
|
27
|
+
constructor(kid: string, key: KeyObject);
|
|
28
|
+
static generate(kid: string): OAuth42ClientAssertion;
|
|
29
|
+
get publicJwk(): Record<string, string>;
|
|
30
|
+
/** Private keys for the server-only NextAuth/openid-client configuration, never registration. */
|
|
31
|
+
get oidcJwks(): {
|
|
32
|
+
keys: {
|
|
33
|
+
kty: string;
|
|
34
|
+
crv: string;
|
|
35
|
+
x: string;
|
|
36
|
+
y: string;
|
|
37
|
+
d: string;
|
|
38
|
+
kid: string;
|
|
39
|
+
}[];
|
|
40
|
+
};
|
|
41
|
+
static validateEndpoint(target: string): void;
|
|
42
|
+
assertion(clientId: string, audience: string): string;
|
|
43
|
+
fetch(clientId: string, target: string, init: RequestInit, dpop?: OAuth42DPoP): Promise<Response>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
type SessionStoreMode = 'jwt' | 'backend';
|
|
47
|
+
interface BackendSessionUser {
|
|
48
|
+
sub?: string;
|
|
49
|
+
email?: string;
|
|
50
|
+
username?: string;
|
|
51
|
+
name?: string;
|
|
52
|
+
emailVerified?: boolean;
|
|
53
|
+
groups?: string[];
|
|
54
|
+
}
|
|
55
|
+
interface BackendSessionResponse {
|
|
56
|
+
token_type?: string;
|
|
57
|
+
session_id: string;
|
|
58
|
+
access_token: string;
|
|
59
|
+
expires_at: number;
|
|
60
|
+
id_token?: string;
|
|
61
|
+
user?: BackendSessionUser;
|
|
62
|
+
/** Wire data must pass subject/shape validation before becoming a session. */
|
|
63
|
+
user_info?: unknown;
|
|
64
|
+
}
|
|
65
|
+
interface BackendSessionConfig {
|
|
66
|
+
issuer: string;
|
|
67
|
+
clientId: string;
|
|
68
|
+
clientSecret: string;
|
|
69
|
+
dpop?: OAuth42DPoP;
|
|
70
|
+
clientAssertionKey?: OAuth42ClientAssertion;
|
|
71
|
+
}
|
|
72
|
+
declare function createBackendSession(params: BackendSessionConfig & {
|
|
73
|
+
accessToken: string;
|
|
74
|
+
refreshToken: string;
|
|
75
|
+
expiresAt?: number;
|
|
76
|
+
idToken?: string;
|
|
77
|
+
user?: BackendSessionUser;
|
|
78
|
+
}): Promise<BackendSessionResponse>;
|
|
79
|
+
declare function resolveBackendSession(params: BackendSessionConfig & {
|
|
80
|
+
sessionId: string;
|
|
81
|
+
}): Promise<BackendSessionResponse | null>;
|
|
82
|
+
declare function revokeBackendSession(params: BackendSessionConfig & {
|
|
83
|
+
sessionId: string;
|
|
84
|
+
}): Promise<void>;
|
|
85
|
+
|
|
86
|
+
interface OAuth42AuthOptions {
|
|
87
|
+
pages?: {
|
|
88
|
+
signIn?: string;
|
|
89
|
+
error?: string;
|
|
90
|
+
};
|
|
91
|
+
callbacks?: {
|
|
92
|
+
authorized?: (params: {
|
|
93
|
+
token: any;
|
|
94
|
+
req: NextRequest;
|
|
95
|
+
}) => boolean | Promise<boolean>;
|
|
96
|
+
};
|
|
97
|
+
protectedPaths?: string[];
|
|
98
|
+
publicPaths?: string[];
|
|
99
|
+
/**
|
|
100
|
+
* Cookie prefix for custom cookie names. Must match the prefix used in createAuth().
|
|
101
|
+
* E.g., 'oauth42-portal' will look for cookie 'oauth42-portal.session-token'
|
|
102
|
+
*/
|
|
103
|
+
cookiePrefix?: string;
|
|
104
|
+
/**
|
|
105
|
+
* OAuth client credentials for middleware refresh. When omitted, the middleware
|
|
106
|
+
* prefers credentials embedded in the NextAuth token by createAuth(), then
|
|
107
|
+
* falls back to OAUTH42_CLIENT_ID / OAUTH42_CLIENT_SECRET.
|
|
108
|
+
*/
|
|
109
|
+
clientId?: string;
|
|
110
|
+
clientSecret?: string;
|
|
111
|
+
issuer?: string;
|
|
112
|
+
dpop?: OAuth42DPoP;
|
|
113
|
+
clientAssertionKey?: OAuth42ClientAssertion;
|
|
114
|
+
/**
|
|
115
|
+
* "backend" keeps OAuth token material in the Rust backend and stores only
|
|
116
|
+
* an opaque backend session id in the NextAuth JWT cookie.
|
|
117
|
+
*/
|
|
118
|
+
sessionStore?: SessionStoreMode;
|
|
119
|
+
/**
|
|
120
|
+
* Session cookie lifetime. Should match createAuth({ session.maxAge }).
|
|
121
|
+
* Defaults to OAuth42's persistent-session policy of 365 days.
|
|
122
|
+
*/
|
|
123
|
+
session?: {
|
|
124
|
+
maxAge?: number;
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Middleware helper for protecting routes with OAuth42
|
|
129
|
+
*
|
|
130
|
+
* This middleware handles:
|
|
131
|
+
* 1. Route protection (redirect to login if no session)
|
|
132
|
+
* 2. Token refresh (refresh expired tokens and update cookie)
|
|
133
|
+
*/
|
|
134
|
+
declare function withOAuth42Auth(options?: OAuth42AuthOptions): (req: NextRequest) => Promise<NextResponse<unknown>>;
|
|
135
|
+
/**
|
|
136
|
+
* Helper to create middleware configuration
|
|
137
|
+
*/
|
|
138
|
+
declare function createMiddlewareConfig(protectedPaths?: string[], publicPaths?: string[]): {
|
|
139
|
+
matcher: string[];
|
|
140
|
+
protectedPaths: string[];
|
|
141
|
+
publicPaths: string[];
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export { type BackendSessionConfig as B, OAuth42DPoP as O, type SessionStoreMode as S, OAuth42ClientAssertion as a, type BackendSessionResponse as b, type BackendSessionUser as c, type OAuth42AuthOptions as d, createBackendSession as e, createMiddlewareConfig as f, revokeBackendSession as g, resolveBackendSession as r, withOAuth42Auth as w };
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { KeyObject } from 'node:crypto';
|
|
2
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
3
|
+
|
|
4
|
+
/** A retained server-side proof key. It represents this backend, not a user's device. */
|
|
5
|
+
declare class OAuth42DPoP {
|
|
6
|
+
private readonly key;
|
|
7
|
+
private readonly jwk;
|
|
8
|
+
private readonly nonces;
|
|
9
|
+
readonly thumbprint: string;
|
|
10
|
+
constructor(key: KeyObject);
|
|
11
|
+
/** Persist keys in protected server storage; regeneration cannot recover a public refresh grant. */
|
|
12
|
+
static generate(): OAuth42DPoP;
|
|
13
|
+
/** Passed directly to openid-client so its callback keeps OIDC issuer/nonce/signature validation. */
|
|
14
|
+
get oidcKey(): KeyObject;
|
|
15
|
+
proof(method: string, target: string, nonce?: string, accessToken?: string): string;
|
|
16
|
+
assertTokenResponse(tokens: {
|
|
17
|
+
token_type?: unknown;
|
|
18
|
+
}): void;
|
|
19
|
+
fetch(target: string, init?: RequestInit, accessToken?: string, prepare?: () => RequestInit): Promise<Response>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Retained software signing key for a confidential backend. Never put this in a session. */
|
|
23
|
+
declare class OAuth42ClientAssertion {
|
|
24
|
+
#private;
|
|
25
|
+
readonly kid: string;
|
|
26
|
+
readonly thumbprint: string;
|
|
27
|
+
constructor(kid: string, key: KeyObject);
|
|
28
|
+
static generate(kid: string): OAuth42ClientAssertion;
|
|
29
|
+
get publicJwk(): Record<string, string>;
|
|
30
|
+
/** Private keys for the server-only NextAuth/openid-client configuration, never registration. */
|
|
31
|
+
get oidcJwks(): {
|
|
32
|
+
keys: {
|
|
33
|
+
kty: string;
|
|
34
|
+
crv: string;
|
|
35
|
+
x: string;
|
|
36
|
+
y: string;
|
|
37
|
+
d: string;
|
|
38
|
+
kid: string;
|
|
39
|
+
}[];
|
|
40
|
+
};
|
|
41
|
+
static validateEndpoint(target: string): void;
|
|
42
|
+
assertion(clientId: string, audience: string): string;
|
|
43
|
+
fetch(clientId: string, target: string, init: RequestInit, dpop?: OAuth42DPoP): Promise<Response>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
type SessionStoreMode = 'jwt' | 'backend';
|
|
47
|
+
interface BackendSessionUser {
|
|
48
|
+
sub?: string;
|
|
49
|
+
email?: string;
|
|
50
|
+
username?: string;
|
|
51
|
+
name?: string;
|
|
52
|
+
emailVerified?: boolean;
|
|
53
|
+
groups?: string[];
|
|
54
|
+
}
|
|
55
|
+
interface BackendSessionResponse {
|
|
56
|
+
token_type?: string;
|
|
57
|
+
session_id: string;
|
|
58
|
+
access_token: string;
|
|
59
|
+
expires_at: number;
|
|
60
|
+
id_token?: string;
|
|
61
|
+
user?: BackendSessionUser;
|
|
62
|
+
/** Wire data must pass subject/shape validation before becoming a session. */
|
|
63
|
+
user_info?: unknown;
|
|
64
|
+
}
|
|
65
|
+
interface BackendSessionConfig {
|
|
66
|
+
issuer: string;
|
|
67
|
+
clientId: string;
|
|
68
|
+
clientSecret: string;
|
|
69
|
+
dpop?: OAuth42DPoP;
|
|
70
|
+
clientAssertionKey?: OAuth42ClientAssertion;
|
|
71
|
+
}
|
|
72
|
+
declare function createBackendSession(params: BackendSessionConfig & {
|
|
73
|
+
accessToken: string;
|
|
74
|
+
refreshToken: string;
|
|
75
|
+
expiresAt?: number;
|
|
76
|
+
idToken?: string;
|
|
77
|
+
user?: BackendSessionUser;
|
|
78
|
+
}): Promise<BackendSessionResponse>;
|
|
79
|
+
declare function resolveBackendSession(params: BackendSessionConfig & {
|
|
80
|
+
sessionId: string;
|
|
81
|
+
}): Promise<BackendSessionResponse | null>;
|
|
82
|
+
declare function revokeBackendSession(params: BackendSessionConfig & {
|
|
83
|
+
sessionId: string;
|
|
84
|
+
}): Promise<void>;
|
|
85
|
+
|
|
86
|
+
interface OAuth42AuthOptions {
|
|
87
|
+
pages?: {
|
|
88
|
+
signIn?: string;
|
|
89
|
+
error?: string;
|
|
90
|
+
};
|
|
91
|
+
callbacks?: {
|
|
92
|
+
authorized?: (params: {
|
|
93
|
+
token: any;
|
|
94
|
+
req: NextRequest;
|
|
95
|
+
}) => boolean | Promise<boolean>;
|
|
96
|
+
};
|
|
97
|
+
protectedPaths?: string[];
|
|
98
|
+
publicPaths?: string[];
|
|
99
|
+
/**
|
|
100
|
+
* Cookie prefix for custom cookie names. Must match the prefix used in createAuth().
|
|
101
|
+
* E.g., 'oauth42-portal' will look for cookie 'oauth42-portal.session-token'
|
|
102
|
+
*/
|
|
103
|
+
cookiePrefix?: string;
|
|
104
|
+
/**
|
|
105
|
+
* OAuth client credentials for middleware refresh. When omitted, the middleware
|
|
106
|
+
* prefers credentials embedded in the NextAuth token by createAuth(), then
|
|
107
|
+
* falls back to OAUTH42_CLIENT_ID / OAUTH42_CLIENT_SECRET.
|
|
108
|
+
*/
|
|
109
|
+
clientId?: string;
|
|
110
|
+
clientSecret?: string;
|
|
111
|
+
issuer?: string;
|
|
112
|
+
dpop?: OAuth42DPoP;
|
|
113
|
+
clientAssertionKey?: OAuth42ClientAssertion;
|
|
114
|
+
/**
|
|
115
|
+
* "backend" keeps OAuth token material in the Rust backend and stores only
|
|
116
|
+
* an opaque backend session id in the NextAuth JWT cookie.
|
|
117
|
+
*/
|
|
118
|
+
sessionStore?: SessionStoreMode;
|
|
119
|
+
/**
|
|
120
|
+
* Session cookie lifetime. Should match createAuth({ session.maxAge }).
|
|
121
|
+
* Defaults to OAuth42's persistent-session policy of 365 days.
|
|
122
|
+
*/
|
|
123
|
+
session?: {
|
|
124
|
+
maxAge?: number;
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Middleware helper for protecting routes with OAuth42
|
|
129
|
+
*
|
|
130
|
+
* This middleware handles:
|
|
131
|
+
* 1. Route protection (redirect to login if no session)
|
|
132
|
+
* 2. Token refresh (refresh expired tokens and update cookie)
|
|
133
|
+
*/
|
|
134
|
+
declare function withOAuth42Auth(options?: OAuth42AuthOptions): (req: NextRequest) => Promise<NextResponse<unknown>>;
|
|
135
|
+
/**
|
|
136
|
+
* Helper to create middleware configuration
|
|
137
|
+
*/
|
|
138
|
+
declare function createMiddlewareConfig(protectedPaths?: string[], publicPaths?: string[]): {
|
|
139
|
+
matcher: string[];
|
|
140
|
+
protectedPaths: string[];
|
|
141
|
+
publicPaths: string[];
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export { type BackendSessionConfig as B, OAuth42DPoP as O, type SessionStoreMode as S, OAuth42ClientAssertion as a, type BackendSessionResponse as b, type BackendSessionUser as c, type OAuth42AuthOptions as d, createBackendSession as e, createMiddlewareConfig as f, revokeBackendSession as g, resolveBackendSession as r, withOAuth42Auth as w };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,13 +1,31 @@
|
|
|
1
|
-
export { C as CreateAuthOptions,
|
|
2
|
-
export { OAuth42AuthOptions, createMiddlewareConfig, withOAuth42Auth } from './
|
|
1
|
+
export { C as CreateAuthOptions, f as OAuth42Profile, O as OAuth42Provider, h as OAuth42ProviderOptions, c as createAuth, g as getOAuth42Session, b as getServerSession, p as pushAuthorizationURL, r as refreshAccessToken, w as withOAuth42ServerSideProps, d as withOAuth42Session, e as withPushedAuthorization } from './par-Cyu6M1e6.mjs';
|
|
2
|
+
export { d as OAuth42AuthOptions, a as OAuth42ClientAssertion, O as OAuth42DPoP, f as createMiddlewareConfig, w as withOAuth42Auth } from './index-BuZTiUds.mjs';
|
|
3
3
|
import { DefaultSession } from 'next-auth';
|
|
4
4
|
export { Session, User } from 'next-auth';
|
|
5
5
|
import 'next-auth/providers/oauth';
|
|
6
6
|
import 'next';
|
|
7
|
+
import 'node:crypto';
|
|
7
8
|
import 'next/server';
|
|
8
9
|
|
|
10
|
+
/** Live verification binds an application session to the issuer's current user. */
|
|
11
|
+
interface VerifiedUserInfo {
|
|
12
|
+
sub: string;
|
|
13
|
+
email?: string;
|
|
14
|
+
name?: string;
|
|
15
|
+
preferred_username?: string;
|
|
16
|
+
username?: string;
|
|
17
|
+
email_verified?: boolean;
|
|
18
|
+
groups?: string[];
|
|
19
|
+
given_name?: string;
|
|
20
|
+
family_name?: string;
|
|
21
|
+
company_id?: string;
|
|
22
|
+
company_name?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
9
25
|
declare module 'next-auth' {
|
|
10
26
|
interface Session extends DefaultSession {
|
|
27
|
+
/** Current issuer profile, verified during this session request. */
|
|
28
|
+
userInfo?: VerifiedUserInfo;
|
|
11
29
|
accessToken?: string;
|
|
12
30
|
idToken?: string;
|
|
13
31
|
user?: {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,31 @@
|
|
|
1
|
-
export { C as CreateAuthOptions,
|
|
2
|
-
export { OAuth42AuthOptions, createMiddlewareConfig, withOAuth42Auth } from './
|
|
1
|
+
export { C as CreateAuthOptions, f as OAuth42Profile, O as OAuth42Provider, h as OAuth42ProviderOptions, c as createAuth, g as getOAuth42Session, b as getServerSession, p as pushAuthorizationURL, r as refreshAccessToken, w as withOAuth42ServerSideProps, d as withOAuth42Session, e as withPushedAuthorization } from './par-DQNt8j-s.js';
|
|
2
|
+
export { d as OAuth42AuthOptions, a as OAuth42ClientAssertion, O as OAuth42DPoP, f as createMiddlewareConfig, w as withOAuth42Auth } from './index-BuZTiUds.js';
|
|
3
3
|
import { DefaultSession } from 'next-auth';
|
|
4
4
|
export { Session, User } from 'next-auth';
|
|
5
5
|
import 'next-auth/providers/oauth';
|
|
6
6
|
import 'next';
|
|
7
|
+
import 'node:crypto';
|
|
7
8
|
import 'next/server';
|
|
8
9
|
|
|
10
|
+
/** Live verification binds an application session to the issuer's current user. */
|
|
11
|
+
interface VerifiedUserInfo {
|
|
12
|
+
sub: string;
|
|
13
|
+
email?: string;
|
|
14
|
+
name?: string;
|
|
15
|
+
preferred_username?: string;
|
|
16
|
+
username?: string;
|
|
17
|
+
email_verified?: boolean;
|
|
18
|
+
groups?: string[];
|
|
19
|
+
given_name?: string;
|
|
20
|
+
family_name?: string;
|
|
21
|
+
company_id?: string;
|
|
22
|
+
company_name?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
9
25
|
declare module 'next-auth' {
|
|
10
26
|
interface Session extends DefaultSession {
|
|
27
|
+
/** Current issuer profile, verified during this session request. */
|
|
28
|
+
userInfo?: VerifiedUserInfo;
|
|
11
29
|
accessToken?: string;
|
|
12
30
|
idToken?: string;
|
|
13
31
|
user?: {
|