@rtorcato/api-auth-hono 0.2.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 +38 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware.d.ts +17 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +27 -0
- package/dist/middleware.js.map +1 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Richard Torcato
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# @rtorcato/api-auth-hono
|
|
2
|
+
|
|
3
|
+
Hono middleware for `@rtorcato/api-auth` — JWT authentication with Bearer-header and cookie support.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @rtorcato/api-auth @rtorcato/api-auth-hono
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`hono` is a peer dependency — you bring your own version.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { Hono } from 'hono'
|
|
17
|
+
import { authMiddleware, type AuthVariables } from '@rtorcato/api-auth-hono'
|
|
18
|
+
|
|
19
|
+
const app = new Hono<{ Variables: AuthVariables }>()
|
|
20
|
+
|
|
21
|
+
app.use(authMiddleware(process.env.JWT_SECRET))
|
|
22
|
+
|
|
23
|
+
app.get('/me', (c) => c.json({ user: c.get('user') }))
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Type the app with `{ Variables: AuthVariables }` to get a typed `c.get('user')`.
|
|
27
|
+
|
|
28
|
+
- `authMiddleware(secret, options?)` — throws `UnauthorizedError` (`missing_token` / `invalid_token`) when the token is absent or invalid. Pair with [`@rtorcato/api-errors-hono`](https://github.com/rtorcato/api-common/tree/main/packages/api-errors-hono)'s `errorHandler` to translate it to a JSON response.
|
|
29
|
+
- `optionalAuthMiddleware(secret, options?)` — sets `c.get('user')` when a valid token is present, otherwise passes through untouched.
|
|
30
|
+
|
|
31
|
+
Options: `{ cookieName?: string; verifyOptions?: VerifyOptions }`. The token is read from the `Authorization: Bearer …` header first, then the cookie named `cookieName` (default `token`).
|
|
32
|
+
|
|
33
|
+
## Related
|
|
34
|
+
|
|
35
|
+
- [`@rtorcato/api-auth`](https://github.com/rtorcato/api-common/tree/main/packages/api-auth) — framework-agnostic core
|
|
36
|
+
- [`@rtorcato/api-auth-express`](https://github.com/rtorcato/api-common/tree/main/packages/api-auth-express) — Express adapter
|
|
37
|
+
|
|
38
|
+
Source: https://github.com/rtorcato/api-common/tree/main/packages/api-auth-hono
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,sBAAsB,EAAE,KAAK,aAAa,EAAE,MAAM,cAAc,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,sBAAsB,EAAsB,MAAM,cAAc,CAAA"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type JwtPayload, type Secret, type VerifyOptions } from '@rtorcato/api-auth';
|
|
2
|
+
import type { MiddlewareHandler } from 'hono';
|
|
3
|
+
export type AuthVariables = {
|
|
4
|
+
user: JwtPayload;
|
|
5
|
+
};
|
|
6
|
+
interface AuthOptions {
|
|
7
|
+
cookieName?: string;
|
|
8
|
+
verifyOptions?: VerifyOptions;
|
|
9
|
+
}
|
|
10
|
+
export declare function authMiddleware(secret: Secret, options?: AuthOptions): MiddlewareHandler<{
|
|
11
|
+
Variables: AuthVariables;
|
|
12
|
+
}>;
|
|
13
|
+
export declare function optionalAuthMiddleware(secret: Secret, options?: AuthOptions): MiddlewareHandler<{
|
|
14
|
+
Variables: AuthVariables;
|
|
15
|
+
}>;
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=middleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,UAAU,EACf,KAAK,MAAM,EAEX,KAAK,aAAa,EAClB,MAAM,oBAAoB,CAAA;AAG3B,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAA;AAG7C,MAAM,MAAM,aAAa,GAAG;IAAE,IAAI,EAAE,UAAU,CAAA;CAAE,CAAA;AAEhD,UAAU,WAAW;IACpB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,aAAa,CAAC,EAAE,aAAa,CAAA;CAC7B;AAED,wBAAgB,cAAc,CAC7B,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,WAAgB,GACvB,iBAAiB,CAAC;IAAE,SAAS,EAAE,aAAa,CAAA;CAAE,CAAC,CAUjD;AAED,wBAAgB,sBAAsB,CACrC,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,WAAgB,GACvB,iBAAiB,CAAC;IAAE,SAAS,EAAE,aAAa,CAAA;CAAE,CAAC,CAejD"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { findToken, verifyToken, } from '@rtorcato/api-auth';
|
|
2
|
+
import { UnauthorizedError } from '@rtorcato/api-errors';
|
|
3
|
+
import { getCookie } from 'hono/cookie';
|
|
4
|
+
export function authMiddleware(secret, options = {}) {
|
|
5
|
+
return async (c, next) => {
|
|
6
|
+
const token = findToken({ headers: { authorization: c.req.header('authorization') }, cookies: getCookie(c) }, { cookieName: options.cookieName });
|
|
7
|
+
if (!token)
|
|
8
|
+
throw new UnauthorizedError('No token provided', 'missing_token');
|
|
9
|
+
c.set('user', verifyToken(token, secret, options.verifyOptions));
|
|
10
|
+
return next();
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export function optionalAuthMiddleware(secret, options = {}) {
|
|
14
|
+
return async (c, next) => {
|
|
15
|
+
const token = findToken({ headers: { authorization: c.req.header('authorization') }, cookies: getCookie(c) }, { cookieName: options.cookieName });
|
|
16
|
+
if (token) {
|
|
17
|
+
try {
|
|
18
|
+
c.set('user', verifyToken(token, secret, options.verifyOptions));
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// ponytail: silently skip invalid token — optional auth never blocks the request
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return next();
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,SAAS,EAGT,WAAW,GAEX,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAWvC,MAAM,UAAU,cAAc,CAC7B,MAAc,EACd,UAAuB,EAAE;IAEzB,OAAO,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE;QACxB,MAAM,KAAK,GAAG,SAAS,CACtB,EAAE,OAAO,EAAE,EAAE,aAAa,EAAE,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,eAAe,CAAC,EAAE,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,EACpF,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAClC,CAAA;QACD,IAAI,CAAC,KAAK;YAAE,MAAM,IAAI,iBAAiB,CAAC,mBAAmB,EAAE,eAAe,CAAC,CAAA;QAC7E,CAAC,CAAC,GAAG,CAAC,MAAM,EAAE,WAAW,CAAa,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC,CAAA;QAC5E,OAAO,IAAI,EAAE,CAAA;IACd,CAAC,CAAA;AACF,CAAC;AAED,MAAM,UAAU,sBAAsB,CACrC,MAAc,EACd,UAAuB,EAAE;IAEzB,OAAO,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE;QACxB,MAAM,KAAK,GAAG,SAAS,CACtB,EAAE,OAAO,EAAE,EAAE,aAAa,EAAE,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,eAAe,CAAC,EAAE,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,EACpF,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAClC,CAAA;QACD,IAAI,KAAK,EAAE,CAAC;YACX,IAAI,CAAC;gBACJ,CAAC,CAAC,GAAG,CAAC,MAAM,EAAE,WAAW,CAAa,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC,CAAA;YAC7E,CAAC;YAAC,MAAM,CAAC;gBACR,iFAAiF;YAClF,CAAC;QACF,CAAC;QACD,OAAO,IAAI,EAAE,CAAA;IACd,CAAC,CAAA;AACF,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rtorcato/api-auth-hono",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Hono middleware for @rtorcato/api-auth — JWT authentication with Bearer-header and cookie support.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Richard Torcato",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=22"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@rtorcato/api-auth": "0.1.0",
|
|
24
|
+
"@rtorcato/api-errors": "0.2.0"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"hono": "^4.0.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"hono": "^4.12.27",
|
|
31
|
+
"typescript": "~6.0.3"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"api",
|
|
38
|
+
"nodejs",
|
|
39
|
+
"typescript",
|
|
40
|
+
"hono",
|
|
41
|
+
"middleware",
|
|
42
|
+
"jwt",
|
|
43
|
+
"auth",
|
|
44
|
+
"authentication"
|
|
45
|
+
],
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "git+https://github.com/rtorcato/api-common.git",
|
|
49
|
+
"directory": "packages/api-auth-hono"
|
|
50
|
+
},
|
|
51
|
+
"homepage": "https://github.com/rtorcato/api-common/tree/main/packages/api-auth-hono#readme",
|
|
52
|
+
"bugs": {
|
|
53
|
+
"url": "https://github.com/rtorcato/api-common/issues"
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "tsc -p tsconfig.build.json",
|
|
57
|
+
"typecheck": "tsc --noEmit",
|
|
58
|
+
"test": "vitest run"
|
|
59
|
+
}
|
|
60
|
+
}
|