From bcb820ea9bce1997a2e36e340042bbb2fa278fb7 Mon Sep 17 00:00:00 2001 From: fanmuchen Date: Thu, 8 May 2025 15:20:36 +0800 Subject: [PATCH] Cleanup after implementing initial logto funtions. --- backend/internal/handlers/logto.go | 94 ++++++++++++++++++-- frontend/src/app/components/AuthStatus.tsx | 52 ----------- frontend/src/app/components/HealthStatus.tsx | 19 ---- frontend/src/app/globals.css | 3 + frontend/src/app/page.tsx | 27 ------ 5 files changed, 90 insertions(+), 105 deletions(-) diff --git a/backend/internal/handlers/logto.go b/backend/internal/handlers/logto.go index ff30d74..16e838b 100644 --- a/backend/internal/handlers/logto.go +++ b/backend/internal/handlers/logto.go @@ -1,6 +1,7 @@ package handlers import ( + "fmt" "net/http" "os" @@ -44,18 +45,97 @@ func getLogtoConfig() *client.LogtoConfig { } // HomeHandler shows auth state and sign-in/sign-out links +// Note: The /service/auth endpoint is for debugging purposes only func HomeHandler(ctx *gin.Context) { session := sessions.Default(ctx) logtoClient := client.NewLogtoClient(getLogtoConfig(), &SessionStorage{session: session}) - authState := "You are not logged in to this website. :(" + + var debugInfo string + debugInfo = "

Logto Auth Debugging Page

" + + // Basic auth state if logtoClient.IsAuthenticated() { - authState = "You are logged in to this website! :)" + debugInfo += "
Authentication Status: Logged In ✓
" + + // Get ID Token claims + idTokenClaims, err := logtoClient.GetIdTokenClaims() + if err != nil { + debugInfo += "
Error fetching ID token claims: " + err.Error() + "
" + } else { + debugInfo += "

ID Token Claims

" + debugInfo += "
"
+			
+			// Display claims in a simpler format
+			debugInfo += ""
+			debugInfo += ""
+			
+			// Display the raw claims directly without any checks that might cause linter errors
+			debugInfo += fmt.Sprintf("", idTokenClaims.Sub)
+			debugInfo += fmt.Sprintf("", idTokenClaims.Name)
+			debugInfo += fmt.Sprintf("", idTokenClaims.Email)
+			debugInfo += fmt.Sprintf("", idTokenClaims.Iss)
+			debugInfo += fmt.Sprintf("", idTokenClaims.Aud)
+			debugInfo += fmt.Sprintf("", idTokenClaims.Exp)
+			debugInfo += fmt.Sprintf("", idTokenClaims.Iat)
+			
+			debugInfo += "
ClaimValue
sub%v
name%v
email%v
issuer%v
audience%v
expires at%v
issued at%v
" + debugInfo += "
" + } + + // Try to get access token info + hasAccessToken := false + var accessTokenErr error + _, accessTokenErr = logtoClient.GetAccessToken("") + if accessTokenErr == nil { + hasAccessToken = true + } + + debugInfo += "

Access Token Information

" + if hasAccessToken { + debugInfo += "
Access Token: Present (not displayed for security)
" + } else { + debugInfo += "
No resource-specific access token available
" + } + + // Session information + debugInfo += "

Session Information

" + debugInfo += "
"
+		
+		// Get all keys from the session
+		for _, key := range []string{"idToken", "accessToken", "refreshToken", "expiresAt"} {
+			value := session.Get(key)
+			debugInfo += "
" + key + ": " + if value == nil { + debugInfo += "Not set" + } else if strValue, ok := value.(string); ok { + if len(strValue) > 100 { + debugInfo += strValue[:100] + "..." + } else { + debugInfo += strValue + } + } else { + debugInfo += "Set (non-string value)" + } + debugInfo += "
" + } + debugInfo += "
" + } else { + debugInfo += "
Authentication Status: Not Logged In ✗
" + debugInfo += "
Sign in to see detailed authentication information
" } - homePage := "

Hello Logto

" + - "
" + authState + "
" + - `
Sign In
` + - `
Sign Out
` - ctx.Data(http.StatusOK, "text/html; charset=utf-8", []byte(homePage)) + + // Config information (excluding secrets) + debugInfo += "

Logto Configuration

" + debugInfo += "
Endpoint: " + os.Getenv("LOGTO_ENDPOINT") + "
" + debugInfo += "
App ID: " + os.Getenv("LOGTO_APP_ID") + "
" + debugInfo += "
Redirect URI: " + os.Getenv("LOGTO_REDIRECT_URI") + "
" + + // Add links for authentication actions + debugInfo += "

Authentication Actions

" + debugInfo += `
Sign In
` + debugInfo += `
Sign Out
` + + ctx.Data(http.StatusOK, "text/html; charset=utf-8", []byte(debugInfo)) } // SignInHandler starts the Logto sign-in flow diff --git a/frontend/src/app/components/AuthStatus.tsx b/frontend/src/app/components/AuthStatus.tsx index ab9a78b..2251de2 100644 --- a/frontend/src/app/components/AuthStatus.tsx +++ b/frontend/src/app/components/AuthStatus.tsx @@ -41,58 +41,6 @@ export default function AuthStatus() { )} - - ); } diff --git a/frontend/src/app/components/HealthStatus.tsx b/frontend/src/app/components/HealthStatus.tsx index 4064627..0b11df3 100644 --- a/frontend/src/app/components/HealthStatus.tsx +++ b/frontend/src/app/components/HealthStatus.tsx @@ -51,25 +51,6 @@ export default function HealthStatus() {

)} - ); } diff --git a/frontend/src/app/globals.css b/frontend/src/app/globals.css index e69de29..b5c61c9 100644 --- a/frontend/src/app/globals.css +++ b/frontend/src/app/globals.css @@ -0,0 +1,3 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; diff --git a/frontend/src/app/page.tsx b/frontend/src/app/page.tsx index 8868c56..28d9f4e 100644 --- a/frontend/src/app/page.tsx +++ b/frontend/src/app/page.tsx @@ -16,33 +16,6 @@ export default function Home() {

Backend Health

- - ); }