Nexus/pkg/internal/auth/token.go

37 lines
786 B
Go
Raw Permalink Normal View History

2024-10-21 16:12:28 +00:00
package auth
import (
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"github.com/gofiber/fiber/v2"
"strings"
)
var JReader *sec.JwtReader
var IReader *sec.InternalTokenReader
var IWriter *sec.InternalTokenWriter
2024-10-21 16:12:28 +00:00
func tokenExtract(c *fiber.Ctx) string {
var atk string
if cookie := c.Cookies(sec.CookieAccessToken); len(cookie) > 0 {
atk = cookie
}
if header := c.Get(fiber.HeaderAuthorization); len(header) > 0 {
tk := strings.Replace(header, "Bearer", "", 1)
atk = strings.TrimSpace(tk)
}
if tk := c.Query("tk"); len(tk) > 0 {
atk = strings.TrimSpace(tk)
}
return atk
}
func tokenRead(in string) (*sec.JwtClaims, error) {
if JReader == nil {
return nil, nil
}
2024-10-30 15:59:04 +00:00
claims, err := sec.ReadJwt[*sec.JwtClaims](JReader, in, &sec.JwtClaims{})
return claims, err
2024-10-21 16:12:28 +00:00
}