refactor: set auth cookie for every api, and optionally pass a specific api name to only set instead

This commit is contained in:
Derrick Hammer 2024-03-17 08:59:34 -04:00
parent 325a368dea
commit 2a8c036dc6
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 25 additions and 9 deletions

View File

@ -8,6 +8,10 @@ import (
"strconv" "strconv"
"time" "time"
"git.lumeweb.com/LumeWeb/portal/api/router"
apiRegistry "git.lumeweb.com/LumeWeb/portal/api/registry"
"github.com/golang-jwt/jwt/v5" "github.com/golang-jwt/jwt/v5"
) )
@ -93,7 +97,17 @@ func JWTVerifyToken(token string, domain string, privateKey ed25519.PrivateKey,
return claim, err return claim, err
} }
func SetAuthCookie(w http.ResponseWriter, name, token string) { func SetAuthCookie(w http.ResponseWriter, token string, apiName string) {
for name, api := range apiRegistry.GetAllAPIs() {
routeableApi, ok := api.(router.RoutableAPI)
if !ok {
continue
}
if len(apiName) > 0 && apiName != name {
continue
}
http.SetCookie(w, &http.Cookie{ http.SetCookie(w, &http.Cookie{
Name: name, Name: name,
Value: token, Value: token,
@ -101,5 +115,7 @@ func SetAuthCookie(w http.ResponseWriter, name, token string) {
Secure: true, Secure: true,
HttpOnly: true, HttpOnly: true,
Path: "/", Path: "/",
Domain: routeableApi.Domain(),
}) })
} }
}