Add Keycloak integration for logout functionality and create login/logout HTML components
This commit is contained in:
32
cmd/start.go
32
cmd/start.go
@ -19,6 +19,7 @@ package cmd
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"context"
|
||||
|
||||
@ -80,6 +81,37 @@ var startCmd = &cobra.Command{
|
||||
// Register handlers
|
||||
httpRequestRouter.HandleFunc("/login", middleware.LoginHandler(authConfig))
|
||||
httpRequestRouter.HandleFunc("/callback", middleware.CallbackHandler(authConfig))
|
||||
// Update the logout handler to include Keycloak integration
|
||||
httpRequestRouter.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) {
|
||||
// 1. Get session and immediately expire it
|
||||
session, _ := authConfig.Store.Get(r, "auth-session")
|
||||
session.Values["authenticated"] = false
|
||||
session.Options.MaxAge = -1 // Immediate deletion
|
||||
session.Save(r, w)
|
||||
|
||||
// 2. Keycloak logout parameters
|
||||
keycloakLogoutURL, err := url.Parse(viper.GetString("issuer-url") + "/protocol/openid-connect/logout")
|
||||
if err != nil {
|
||||
log.Printf("Error parsing logout URL: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// 3. Build logout URL with post_logout_redirect_uri and id_token_hint
|
||||
q := keycloakLogoutURL.Query()
|
||||
q.Set("post_logout_redirect_uri", viper.GetString("redirect-url"))
|
||||
q.Set("client_id", viper.GetString("client-id"))
|
||||
|
||||
// Retrieve ID token from session if available
|
||||
if idToken, ok := session.Values["id_token"].(string); ok {
|
||||
q.Set("id_token_hint", idToken)
|
||||
}
|
||||
|
||||
keycloakLogoutURL.RawQuery = q.Encode()
|
||||
|
||||
// 4. Redirect to Keycloak for global session termination
|
||||
http.Redirect(w, r, keycloakLogoutURL.String(), http.StatusFound)
|
||||
})
|
||||
|
||||
// Update middleware stack
|
||||
stack := middleware.CreateStack(
|
||||
|
51
site/components/dashboard.html
Normal file
51
site/components/dashboard.html
Normal file
@ -0,0 +1,51 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Dashboard</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
header {
|
||||
background-color: #f0f2f5;
|
||||
padding: 1rem;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||
}
|
||||
.user-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
.user-info span {
|
||||
margin-right: 1rem;
|
||||
}
|
||||
.btn-logout {
|
||||
background-color: #f44336;
|
||||
color: white;
|
||||
padding: 8px 16px;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
}
|
||||
.btn-logout:hover {
|
||||
background-color: #d32f2f;
|
||||
}
|
||||
</style>
|
||||
<script src="https://unpkg.com/htmx.org"></script>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<div class="user-info">
|
||||
<span>Welcome, User!</span>
|
||||
<a href="/logout" class="btn-logout">Logout</a>
|
||||
</div>
|
||||
</header>
|
||||
<!-- Rest of your protected content -->
|
||||
</body>
|
||||
</html>
|
48
site/components/login.html
Normal file
48
site/components/login.html
Normal file
@ -0,0 +1,48 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Login</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 100vh;
|
||||
margin: 0;
|
||||
background-color: #f0f2f5;
|
||||
}
|
||||
.login-container {
|
||||
background: white;
|
||||
padding: 2rem;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||
text-align: center;
|
||||
}
|
||||
.login-button {
|
||||
background-color: #4CAF50;
|
||||
color: white;
|
||||
padding: 14px 20px;
|
||||
margin: 8px 0;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
}
|
||||
.login-button:hover {
|
||||
background-color: #45a049;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="login-container">
|
||||
<h1>Welcome</h1>
|
||||
<p>Please log in to continue</p>
|
||||
<button class="login-button" onclick="window.location.href='/login'">
|
||||
Login with Keycloak
|
||||
</button>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
48
site/components/logout.html
Normal file
48
site/components/logout.html
Normal file
@ -0,0 +1,48 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Logout</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 100vh;
|
||||
margin: 0;
|
||||
background-color: #f0f2f5;
|
||||
}
|
||||
.logout-container {
|
||||
background: white;
|
||||
padding: 2rem;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||
text-align: center;
|
||||
}
|
||||
.logout-button {
|
||||
background-color: #f44336;
|
||||
color: white;
|
||||
padding: 14px 20px;
|
||||
margin: 8px 0;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
}
|
||||
.logout-button:hover {
|
||||
background-color: #d32f2f;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="logout-container">
|
||||
<h1>Logout</h1>
|
||||
<p>Are you sure you want to log out?</p>
|
||||
<button class="logout-button" onclick="window.location.href='/logout'">
|
||||
Logout
|
||||
</button>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user