This commit is contained in:
mr 2024-11-28 11:49:20 +01:00
parent 195f5b0794
commit 0349ad9a29

View File

@ -1,8 +1,11 @@
package oclib
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"runtime/debug"
@ -118,6 +121,48 @@ func InitDaemon(appName string) {
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
}
type IDTokenClaims struct {
PeerID string `json:"peer_id"`
Groups []string `json:"groups"`
}
// SessionClaims struct
type SessionClaims struct {
AccessToken map[string]interface{} `json:"access_token"`
IDToken IDTokenClaims `json:"id_token"`
}
// Claims struct
type Claims struct {
Session SessionClaims `json:"session"`
}
func ExtractTokenInfo(request http.Request) (string, []string) {
reqToken := request.Header.Get("Authorization")
splitToken := strings.Split(reqToken, "Bearer ")
if len(splitToken) < 2 {
reqToken = ""
} else {
reqToken = splitToken[1]
}
if reqToken != "" {
token := strings.Split(reqToken, ".")
if len(token) > 2 {
bytes, err := base64.StdEncoding.DecodeString(token[2])
if err != nil {
return "", []string{}
}
var c Claims
err = json.Unmarshal(bytes, &c)
if err != nil {
return "", []string{}
}
return c.Session.IDToken.PeerID, c.Session.IDToken.Groups
}
}
return "", []string{}
}
func Init(appName string) {
InitDaemon(appName)
api := &tools.API{}