authenik8-core 0.1.4 → 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/dist/auth/refreshService.d.ts.map +1 -1
- package/dist/auth/refreshService.js +0 -1
- package/dist/auth/refreshService.js.map +1 -1
- package/dist/createAuthenik8.d.ts.map +1 -1
- package/dist/createAuthenik8.js +52 -9
- package/dist/createAuthenik8.js.map +1 -1
- package/dist/oauth/adapters/memoryAdapter.d.ts +23 -0
- package/dist/oauth/adapters/memoryAdapter.d.ts.map +1 -0
- package/dist/oauth/adapters/memoryAdapter.js +43 -0
- package/dist/oauth/adapters/memoryAdapter.js.map +1 -0
- package/dist/oauth/brain/identityEngine.d.ts +18 -0
- package/dist/oauth/brain/identityEngine.d.ts.map +1 -0
- package/dist/oauth/brain/identityEngine.js +95 -0
- package/dist/oauth/brain/identityEngine.js.map +1 -0
- package/dist/oauth/brain/identityPolicy.d.ts +5 -0
- package/dist/oauth/brain/identityPolicy.d.ts.map +1 -0
- package/dist/oauth/brain/identityPolicy.js +8 -0
- package/dist/oauth/brain/identityPolicy.js.map +1 -0
- package/dist/oauth/brain/identityResolver.d.ts +7 -0
- package/dist/oauth/brain/identityResolver.d.ts.map +1 -0
- package/dist/oauth/brain/identityResolver.js +32 -0
- package/dist/oauth/brain/identityResolver.js.map +1 -0
- package/dist/oauth/brain/types.d.ts +9 -0
- package/dist/oauth/brain/types.d.ts.map +1 -0
- package/dist/oauth/brain/types.js +3 -0
- package/dist/oauth/brain/types.js.map +1 -0
- package/dist/oauth/core.d.ts +28 -0
- package/dist/oauth/core.d.ts.map +1 -0
- package/dist/oauth/core.js +16 -0
- package/dist/oauth/core.js.map +1 -0
- package/dist/oauth/identity/types.d.ts +32 -0
- package/dist/oauth/identity/types.d.ts.map +1 -0
- package/dist/oauth/identity/types.js +3 -0
- package/dist/oauth/identity/types.js.map +1 -0
- package/dist/oauth/providers/github.d.ts +14 -0
- package/dist/oauth/providers/github.d.ts.map +1 -0
- package/dist/oauth/providers/github.js +100 -0
- package/dist/oauth/providers/github.js.map +1 -0
- package/dist/oauth/providers/google.d.ts +13 -0
- package/dist/oauth/providers/google.d.ts.map +1 -0
- package/dist/oauth/providers/google.js +138 -0
- package/dist/oauth/providers/google.js.map +1 -0
- package/dist/oauth/types/identity.d.ts +16 -0
- package/dist/oauth/types/identity.d.ts.map +1 -0
- package/dist/oauth/types/identity.js +3 -0
- package/dist/oauth/types/identity.js.map +1 -0
- package/dist/oauth/types.d.ts +23 -0
- package/dist/oauth/types.d.ts.map +1 -0
- package/dist/oauth/types.js +3 -0
- package/dist/oauth/types.js.map +1 -0
- package/dist/oauth/userStore.d.ts +19 -0
- package/dist/oauth/userStore.d.ts.map +1 -0
- package/dist/oauth/userStore.js +41 -0
- package/dist/oauth/userStore.js.map +1 -0
- package/dist/tests/oauth.intergration.test.d.ts +2 -0
- package/dist/tests/oauth.intergration.test.d.ts.map +1 -0
- package/dist/tests/oauth.intergration.test.js +38 -0
- package/dist/tests/oauth.intergration.test.js.map +1 -0
- package/dist/tests/oauth1.test.d.ts +9 -0
- package/dist/tests/oauth1.test.d.ts.map +1 -0
- package/dist/tests/oauth1.test.js +126 -0
- package/dist/tests/oauth1.test.js.map +1 -0
- package/dist/tests/oauth2.test.d.ts +2 -0
- package/dist/tests/oauth2.test.d.ts.map +1 -0
- package/dist/tests/oauth2.test.js +145 -0
- package/dist/tests/oauth2.test.js.map +1 -0
- package/dist/tests/oauth3.test.d.ts +2 -0
- package/dist/tests/oauth3.test.d.ts.map +1 -0
- package/dist/tests/oauth3.test.js +148 -0
- package/dist/tests/oauth3.test.js.map +1 -0
- package/dist/types/config.d.ts +2 -0
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/public.d.ts +25 -0
- package/dist/types/public.d.ts.map +1 -1
- package/dist/types/tokens.d.ts +10 -0
- package/dist/types/tokens.d.ts.map +1 -0
- package/dist/types/tokens.js +3 -0
- package/dist/types/tokens.js.map +1 -0
- package/dump.rdb +0 -0
- package/package.json +2 -1
- package/src/auth/refreshService.ts +0 -1
- package/src/createAuthenik8.ts +79 -12
- package/src/dump.rdb +0 -0
- package/src/oauth/adapters/memoryAdapter.ts +74 -0
- package/src/oauth/brain/identityEngine.ts +141 -0
- package/src/oauth/brain/identityPolicy.ts +4 -0
- package/src/oauth/brain/identityResolver.ts +43 -0
- package/src/oauth/brain/types.ts +9 -0
- package/src/oauth/core.ts +19 -0
- package/src/oauth/identity/1 +44 -0
- package/src/oauth/identity/types.ts +44 -0
- package/src/oauth/providers/github.ts +154 -0
- package/src/oauth/providers/google.ts +189 -0
- package/src/oauth/types/identity.ts +17 -0
- package/src/oauth/types.ts +24 -0
- package/src/oauth/userStore.ts +73 -0
- package/src/tests/dump.rdb +0 -0
- package/src/tests/oauth.intergration.test.ts +39 -0
- package/src/tests/oauth1.test.ts +145 -0
- package/src/tests/oauth2.test.ts +158 -0
- package/src/tests/oauth3.test.ts +171 -0
- package/src/types/config.ts +6 -0
- package/src/types/public.ts +26 -0
- package/src/types/tokens.ts +10 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import crypto from "crypto";
|
|
2
|
+
import { OAuthProfile } from "./types";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
export type Provider = "google" | "github";
|
|
6
|
+
|
|
7
|
+
export type User = {
|
|
8
|
+
id: string;
|
|
9
|
+
email: string;
|
|
10
|
+
role?:string;
|
|
11
|
+
providers: {
|
|
12
|
+
provider: Provider;
|
|
13
|
+
providerId: string;
|
|
14
|
+
}[];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
const users: User[] = [];
|
|
19
|
+
|
|
20
|
+
export async function findUserByEmail(email: string): Promise<User | undefined> {
|
|
21
|
+
return users.find((u) => u.email === email);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function findUserByProvider(
|
|
25
|
+
provider: Provider,
|
|
26
|
+
providerId: string
|
|
27
|
+
): Promise<User | undefined> {
|
|
28
|
+
return users.find((u) =>
|
|
29
|
+
u.providers.some(
|
|
30
|
+
(p) => p.provider === provider && p.providerId === providerId
|
|
31
|
+
)
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function createUser(data: {
|
|
36
|
+
email: string;
|
|
37
|
+
provider: Provider;
|
|
38
|
+
providerId: string;
|
|
39
|
+
}): Promise<User> {
|
|
40
|
+
const newUser: User = {
|
|
41
|
+
id: crypto.randomUUID(),
|
|
42
|
+
email: data.email,
|
|
43
|
+
providers: [
|
|
44
|
+
{
|
|
45
|
+
provider: data.provider,
|
|
46
|
+
providerId: data.providerId,
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
users.push(newUser);
|
|
52
|
+
return newUser;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function linkProvider(
|
|
56
|
+
userId: string,
|
|
57
|
+
provider: Provider,
|
|
58
|
+
providerId: string
|
|
59
|
+
): Promise<void> {
|
|
60
|
+
const user = users.find((u) => u.id === userId);
|
|
61
|
+
|
|
62
|
+
if (!user) return;
|
|
63
|
+
|
|
64
|
+
const alreadyLinked = user.providers.some(
|
|
65
|
+
(p) => p.provider === provider
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
if (!alreadyLinked) {
|
|
69
|
+
user.providers.push({ provider, providerId });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
package/src/tests/dump.rdb
CHANGED
|
Binary file
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import request from "supertest";
|
|
2
|
+
import express from "express";
|
|
3
|
+
import { createAuthenik8 } from "../createAuthenik8";
|
|
4
|
+
|
|
5
|
+
test("OAuth callback issues tokens", async () => {
|
|
6
|
+
const app = express();
|
|
7
|
+
|
|
8
|
+
const auth = await createAuthenik8({
|
|
9
|
+
jwtSecret: "test",
|
|
10
|
+
refreshSecret: "test",
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
// 🔥 Mock OAuth provider
|
|
14
|
+
auth.oauth = {
|
|
15
|
+
google: {
|
|
16
|
+
handleCallback: async () => ({
|
|
17
|
+
profile: {
|
|
18
|
+
email: "test@example.com",
|
|
19
|
+
name: "Test User",
|
|
20
|
+
provider: "google",
|
|
21
|
+
providerId: "123",
|
|
22
|
+
},
|
|
23
|
+
}),
|
|
24
|
+
},
|
|
25
|
+
} as any;
|
|
26
|
+
|
|
27
|
+
app.get("/callback", async (req, res) => {
|
|
28
|
+
const { profile } = await auth.oauth!.google!.handleCallback(req);
|
|
29
|
+
const tokens = await auth.issueTokensFromProfile(profile);
|
|
30
|
+
|
|
31
|
+
res.json(tokens);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const res = await request(app).get("/callback");
|
|
35
|
+
|
|
36
|
+
expect(res.status).toBe(200);
|
|
37
|
+
expect(res.body.accessToken).toBeDefined();
|
|
38
|
+
expect(res.body.refreshToken).toBeDefined();
|
|
39
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
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
|
+
const tokenService = {
|
|
9
|
+
signAccessToken: () => "test_access_token",
|
|
10
|
+
generateRefreshToken: async () => "test_refresh_token",
|
|
11
|
+
};
|
|
12
|
+
const identityEngine = createIdentityEngine(memoryAdapter,tokenService);
|
|
13
|
+
|
|
14
|
+
dotenv.config();
|
|
15
|
+
|
|
16
|
+
const app = express();
|
|
17
|
+
|
|
18
|
+
declare global {
|
|
19
|
+
namespace Express {
|
|
20
|
+
interface Request {
|
|
21
|
+
user?: TokenPayload;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function start() {
|
|
27
|
+
const auth = await createAuthenik8({
|
|
28
|
+
jwtSecret: "test",
|
|
29
|
+
refreshSecret: "test",
|
|
30
|
+
oauth: {
|
|
31
|
+
google: {
|
|
32
|
+
clientId: process.env.GOOGLE_CLIENT_ID!,
|
|
33
|
+
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
|
|
34
|
+
redirectUri: "http://localhost:4000/callback",
|
|
35
|
+
},
|
|
36
|
+
github: {
|
|
37
|
+
clientId: process.env.GITHUB_CLIENT_ID!,
|
|
38
|
+
clientSecret: process.env.GITHUB_CLIENT_SECRET!,
|
|
39
|
+
redirectUri: "http://localhost:4000/auth/github/callback",
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
function requireAuth(req: Request, res: Response, next: NextFunction) {
|
|
45
|
+
const token = req.headers.authorization?.split(" ")[1];
|
|
46
|
+
|
|
47
|
+
if (!token) return res.status(401).json({ error: "Unauthorized" });
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
req.user = auth.verifyToken(token);
|
|
51
|
+
next();
|
|
52
|
+
} catch {
|
|
53
|
+
return res.status(401).json({ error: "Invalid token" });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// =========================
|
|
58
|
+
// GOOGLE LOGIN
|
|
59
|
+
// =========================
|
|
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} = await auth.oauth!.google!.handleCallback(req);
|
|
68
|
+
|
|
69
|
+
console.log("ENGINE INPUT:", {
|
|
70
|
+
mode,
|
|
71
|
+
userId,
|
|
72
|
+
});
|
|
73
|
+
const result = await identityEngine.resolveOAuth({
|
|
74
|
+
profile,
|
|
75
|
+
mode,
|
|
76
|
+
userId,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
return res.json({
|
|
80
|
+
provider: "google",
|
|
81
|
+
message: "Google login successful",
|
|
82
|
+
...result,
|
|
83
|
+
});
|
|
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
|
+
// GITHUB LOGIN
|
|
94
|
+
// =========================
|
|
95
|
+
|
|
96
|
+
app.get("/auth/github", (req, res) => {
|
|
97
|
+
auth.oauth!.github!.redirect(req, res);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
app.get("/auth/github/callback", async (req, res) => {
|
|
101
|
+
try {
|
|
102
|
+
const result = await auth.oauth!.github!.handleCallback(req);
|
|
103
|
+
|
|
104
|
+
return res.json({
|
|
105
|
+
provider: "github",
|
|
106
|
+
message: "GitHub login successful",
|
|
107
|
+
...result,
|
|
108
|
+
});
|
|
109
|
+
} catch (err) {
|
|
110
|
+
return res.status(500).json({
|
|
111
|
+
error: err instanceof Error ? err.message : "Unknown error",
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// =========================
|
|
117
|
+
// LINK FLOW (GITHUB)
|
|
118
|
+
// =========================
|
|
119
|
+
|
|
120
|
+
app.get("/auth/github/link", requireAuth, (req, res) => {
|
|
121
|
+
auth.oauth!.github!.redirect(req, res);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
app.get("/auth/github/link/callback", requireAuth, async (req, res) => {
|
|
125
|
+
try {
|
|
126
|
+
const result = await auth.oauth!.github!.handleCallback(req);
|
|
127
|
+
|
|
128
|
+
return res.json({
|
|
129
|
+
provider: "github",
|
|
130
|
+
message: "GitHub account linked successfully",
|
|
131
|
+
...result,
|
|
132
|
+
});
|
|
133
|
+
} catch (err) {
|
|
134
|
+
return res.status(400).json({
|
|
135
|
+
error: err instanceof Error ? err.message : "Linking error",
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
start().then(() => {
|
|
142
|
+
app.listen(4000, () => {
|
|
143
|
+
console.log("OAuth test server running on http://localhost:4000");
|
|
144
|
+
});
|
|
145
|
+
});
|
|
@@ -0,0 +1,158 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,171 @@
|
|
|
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
|
+
});
|
package/src/types/config.ts
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
|
|
2
2
|
import { SignOptions } from "jsonwebtoken";
|
|
3
3
|
import { Redis } from "ioredis";
|
|
4
|
+
import { GoogleOAuthConfig } from "../oauth/types";
|
|
5
|
+
import { OAuthConfig } from "../oauth/types";
|
|
6
|
+
|
|
7
|
+
|
|
4
8
|
|
|
5
9
|
export interface Authenik8Config {
|
|
6
10
|
jwtSecret: string;
|
|
7
11
|
jwtExpiry?: SignOptions["expiresIn"];
|
|
8
12
|
refreshSecret: string;
|
|
13
|
+
oauth?:OAuthConfig
|
|
9
14
|
|
|
10
15
|
redis?: Redis;
|
|
11
16
|
}
|
|
17
|
+
|
package/src/types/public.ts
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
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
|
+
|
|
1
20
|
|
|
2
21
|
export interface Authenik8Instance {
|
|
3
22
|
signToken: (payload: any) => string;
|
|
@@ -19,4 +38,11 @@ export interface Authenik8Instance {
|
|
|
19
38
|
incognito: any;
|
|
20
39
|
|
|
21
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>;
|
|
22
48
|
}
|