Skip to content

Commit

Permalink
feat: demo how to get current user(apiKey)
Browse files Browse the repository at this point in the history
  • Loading branch information
fanhousanbu committed Mar 2, 2024
1 parent d5846fb commit da1007a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
24 changes: 24 additions & 0 deletions rpc_server/api/utils/current_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package utils

import (
jwt "github.com/appleboy/gin-jwt/v2"
"github.com/gin-gonic/gin"
)

// CurrentUser is a util tool for getting current user(ApiKey) from each rpc request
func CurrentUser(ctx *gin.Context) (exists bool, user string) {

defer func() {
if r := recover(); r != nil {
exists = false
}
}()

mapping := ctx.MustGet("JWT_PAYLOAD").(jwt.MapClaims)

user = mapping["jti"].(string)

exists = true

return
}
21 changes: 14 additions & 7 deletions rpc_server/api/v1/get_support_entrypoint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1

import (
"AAStarCommunity/EthPaymaster_BackService/rpc_server/api/utils"
"AAStarCommunity/EthPaymaster_BackService/rpc_server/models"
"AAStarCommunity/EthPaymaster_BackService/service"
"fmt"
Expand All @@ -17,13 +18,19 @@ import (
// @Success 200
// @Security JWT
func GetSupportEntrypoint(c *gin.Context) {
//1.TODO API validate
//2. recall service
result, err := service.GetSupportEntrypointExecute()
response := models.GetResponse()
if err != nil {
errStr := fmt.Sprintf("%v", err)
response.SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr)
if ok, apiKey := utils.CurrentUser(c); ok {
_ = apiKey

//1.TODO API validate
//2. recall service
result, err := service.GetSupportEntrypointExecute()
if err != nil {
errStr := fmt.Sprintf("%v", err)
response.SetHttpCode(http.StatusInternalServerError).FailCode(c, http.StatusInternalServerError, errStr)
}
response.WithData(result).Success(c)
} else {
response.SetHttpCode(http.StatusUnauthorized)
}
response.WithData(result).Success(c)
}
5 changes: 5 additions & 0 deletions rpc_server/middlewares/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func AuthHandler() gin.HandlerFunc {
MaxRefresh: time.Hour / 2,
IdentityKey: "jti",
PayloadFunc: func(data interface{}) jwt.MapClaims {
if v, ok := data.(string); ok {
return jwt.MapClaims{
"jti": v,
}
}
return jwt.MapClaims{}
},
Authenticator: func(c *gin.Context) (interface{}, error) {
Expand Down

0 comments on commit da1007a

Please sign in to comment.