@axeom/auth 0.1.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/LICENSE +21 -0
- package/dist/index.cjs +132 -0
- package/dist/index.d.cts +50 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +105 -0
- package/package.json +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Axeom
|
|
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,
|
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
18
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
19
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
20
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
|
|
21
|
+
OR OTHER DEALINGS IN THE SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
authPlugin: () => authPlugin,
|
|
24
|
+
authRoutes: () => authRoutes,
|
|
25
|
+
bearerGuard: () => bearerGuard,
|
|
26
|
+
default: () => index_default
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
var import_schema = require("@axeom/schema");
|
|
30
|
+
var import_jose = require("jose");
|
|
31
|
+
var authPlugin = (options) => {
|
|
32
|
+
const encoder = new TextEncoder();
|
|
33
|
+
const rawSecret = encoder.encode(options.secret);
|
|
34
|
+
return (app) => {
|
|
35
|
+
return app.decorate({
|
|
36
|
+
auth: {
|
|
37
|
+
/**
|
|
38
|
+
* Signs a new JWT token with the provided payload.
|
|
39
|
+
*/
|
|
40
|
+
sign: async (payload) => {
|
|
41
|
+
return await new import_jose.SignJWT(payload).setProtectedHeader({ alg: "HS256" }).setIssuedAt().setIssuer(options.issuer || "Axeom").setAudience(options.audience || "Axeom-app").setExpirationTime(options.expiresIn || "2h").sign(rawSecret);
|
|
42
|
+
},
|
|
43
|
+
/**
|
|
44
|
+
* Verifies a JWT token and returns the payload if valid.
|
|
45
|
+
*/
|
|
46
|
+
verify: async (token) => {
|
|
47
|
+
try {
|
|
48
|
+
const { payload } = await (0, import_jose.jwtVerify)(token, rawSecret, {
|
|
49
|
+
issuer: options.issuer || "Axeom",
|
|
50
|
+
audience: options.audience || "Axeom-app"
|
|
51
|
+
});
|
|
52
|
+
return payload;
|
|
53
|
+
} catch (e) {
|
|
54
|
+
console.error("[Auth Plugin] Verify Error:", e);
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
var bearerGuard = () => {
|
|
63
|
+
return async (ctx) => {
|
|
64
|
+
let token = null;
|
|
65
|
+
const authHeader = ctx.headers.get("Authorization");
|
|
66
|
+
if (authHeader && authHeader.startsWith("Bearer ")) {
|
|
67
|
+
token = authHeader.split(" ")[1];
|
|
68
|
+
} else {
|
|
69
|
+
const cookieHeader = ctx.headers.get("Cookie");
|
|
70
|
+
if (cookieHeader) {
|
|
71
|
+
const match = cookieHeader.match(/axeom_token=([^;]+)/);
|
|
72
|
+
if (match) token = match[1];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (!token) {
|
|
76
|
+
return new Response(
|
|
77
|
+
JSON.stringify({
|
|
78
|
+
status: "error",
|
|
79
|
+
code: "UNAUTHORIZED",
|
|
80
|
+
message: "Authorization required (Header or Cookie missing)"
|
|
81
|
+
}),
|
|
82
|
+
{
|
|
83
|
+
status: 401,
|
|
84
|
+
headers: { "Content-Type": "application/json" }
|
|
85
|
+
}
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
const user = await ctx.auth.verify(token);
|
|
89
|
+
if (!user) {
|
|
90
|
+
return new Response(
|
|
91
|
+
JSON.stringify({
|
|
92
|
+
status: "error",
|
|
93
|
+
code: "UNAUTHORIZED",
|
|
94
|
+
message: "Invalid or expired session"
|
|
95
|
+
}),
|
|
96
|
+
{
|
|
97
|
+
status: 401,
|
|
98
|
+
headers: { "Content-Type": "application/json" }
|
|
99
|
+
}
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
return { user };
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
var authRoutes = (app) => {
|
|
106
|
+
const authApp = app;
|
|
107
|
+
return authApp.group("/auth", (group) => {
|
|
108
|
+
return group.post(
|
|
109
|
+
"/login",
|
|
110
|
+
async (ctx) => {
|
|
111
|
+
const { username } = ctx.body;
|
|
112
|
+
const token = await ctx.auth.sign({ id: "123", name: username });
|
|
113
|
+
return { token };
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
body: import_schema.s.object({
|
|
117
|
+
username: import_schema.s.string().min(3),
|
|
118
|
+
password: import_schema.s.string().min(6)
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
).group("/me", (me) => {
|
|
122
|
+
return me.derive(bearerGuard()).get("/", ({ user }) => user);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
var index_default = authPlugin;
|
|
127
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
128
|
+
0 && (module.exports = {
|
|
129
|
+
authPlugin,
|
|
130
|
+
authRoutes,
|
|
131
|
+
bearerGuard
|
|
132
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Axeom, Context } from '@axeom/core';
|
|
2
|
+
|
|
3
|
+
interface AuthOptions {
|
|
4
|
+
secret: string;
|
|
5
|
+
issuer?: string;
|
|
6
|
+
audience?: string;
|
|
7
|
+
expiresIn?: string;
|
|
8
|
+
}
|
|
9
|
+
interface User {
|
|
10
|
+
id: string;
|
|
11
|
+
[key: string]: any;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The Axeom Auth Plugin.
|
|
15
|
+
* Adds JWT signing and verification utilities to the context.
|
|
16
|
+
*/
|
|
17
|
+
declare const authPlugin: (options: AuthOptions) => <T extends Record<string, any>, D extends Record<string, any>>(app: Axeom<T, D>) => Axeom<T, D & {
|
|
18
|
+
auth: {
|
|
19
|
+
/**
|
|
20
|
+
* Signs a new JWT token with the provided payload.
|
|
21
|
+
*/
|
|
22
|
+
sign: (payload: any) => Promise<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Verifies a JWT token and returns the payload if valid.
|
|
25
|
+
*/
|
|
26
|
+
verify: (token: string) => Promise<User | null>;
|
|
27
|
+
};
|
|
28
|
+
}>;
|
|
29
|
+
/**
|
|
30
|
+
* A derivation that verifies the 'Authorization: Bearer <token>' header.
|
|
31
|
+
*
|
|
32
|
+
* If successful, it adds 'user' to the context.
|
|
33
|
+
* If it fails, it returns a 401 Unauthorized response.
|
|
34
|
+
*
|
|
35
|
+
* Usage:
|
|
36
|
+
* Axeom.group("/admin", (admin) => admin.derive(bearerGuard()).get("/dashboard", ({ user }) => ...))
|
|
37
|
+
*/
|
|
38
|
+
declare const bearerGuard: () => <T extends Record<string, any>, D extends Record<string, any>>(ctx: Context<any, any, D & {
|
|
39
|
+
auth: {
|
|
40
|
+
verify: (t: string) => Promise<User | null>;
|
|
41
|
+
};
|
|
42
|
+
}>) => Promise<Response | {
|
|
43
|
+
user: User;
|
|
44
|
+
}>;
|
|
45
|
+
/**
|
|
46
|
+
* A helper to register standard login/profile routes.
|
|
47
|
+
*/
|
|
48
|
+
declare const authRoutes: <T extends Record<string, any>, D extends Record<string, any>>(app: Axeom<T, D>) => Axeom<any, any>;
|
|
49
|
+
|
|
50
|
+
export { type AuthOptions, type User, authPlugin, authRoutes, bearerGuard, authPlugin as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Axeom, Context } from '@axeom/core';
|
|
2
|
+
|
|
3
|
+
interface AuthOptions {
|
|
4
|
+
secret: string;
|
|
5
|
+
issuer?: string;
|
|
6
|
+
audience?: string;
|
|
7
|
+
expiresIn?: string;
|
|
8
|
+
}
|
|
9
|
+
interface User {
|
|
10
|
+
id: string;
|
|
11
|
+
[key: string]: any;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The Axeom Auth Plugin.
|
|
15
|
+
* Adds JWT signing and verification utilities to the context.
|
|
16
|
+
*/
|
|
17
|
+
declare const authPlugin: (options: AuthOptions) => <T extends Record<string, any>, D extends Record<string, any>>(app: Axeom<T, D>) => Axeom<T, D & {
|
|
18
|
+
auth: {
|
|
19
|
+
/**
|
|
20
|
+
* Signs a new JWT token with the provided payload.
|
|
21
|
+
*/
|
|
22
|
+
sign: (payload: any) => Promise<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Verifies a JWT token and returns the payload if valid.
|
|
25
|
+
*/
|
|
26
|
+
verify: (token: string) => Promise<User | null>;
|
|
27
|
+
};
|
|
28
|
+
}>;
|
|
29
|
+
/**
|
|
30
|
+
* A derivation that verifies the 'Authorization: Bearer <token>' header.
|
|
31
|
+
*
|
|
32
|
+
* If successful, it adds 'user' to the context.
|
|
33
|
+
* If it fails, it returns a 401 Unauthorized response.
|
|
34
|
+
*
|
|
35
|
+
* Usage:
|
|
36
|
+
* Axeom.group("/admin", (admin) => admin.derive(bearerGuard()).get("/dashboard", ({ user }) => ...))
|
|
37
|
+
*/
|
|
38
|
+
declare const bearerGuard: () => <T extends Record<string, any>, D extends Record<string, any>>(ctx: Context<any, any, D & {
|
|
39
|
+
auth: {
|
|
40
|
+
verify: (t: string) => Promise<User | null>;
|
|
41
|
+
};
|
|
42
|
+
}>) => Promise<Response | {
|
|
43
|
+
user: User;
|
|
44
|
+
}>;
|
|
45
|
+
/**
|
|
46
|
+
* A helper to register standard login/profile routes.
|
|
47
|
+
*/
|
|
48
|
+
declare const authRoutes: <T extends Record<string, any>, D extends Record<string, any>>(app: Axeom<T, D>) => Axeom<any, any>;
|
|
49
|
+
|
|
50
|
+
export { type AuthOptions, type User, authPlugin, authRoutes, bearerGuard, authPlugin as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { s } from "@axeom/schema";
|
|
3
|
+
import { jwtVerify, SignJWT } from "jose";
|
|
4
|
+
var authPlugin = (options) => {
|
|
5
|
+
const encoder = new TextEncoder();
|
|
6
|
+
const rawSecret = encoder.encode(options.secret);
|
|
7
|
+
return (app) => {
|
|
8
|
+
return app.decorate({
|
|
9
|
+
auth: {
|
|
10
|
+
/**
|
|
11
|
+
* Signs a new JWT token with the provided payload.
|
|
12
|
+
*/
|
|
13
|
+
sign: async (payload) => {
|
|
14
|
+
return await new SignJWT(payload).setProtectedHeader({ alg: "HS256" }).setIssuedAt().setIssuer(options.issuer || "Axeom").setAudience(options.audience || "Axeom-app").setExpirationTime(options.expiresIn || "2h").sign(rawSecret);
|
|
15
|
+
},
|
|
16
|
+
/**
|
|
17
|
+
* Verifies a JWT token and returns the payload if valid.
|
|
18
|
+
*/
|
|
19
|
+
verify: async (token) => {
|
|
20
|
+
try {
|
|
21
|
+
const { payload } = await jwtVerify(token, rawSecret, {
|
|
22
|
+
issuer: options.issuer || "Axeom",
|
|
23
|
+
audience: options.audience || "Axeom-app"
|
|
24
|
+
});
|
|
25
|
+
return payload;
|
|
26
|
+
} catch (e) {
|
|
27
|
+
console.error("[Auth Plugin] Verify Error:", e);
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
var bearerGuard = () => {
|
|
36
|
+
return async (ctx) => {
|
|
37
|
+
let token = null;
|
|
38
|
+
const authHeader = ctx.headers.get("Authorization");
|
|
39
|
+
if (authHeader && authHeader.startsWith("Bearer ")) {
|
|
40
|
+
token = authHeader.split(" ")[1];
|
|
41
|
+
} else {
|
|
42
|
+
const cookieHeader = ctx.headers.get("Cookie");
|
|
43
|
+
if (cookieHeader) {
|
|
44
|
+
const match = cookieHeader.match(/axeom_token=([^;]+)/);
|
|
45
|
+
if (match) token = match[1];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (!token) {
|
|
49
|
+
return new Response(
|
|
50
|
+
JSON.stringify({
|
|
51
|
+
status: "error",
|
|
52
|
+
code: "UNAUTHORIZED",
|
|
53
|
+
message: "Authorization required (Header or Cookie missing)"
|
|
54
|
+
}),
|
|
55
|
+
{
|
|
56
|
+
status: 401,
|
|
57
|
+
headers: { "Content-Type": "application/json" }
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
const user = await ctx.auth.verify(token);
|
|
62
|
+
if (!user) {
|
|
63
|
+
return new Response(
|
|
64
|
+
JSON.stringify({
|
|
65
|
+
status: "error",
|
|
66
|
+
code: "UNAUTHORIZED",
|
|
67
|
+
message: "Invalid or expired session"
|
|
68
|
+
}),
|
|
69
|
+
{
|
|
70
|
+
status: 401,
|
|
71
|
+
headers: { "Content-Type": "application/json" }
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
return { user };
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
var authRoutes = (app) => {
|
|
79
|
+
const authApp = app;
|
|
80
|
+
return authApp.group("/auth", (group) => {
|
|
81
|
+
return group.post(
|
|
82
|
+
"/login",
|
|
83
|
+
async (ctx) => {
|
|
84
|
+
const { username } = ctx.body;
|
|
85
|
+
const token = await ctx.auth.sign({ id: "123", name: username });
|
|
86
|
+
return { token };
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
body: s.object({
|
|
90
|
+
username: s.string().min(3),
|
|
91
|
+
password: s.string().min(6)
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
).group("/me", (me) => {
|
|
95
|
+
return me.derive(bearerGuard()).get("/", ({ user }) => user);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
var index_default = authPlugin;
|
|
100
|
+
export {
|
|
101
|
+
authPlugin,
|
|
102
|
+
authRoutes,
|
|
103
|
+
bearerGuard,
|
|
104
|
+
index_default as default
|
|
105
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@axeom/auth",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"jose": "^6.0.8",
|
|
9
|
+
"@axeom/core": "0.1.1",
|
|
10
|
+
"@axeom/schema": "0.1.1"
|
|
11
|
+
},
|
|
12
|
+
"module": "./dist/index.js",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js",
|
|
17
|
+
"require": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsup",
|
|
25
|
+
"dev": "tsup --watch"
|
|
26
|
+
}
|
|
27
|
+
}
|