From 46abdc91201551d67095b5250bc5852cde0ec313 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Tue, 2 Aug 2022 11:41:13 +0200 Subject: [PATCH] Interpret Groups field in authToken --- auth.go | 13 ++++++++++--- users.go | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/auth.go b/auth.go index d3143f0..5b1af67 100644 --- a/auth.go +++ b/auth.go @@ -3,7 +3,9 @@ package main import ( "encoding/base64" "net/http" + "strings" "time" + "unicode" "github.com/gin-gonic/gin" ) @@ -26,7 +28,8 @@ func declareAPIAuthRoutes(router *gin.RouterGroup) { type authToken struct { *User - CurrentPromo uint `json:"current_promo"` + CurrentPromo uint `json:"current_promo"` + Groups []string `json:"groups"` } func validateAuthToken(c *gin.Context) { @@ -34,7 +37,11 @@ func validateAuthToken(c *gin.Context) { c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"errmsg": "Not connected"}) return } else { - c.JSON(http.StatusOK, authToken{u.(*User), currentPromo}) + t := authToken{User: u.(*User), CurrentPromo: currentPromo} + + t.Groups = strings.Split(strings.TrimFunc(t.User.Groups, func(r rune) bool { return !unicode.IsLetter(r) }), ",") + + c.JSON(http.StatusOK, t) } } @@ -107,6 +114,6 @@ func dummyAuth(c *gin.Context) { c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"errmsg": err.Error()}) return } else { - c.JSON(http.StatusOK, authToken{usr, currentPromo}) + c.JSON(http.StatusOK, authToken{User: usr, CurrentPromo: currentPromo}) } } diff --git a/users.go b/users.go index 6e778d7..c18f9d1 100644 --- a/users.go +++ b/users.go @@ -111,7 +111,7 @@ type User struct { Lastname string `json:"lastname"` Time time.Time `json:"time"` Promo uint `json:"promo"` - Groups string `json:"groups"` + Groups string `json:"groups,omitempty"` IsAdmin bool `json:"is_admin"` }