Bulk commit: November work

This commit is contained in:
2025-11-06 11:46:35 +01:00
parent cf82dede3b
commit 183875baf4
60 changed files with 16590 additions and 102 deletions

View File

@ -2,10 +2,9 @@ package utils
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"slices"
"time"
"git.kocoder.xyz/kocoded/vt/model"
@ -31,6 +30,9 @@ func setCallbackCookieExp(w http.ResponseWriter, r *http.Request, name, value st
http.SetCookie(w, c)
}
type Info struct {
}
func CreateOIDCClient(ctx context.Context, app *fiber.App, appCtx Application) {
provider, err := oidc.NewProvider(ctx, "https://keycloak.kocoder.xyz/realms/che")
if err != nil {
@ -40,7 +42,7 @@ func CreateOIDCClient(ctx context.Context, app *fiber.App, appCtx Application) {
oauthConfig := oauth2.Config{
ClientID: os.Getenv("CLIENT_ID"),
ClientSecret: os.Getenv("CLIENT_SECRET"),
RedirectURL: "http://localhost:3000/api/auth/callback",
RedirectURL: os.Getenv("BACKEND_URI") + "/api/auth/callback",
Endpoint: provider.Endpoint(),
@ -92,15 +94,15 @@ func CreateOIDCClient(ctx context.Context, app *fiber.App, appCtx Application) {
UserInfo *oidc.UserInfo
}{oauth2Token, userInfo}
data, err := json.MarshalIndent(resp, "", " ")
claims := &model.User{}
err = resp.UserInfo.Claims(claims)
if err != nil {
appCtx.Logger.Warn("Failed to parse JSON", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
panic(err)
}
fmt.Println(claims)
user := &model.User{}
if appCtx.DB.Where(model.User{Email: resp.UserInfo.Email}).Assign(model.User{Sub: resp.UserInfo.Subject}).FirstOrCreate(user).Error != nil {
if appCtx.DB.Where(model.User{Email: resp.UserInfo.Email}).Assign(claims).FirstOrCreate(user).Error != nil {
appCtx.Logger.Warn("Failed to create user in DB")
http.Error(w, "failed to create user", http.StatusInternalServerError)
return
@ -116,25 +118,56 @@ func CreateOIDCClient(ctx context.Context, app *fiber.App, appCtx Application) {
}
setCallbackCookieExp(w, r, "auth-cookie", cookie, int(time.Hour.Seconds()))
appCtx.ActiveSessions = append(appCtx.ActiveSessions, Session{Token: cookie, UserID: user.ID, CreatedAt: time.Now()})
appCtx.AddSession(&Session{Token: cookie, UserID: user.ID, MandantId: 1})
http.Redirect(w, r, "http://localhost:3001", http.StatusFound)
_, err = w.Write(data)
if err != nil {
appCtx.Logger.Error("Unable to send response", "error", err)
}
http.Redirect(w, r, os.Getenv("FRONTEND_URI")+"/dashboard", http.StatusFound)
}))
app.Get("/api/auth/currentSession", func(c *fiber.Ctx) error {
authToken := c.Cookies("auth-cookie")
sessionId := slices.IndexFunc(appCtx.ActiveSessions, func(s Session) bool {
return s.Token == authToken
})
session := appCtx.ActiveSessions[sessionId]
session, err := appCtx.GetSessionFromToken(authToken)
if err != nil {
return err
}
return c.JSON(session)
})
app.Get("/api/auth/logout", func(c *fiber.Ctx) error {
authToken := c.Cookies("auth-cookie")
appCtx.RemoveSession(authToken)
cookie := new(fiber.Cookie)
cookie.Name = "auth-cookie"
cookie.Expires = time.Now().Add(-1 * time.Minute)
c.Cookie(cookie)
return c.Redirect(os.Getenv("FRONTEND_URI"))
})
}
type keyType struct{}
var UserKey keyType
func IsAuthenticated(appCtx Application) fiber.Handler {
fmt.Println("Gettings Session")
return func(c *fiber.Ctx) error {
authToken := c.Cookies("auth-cookie")
fmt.Println("Gettings Session", "sessiontoken", authToken)
session, err := appCtx.GetSessionFromToken(authToken)
if err != nil {
appCtx.Logger.Warn("Unauthorized GET Attempt", "reason", err)
return c.SendStatus(fiber.StatusUnauthorized)
}
fmt.Println("Saving Session", "session", session)
c.Locals("USER_KEY", session)
return c.Next()
}
}