authenik8-core 1.0.3 → 1.0.29
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/README.md +7 -0
- package/dist/auth/guestModeService.d.ts +4 -1
- package/dist/auth/guestModeService.d.ts.map +1 -1
- package/dist/auth/guestModeService.js +31 -20
- package/dist/auth/guestModeService.js.map +1 -1
- package/dist/auth/jwtAuth.d.ts +2 -1
- package/dist/auth/jwtAuth.d.ts.map +1 -1
- package/dist/auth/jwtAuth.js +26 -8
- package/dist/auth/jwtAuth.js.map +1 -1
- package/dist/auth/refreshService.d.ts +2 -0
- package/dist/auth/refreshService.d.ts.map +1 -1
- package/dist/auth/refreshService.js +12 -5
- package/dist/auth/refreshService.js.map +1 -1
- package/dist/createAuthenik8.d.ts.map +1 -1
- package/dist/createAuthenik8.js +31 -13
- package/dist/createAuthenik8.js.map +1 -1
- package/dist/middleware/adminService.d.ts +1 -1
- package/dist/middleware/adminService.d.ts.map +1 -1
- package/dist/middleware/adminService.js +6 -10
- package/dist/middleware/adminService.js.map +1 -1
- package/dist/oauth/adapters/memoryAdapter.d.ts.map +1 -1
- package/dist/oauth/adapters/memoryAdapter.js +5 -4
- package/dist/oauth/adapters/memoryAdapter.js.map +1 -1
- package/dist/oauth/adapters/redisAdapter.d.ts +23 -0
- package/dist/oauth/adapters/redisAdapter.d.ts.map +1 -0
- package/dist/oauth/adapters/redisAdapter.js +116 -0
- package/dist/oauth/adapters/redisAdapter.js.map +1 -0
- package/dist/oauth/brain/identityEngine.d.ts.map +1 -1
- package/dist/oauth/brain/identityEngine.js +26 -7
- package/dist/oauth/brain/identityEngine.js.map +1 -1
- package/dist/oauth/identity/types.d.ts +6 -1
- package/dist/oauth/identity/types.d.ts.map +1 -1
- package/dist/oauth/providers/github.d.ts.map +1 -1
- package/dist/oauth/providers/github.js +6 -7
- package/dist/oauth/providers/github.js.map +1 -1
- package/dist/oauth/providers/google.d.ts.map +1 -1
- package/dist/oauth/providers/google.js +3 -28
- package/dist/oauth/providers/google.js.map +1 -1
- package/dist/oauth/types.d.ts +1 -0
- package/dist/oauth/types.d.ts.map +1 -1
- package/dist/oauth/userStore.d.ts +1 -0
- package/dist/oauth/userStore.d.ts.map +1 -1
- package/dist/oauth/userStore.js +6 -1
- package/dist/oauth/userStore.js.map +1 -1
- package/dist/redis/redisService.d.ts +3 -1
- package/dist/redis/redisService.d.ts.map +1 -1
- package/dist/redis/redisService.js +10 -9
- package/dist/redis/redisService.js.map +1 -1
- package/dist/security/ipService.d.ts +3 -0
- package/dist/security/ipService.d.ts.map +1 -1
- package/dist/security/ipService.js +30 -9
- package/dist/security/ipService.js.map +1 -1
- package/dist/security/limiter.d.ts +16 -1
- package/dist/security/limiter.d.ts.map +1 -1
- package/dist/security/limiter.js +34 -13
- package/dist/security/limiter.js.map +1 -1
- package/dist/storage/RedisTokenStore.d.ts.map +1 -1
- package/dist/storage/RedisTokenStore.js +3 -2
- package/dist/storage/RedisTokenStore.js.map +1 -1
- package/dist/storage/userStorage.js +1 -2
- package/dist/storage/userStorage.js.map +1 -1
- package/dist/types/config.d.ts +3 -0
- package/dist/types/config.d.ts.map +1 -1
- package/package.json +23 -13
- package/dump.rdb +0 -0
- package/jest.config.js +0 -11
- package/src/auth/guestModeService.ts +0 -31
- package/src/auth/jwtAuth.ts +0 -99
- package/src/auth/refreshService.ts +0 -157
- package/src/createAuthenik8.ts +0 -135
- package/src/dump.rdb +0 -0
- package/src/index.ts +0 -2
- package/src/middleware/adminService.ts +0 -50
- package/src/oauth/adapters/memoryAdapter.ts +0 -74
- package/src/oauth/brain/identityEngine.ts +0 -141
- package/src/oauth/brain/identityPolicy.ts +0 -4
- package/src/oauth/brain/identityResolver.ts +0 -43
- package/src/oauth/brain/types.ts +0 -9
- package/src/oauth/core.ts +0 -19
- package/src/oauth/identity/1 +0 -44
- package/src/oauth/identity/types.ts +0 -44
- package/src/oauth/providers/github.ts +0 -154
- package/src/oauth/providers/google.ts +0 -189
- package/src/oauth/types/identity.ts +0 -17
- package/src/oauth/types.ts +0 -24
- package/src/oauth/userStore.ts +0 -73
- package/src/redis/redisService.ts +0 -137
- package/src/security/ipService.ts +0 -180
- package/src/security/limiter.ts +0 -116
- package/src/storage/RedisTokenStore.ts +0 -110
- package/src/storage/userStorage.ts +0 -16
- package/src/tests/con.test.ts +0 -76
- package/src/tests/dump.rdb +0 -0
- package/src/tests/full.intergration.test.ts +0 -100
- package/src/tests/oauth.intergration.test.ts +0 -39
- package/src/tests/oauth1.test.ts +0 -145
- package/src/tests/oauth2.test.ts +0 -158
- package/src/tests/oauth3.test.ts +0 -171
- package/src/tests/testApp.ts +0 -56
- package/src/types/admin.ts +0 -7
- package/src/types/config.ts +0 -17
- package/src/types/public.ts +0 -48
- package/src/types/storage.ts +0 -15
- package/src/types/tokens.ts +0 -10
- package/src/utility/lockHelper.ts +0 -33
- package/tsconfig.json +0 -51
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
export type IdentityState =
|
|
2
|
-
| "EXISTING_PROVIDER_LOGIN"
|
|
3
|
-
| "EXISTING_EMAIL_CONFLICT"
|
|
4
|
-
| "NEW_USER_CREATION"
|
|
5
|
-
| "LINK_PROVIDER"
|
|
6
|
-
| "INVALID_LINK_REQUEST";
|
|
7
|
-
|
|
8
|
-
export type IdentityContext = {
|
|
9
|
-
email: string;
|
|
10
|
-
provider: string;
|
|
11
|
-
providerId: string;
|
|
12
|
-
|
|
13
|
-
mode: "login" | "link";
|
|
14
|
-
userId?: string;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export type IdentityResult =
|
|
18
|
-
| {
|
|
19
|
-
type: "EXISTING_PROVIDER_LOGIN";
|
|
20
|
-
user: any;
|
|
21
|
-
accessToken: string;
|
|
22
|
-
refreshToken: string;
|
|
23
|
-
}
|
|
24
|
-
| {
|
|
25
|
-
type: "EXISTING_EMAIL_CONFLICT";
|
|
26
|
-
email: string;
|
|
27
|
-
user:any
|
|
28
|
-
message: string;
|
|
29
|
-
}
|
|
30
|
-
| {
|
|
31
|
-
type: "NEW_USER_CREATION";
|
|
32
|
-
user: any;
|
|
33
|
-
accessToken: string;
|
|
34
|
-
refreshToken: string;
|
|
35
|
-
}
|
|
36
|
-
| {
|
|
37
|
-
type: "LINK_PROVIDER";
|
|
38
|
-
success: true;
|
|
39
|
-
user:any;
|
|
40
|
-
}
|
|
41
|
-
| {
|
|
42
|
-
type: "INVALID_LINK_REQUEST";
|
|
43
|
-
message: string;
|
|
44
|
-
};
|
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
import crypto from "crypto";
|
|
2
|
-
import { Request, Response } from "express";
|
|
3
|
-
import { OAuthProfile } from "../types";
|
|
4
|
-
import type { Redis as RedisClient } from "ioredis";
|
|
5
|
-
import { GitHubOAuthConfig, GoogleOAuthConfig } from "../types";
|
|
6
|
-
import { linkProvider } from "../userStore";
|
|
7
|
-
import type { IdentityEngine } from "../brain/types";
|
|
8
|
-
|
|
9
|
-
export function createGitHubProvider(config:GitHubOAuthConfig,
|
|
10
|
-
redisClient:RedisClient,
|
|
11
|
-
identityEngine:IdentityEngine) {
|
|
12
|
-
const { clientId, clientSecret, redirectUri } = config;
|
|
13
|
-
|
|
14
|
-
return {
|
|
15
|
-
redirect: async (req: Request, res: Response,mode: "login" | "link" = "login"):Promise<void> => {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if (res.headersSent) {
|
|
20
|
-
console.log("🚨 HEADERS ALREADY SENT — SKIPPING");
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const state= crypto.randomBytes(32).toString("hex");
|
|
26
|
-
const authUser = (req as any).user ?? null;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
await redisClient.setex(
|
|
32
|
-
`oauth:state:${state}`,
|
|
33
|
-
300,
|
|
34
|
-
JSON.stringify({
|
|
35
|
-
userId: authUser?.userId ?? null,
|
|
36
|
-
mode,
|
|
37
|
-
})
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const url = new URL("https://github.com/login/oauth/authorize");
|
|
43
|
-
|
|
44
|
-
url.searchParams.set("client_id", clientId);
|
|
45
|
-
url.searchParams.set("redirect_uri", redirectUri);
|
|
46
|
-
url.searchParams.set("scope", "read:user user:email");
|
|
47
|
-
url.searchParams.set("state",state);
|
|
48
|
-
|
|
49
|
-
console.log("REDIRECT STATE:", {
|
|
50
|
-
userId: authUser?.userId,
|
|
51
|
-
mode,
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
res.redirect(url.toString());
|
|
55
|
-
return
|
|
56
|
-
|
|
57
|
-
},
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
handleCallback:
|
|
61
|
-
async (req: Request): Promise<{
|
|
62
|
-
profile: OAuthProfile;
|
|
63
|
-
mode: "login" | "link";
|
|
64
|
-
userId: string | null;
|
|
65
|
-
}> => {
|
|
66
|
-
const code = req.query.code as string;
|
|
67
|
-
const state = req.query.state as string;
|
|
68
|
-
|
|
69
|
-
if (!state) {
|
|
70
|
-
throw new Error("OAuthError:Missing state");
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const stored = await redisClient.get(`oauth:state:${state}`);
|
|
74
|
-
|
|
75
|
-
if (!stored) {
|
|
76
|
-
throw new Error("OAuthError:Invalid or expired state");
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
const { userId, mode } = JSON.parse(stored);
|
|
82
|
-
|
|
83
|
-
if (!code) {
|
|
84
|
-
throw new Error("OAuthError: Missing code");
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const params = new URLSearchParams();
|
|
88
|
-
params.append("client_id", clientId);
|
|
89
|
-
params.append("client_secret", clientSecret);
|
|
90
|
-
params.append("code", code);
|
|
91
|
-
|
|
92
|
-
const tokenRes = await fetch("https://github.com/login/oauth/access_token", {
|
|
93
|
-
method: "POST",
|
|
94
|
-
headers: {
|
|
95
|
-
Accept: "application/json",
|
|
96
|
-
},
|
|
97
|
-
body: params,
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
const tokenData = await tokenRes.json();
|
|
101
|
-
|
|
102
|
-
if (!tokenData.access_token) {
|
|
103
|
-
throw new Error("OAuthError: No access token from Github");
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const accessToken = tokenData.access_token;
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const userRes = await fetch("https://api.github.com/user", {
|
|
110
|
-
headers: {
|
|
111
|
-
Authorization: `Bearer ${accessToken}`,
|
|
112
|
-
},
|
|
113
|
-
});
|
|
114
|
-
if (!userRes.ok) {
|
|
115
|
-
throw new Error("OAuthError: Failed to fetch GitHub user");
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const userData = await userRes.json();
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const emailRes = await fetch("https://api.github.com/user/emails", {
|
|
122
|
-
headers: {
|
|
123
|
-
Authorization: `Bearer ${accessToken}`,
|
|
124
|
-
},
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
const emails = await emailRes.json();
|
|
128
|
-
|
|
129
|
-
const primaryEmail = emails.find((e: any) => e.primary)?.email;
|
|
130
|
-
|
|
131
|
-
if (!primaryEmail) {
|
|
132
|
-
throw new Error("OAuthError: No primary email found");
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const profile: OAuthProfile = {
|
|
136
|
-
email: primaryEmail,
|
|
137
|
-
name: userData.name,
|
|
138
|
-
provider: "github",
|
|
139
|
-
providerId: userData.id.toString(),
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
await redisClient.del(`oauth:state:${state}`);
|
|
144
|
-
|
|
145
|
-
return {
|
|
146
|
-
profile,
|
|
147
|
-
mode,
|
|
148
|
-
userId
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
},
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
|
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
import { OAuthProfile, GoogleOAuthConfig } from "../types";
|
|
2
|
-
import { OAuth2Client } from "google-auth-library";
|
|
3
|
-
import {Request,Response} from "express";
|
|
4
|
-
import jwt from "jsonwebtoken";
|
|
5
|
-
import crypto from "crypto";
|
|
6
|
-
import type { Redis as RedisClient } from "ioredis";
|
|
7
|
-
import type { IdentityEngine } from "../brain/types";
|
|
8
|
-
|
|
9
|
-
export function createGoogleProvider(config: GoogleOAuthConfig,
|
|
10
|
-
redisClient:RedisClient,
|
|
11
|
-
identityEngine: IdentityEngine
|
|
12
|
-
) {
|
|
13
|
-
const { clientId, clientSecret, redirectUri } = config;
|
|
14
|
-
|
|
15
|
-
return {
|
|
16
|
-
|
|
17
|
-
redirect:async (req:Request, res:Response): Promise<void> => {
|
|
18
|
-
try{
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const state = crypto.randomBytes(32).toString("hex");
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const mode = req.path.includes("link") ? "link" : "login";
|
|
25
|
-
const authUser = (req as any).user ?? null;
|
|
26
|
-
|
|
27
|
-
await redisClient.setex(
|
|
28
|
-
`oauth:state:${state}`,
|
|
29
|
-
300,
|
|
30
|
-
JSON.stringify({
|
|
31
|
-
userId: authUser?.userId ?? null,
|
|
32
|
-
mode,
|
|
33
|
-
})
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const url = new URL("https://accounts.google.com/o/oauth2/v2/auth");
|
|
40
|
-
url.searchParams.set("client_id", clientId);
|
|
41
|
-
url.searchParams.set("redirect_uri", redirectUri);
|
|
42
|
-
url.searchParams.set("response_type", "code");
|
|
43
|
-
url.searchParams.set("scope", "openid email profile");
|
|
44
|
-
url.searchParams.set("access_type", "offline");
|
|
45
|
-
url.searchParams.set("prompt", "consent");
|
|
46
|
-
|
|
47
|
-
url.searchParams.set("state", state)
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
res.redirect(url.toString());
|
|
51
|
-
return;
|
|
52
|
-
} catch (err) {
|
|
53
|
-
console.error("Google redirect error:", err);
|
|
54
|
-
res.status(500).json({ error: "OAuth redirect failed" });
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
handleCallback: async (req:Request): Promise<{
|
|
59
|
-
profile: OAuthProfile;
|
|
60
|
-
mode: "login" | "link";
|
|
61
|
-
userId: string | null;
|
|
62
|
-
}> => {
|
|
63
|
-
const code = req.query.code as string;
|
|
64
|
-
const client = new OAuth2Client(clientId);
|
|
65
|
-
|
|
66
|
-
const state = req.query.state as string;
|
|
67
|
-
|
|
68
|
-
if (!state) {
|
|
69
|
-
throw new Error("OAuthError:Missing state");
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const stored = await redisClient.get(`oauth:state:${state}`);
|
|
73
|
-
|
|
74
|
-
if (!stored) {
|
|
75
|
-
throw new Error("OAuthError:Invalid or expired state");
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const { userId, mode } = JSON.parse(stored);
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
if (!code) {
|
|
82
|
-
throw new Error("OauthError:Missing authorization code");
|
|
83
|
-
}
|
|
84
|
-
const params = new URLSearchParams();
|
|
85
|
-
params.append("client_id", clientId);
|
|
86
|
-
params.append("client_secret", clientSecret);
|
|
87
|
-
params.append("code", code);
|
|
88
|
-
params.append("grant_type", "authorization_code");
|
|
89
|
-
params.append("redirect_uri", redirectUri);
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
// 1. Exchange code for tokens
|
|
94
|
-
const tokenRes = await fetch("https://oauth2.googleapis.com/token", {
|
|
95
|
-
method: "POST",
|
|
96
|
-
headers: {
|
|
97
|
-
"Content-Type": "application/x-www-form-urlencoded",
|
|
98
|
-
},
|
|
99
|
-
body:params
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
if(!tokenRes.ok){
|
|
103
|
-
const err = await tokenRes.text();
|
|
104
|
-
throw new Error(`OAuthError:Token exchange failed->${err}`)
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const tokenData = await tokenRes.json();
|
|
108
|
-
if(!tokenData.access_token){
|
|
109
|
-
|
|
110
|
-
throw new Error("OAuthError:No access token returned")
|
|
111
|
-
}
|
|
112
|
-
if (!tokenData.id_token) {
|
|
113
|
-
console.log("TOKEN DATA:", tokenData);
|
|
114
|
-
throw new Error("OAuthError:No id_token returned from Google");
|
|
115
|
-
}
|
|
116
|
-
const ticket = await client.verifyIdToken({
|
|
117
|
-
idToken: tokenData.id_token,
|
|
118
|
-
audience: clientId,
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
const payload = ticket.getPayload();
|
|
122
|
-
|
|
123
|
-
if(!payload){
|
|
124
|
-
throw new Error("OAuthError:Invalid ID token payload");
|
|
125
|
-
}
|
|
126
|
-
if(!payload.email){
|
|
127
|
-
throw new Error("OAuthError:Email not present in ID token");
|
|
128
|
-
}
|
|
129
|
-
if(!payload.email_verified){
|
|
130
|
-
throw new Error("OAuthError:Email not verified");
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (
|
|
134
|
-
payload.iss !== "https://accounts.google.com" &&
|
|
135
|
-
payload.iss !== "accounts.google.com"
|
|
136
|
-
) {
|
|
137
|
-
throw new Error("OAuthError: Invalid issuer");
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const profile: OAuthProfile = {
|
|
141
|
-
email: payload.email,
|
|
142
|
-
name: payload.name,
|
|
143
|
-
provider: "google",
|
|
144
|
-
providerId: payload.sub,
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
const accessToken = tokenData.access_token;
|
|
148
|
-
console.log("Token data:",tokenData);
|
|
149
|
-
console.log("[OAuth] Fetching user profile...");
|
|
150
|
-
|
|
151
|
-
console.log("FINAL ENGINE INPUT:", {
|
|
152
|
-
profile,
|
|
153
|
-
mode,
|
|
154
|
-
userId,
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
let result
|
|
158
|
-
try{
|
|
159
|
-
result = await identityEngine.resolveOAuth({
|
|
160
|
-
profile,
|
|
161
|
-
mode,
|
|
162
|
-
userId,
|
|
163
|
-
})
|
|
164
|
-
|
|
165
|
-
}catch(err){
|
|
166
|
-
console.error("ENGINE ERROR:",err)
|
|
167
|
-
throw err;
|
|
168
|
-
}
|
|
169
|
-
if (
|
|
170
|
-
result.type !== "INVALID_LINK_REQUEST" &&
|
|
171
|
-
result.type !== "EXISTING_EMAIL_CONFLICT" &&
|
|
172
|
-
!result.user
|
|
173
|
-
) {
|
|
174
|
-
throw new Error("Invalid engine result: missing user");
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
await redisClient.del(`oauth:state:${state}`);
|
|
179
|
-
|
|
180
|
-
return {
|
|
181
|
-
profile,
|
|
182
|
-
mode,
|
|
183
|
-
userId,
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export type Provider = "google" | "github";
|
|
2
|
-
|
|
3
|
-
export type Identity = {
|
|
4
|
-
provider: Provider;
|
|
5
|
-
providerId: string;
|
|
6
|
-
email?: string;
|
|
7
|
-
verified: boolean;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export type User = {
|
|
11
|
-
userId: string;
|
|
12
|
-
email: string;
|
|
13
|
-
emailVerified: boolean;
|
|
14
|
-
providers: Identity[];
|
|
15
|
-
createdAt: number;
|
|
16
|
-
lastLoginAt?: number;
|
|
17
|
-
};
|
package/src/oauth/types.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
export type Provider ="google" | "github"
|
|
3
|
-
export type OAuthProfile = {
|
|
4
|
-
email: string;
|
|
5
|
-
name?: string;
|
|
6
|
-
provider: Provider;
|
|
7
|
-
providerId: string;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export type GoogleOAuthConfig = {
|
|
11
|
-
clientId: string;
|
|
12
|
-
clientSecret: string;
|
|
13
|
-
redirectUri: string;
|
|
14
|
-
};
|
|
15
|
-
export type GitHubOAuthConfig = {
|
|
16
|
-
clientId: string;
|
|
17
|
-
clientSecret: string;
|
|
18
|
-
redirectUri: string;
|
|
19
|
-
enterprise?:boolean
|
|
20
|
-
};
|
|
21
|
-
export type OAuthConfig = {
|
|
22
|
-
google?: GoogleOAuthConfig;
|
|
23
|
-
github?: GitHubOAuthConfig;
|
|
24
|
-
};
|
package/src/oauth/userStore.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
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
|
-
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import dotenv from "dotenv";
|
|
2
|
-
import {RedisStore} from "connect-redis";
|
|
3
|
-
import Redis ,{ Redis as RedisCon }from "ioredis";
|
|
4
|
-
dotenv.config();
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
interface RedisConfig{
|
|
10
|
-
url?: string;
|
|
11
|
-
host?: string
|
|
12
|
-
port?: number;
|
|
13
|
-
password?: string;
|
|
14
|
-
maxRetriesPerRequest?:number;
|
|
15
|
-
connectTimeout: number;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
interface RedisStoreOptions{
|
|
19
|
-
prefix?: string;
|
|
20
|
-
ttl?: number;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
interface SetupRedisOptions{
|
|
24
|
-
redisConfig?: Partial<RedisConfig>;
|
|
25
|
-
storeOptions?:Partial<RedisStoreOptions>;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
let redisClientInstance:RedisCon | null = null
|
|
29
|
-
let redisStoreInstance:RedisStore |null = null
|
|
30
|
-
|
|
31
|
-
const DEFAULT_REDIS_CONFIG: RedisConfig = {
|
|
32
|
-
host:process.env.REDIS_HOST ?? "127.0.0.1",
|
|
33
|
-
port: Number(process.env.REDIS_PORT ?? "6379"),
|
|
34
|
-
maxRetriesPerRequest: 10,
|
|
35
|
-
connectTimeout: 5000
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const DEFAULT_STORE_OPTIONS : RedisStoreOptions = {
|
|
39
|
-
prefix: "session",
|
|
40
|
-
ttl: 86400
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const validateRedisConfig = (config:RedisConfig) => {
|
|
44
|
-
if (!config.url && !config.host) {
|
|
45
|
-
throw new Error("Redis configuration requires either URL or host/port");
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (config.url && !config.url.startsWith("redis://") && !config.url.startsWith("rediss://")
|
|
49
|
-
) {
|
|
50
|
-
throw new Error("Redis URL must use 'redis://' protocol");
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
const getRedisConfig = (options?:Partial<RedisConfig>): RedisConfig => {
|
|
56
|
-
const port = options?.port ?
|
|
57
|
-
Number(options.port) :
|
|
58
|
-
process.env.REDIS_PORT ? Number(process.env.REDIS_PORT) :
|
|
59
|
-
Number(DEFAULT_REDIS_CONFIG.port);
|
|
60
|
-
|
|
61
|
-
const config: RedisConfig = {
|
|
62
|
-
...DEFAULT_REDIS_CONFIG,
|
|
63
|
-
host: options?.host || process.env.REDIS_HOST|| DEFAULT_REDIS_CONFIG.host,
|
|
64
|
-
port: port,
|
|
65
|
-
password: options?.password || process.env.REDIS_PASSWORD|| undefined,
|
|
66
|
-
...options
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
validateRedisConfig(config);
|
|
70
|
-
return config;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const setupRedis = async (options?: SetupRedisOptions) => {
|
|
74
|
-
|
|
75
|
-
try {
|
|
76
|
-
const config = getRedisConfig(options?.redisConfig);
|
|
77
|
-
const storeOptions = {...DEFAULT_STORE_OPTIONS, ...options?.storeOptions}
|
|
78
|
-
|
|
79
|
-
const redisClient = new Redis({
|
|
80
|
-
host: config.host as string,
|
|
81
|
-
port:Number(config.port) ,
|
|
82
|
-
connectTimeout:config.connectTimeout,
|
|
83
|
-
password :config.password,
|
|
84
|
-
retryStrategy:(times:number)=> Math.min(times * 50,2000),
|
|
85
|
-
maxRetriesPerRequest:config.maxRetriesPerRequest
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
await new Promise<void>((resolve, reject)=>{
|
|
89
|
-
redisClient.once("ready", async () =>{
|
|
90
|
-
try{
|
|
91
|
-
const pong = await redisClient.ping();
|
|
92
|
-
console.log("Redis ping response:",pong);
|
|
93
|
-
resolve();
|
|
94
|
-
}catch(err){
|
|
95
|
-
reject(err);
|
|
96
|
-
}
|
|
97
|
-
})
|
|
98
|
-
redisClient.once("error",(err)=>{
|
|
99
|
-
reject(err);
|
|
100
|
-
})
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
const redisStore = new RedisStore({
|
|
104
|
-
client: redisClient,
|
|
105
|
-
prefix:storeOptions.prefix,
|
|
106
|
-
ttl:storeOptions.ttl
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
redisClient.on("error", (err:Error) => {
|
|
111
|
-
console.error("Redis client error:", err);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
redisClient.on("ready", () => {
|
|
115
|
-
console.log("Redis client is ready");
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
redisClient.on("reconnecting", () => {
|
|
119
|
-
console.log("Redis client reconnecting...");
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
return{ redisClient, redisStore };
|
|
123
|
-
} catch (error) {
|
|
124
|
-
console.error("Redis setup failed:", error);
|
|
125
|
-
throw error;
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
const initializeRedisClient =async () => {
|
|
129
|
-
if(!redisClientInstance){
|
|
130
|
-
const {redisClient} = await setupRedis();
|
|
131
|
-
redisClientInstance= redisClient;
|
|
132
|
-
}
|
|
133
|
-
return redisClientInstance
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
export {setupRedis, initializeRedisClient};
|
|
137
|
-
export type {RedisConfig, RedisStoreOptions, SetupRedisOptions}
|