Fix GitHub OAuth callback type errors completely
- Add USED_CALLBACK_URL export to fix type error - Update the callback route to use the exported URL - Enhance update-package.js to fix all OAuth-related issues 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
5957e8104c
commit
60f8a87028
@ -1,6 +1,6 @@
|
|||||||
import { lucia } from "@/server/auth";
|
import { lucia } from "@/server/auth";
|
||||||
import { type GitHubUserResult, github, isValidCallbackHost } from "@/server/auth/oauth";
|
import { type GitHubUserResult, github, isValidCallbackHost, USED_CALLBACK_URL } from "@/server/auth/oauth";
|
||||||
import { ALLOWED_CALLBACK_HOSTS, OAUTH_CALLBACK_URL } from "@/utils/api-config";
|
import { ALLOWED_CALLBACK_HOSTS } from "@/utils/api-config";
|
||||||
import { db } from "@/server/db";
|
import { db } from "@/server/db";
|
||||||
import { users } from "@/server/db/schema";
|
import { users } from "@/server/db/schema";
|
||||||
import { OAuth2RequestError } from "arctic";
|
import { OAuth2RequestError } from "arctic";
|
||||||
@ -45,7 +45,7 @@ export async function GET(request: Request): Promise<Response> {
|
|||||||
const tokens = await github.validateAuthorizationCode(code);
|
const tokens = await github.validateAuthorizationCode(code);
|
||||||
|
|
||||||
// Log zur Fehlersuche
|
// Log zur Fehlersuche
|
||||||
console.log("GitHub OAuth Token-Validierung erfolgreich");
|
console.log(`GitHub OAuth Token-Validierung erfolgreich, verwendete Callback-URL: ${USED_CALLBACK_URL}`);
|
||||||
|
|
||||||
const githubUserResponse = await fetch("https://git.i.mercedes-benz.com/api/v3/user", {
|
const githubUserResponse = await fetch("https://git.i.mercedes-benz.com/api/v3/user", {
|
||||||
headers: {
|
headers: {
|
||||||
|
@ -21,13 +21,16 @@ const getCallbackUrl = () => {
|
|||||||
return OAUTH_CALLBACK_URL;
|
return OAUTH_CALLBACK_URL;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Berechne die Callback-URL
|
||||||
|
export const USED_CALLBACK_URL = getCallbackUrl();
|
||||||
|
|
||||||
// Erstelle GitHub OAuth-Client mit expliziter Redirect-URI
|
// Erstelle GitHub OAuth-Client mit expliziter Redirect-URI
|
||||||
export const github = new GitHub(
|
export const github = new GitHub(
|
||||||
process.env.OAUTH_CLIENT_ID as string,
|
process.env.OAUTH_CLIENT_ID as string,
|
||||||
process.env.OAUTH_CLIENT_SECRET as string,
|
process.env.OAUTH_CLIENT_SECRET as string,
|
||||||
{
|
{
|
||||||
enterpriseDomain: "https://git.i.mercedes-benz.com",
|
enterpriseDomain: "https://git.i.mercedes-benz.com",
|
||||||
redirectURI: getCallbackUrl(),
|
redirectURI: USED_CALLBACK_URL,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -10,31 +10,85 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
// Pfad zur OAuth-Callback-Route
|
// Pfad zur OAuth-Callback-Route und OAuth-Konfiguration
|
||||||
const callbackRoutePath = path.join(__dirname, 'src/app/auth/login/callback/route.ts');
|
const callbackRoutePath = path.join(__dirname, 'src/app/auth/login/callback/route.ts');
|
||||||
|
const oauthConfigPath = path.join(__dirname, 'src/server/auth/oauth.ts');
|
||||||
|
|
||||||
// Lese die aktuelle Datei
|
// Aktualisiere die OAuth-Konfiguration
|
||||||
try {
|
try {
|
||||||
let content = fs.readFileSync(callbackRoutePath, 'utf8');
|
// 1. Prüfe, ob wir die USED_CALLBACK_URL exportieren müssen
|
||||||
|
let oauthContent = fs.readFileSync(oauthConfigPath, 'utf8');
|
||||||
|
|
||||||
// Prüfe, ob die Datei den fehlerhaften Code enthält
|
if (!oauthContent.includes('export const USED_CALLBACK_URL')) {
|
||||||
if (content.includes('await github.validateAuthorizationCode(code, OAUTH_CALLBACK_URL)')) {
|
console.log('✅ Aktualisiere OAuth-Konfiguration...');
|
||||||
console.log('✅ Aktualisiere OAuth-Callback-Route...');
|
|
||||||
|
|
||||||
// Ersetze den fehlerhaften Code
|
// Füge die USED_CALLBACK_URL-Export hinzu
|
||||||
content = content.replace(
|
oauthContent = oauthContent.replace(
|
||||||
/await github\.validateAuthorizationCode\(code, OAUTH_CALLBACK_URL\);/g,
|
'// Erstelle GitHub OAuth-Client mit expliziter Redirect-URI',
|
||||||
'await github.validateAuthorizationCode(code);'
|
'// Berechne die Callback-URL\nexport const USED_CALLBACK_URL = getCallbackUrl();\n\n// Erstelle GitHub OAuth-Client mit expliziter Redirect-URI'
|
||||||
);
|
|
||||||
|
|
||||||
// Aktualisiere auch die Logging-Nachricht
|
|
||||||
content = content.replace(
|
|
||||||
/console\.log\(`GitHub OAuth Token-Validierung mit Callback-URL: \${OAUTH_CALLBACK_URL}\`\);/g,
|
|
||||||
'console.log("GitHub OAuth Token-Validierung erfolgreich");'
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Schreibe die aktualisierte Datei
|
// Schreibe die aktualisierte Datei
|
||||||
fs.writeFileSync(callbackRoutePath, content, 'utf8');
|
fs.writeFileSync(oauthConfigPath, oauthContent, 'utf8');
|
||||||
|
console.log('✅ OAuth-Konfiguration erfolgreich aktualisiert.');
|
||||||
|
} else {
|
||||||
|
console.log('ℹ️ OAuth-Konfiguration ist bereits aktuell.');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Fehler beim Aktualisieren der OAuth-Konfiguration:', error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Aktualisiere die OAuth-Callback-Route
|
||||||
|
try {
|
||||||
|
let callbackContent = fs.readFileSync(callbackRoutePath, 'utf8');
|
||||||
|
|
||||||
|
// Prüfe, ob Änderungen nötig sind
|
||||||
|
const needsUpdate =
|
||||||
|
callbackContent.includes('await github.validateAuthorizationCode(code, OAUTH_CALLBACK_URL)') ||
|
||||||
|
!callbackContent.includes('USED_CALLBACK_URL');
|
||||||
|
|
||||||
|
if (needsUpdate) {
|
||||||
|
console.log('✅ Aktualisiere OAuth-Callback-Route...');
|
||||||
|
|
||||||
|
// 1. Aktualisiere den Import
|
||||||
|
if (!callbackContent.includes('USED_CALLBACK_URL')) {
|
||||||
|
callbackContent = callbackContent.replace(
|
||||||
|
'import { type GitHubUserResult, github, isValidCallbackHost } from "@/server/auth/oauth";',
|
||||||
|
'import { type GitHubUserResult, github, isValidCallbackHost, USED_CALLBACK_URL } from "@/server/auth/oauth";'
|
||||||
|
);
|
||||||
|
|
||||||
|
// Entferne den OAUTH_CALLBACK_URL-Import, wenn er nicht mehr benötigt wird
|
||||||
|
if (callbackContent.includes('OAUTH_CALLBACK_URL')) {
|
||||||
|
callbackContent = callbackContent.replace(
|
||||||
|
', OAUTH_CALLBACK_URL } from "@/utils/api-config"',
|
||||||
|
' } from "@/utils/api-config"'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Korrigiere die validateAuthorizationCode-Funktion
|
||||||
|
if (callbackContent.includes('await github.validateAuthorizationCode(code, OAUTH_CALLBACK_URL)')) {
|
||||||
|
callbackContent = callbackContent.replace(
|
||||||
|
'await github.validateAuthorizationCode(code, OAUTH_CALLBACK_URL)',
|
||||||
|
'await github.validateAuthorizationCode(code)'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Aktualisiere die Logging-Nachricht
|
||||||
|
if (callbackContent.includes('console.log(`GitHub OAuth Token-Validierung mit Callback-URL: ${OAUTH_CALLBACK_URL}`)')) {
|
||||||
|
callbackContent = callbackContent.replace(
|
||||||
|
'console.log(`GitHub OAuth Token-Validierung mit Callback-URL: ${OAUTH_CALLBACK_URL}`)',
|
||||||
|
'console.log(`GitHub OAuth Token-Validierung erfolgreich, verwendete Callback-URL: ${USED_CALLBACK_URL}`)'
|
||||||
|
);
|
||||||
|
} else if (callbackContent.includes('console.log("GitHub OAuth Token-Validierung erfolgreich")')) {
|
||||||
|
callbackContent = callbackContent.replace(
|
||||||
|
'console.log("GitHub OAuth Token-Validierung erfolgreich")',
|
||||||
|
'console.log(`GitHub OAuth Token-Validierung erfolgreich, verwendete Callback-URL: ${USED_CALLBACK_URL}`)'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schreibe die aktualisierte Datei
|
||||||
|
fs.writeFileSync(callbackRoutePath, callbackContent, 'utf8');
|
||||||
console.log('✅ OAuth-Callback-Route erfolgreich aktualisiert.');
|
console.log('✅ OAuth-Callback-Route erfolgreich aktualisiert.');
|
||||||
} else {
|
} else {
|
||||||
console.log('ℹ️ OAuth-Callback-Route ist bereits aktuell.');
|
console.log('ℹ️ OAuth-Callback-Route ist bereits aktuell.');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user