Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prod Release 12/16 #536

Merged
merged 6 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 0 additions & 66 deletions booking-app/app/api/nyu/auth/token/route.ts

This file was deleted.

24 changes: 5 additions & 19 deletions booking-app/app/api/nyu/identity/[uniqueId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ensureNYUToken } from "@/lib/server/nyuApiAuth";
import { NYUTokenManager } from "@/lib/server/nyuTokenCache";
import { getNYUToken } from "@/lib/server/nyuApiAuth";
import { NextRequest, NextResponse } from "next/server";

const NYU_API_BASE = "https://api.nyu.edu/identity-v2-sys";
Expand All @@ -9,16 +8,15 @@ export async function GET(
{ params }: { params: { uniqueId: string } },
) {
try {
const authResult = await ensureNYUToken();
if (!authResult.isAuthenticated || !authResult.token) {
const token = await getNYUToken();
if (!token) {
return NextResponse.json(
{ error: authResult.error || "Authentication required" },
{ error: "Authentication failed" },
{ status: 401 },
);
}

const apiAccessId = process.env.NYU_API_ACCESS_ID;

if (!apiAccessId) {
return NextResponse.json(
{ error: "API access ID not configured" },
Expand All @@ -33,32 +31,20 @@ export async function GET(

const response = await fetch(url.toString(), {
headers: {
Authorization: `Bearer ${authResult.token}`,
Authorization: `Bearer ${token}`,
Accept: "application/json",
},
});
console.log("response", response);

if (!response.ok) {
const errorText = await response.text();
console.error("NYU Identity API Error:", {
status: response.status,
body: errorText,
uniqueId: params.uniqueId,
});

if (response.status === 401) {
NYUTokenManager.getInstance().clearToken();
}

return NextResponse.json(
{ error: `NYU API call failed: ${response.status}` },
{ status: response.status },
);
}

const userData = await response.json();

return NextResponse.json(userData);
} catch (error) {
console.error("Identity API error:", error);
Expand Down
64 changes: 40 additions & 24 deletions booking-app/lib/server/nyuApiAuth.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
import { AuthResult } from "@/components/src/types";
import { NYUTokenManager } from "./nyuTokenCache";
const NYU_AUTH_URL = "https://auth.nyu.edu/oauth2/token";

export async function ensureNYUToken(): Promise<AuthResult> {
export async function getNYUToken(): Promise<string | null> {
try {
const tokenManager = NYUTokenManager.getInstance();
const tokenCache = await tokenManager.getToken();
const clientId = process.env.NYU_API_CLIENT_ID;
const clientSecret = process.env.NYU_API_CLIENT_SECRET;
const username = process.env.NYU_API_USER_NAME;
const password = process.env.NYU_API_PASSWORD;

if (!tokenCache) {
return {
isAuthenticated: false,
token: "",
expiresAt: "",
error: "Failed to get token",
};
if (!clientId || !clientSecret || !username || !password) {
throw new Error("NYU credentials not configured");
}

return {
isAuthenticated: true,
token: tokenCache.access_token,
expiresAt: new Date(tokenCache.expires_at).toISOString(),
};
const basicAuth = Buffer.from(`${clientId}:${clientSecret}`).toString(
"base64"
);

const params = new URLSearchParams({
grant_type: "password",
username,
password,
scope: "openid",
});

const response = await fetch(NYU_AUTH_URL, {
method: "POST",
headers: {
Authorization: `Basic ${basicAuth}`,
"Content-Type": "application/x-www-form-urlencoded",
},
cache: "no-store",
next: { revalidate: 0 },
body: params.toString(),
});

if (!response.ok) {
console.log("Error response", response);
throw new Error(`Token fetch failed: ${response.status}`);
}

const data = await response.json();
console.log("token", data.access_token);
return data.access_token;
} catch (error) {
console.error("NYU Auth error:", error);
return {
isAuthenticated: false,
token: "",
expiresAt: "",
error: error instanceof Error ? error.message : "Internal error",
};
console.error("Failed to get NYU token:", error);
return null;
}
}
12 changes: 6 additions & 6 deletions booking-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading