Interpret Groups field in authToken

This commit is contained in:
nemunaire 2022-08-02 11:41:13 +02:00
parent a203cdc36a
commit 46abdc9120
2 changed files with 11 additions and 4 deletions

13
auth.go
View File

@ -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})
}
}

View File

@ -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"`
}