Skip to content

Commit

Permalink
Merge pull request #536 from ITPNYU/main
Browse files Browse the repository at this point in the history
Prod Release 12/16
  • Loading branch information
rlho authored Dec 16, 2024
2 parents 5890511 + b3598e4 commit a8d5ce0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 115 deletions.
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.

0 comments on commit a8d5ce0

Please sign in to comment.