-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathindex.ts
More file actions
115 lines (106 loc) · 3.66 KB
/
Copy pathindex.ts
File metadata and controls
115 lines (106 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { UnknownAction } from "../errors.js"
import { SessionStore } from "./utils/cookie.js"
import { init } from "./init.js"
import renderPage from "./pages/index.js"
import * as actions from "./actions/index.js"
import { validateCSRF } from "./actions/callback/oauth/csrf-token.js"
import { setLogger } from "./utils/logger.js"
import type { RequestInternal, ResponseInternal } from "../types.js"
import type { AuthConfig } from "../index.js"
import { skipCSRFCheck } from "./symbols.js"
export { customFetch, raw, skipCSRFCheck } from "./symbols.js"
/** @internal */
export async function AuthInternal(
request: RequestInternal,
authOptions: AuthConfig
): Promise<ResponseInternal> {
const { action, providerId, error, method } = request
// Handle _log action: accept client-side debug log messages.
// When debug is enabled, log the message server-side; always return 200.
if (action === "_log") {
const logger = setLogger(authOptions)
if (request.body) {
const { level, code, message: msg } = request.body
if (level === "error") {
logger.debug("client_error", { code, message: msg, ...request.body })
} else if (level === "warn") {
logger.debug("client_warn", { code, message: msg, ...request.body })
} else {
logger.debug("client_log", { message: msg, ...request.body })
}
}
return { status: 200, body: "" }
}
const csrfDisabled = authOptions.skipCSRFCheck === skipCSRFCheck
const { options, cookies } = await init({
authOptions,
action,
providerId,
url: request.url,
callbackUrl: request.body?.callbackUrl ?? request.query?.callbackUrl,
csrfToken: request.body?.csrfToken,
cookies: request.cookies,
isPost: method === "POST",
csrfDisabled,
})
const sessionStore = new SessionStore(
options.cookies.sessionToken,
request.cookies,
options.logger
)
if (method === "GET") {
const render = renderPage({ ...options, query: request.query, cookies })
switch (action) {
case "callback":
return await actions.callback(request, options, sessionStore, cookies)
case "csrf":
return render.csrf(csrfDisabled, options, cookies)
case "error":
return render.error(error)
case "providers":
return render.providers(options.providers)
case "session":
return await actions.session(options, sessionStore, cookies)
case "signin":
return render.signin(providerId, error)
case "signout":
return render.signout()
case "verify-request":
return render.verifyRequest()
case "webauthn-options":
return await actions.webAuthnOptions(
request,
options,
sessionStore,
cookies
)
default:
}
} else {
const { csrfTokenVerified } = options
switch (action) {
case "callback":
if (options.provider.type === "credentials")
// Verified CSRF Token required for credentials providers only
validateCSRF(action, csrfTokenVerified)
return await actions.callback(request, options, sessionStore, cookies)
case "session":
validateCSRF(action, csrfTokenVerified)
return await actions.session(
options,
sessionStore,
cookies,
true,
request.body?.data
)
case "signin":
validateCSRF(action, csrfTokenVerified)
return await actions.signIn(request, cookies, options)
case "signout":
validateCSRF(action, csrfTokenVerified)
return await actions.signOut(cookies, sessionStore, options)
default:
}
}
throw new UnknownAction(`Cannot handle action: ${action}`)
}