-
-
Notifications
You must be signed in to change notification settings - Fork 624
/
index.ts
159 lines (151 loc) · 5.89 KB
/
index.ts
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* @module
* Bearer Auth Middleware for Hono.
*/
import type { Context } from '../../context'
import { HTTPException } from '../../http-exception'
import type { MiddlewareHandler } from '../../types'
import { timingSafeEqual } from '../../utils/buffer'
import type { StatusCode } from '../../utils/http-status'
const TOKEN_STRINGS = '[A-Za-z0-9._~+/-]+=*'
const PREFIX = 'Bearer'
const HEADER = 'Authorization'
type MessageFunction = (c: Context) => string | object | Promise<string | object>
type BearerAuthOptions =
| {
token: string | string[]
realm?: string
prefix?: string
headerName?: string
hashFunction?: Function
noAuthenticationHeaderMessage?: string | object | MessageFunction
invalidAuthenticationHeaderMessage?: string | object | MessageFunction
invalidTokenMessage?: string | object | MessageFunction
}
| {
realm?: string
prefix?: string
headerName?: string
verifyToken: (token: string, c: Context) => boolean | Promise<boolean>
hashFunction?: Function
noAuthenticationHeaderMessage?: string | object | MessageFunction
invalidAuthenticationHeaderMessage?: string | object | MessageFunction
invalidTokenMessage?: string | object | MessageFunction
}
/**
* Bearer Auth Middleware for Hono.
*
* @see {@link https://hono.dev/docs/middleware/builtin/bearer-auth}
*
* @param {BearerAuthOptions} options - The options for the bearer authentication middleware.
* @param {string | string[]} [options.token] - The string or array of strings to validate the incoming bearer token against.
* @param {Function} [options.verifyToken] - The function to verify the token.
* @param {string} [options.realm=""] - The domain name of the realm, as part of the returned WWW-Authenticate challenge header.
* @param {string} [options.prefix="Bearer"] - The prefix (or known as `schema`) for the Authorization header value. If set to the empty string, no prefix is expected.
* @param {string} [options.headerName=Authorization] - The header name.
* @param {Function} [options.hashFunction] - A function to handle hashing for safe comparison of authentication tokens.
* @param {string | object | MessageFunction} [options.noAuthenticationHeaderMessage="Unauthorized"] - The no authentication header message.
* @param {string | object | MessageFunction} [options.invalidAuthenticationHeaderMeasage="Bad Request"] - The invalid authentication header message.
* @param {string | object | MessageFunction} [options.invalidTokenMessage="Unauthorized"] - The invalid token message.
* @returns {MiddlewareHandler} The middleware handler function.
* @throws {Error} If neither "token" nor "verifyToken" options are provided.
* @throws {HTTPException} If authentication fails, with 401 status code for missing or invalid token, or 400 status code for invalid request.
*
* @example
* ```ts
* const app = new Hono()
*
* const token = 'honoiscool'
*
* app.use('/api/*', bearerAuth({ token }))
*
* app.get('/api/page', (c) => {
* return c.json({ message: 'You are authorized' })
* })
* ```
*/
export const bearerAuth = (options: BearerAuthOptions): MiddlewareHandler => {
if (!('token' in options || 'verifyToken' in options)) {
throw new Error('bearer auth middleware requires options for "token"')
}
if (!options.realm) {
options.realm = ''
}
if (options.prefix === undefined) {
options.prefix = PREFIX
}
const realm = options.realm?.replace(/"/g, '\\"')
const prefixRegexStr = options.prefix === '' ? '' : `${options.prefix} +`
const regexp = new RegExp(`^${prefixRegexStr}(${TOKEN_STRINGS}) *$`)
const wwwAuthenticatePrefix = options.prefix === '' ? '' : `${options.prefix} `
const throwHTTPException = async (
c: Context,
status: StatusCode,
wwwAuthenticateHeader: string,
messageOption: string | object | MessageFunction
): Promise<Response> => {
const headers = {
'WWW-Authenticate': wwwAuthenticateHeader,
}
const responseMessage =
typeof messageOption === 'function' ? await messageOption(c) : messageOption
const res =
typeof responseMessage === 'string'
? new Response(responseMessage, { status, headers })
: new Response(JSON.stringify(responseMessage), {
status,
headers: {
...headers,
'content-type': 'application/json; charset=UTF-8',
},
})
throw new HTTPException(status, { res })
}
return async function bearerAuth(c, next) {
const headerToken = c.req.header(options.headerName || HEADER)
if (!headerToken) {
// No Authorization header
await throwHTTPException(
c,
401,
`${wwwAuthenticatePrefix}realm="${realm}"`,
options.noAuthenticationHeaderMessage || 'Unauthorized'
)
} else {
const match = regexp.exec(headerToken)
if (!match) {
// Invalid Request
await throwHTTPException(
c,
400,
`${wwwAuthenticatePrefix}error="invalid_request"`,
options.invalidAuthenticationHeaderMessage || 'Bad Request'
)
} else {
let equal = false
if ('verifyToken' in options) {
equal = await options.verifyToken(match[1], c)
} else if (typeof options.token === 'string') {
equal = await timingSafeEqual(options.token, match[1], options.hashFunction)
} else if (Array.isArray(options.token) && options.token.length > 0) {
for (const token of options.token) {
if (await timingSafeEqual(token, match[1], options.hashFunction)) {
equal = true
break
}
}
}
if (!equal) {
// Invalid Token
await throwHTTPException(
c,
401,
`${wwwAuthenticatePrefix}error="invalid_token"`,
options.invalidTokenMessage || 'Unauthorized'
)
}
}
}
await next()
}
}