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,157 +0,0 @@
|
|
|
1
|
-
import jwt from "jsonwebtoken";
|
|
2
|
-
import { SignOptions } from "jsonwebtoken";
|
|
3
|
-
import {randomUUID} from "crypto"
|
|
4
|
-
import {RedisLock} from "../utility/lockHelper"
|
|
5
|
-
|
|
6
|
-
export class MissingTokenError extends Error{
|
|
7
|
-
constructor(message="Missing Token")
|
|
8
|
-
{
|
|
9
|
-
super(message);
|
|
10
|
-
this.name ="MissingTokenError";
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export class InvalidTokenError extends
|
|
15
|
-
Error{
|
|
16
|
-
constructor(message = "Invalid refresh token"){
|
|
17
|
-
super(message);
|
|
18
|
-
this.name = "InvalidTokenError";
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
interface TokenPayload{
|
|
24
|
-
userId: string;
|
|
25
|
-
email: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
export interface TokenStore{
|
|
30
|
-
get(key:string):Promise<string| null>;
|
|
31
|
-
set?(key: string, value: string, expiry?:number):Promise<void>;
|
|
32
|
-
del?(key :string):Promise<void>;
|
|
33
|
-
getset?(key: string, value: string, expiry?: number): Promise<string | null>;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
export interface RefreshServiceOptions {
|
|
38
|
-
tokenStore:TokenStore;
|
|
39
|
-
accessTokenSecret:string;
|
|
40
|
-
redisClient:any;
|
|
41
|
-
refreshTokenSecret:string;
|
|
42
|
-
accessTokenExpiry:SignOptions["expiresIn"];
|
|
43
|
-
rotateRefreshTokens?:boolean;
|
|
44
|
-
refreshTokenExpiry?:string | number;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export interface RefreshResult{
|
|
48
|
-
accessToken:string;
|
|
49
|
-
refreshToken?:string;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
export class RefreshService{
|
|
54
|
-
private tokenStore:TokenStore;
|
|
55
|
-
private accessTokenSecret:string;
|
|
56
|
-
private refreshTokenSecret:string;
|
|
57
|
-
private accessTokenExpiry:SignOptions["expiresIn"];
|
|
58
|
-
private rotateRefreshTokens:boolean;
|
|
59
|
-
private refreshTokenExpiry:string | number;
|
|
60
|
-
private lock:RedisLock;
|
|
61
|
-
|
|
62
|
-
constructor(options:RefreshServiceOptions){
|
|
63
|
-
this.tokenStore = options.tokenStore;
|
|
64
|
-
this.accessTokenSecret = options.accessTokenSecret;
|
|
65
|
-
this.refreshTokenSecret =options.refreshTokenSecret;
|
|
66
|
-
this.accessTokenExpiry= options.accessTokenExpiry ?? "15m";
|
|
67
|
-
this.rotateRefreshTokens = options.rotateRefreshTokens ?? false;
|
|
68
|
-
this.refreshTokenExpiry = options.refreshTokenExpiry ?? "7d"
|
|
69
|
-
this.lock = new RedisLock(options.redisClient)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
async generateRefreshToken(payload: TokenPayload): Promise<string> {
|
|
74
|
-
|
|
75
|
-
if (!payload.userId) throw new Error("generateRefreshToken: payload.userId is missing");
|
|
76
|
-
|
|
77
|
-
const token = jwt.sign({...payload,jti:randomUUID(),},this.refreshTokenSecret, {
|
|
78
|
-
expiresIn: this.refreshTokenExpiry as SignOptions["expiresIn"],
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
if(this.tokenStore.set){
|
|
82
|
-
await this.tokenStore.set(
|
|
83
|
-
`refresh:${payload.userId}`,token, 60 * 60 * 24 * 7
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
return token
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
async refresh(refreshToken?:string):Promise<RefreshResult>{
|
|
93
|
-
if(!refreshToken){
|
|
94
|
-
throw new MissingTokenError()
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
let decoded:TokenPayload;
|
|
98
|
-
try{
|
|
99
|
-
|
|
100
|
-
decoded = jwt.verify(refreshToken,this.refreshTokenSecret) as TokenPayload;
|
|
101
|
-
}catch(err){
|
|
102
|
-
throw new InvalidTokenError()
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const lockKey = `lock:${decoded.userId}`;
|
|
106
|
-
const lockValue = await this.lock.acquire(lockKey, 5000);
|
|
107
|
-
|
|
108
|
-
let hasLock = !!lockValue
|
|
109
|
-
|
|
110
|
-
if (!lockValue) {
|
|
111
|
-
throw new InvalidTokenError("Concurrent refresh detected");
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
try{
|
|
116
|
-
const key = `refresh:${decoded.userId}`;
|
|
117
|
-
const storedToken = await this.tokenStore.get(key);
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (!storedToken || storedToken !== refreshToken){
|
|
121
|
-
throw new InvalidTokenError();
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const newAccessToken = jwt.sign(
|
|
125
|
-
{ userId: decoded.userId, email: decoded.email },
|
|
126
|
-
this.accessTokenSecret,
|
|
127
|
-
{ expiresIn: this.accessTokenExpiry as jwt.SignOptions["expiresIn"] }
|
|
128
|
-
);
|
|
129
|
-
|
|
130
|
-
let newRefreshToken:string | undefined;
|
|
131
|
-
|
|
132
|
-
if(this.rotateRefreshTokens && this.tokenStore.set){
|
|
133
|
-
const key = `refresh:${decoded.userId}`
|
|
134
|
-
|
|
135
|
-
;
|
|
136
|
-
newRefreshToken =jwt.sign({userId:decoded.userId,email : decoded.email,jti:randomUUID(),},
|
|
137
|
-
this.refreshTokenSecret,
|
|
138
|
-
{expiresIn:this.refreshTokenExpiry as jwt.SignOptions["expiresIn"]});
|
|
139
|
-
|
|
140
|
-
if (!this.tokenStore.getset) {
|
|
141
|
-
throw new Error("TokenStore must implement getset for atomic refresh rotation");
|
|
142
|
-
}
|
|
143
|
-
const PreviousToken =await this.tokenStore.getset(key,newRefreshToken,60 * 60 * 24 * 7)
|
|
144
|
-
if(PreviousToken !== refreshToken && PreviousToken !== storedToken){
|
|
145
|
-
throw new InvalidTokenError("Concurrent refresh detected")}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
return{
|
|
149
|
-
accessToken:newAccessToken,
|
|
150
|
-
refreshToken:newRefreshToken ?? refreshToken
|
|
151
|
-
};
|
|
152
|
-
}finally {
|
|
153
|
-
if (hasLock && lockValue) await this.lock.release(lockKey, lockValue);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
}
|
package/src/createAuthenik8.ts
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import {SecurityModule} from "./security/ipService";
|
|
2
|
-
import {RefreshService } from "./auth/refreshService"
|
|
3
|
-
import {Authenik8Config} from "./types/config";
|
|
4
|
-
import {Incognito} from "./auth/guestModeService"
|
|
5
|
-
import {requireAdmin} from "./middleware/adminService";
|
|
6
|
-
import {JWTService} from "./auth/jwtAuth"
|
|
7
|
-
import { initializeRedisClient } from "./redis/redisService"
|
|
8
|
-
import {Authenik8Instance} from "./types/public"
|
|
9
|
-
import {RedisTokenStore} from "./storage/RedisTokenStore"
|
|
10
|
-
import { createOAuth } from "./oauth/core";
|
|
11
|
-
import { TokenPayload, TokenPair } from "./types/tokens";
|
|
12
|
-
import { OAuthProfile } from "./oauth/types";
|
|
13
|
-
import { createIdentityEngine } from "./oauth/brain/identityEngine";
|
|
14
|
-
import { memoryAdapter } from "./oauth/adapters/memoryAdapter";
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
export const createAuthenik8 = async (config:Authenik8Config): Promise<Authenik8Instance> =>{
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const redisClient = config.redis ?? await initializeRedisClient()
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const tokenStore = new RedisTokenStore(redisClient);
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const refreshService = new RefreshService({
|
|
29
|
-
tokenStore,
|
|
30
|
-
redisClient,
|
|
31
|
-
accessTokenSecret: config.jwtSecret,
|
|
32
|
-
refreshTokenSecret: config.refreshSecret,
|
|
33
|
-
accessTokenExpiry: config.jwtExpiry ?? "15m",
|
|
34
|
-
rotateRefreshTokens: true,
|
|
35
|
-
refreshTokenExpiry: config.jwtExpiry ?? "7d",
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const jwtService =new JWTService({
|
|
40
|
-
jwtSecret:config.jwtSecret,
|
|
41
|
-
expiry:config.jwtExpiry ?? "15m",
|
|
42
|
-
redisClient:redisClient
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const issueTokens = async (payload: TokenPayload): Promise<TokenPair> => {
|
|
48
|
-
const accessToken = jwtService.signToken(payload);
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const refreshToken = await refreshService.generateRefreshToken({
|
|
52
|
-
userId: payload.userId,
|
|
53
|
-
email: payload.email,
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
return {
|
|
57
|
-
accessToken,
|
|
58
|
-
refreshToken,
|
|
59
|
-
};
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
const tokenService = {
|
|
63
|
-
signAccessToken: jwtService.signToken.bind(jwtService),
|
|
64
|
-
generateRefreshToken: refreshService.generateRefreshToken.bind(refreshService),
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
// =========================
|
|
68
|
-
// 5. Identity Engine (NO circular deps)
|
|
69
|
-
// =========================
|
|
70
|
-
const identityEngine = createIdentityEngine(
|
|
71
|
-
memoryAdapter,
|
|
72
|
-
tokenService
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
// =========================
|
|
76
|
-
// 6. OAuth (depends on identity engine)
|
|
77
|
-
// =========================
|
|
78
|
-
const oauth = config.oauth
|
|
79
|
-
? createOAuth({
|
|
80
|
-
...config.oauth,
|
|
81
|
-
redisClient,
|
|
82
|
-
identityEngine,
|
|
83
|
-
})
|
|
84
|
-
: undefined;
|
|
85
|
-
|
|
86
|
-
// ===============
|
|
87
|
-
const issueTokensFromProfile = async (
|
|
88
|
-
profile: OAuthProfile
|
|
89
|
-
): Promise<TokenPair> => {
|
|
90
|
-
return issueTokens({
|
|
91
|
-
userId: profile.providerId,
|
|
92
|
-
email: profile.email,
|
|
93
|
-
role: "user",
|
|
94
|
-
});
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
const security = new SecurityModule({
|
|
98
|
-
redisClient:redisClient,
|
|
99
|
-
rateLimiterEnabled: true,
|
|
100
|
-
helmetEnabled:true,
|
|
101
|
-
whiteListEnabled:true
|
|
102
|
-
});
|
|
103
|
-
return{
|
|
104
|
-
//auth
|
|
105
|
-
redis:redisClient,
|
|
106
|
-
signToken:jwtService.signToken.bind(jwtService),
|
|
107
|
-
verifyToken:jwtService.verifyToken.bind(jwtService),
|
|
108
|
-
guestToken:jwtService.guestToken.bind(jwtService),
|
|
109
|
-
|
|
110
|
-
//refresh
|
|
111
|
-
refreshToken:
|
|
112
|
-
refreshService.refresh.bind(refreshService),
|
|
113
|
-
generateRefreshToken: refreshService.generateRefreshToken.bind(refreshService),
|
|
114
|
-
//security
|
|
115
|
-
rateLimit: security.rateLimiterMiddleware(),
|
|
116
|
-
ipWhitelist: security.whiteListMiddleware(),
|
|
117
|
-
helmet: security.helmetMiddleware(),
|
|
118
|
-
|
|
119
|
-
//Whitelist management
|
|
120
|
-
addIP: security.addIP.bind(security),
|
|
121
|
-
removeIP: security.removeIP.bind(security),
|
|
122
|
-
listIPs: security.listIPs.bind(security),
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
//middleware
|
|
126
|
-
requireAdmin :requireAdmin({ jwtSecret:
|
|
127
|
-
config.jwtSecret,
|
|
128
|
-
redis:redisClient
|
|
129
|
-
}),
|
|
130
|
-
incognito:Incognito,
|
|
131
|
-
oauth,
|
|
132
|
-
issueTokens,
|
|
133
|
-
issueTokensFromProfile
|
|
134
|
-
}
|
|
135
|
-
}
|
package/src/dump.rdb
DELETED
|
Binary file
|
package/src/index.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import {Request, Response, NextFunction} from "express";
|
|
3
|
-
import jwt from "jsonwebtoken";
|
|
4
|
-
import {Authenik8Config} from "../types/config";
|
|
5
|
-
import { RequireAdminOptions } from "../types/admin";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
interface JwtPayload {
|
|
9
|
-
id:string;
|
|
10
|
-
role: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
export const requireAdmin=(options:RequireAdminOptions)=>{
|
|
16
|
-
const {jwtSecret,redis}= options;
|
|
17
|
-
return(req:Request, res:Response,next:NextFunction)=>{
|
|
18
|
-
|
|
19
|
-
const authHeader = req.headers.authorization
|
|
20
|
-
const cookieToken =req.cookies?.token
|
|
21
|
-
|
|
22
|
-
let token:string | undefined;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if(authHeader && authHeader.startsWith("Bearer")){
|
|
26
|
-
token=authHeader.split(" ")[1];
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
if (!token && cookieToken){
|
|
30
|
-
token = cookieToken
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if(!token){
|
|
34
|
-
return res.status(401).json({error:"Unauthorized:No token provided"});}
|
|
35
|
-
try{
|
|
36
|
-
const decoded = jwt.verify(token,options.jwtSecret) as JwtPayload;
|
|
37
|
-
|
|
38
|
-
if(typeof decoded.role !== "string"){
|
|
39
|
-
res.status(403).json({error:"Forbidden: Admin only"})
|
|
40
|
-
}
|
|
41
|
-
const payload = decoded as JwtPayload
|
|
42
|
-
|
|
43
|
-
(req as any).user =decoded;
|
|
44
|
-
next();
|
|
45
|
-
}catch(error){
|
|
46
|
-
return
|
|
47
|
-
res.status(401).json({error:"Invalid or expired token"})
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "crypto";
|
|
2
|
-
|
|
3
|
-
type Provider = {
|
|
4
|
-
provider: string;
|
|
5
|
-
providerId: string;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
type User = {
|
|
9
|
-
id: string;
|
|
10
|
-
email: string;
|
|
11
|
-
providers: Provider[];
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
// 🔥 SINGLE SOURCE OF TRUTH (REAL MEMORY STORE)
|
|
15
|
-
const users = new Map<string, User>();
|
|
16
|
-
|
|
17
|
-
export const memoryAdapter = {
|
|
18
|
-
async findUserByEmail(email: string) {
|
|
19
|
-
return [...users.values()].find((u) => u.email === email) || null;
|
|
20
|
-
},
|
|
21
|
-
|
|
22
|
-
async findUserByProvider(provider: string, providerId: string) {
|
|
23
|
-
return (
|
|
24
|
-
[...users.values()].find((u) =>
|
|
25
|
-
u.providers.some(
|
|
26
|
-
(p) => p.provider === provider && p.providerId === providerId
|
|
27
|
-
)
|
|
28
|
-
) || null
|
|
29
|
-
);
|
|
30
|
-
},
|
|
31
|
-
|
|
32
|
-
async createUser(data: {
|
|
33
|
-
email: string;
|
|
34
|
-
provider: string;
|
|
35
|
-
providerId: string;
|
|
36
|
-
}) {
|
|
37
|
-
const user: User = {
|
|
38
|
-
id: randomUUID(),
|
|
39
|
-
email: data.email,
|
|
40
|
-
providers: [
|
|
41
|
-
{
|
|
42
|
-
provider: data.provider,
|
|
43
|
-
providerId: data.providerId,
|
|
44
|
-
},
|
|
45
|
-
],
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
users.set(user.id, user);
|
|
49
|
-
return user;
|
|
50
|
-
},
|
|
51
|
-
|
|
52
|
-
async linkProvider(
|
|
53
|
-
userId: string,
|
|
54
|
-
provider: string,
|
|
55
|
-
providerId: string
|
|
56
|
-
) {
|
|
57
|
-
const user = users.get(userId);
|
|
58
|
-
|
|
59
|
-
if (!user) {
|
|
60
|
-
throw new Error(`User not found: ${userId}`);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
user.providers.push({ provider, providerId });
|
|
64
|
-
users.set(userId, user);
|
|
65
|
-
},
|
|
66
|
-
|
|
67
|
-
reset() {
|
|
68
|
-
users.clear();
|
|
69
|
-
},
|
|
70
|
-
|
|
71
|
-
dump() {
|
|
72
|
-
return [...users.values()];
|
|
73
|
-
},
|
|
74
|
-
};
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { OAuthProfile } from "../types";
|
|
2
|
-
import { memoryAdapter } from "../adapters/memoryAdapter";
|
|
3
|
-
import { IdentityEngine } from "./types";
|
|
4
|
-
import { IdentityContext, IdentityResult } from "../identity/types";
|
|
5
|
-
|
|
6
|
-
const auditLogs: any[] = [];
|
|
7
|
-
type IdentityAdapter = {
|
|
8
|
-
findUserByEmail(email: string): Promise<any>;
|
|
9
|
-
findUserByProvider(provider: string, providerId: string): Promise<any>;
|
|
10
|
-
createUser(data: {
|
|
11
|
-
email: string;
|
|
12
|
-
provider: string;
|
|
13
|
-
providerId: string;
|
|
14
|
-
}): Promise<any>;
|
|
15
|
-
linkProvider(
|
|
16
|
-
userId: string,
|
|
17
|
-
provider: string,
|
|
18
|
-
providerId: string
|
|
19
|
-
): Promise<void>;
|
|
20
|
-
};
|
|
21
|
-
type TokenService = {
|
|
22
|
-
signAccessToken(payload: any): string;
|
|
23
|
-
generateRefreshToken(payload: any): Promise<string>;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export function createIdentityEngine(
|
|
27
|
-
adapter:IdentityAdapter,
|
|
28
|
-
tokenService:TokenService
|
|
29
|
-
): IdentityEngine {
|
|
30
|
-
return {
|
|
31
|
-
async resolveOAuth(args):Promise<IdentityResult>{
|
|
32
|
-
const ctx:IdentityContext = {
|
|
33
|
-
email: args.profile.email,
|
|
34
|
-
provider:args.profile.provider,
|
|
35
|
-
providerId:args.profile.providerId,
|
|
36
|
-
mode:args.mode,
|
|
37
|
-
userId: args.userId ?? undefined,
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (!ctx.email) {
|
|
41
|
-
throw new Error("OAuth profile missing email");
|
|
42
|
-
}
|
|
43
|
-
// 1. Existing provider
|
|
44
|
-
const existingProvider = await adapter.findUserByProvider(
|
|
45
|
-
ctx.provider,
|
|
46
|
-
ctx.providerId
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
if (existingProvider) {
|
|
50
|
-
const payload ={userId:existingProvider.id}
|
|
51
|
-
return {
|
|
52
|
-
type:"EXISTING_PROVIDER_LOGIN",
|
|
53
|
-
user: existingProvider,
|
|
54
|
-
accessToken:tokenService.signAccessToken(payload),
|
|
55
|
-
refreshToken: await tokenService.generateRefreshToken(payload),
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// 2. LINK FLOW
|
|
60
|
-
if (ctx.mode === "link") {
|
|
61
|
-
|
|
62
|
-
if (!ctx.userId) {
|
|
63
|
-
return{
|
|
64
|
-
|
|
65
|
-
type:"INVALID_LINK_REQUEST",
|
|
66
|
-
message:"Missing authenticated user for linking",
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
if (existingProvider && existingProvider.id !== ctx.userId) {
|
|
70
|
-
throw new Error("Provider already linked to another user");
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
await adapter.linkProvider(
|
|
74
|
-
ctx.userId,
|
|
75
|
-
ctx.provider,
|
|
76
|
-
ctx.providerId
|
|
77
|
-
);
|
|
78
|
-
let user = await adapter.findUserByEmail(ctx.email);
|
|
79
|
-
|
|
80
|
-
if (!user) {
|
|
81
|
-
user = await adapter.findUserByProvider(ctx.provider, ctx.providerId);
|
|
82
|
-
}
|
|
83
|
-
if (!user) {
|
|
84
|
-
throw new Error("LINK_PROVIDER: user resolution failed");
|
|
85
|
-
}
|
|
86
|
-
auditLogs.push({
|
|
87
|
-
userId: user.id,
|
|
88
|
-
action: "PROVIDER_LINKED",
|
|
89
|
-
timestamp: Date.now(),
|
|
90
|
-
});
|
|
91
|
-
return{
|
|
92
|
-
type: "LINK_PROVIDER",
|
|
93
|
-
user,
|
|
94
|
-
success: true,
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// 3. Check existing by email OR provider (idempotency safety)
|
|
99
|
-
|
|
100
|
-
let existingUser = await adapter.findUserByEmail(ctx.email);
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (existingUser) {
|
|
104
|
-
const payload = { userId: existingUser.id };
|
|
105
|
-
|
|
106
|
-
return {
|
|
107
|
-
type: "EXISTING_PROVIDER_LOGIN",
|
|
108
|
-
user: existingUser,
|
|
109
|
-
accessToken: tokenService.signAccessToken(payload),
|
|
110
|
-
refreshToken: await tokenService.generateRefreshToken(payload),
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
// 4. Create new user
|
|
117
|
-
const user = await adapter.createUser({
|
|
118
|
-
email:ctx.email,
|
|
119
|
-
provider:ctx.provider,
|
|
120
|
-
providerId:ctx.providerId,
|
|
121
|
-
});
|
|
122
|
-
if (!ctx.providerId) {
|
|
123
|
-
throw new Error("Missing providerId");
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const payload = { userId: user.id };
|
|
127
|
-
auditLogs.push({
|
|
128
|
-
userId: user.id,
|
|
129
|
-
action: "USER_CREATED",
|
|
130
|
-
timestamp: Date.now(),
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
return {
|
|
134
|
-
type:"NEW_USER_CREATION",
|
|
135
|
-
user,
|
|
136
|
-
accessToken:tokenService.signAccessToken(payload),
|
|
137
|
-
refreshToken:await tokenService.generateRefreshToken(payload)
|
|
138
|
-
};
|
|
139
|
-
},
|
|
140
|
-
};
|
|
141
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { findUserByEmail, findUserByProvider, createUser, linkProvider } from "../userStore";
|
|
2
|
-
import { identityPolicy } from "./identityPolicy";
|
|
3
|
-
import { OAuthProfile } from "../types";
|
|
4
|
-
|
|
5
|
-
export async function resolveIdentity(profile: OAuthProfile) {
|
|
6
|
-
const { email, provider, providerId } = profile;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const byProvider = await findUserByProvider(provider, providerId);
|
|
10
|
-
if (byProvider) {
|
|
11
|
-
return byProvider;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const existingUser = await findUserByEmail(email);
|
|
16
|
-
|
|
17
|
-
if (!existingUser) {
|
|
18
|
-
return createUser({
|
|
19
|
-
email,
|
|
20
|
-
provider,
|
|
21
|
-
providerId,
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const isVerifiedEmail = true;
|
|
28
|
-
|
|
29
|
-
if (
|
|
30
|
-
identityPolicy.autoLinkOnVerifiedEmailMatch &&
|
|
31
|
-
isVerifiedEmail
|
|
32
|
-
) {
|
|
33
|
-
await linkProvider(existingUser.id, provider, providerId);
|
|
34
|
-
return existingUser;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return {
|
|
39
|
-
type: "IDENTITY_CONFLICT",
|
|
40
|
-
user: existingUser,
|
|
41
|
-
message: "Account exists. Explicit linking required.",
|
|
42
|
-
};
|
|
43
|
-
}
|
package/src/oauth/brain/types.ts
DELETED
package/src/oauth/core.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { createGoogleProvider } from "./providers/google";
|
|
2
|
-
import { GoogleOAuthConfig,GitHubOAuthConfig } from "./types";
|
|
3
|
-
import type { Redis as RedisClient } from "ioredis";
|
|
4
|
-
import { OAuthProfile } from "./types";
|
|
5
|
-
import type { IdentityEngine } from "./brain/types";
|
|
6
|
-
|
|
7
|
-
import { createGitHubProvider } from "./providers/github";
|
|
8
|
-
|
|
9
|
-
export function createOAuth(config:{google?: GoogleOAuthConfig;github?:GitHubOAuthConfig;redisClient:RedisClient;identityEngine:IdentityEngine} ) {
|
|
10
|
-
return {
|
|
11
|
-
google: config.google
|
|
12
|
-
? createGoogleProvider(config.google,config.redisClient,config.identityEngine)
|
|
13
|
-
: undefined,
|
|
14
|
-
github: config.github
|
|
15
|
-
? createGitHubProvider(config.github,config.redisClient,config.identityEngine)
|
|
16
|
-
: undefined,
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
|
package/src/oauth/identity/1
DELETED
|
@@ -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
|
-
};
|