authenik8-core 0.2.0 → 1.0.22

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.
Files changed (97) hide show
  1. package/README.md +6 -0
  2. package/dist/auth/guestModeService.d.ts +4 -1
  3. package/dist/auth/guestModeService.d.ts.map +1 -1
  4. package/dist/auth/guestModeService.js +31 -20
  5. package/dist/auth/guestModeService.js.map +1 -1
  6. package/dist/auth/jwtAuth.d.ts +2 -1
  7. package/dist/auth/jwtAuth.d.ts.map +1 -1
  8. package/dist/auth/jwtAuth.js +26 -8
  9. package/dist/auth/jwtAuth.js.map +1 -1
  10. package/dist/auth/refreshService.d.ts +2 -0
  11. package/dist/auth/refreshService.d.ts.map +1 -1
  12. package/dist/auth/refreshService.js +15 -7
  13. package/dist/auth/refreshService.js.map +1 -1
  14. package/dist/createAuthenik8.d.ts.map +1 -1
  15. package/dist/createAuthenik8.js +31 -13
  16. package/dist/createAuthenik8.js.map +1 -1
  17. package/dist/middleware/adminService.d.ts +1 -1
  18. package/dist/middleware/adminService.d.ts.map +1 -1
  19. package/dist/middleware/adminService.js +6 -10
  20. package/dist/middleware/adminService.js.map +1 -1
  21. package/dist/oauth/adapters/redisAdapter.d.ts +23 -0
  22. package/dist/oauth/adapters/redisAdapter.d.ts.map +1 -0
  23. package/dist/oauth/adapters/redisAdapter.js +116 -0
  24. package/dist/oauth/adapters/redisAdapter.js.map +1 -0
  25. package/dist/oauth/brain/identityEngine.d.ts.map +1 -1
  26. package/dist/oauth/brain/identityEngine.js +26 -7
  27. package/dist/oauth/brain/identityEngine.js.map +1 -1
  28. package/dist/oauth/identity/types.d.ts +6 -1
  29. package/dist/oauth/identity/types.d.ts.map +1 -1
  30. package/dist/oauth/providers/github.d.ts.map +1 -1
  31. package/dist/oauth/providers/github.js +6 -7
  32. package/dist/oauth/providers/github.js.map +1 -1
  33. package/dist/oauth/providers/google.d.ts.map +1 -1
  34. package/dist/oauth/providers/google.js +3 -28
  35. package/dist/oauth/providers/google.js.map +1 -1
  36. package/dist/oauth/types.d.ts +1 -0
  37. package/dist/oauth/types.d.ts.map +1 -1
  38. package/dist/redis/redisService.js +7 -8
  39. package/dist/redis/redisService.js.map +1 -1
  40. package/dist/security/ipService.d.ts +3 -0
  41. package/dist/security/ipService.d.ts.map +1 -1
  42. package/dist/security/ipService.js +30 -9
  43. package/dist/security/ipService.js.map +1 -1
  44. package/dist/security/limiter.d.ts +12 -1
  45. package/dist/security/limiter.d.ts.map +1 -1
  46. package/dist/security/limiter.js +29 -10
  47. package/dist/security/limiter.js.map +1 -1
  48. package/dist/storage/RedisTokenStore.d.ts.map +1 -1
  49. package/dist/storage/RedisTokenStore.js +3 -2
  50. package/dist/storage/RedisTokenStore.js.map +1 -1
  51. package/dist/storage/userStorage.js +1 -2
  52. package/dist/storage/userStorage.js.map +1 -1
  53. package/dist/types/config.d.ts +3 -0
  54. package/dist/types/config.d.ts.map +1 -1
  55. package/package.json +23 -13
  56. package/dump.rdb +0 -0
  57. package/jest.config.js +0 -11
  58. package/src/auth/guestModeService.ts +0 -31
  59. package/src/auth/jwtAuth.ts +0 -99
  60. package/src/auth/refreshService.ts +0 -155
  61. package/src/createAuthenik8.ts +0 -135
  62. package/src/dump.rdb +0 -0
  63. package/src/index.ts +0 -2
  64. package/src/middleware/adminService.ts +0 -50
  65. package/src/oauth/adapters/memoryAdapter.ts +0 -74
  66. package/src/oauth/brain/identityEngine.ts +0 -141
  67. package/src/oauth/brain/identityPolicy.ts +0 -4
  68. package/src/oauth/brain/identityResolver.ts +0 -43
  69. package/src/oauth/brain/types.ts +0 -9
  70. package/src/oauth/core.ts +0 -19
  71. package/src/oauth/identity/1 +0 -44
  72. package/src/oauth/identity/types.ts +0 -44
  73. package/src/oauth/providers/github.ts +0 -154
  74. package/src/oauth/providers/google.ts +0 -189
  75. package/src/oauth/types/identity.ts +0 -17
  76. package/src/oauth/types.ts +0 -24
  77. package/src/oauth/userStore.ts +0 -73
  78. package/src/redis/redisService.ts +0 -137
  79. package/src/security/ipService.ts +0 -180
  80. package/src/security/limiter.ts +0 -116
  81. package/src/storage/RedisTokenStore.ts +0 -110
  82. package/src/storage/userStorage.ts +0 -16
  83. package/src/tests/con.test.ts +0 -76
  84. package/src/tests/dump.rdb +0 -0
  85. package/src/tests/full.intergration.test.ts +0 -100
  86. package/src/tests/oauth.intergration.test.ts +0 -39
  87. package/src/tests/oauth1.test.ts +0 -145
  88. package/src/tests/oauth2.test.ts +0 -158
  89. package/src/tests/oauth3.test.ts +0 -171
  90. package/src/tests/testApp.ts +0 -56
  91. package/src/types/admin.ts +0 -7
  92. package/src/types/config.ts +0 -17
  93. package/src/types/public.ts +0 -48
  94. package/src/types/storage.ts +0 -15
  95. package/src/types/tokens.ts +0 -10
  96. package/src/utility/lockHelper.ts +0 -33
  97. package/tsconfig.json +0 -51
@@ -1,158 +0,0 @@
1
- import dotenv from "dotenv";
2
- import express, { Request, Response, NextFunction } from "express";
3
- import { createAuthenik8 } from "../createAuthenik8";
4
- import { TokenPayload } from "../types/tokens";
5
- import { createIdentityEngine } from "../oauth/brain/identityEngine";
6
- import { memoryAdapter } from "../oauth/adapters/memoryAdapter";
7
-
8
- dotenv.config();
9
-
10
- const app = express();
11
-
12
- // =========================
13
- // TOKEN SERVICE (TEST)
14
- // =========================
15
- const tokenService = {
16
- signAccessToken: (payload: any) => {
17
- return `test_access_token_${payload.userId}`;
18
- },
19
- generateRefreshToken: async (payload: any) => {
20
- return `test_refresh_token_${payload.userId}`;
21
- },
22
- };
23
-
24
- const identityEngine = createIdentityEngine(memoryAdapter, tokenService);
25
-
26
- // =========================
27
- // AUTH INIT
28
- // =========================
29
- async function start() {
30
- const auth = await createAuthenik8({
31
- jwtSecret: "test",
32
- refreshSecret: "test",
33
- oauth: {
34
- google: {
35
- clientId: process.env.GOOGLE_CLIENT_ID!,
36
- clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
37
- redirectUri: "http://localhost:4000/callback",
38
- },
39
- github: {
40
- clientId: process.env.GITHUB_CLIENT_ID!,
41
- clientSecret: process.env.GITHUB_CLIENT_SECRET!,
42
- redirectUri: "http://localhost:4000/auth/github/callback",
43
- },
44
- },
45
- });
46
-
47
- // =========================
48
- // AUTH MIDDLEWARE
49
- // =========================
50
- function requireAuth(req: Request, res: Response, next: NextFunction) {
51
- const token = req.headers.authorization?.split(" ")[1];
52
-
53
- if (!token) return res.status(401).json({ error: "Unauthorized" });
54
-
55
- try {
56
- req.user = auth.verifyToken(token);
57
- next();
58
- } catch {
59
- return res.status(401).json({ error: "Invalid token" });
60
- }
61
- }
62
-
63
- // =====================================================
64
- // STEP 1: GOOGLE LOGIN (CREATE USER)
65
- // =====================================================
66
- app.get("/auth/google", (req, res) => {
67
- auth.oauth!.google!.redirect(req, res);
68
- });
69
-
70
- app.get("/callback", async (req, res) => {
71
- try {
72
- const { profile, mode, userId } =
73
- await auth.oauth!.google!.handleCallback(req);
74
-
75
- const result = await identityEngine.resolveOAuth({
76
- profile,
77
- mode,
78
- userId,
79
- });
80
-
81
- return res.json({
82
- step: "google-login",
83
- ...result,
84
- });
85
- } catch (err) {
86
- return res.status(500).json({
87
- error: err instanceof Error ? err.message : "Unknown error",
88
- });
89
- }
90
- });
91
-
92
- // =====================================================
93
- // STEP 2: GITHUB LOGIN (SEPARATE LOGIN FLOW)
94
- // =====================================================
95
- app.get("/auth/github", (req, res) => {
96
- auth.oauth!.github!.redirect(req, res);
97
- });
98
-
99
- app.get("/auth/github/callback", async (req, res) => {
100
- try {
101
- const { profile, mode, userId } =
102
- await auth.oauth!.github!.handleCallback(req);
103
-
104
- const result = await identityEngine.resolveOAuth({
105
- profile,
106
- mode,
107
- userId,
108
- });
109
-
110
- return res.json({
111
- step: "github-login",
112
- ...result,
113
- });
114
- } catch (err) {
115
- return res.status(500).json({
116
- error: err instanceof Error ? err.message : "Unknown error",
117
- });
118
- }
119
- });
120
-
121
- // =====================================================
122
- // STEP 3: GITHUB LINK FLOW (LINK TO SAME USER)
123
- // =====================================================
124
- app.get("/auth/github/link", requireAuth, (req, res) => {
125
- auth.oauth!.github!.redirect(req, res);
126
- });
127
-
128
- app.get("/auth/github/link/callback", requireAuth, async (req, res) => {
129
- try {
130
- const { profile, mode } =
131
- await auth.oauth!.github!.handleCallback(req);
132
-
133
- const result = await identityEngine.resolveOAuth({
134
- profile,
135
- mode: "link",
136
- userId: req.user!.userId,
137
- });
138
-
139
- return res.json({
140
- step: "github-link",
141
- ...result,
142
- });
143
- } catch (err) {
144
- return res.status(400).json({
145
- error: err instanceof Error ? err.message : "Linking error",
146
- });
147
- }
148
- });
149
- }
150
-
151
- // =========================
152
- // START SERVER
153
- // =========================
154
- start().then(() => {
155
- app.listen(4000, () => {
156
- console.log("OAuth test server running on http://localhost:4000");
157
- });
158
- });
@@ -1,171 +0,0 @@
1
- import dotenv from "dotenv";
2
- import express from "express";
3
- import { createAuthenik8 } from "../createAuthenik8";
4
- import { createIdentityEngine } from "../oauth/brain/identityEngine";
5
- import { memoryAdapter } from "../oauth/adapters/memoryAdapter";
6
-
7
- dotenv.config();
8
-
9
- const app = express();
10
- app.use(express.json());
11
-
12
- // =========================
13
- // TOKEN SERVICE (TEST)
14
- // =========================
15
- const tokenService = {
16
- signAccessToken: (payload: any) =>
17
- `test_access_${payload.userId}`,
18
-
19
- generateRefreshToken: async (payload: any) =>
20
- `test_refresh_${payload.userId}`,
21
- };
22
-
23
- const identityEngine = createIdentityEngine(memoryAdapter, tokenService);
24
-
25
- // =========================
26
- // SIMPLE AUTH PARSER (NO req.user TYPE ISSUES)
27
- // =========================
28
- function getUserFromHeader(req: any) {
29
- const header = req.headers["x-user-id"];
30
- if (!header) return null;
31
-
32
- return {
33
- userId: header as string,
34
- };
35
- }
36
-
37
- // =========================
38
- // INIT AUTH
39
- // =========================
40
- async function start() {
41
- const auth = await createAuthenik8({
42
- jwtSecret: "test",
43
- refreshSecret: "test",
44
- oauth: {
45
- google: {
46
- clientId: process.env.GOOGLE_CLIENT_ID!,
47
- clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
48
- redirectUri: "http://localhost:4000/callback",
49
- },
50
- github: {
51
- clientId: process.env.GITHUB_CLIENT_ID!,
52
- clientSecret: process.env.GITHUB_CLIENT_SECRET!,
53
- redirectUri: "http://localhost:4000/auth/github/callback",
54
- },
55
- },
56
- });
57
-
58
- // =====================================================
59
- // STEP 1: GOOGLE LOGIN (CREATE USER)
60
- // =====================================================
61
- app.get("/auth/google", (req, res) => {
62
- auth.oauth!.google!.redirect(req, res);
63
- });
64
-
65
- app.get("/callback", async (req, res) => {
66
- try {
67
- const { profile, mode, userId } =
68
- await auth.oauth!.google!.handleCallback(req);
69
-
70
- const result = await identityEngine.resolveOAuth({
71
- profile,
72
- mode,
73
- userId,
74
- });
75
-
76
- return res.json({
77
- step: "google-login",
78
- ...result,
79
- });
80
- } catch (err) {
81
- return res.status(500).json({
82
- error: err instanceof Error ? err.message : "Unknown error",
83
- });
84
- }
85
- });
86
-
87
- // =====================================================
88
- // STEP 2: GITHUB LOGIN (DIRECT LOGIN)
89
- // =====================================================
90
- app.get("/auth/github", (req, res) => {
91
- console.log("🔥 HIT /auth/github");
92
-
93
- auth.oauth!.github!.redirect(req, res);
94
- return
95
- });
96
-
97
- app.get("/auth/github/callback", async (req, res) => {
98
- try {
99
- const { profile, mode, userId } =
100
- await auth.oauth!.github!.handleCallback(req);
101
-
102
- const result = await identityEngine.resolveOAuth({
103
- profile,
104
- mode,
105
- userId,
106
- });
107
-
108
- return res.json({
109
- step: "github-login",
110
- ...result,
111
- });
112
- } catch (err) {
113
- return res.status(500).json({
114
- error: err instanceof Error ? err.message : "Unknown error",
115
- });
116
- }
117
- });
118
-
119
- // =====================================================
120
- // STEP 3: LINK GITHUB TO EXISTING USER (NO req.user)
121
- // =====================================================
122
- app.get("/auth/github/link",(req, res) => {
123
- // auth.oauth!.github!.redirect(req, res);
124
- console.log("🔥 HIT /auth/github/link");
125
- const user = getUserFromHeader(req); // ✅ REQUIRED HERE
126
-
127
- if (!user) {
128
- return res.status(401).json({ error: "Unauthorized" });
129
- }
130
- (req as any).user = user;
131
-
132
-
133
- auth.oauth!.github!.redirect(req, res, "link");
134
- return
135
- });
136
-
137
- app.get("/auth/github/link/callback", async (req, res) => {
138
- try{
139
- const { profile ,mode,userId} =
140
- await auth.oauth!.github!.handleCallback(req);
141
-
142
-
143
-
144
-
145
- const result = await identityEngine.resolveOAuth({
146
- profile,
147
- mode,
148
- userId,
149
- });
150
-
151
- return res.json({
152
- step: "github-link",
153
- ...result,
154
- });
155
- } catch (err) {
156
- return res.status(400).json({
157
- error: err instanceof Error ? err.message : "Link error",
158
- });
159
- }
160
- });
161
- }
162
-
163
- // =========================
164
- // START SERVER
165
- // =========================
166
- console.log("USERS:", memoryAdapter.dump());
167
- start().then(() => {
168
- app.listen(4000, () => {
169
- console.log("OAuth test running on http://localhost:4000");
170
- });
171
- });
@@ -1,56 +0,0 @@
1
- // tests/testApp.ts
2
- import express from "express";
3
- import request from "supertest";
4
- import { createAuthenik8 } from "../createAuthenik8";
5
-
6
- export const createTestApp = async () => {
7
- const auth = await createAuthenik8({
8
- jwtSecret: "test-secret",
9
- refreshSecret: "refresh-secret",
10
- jwtExpiry: "15m"
11
- });
12
-
13
- const app = express();
14
- app.use(express.json());
15
-
16
- // 🔐 Login (simulate user)
17
- app.post("/login", async(req, res) => {
18
- const user = { userId: "user_1", email:"test@test.com" };
19
-
20
- const accessToken = auth.signToken(user);
21
- const refreshToken = await auth.generateRefreshToken(user);
22
-
23
- res.json({ accessToken, refreshToken });
24
- console.log("Refresh Token",refreshToken)
25
- });
26
-
27
- // 🔒 Protected route
28
- app.get("/protected", (req, res) => {
29
- const token = req.headers.authorization?.split(" ")[1];
30
-
31
- if(!token){
32
- return res.status(401).json({error:"Invalid token"})
33
- }
34
-
35
- const decoded = auth.verifyToken(token);
36
-
37
- if (!decoded) {
38
- return res.status(401).json({ error: "Unauthorized" });
39
- }
40
-
41
- res.json({ data: "secure data", user: decoded });
42
- });
43
-
44
- // 🔄 Refresh
45
- app.post("/refresh", async (req, res) => {
46
- try {
47
- const result = await auth.refreshToken(req.body.refreshToken);
48
- res.json(result);
49
- } catch (err) {
50
- res.status(401).json({ error: "Invalid refresh token" });
51
- }
52
-
53
- });
54
-
55
- return { app, auth, request: request(app) };
56
- };
@@ -1,7 +0,0 @@
1
-
2
- import { Redis } from "ioredis";
3
-
4
- export interface RequireAdminOptions {
5
- jwtSecret: string;
6
- redis?: Redis;
7
- }
@@ -1,17 +0,0 @@
1
-
2
- import { SignOptions } from "jsonwebtoken";
3
- import { Redis } from "ioredis";
4
- import { GoogleOAuthConfig } from "../oauth/types";
5
- import { OAuthConfig } from "../oauth/types";
6
-
7
-
8
-
9
- export interface Authenik8Config {
10
- jwtSecret: string;
11
- jwtExpiry?: SignOptions["expiresIn"];
12
- refreshSecret: string;
13
- oauth?:OAuthConfig
14
-
15
- redis?: Redis;
16
- }
17
-
@@ -1,48 +0,0 @@
1
- import { TokenPayload, TokenPair } from "./tokens";
2
- import { OAuthProfile } from "../oauth/types";
3
-
4
- type GitHubProvider = {
5
- redirect: (req:any, res: any,mode?: "login" | "link") =>Promise<void>;
6
- handleCallback: (req: any) => Promise<{
7
- profile: OAuthProfile ;
8
- mode: "login" | "link";
9
- userId: string | null}>;
10
- };
11
-
12
- type GoogleProvider = {
13
- redirect: (req: any, res: any,mode?: "login" | "link") => Promise<void>;
14
- handleCallback: (req: any) => Promise<{
15
- profile: OAuthProfile;
16
- mode: "login" | "link";
17
- userId: string | null }>;
18
- };
19
-
20
-
21
- export interface Authenik8Instance {
22
- signToken: (payload: any) => string;
23
- verifyToken: (token: string) => any;
24
- guestToken: () => string;
25
-
26
- refreshToken: (token: string) => Promise<any>;
27
- generateRefreshToken: (payload: any) => Promise<string>;
28
-
29
- rateLimit: any;
30
- ipWhitelist: any;
31
- helmet: any;
32
-
33
- addIP: (ip: string) => Promise<void>;
34
- removeIP: (ip: string) => Promise<void>;
35
- listIPs: () => Promise<string[]>;
36
-
37
- requireAdmin: any;
38
- incognito: any;
39
-
40
- redis?: any;
41
- oauth?: {
42
- google?: GoogleProvider;
43
- github?: GitHubProvider;
44
- };
45
-
46
- issueTokens: (payload: TokenPayload) => Promise<TokenPair>;
47
- issueTokensFromProfile: (profile: OAuthProfile) => Promise<TokenPair>;
48
- }
@@ -1,15 +0,0 @@
1
- export interface User{
2
- id:string;
3
- email:string;
4
- password:string;
5
- role?:string;
6
- type?:string;
7
- }
8
-
9
- export interface UserStore{
10
- findByEmail(email:string):Promise<User | null>;
11
- findById(id:string):Promise<User | null>;
12
- create(user: Partial<User>):
13
- Promise<User>;
14
- update(id:string, data:Partial<User>):Promise<User>;
15
- }
@@ -1,10 +0,0 @@
1
- export type TokenPayload = {
2
- email: string;
3
- role?: string;
4
- userId: string;
5
- }
6
-
7
- export type TokenPair = {
8
- accessToken: string;
9
- refreshToken: string;
10
- };
@@ -1,33 +0,0 @@
1
- import { randomUUID } from "crypto";
2
-
3
- export class RedisLock {
4
- constructor(private redis: any) {}
5
-
6
- async acquire(key: string, ttl = 5000): Promise<string | null> {
7
- const value = randomUUID();
8
-
9
- const result = await this.redis.set(
10
- key,
11
- value,
12
- "PX",
13
- ttl,
14
- "NX"
15
- );
16
-
17
- if (result !== "OK") return null;
18
-
19
- return value;
20
- }
21
-
22
- async release(key: string, value: string): Promise<void> {
23
- const luaScript = `
24
- if redis.call("GET", KEYS[1]) == ARGV[1] then
25
- return redis.call("DEL", KEYS[1])
26
- else
27
- return 0
28
- end
29
- `;
30
-
31
- await this.redis.eval(luaScript, 1, key, value);
32
- }
33
- }
package/tsconfig.json DELETED
@@ -1,51 +0,0 @@
1
- {
2
- // Visit https://aka.ms/tsconfig to read more about this file
3
- "compilerOptions": {
4
- // File Layout
5
- "rootDir": "./src",
6
- "outDir": "./dist",
7
- "strict": true,
8
- "esModuleInterop": true,
9
-
10
- "declaration": true,
11
- "sourceMap": true,
12
-
13
- "skipLibCheck": true,
14
- "forceConsistentCasingInFileNames": true,
15
- // Environment Settings
16
- // See also https://aka.ms/tsconfig/module
17
- "module": "CommonJS",
18
- "target": "ES2019",
19
- "types": [],
20
- // For nodejs:
21
- // "lib": ["esnext"],
22
- "types": ["node","jest"],
23
- // and npm install -D @types/node
24
-
25
- // Other Outputs
26
- "sourceMap": true,
27
- "declaration": true,
28
- "declarationMap": true,
29
-
30
- // Stricter Typechecking Options
31
- "noUncheckedIndexedAccess": true,
32
- //"exactOptionalPropertyTypes": true,
33
-
34
- // Style Options
35
- // "noImplicitReturns": true,
36
- // "noImplicitOverride": true,
37
- // "noUnusedLocals": true,
38
- // "noUnusedParameters": true,
39
- // "noFallthroughCasesInSwitch": true,
40
- // "noPropertyAccessFromIndexSignature": true,
41
-
42
- // Recommended Options
43
- "strict": true,
44
- "jsx": "react-jsx",
45
- //"verbatimModuleSyntax": true,
46
- "isolatedModules": true,
47
- "noUncheckedSideEffectImports": true,
48
- "moduleDetection": "force",
49
- "skipLibCheck": true,
50
- }
51
- }