From 19f5ff61c0f7fbf434a1b7758c85824908910e70 Mon Sep 17 00:00:00 2001 From: Lakhan Samani Date: Tue, 28 Feb 2023 12:51:11 +0530 Subject: [PATCH] [server][fix]: add sub to userinfo Resolves: #327 --- server/handlers/userinfo.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/server/handlers/userinfo.go b/server/handlers/userinfo.go index a603fe0cd..a5a8b4e5b 100644 --- a/server/handlers/userinfo.go +++ b/server/handlers/userinfo.go @@ -1,6 +1,8 @@ package handlers import ( + "encoding/json" + "fmt" "net/http" "github.com/gin-gonic/gin" @@ -39,7 +41,28 @@ func UserInfoHandler() gin.HandlerFunc { }) return } - - gc.JSON(http.StatusOK, user.AsAPIUser()) + apiUser := user.AsAPIUser() + userBytes, err := json.Marshal(apiUser) + if err != nil { + log.Debug("Error marshalling user: ", err) + gc.JSON(http.StatusUnauthorized, gin.H{ + "error": err.Error(), + }) + return + } + fmt.Println("=> str:", string(userBytes)) + res := map[string]interface{}{} + err = json.Unmarshal(userBytes, &res) + if err != nil { + log.Debug("Error un-marshalling user: ", err) + gc.JSON(http.StatusUnauthorized, gin.H{ + "error": err.Error(), + }) + return + } + // add sub field to user as per openid standards + // https://github.com/authorizerdev/authorizer/issues/327 + res["sub"] = userID + gc.JSON(http.StatusOK, res) } }