felt/admin/util/util.go

57 lines
1.4 KiB
Go

package util
import (
"encoding/base64"
"forge.lightcrystal.systems/nilix/felt/models"
"forge.lightcrystal.systems/nilix/quartzgun/auth"
"net/http"
"strings"
)
func GetUserFromToken(req *http.Request) string {
authHeader := req.Header.Get("Authorization")
if strings.HasPrefix(authHeader, "Bearer ") {
authToken := strings.Split(authHeader, "Bearer ")[1]
data, err := base64.StdEncoding.DecodeString(authToken)
if err == nil {
parts := strings.Split(string(data), "\n")
if len(parts) == 2 {
return parts[0]
}
}
}
return ""
}
func GetTablesByUser(user string, udb auth.UserStore) ([]models.TableKey, error) {
rawTableData, err := udb.GetData(user, "tables")
if err != nil {
return []models.TableKey{}, err
}
// split rawTableData - tableName,passCode;tableName,passCode;
tables := strings.Split(rawTableData.(string), ";")
self := []models.TableKey{}
for _, t := range tables {
parts := strings.Split(t, ",")
if len(parts) == 2 {
self = append(self, models.TableKey{
Name: parts[0],
Passcode: parts[1],
})
}
}
return self, nil
}
func SetTablesForUser(user string, tables []models.TableKey, udb auth.UserStore) error {
rawTableData := ""
for _, t := range tables {
rawTableData += t.Name + "," + t.Passcode + ";"
}
rawTableData = strings.TrimSuffix(rawTableData, ";")
return udb.SetData(user, "tables", rawTableData)
}