generated from langchain-ai/new-langgraphjs-project
-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathauth-server.ts
270 lines (236 loc) · 7.4 KB
/
auth-server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import express, { Request, Response, NextFunction } from "express";
import passport from "passport";
import { Strategy as TwitterStrategy } from "passport-twitter";
import session from "express-session";
import dotenv from "dotenv";
dotenv.config();
// Extend Express Session type
declare module "express-session" {
interface SessionData {
linkedinToken?: string;
linkedinUserInfo?: any;
}
}
interface TwitterUser {
id: string;
username: string;
displayName: string;
photos?: { value: string }[];
_json: {
id_str: string;
screen_name: string;
name: string;
profile_image_url_https?: string;
};
}
export class SocialAuthServer {
private app: express.Application;
private port: number;
constructor(port = 3000) {
this.app = express();
this.port = port;
this.configureMiddleware();
this.configurePassport();
this.setupRoutes();
}
private configureMiddleware(): void {
this.app.use(
session({
secret: process.env.SESSION_SECRET || "your-session-secret",
resave: true,
saveUninitialized: true,
cookie: {
secure: false,
maxAge: 60000,
},
}),
);
this.app.use(passport.initialize());
this.app.use(passport.session());
}
private configurePassport(): void {
this.configureTwitterStrategy();
this.configureLinkedInStrategy();
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser<any>((user, done) => {
done(null, user);
});
}
private configureTwitterStrategy(): void {
if (!process.env.TWITTER_API_KEY || !process.env.TWITTER_API_KEY_SECRET) {
throw new Error("Twitter API credentials are not configured");
}
passport.use(
new TwitterStrategy(
{
consumerKey: process.env.TWITTER_API_KEY,
consumerSecret: process.env.TWITTER_API_KEY_SECRET,
callbackURL: `http://localhost:${this.port}/auth/twitter/callback`,
},
(
token: string,
tokenSecret: string,
profile: TwitterUser,
done: (error: any, user?: any) => void,
) => {
const user = {
...profile,
token,
tokenSecret,
};
console.log("\n✅ TWITTER USER AUTHENTICATED ✅\n");
console.dir(user, { depth: null });
return done(null, user);
},
),
);
}
private configureLinkedInStrategy(): void {
if (
!process.env.LINKEDIN_CLIENT_ID ||
!process.env.LINKEDIN_CLIENT_SECRET
) {
throw new Error("LinkedIn API credentials are not configured");
}
// We'll handle LinkedIn authentication directly through routes
// instead of using passport strategy since we're implementing
// the OAuth flow manually
}
private setupRoutes(): void {
this.app.get("/", (_req: Request, res: Response) => {
res.send(
'<a href="/auth/twitter">Login with Twitter</a><br><a href="/auth/linkedin">Login with LinkedIn</a>',
);
});
// Twitter routes
this.app.get("/auth/twitter", passport.authenticate("twitter"));
this.app.get("/auth/twitter/callback", (req: Request, res: Response) => {
passport.authenticate("twitter", {
failureRedirect: "/login",
})(req, res, (err: any) => {
if (err) {
console.error("Authentication error:", err);
return res.redirect("/");
}
res.redirect("/");
});
});
// LinkedIn routes
this.app.get("/auth/linkedin", (_req: Request, res: Response) => {
const authUrl = new URL(
"https://www.linkedin.com/oauth/v2/authorization",
);
authUrl.searchParams.append("response_type", "code");
authUrl.searchParams.append("client_id", process.env.LINKEDIN_CLIENT_ID!);
authUrl.searchParams.append(
"redirect_uri",
"http://localhost:3000/auth/linkedin/callback",
);
authUrl.searchParams.append("state", process.env.SESSION_SECRET!);
authUrl.searchParams.append(
"scope",
"openid profile email w_member_social",
// Use these scopes if you plan on posting to a LinkedIn company page.
// Posting to company pages requires the "w_organization_social" scope.
// "openid profile email w_member_social w_organization_social",
);
res.redirect(authUrl.toString());
});
this.app.get("/auth/linkedin/callback", (req: Request, res: Response) => {
const { code, state } = req.query;
if (state !== process.env.SESSION_SECRET) {
return res
.status(401)
.send("State mismatch. Possible CSRF attack.") as any;
}
if (!code) {
return res.redirect("/");
}
// Handle the LinkedIn OAuth token exchange
(async () => {
try {
const tokenResponse = await fetch(
"https://www.linkedin.com/oauth/v2/accessToken",
{
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
grant_type: "authorization_code",
code: code as string,
client_id: process.env.LINKEDIN_CLIENT_ID!,
client_secret: process.env.LINKEDIN_CLIENT_SECRET!,
redirect_uri: "http://localhost:3000/auth/linkedin/callback",
}),
},
);
if (!tokenResponse.ok) {
throw new Error("Failed to get access token");
}
const data = await tokenResponse.json();
console.log("\n✅ LINKEDIN USER AUTHENTICATED ✅\n");
console.dir(data, { depth: null });
// Fetch user info using OpenID Connect
const userInfoResponse = await fetch(
"https://api.linkedin.com/v2/userinfo",
{
headers: {
Authorization: `Bearer ${data.access_token}`,
},
},
);
if (!userInfoResponse.ok) {
throw new Error("Failed to get user info");
}
const userInfo = await userInfoResponse.json();
console.log("\n✅ LINKEDIN USER INFO ✅\n");
console.dir(userInfo, { depth: null });
// Store both the token and user info in session
if (req.session) {
req.session.linkedinToken = data.access_token;
req.session.linkedinUserInfo = userInfo;
}
res.redirect("/");
} catch (error) {
console.error("LinkedIn authentication error:", error);
res.redirect("/");
}
})().catch(console.error);
});
this.app.get(
"/profile",
this.ensureAuthenticated,
(req: Request, res: Response) => {
res.json(req.user);
},
);
}
private ensureAuthenticated(
req: Request,
res: Response,
next: NextFunction,
): void {
if (req.isAuthenticated()) {
return next();
}
res.redirect("/");
}
public start(): void {
this.app.listen(this.port, () => {
console.log(
`Social authentication server is running on port ${this.port}. Go to http://localhost:${this.port} to login`,
);
});
}
}
async function main() {
const server = new SocialAuthServer();
server.start();
}
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch(console.error);
}
export default SocialAuthServer;