From 0349ad9a29ad509716f3961fa73bc494df21eae4 Mon Sep 17 00:00:00 2001 From: mr Date: Thu, 28 Nov 2024 11:49:20 +0100 Subject: [PATCH] some --- entrypoint.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/entrypoint.go b/entrypoint.go index 5f1215b..a4de72d 100644 --- a/entrypoint.go +++ b/entrypoint.go @@ -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{}