From d5846fb78784f40f980e1a02178d06829c50d734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E8=B6=85=E8=B6=8A?= <993921@qq.com> Date: Sat, 2 Mar 2024 16:27:47 +0800 Subject: [PATCH] feat: oauth2(client_credential mode) support with swagger --- cmd/server/main.go | 7 +++ conf/appsettings.yaml | 4 ++ conf/conf.go | 2 +- conf/jwt.go | 28 +++++++++ docs/docs.go | 65 ++++++++++++++++++++- docs/swagger.json | 65 ++++++++++++++++++++- docs/swagger.yaml | 39 ++++++++++++- go.mod | 5 ++ go.sum | 60 +++++++------------ rpc_server/api/auth.go | 18 ++++++ rpc_server/api/v1/get_support_entrypoint.go | 3 +- rpc_server/api/v1/get_support_strategy.go | 3 +- rpc_server/api/v1/try_pay_user_operation.go | 3 +- rpc_server/middlewares/auth.go | 63 ++++++++++++++++++++ rpc_server/models/client_credential.go | 5 ++ rpc_server/routers/boot.go | 3 +- rpc_server/routers/builder.go | 29 +++++---- rpc_server/routers/routers.go | 4 +- 18 files changed, 345 insertions(+), 61 deletions(-) create mode 100644 conf/jwt.go create mode 100644 rpc_server/api/auth.go create mode 100644 rpc_server/middlewares/auth.go create mode 100644 rpc_server/models/client_credential.go diff --git a/cmd/server/main.go b/cmd/server/main.go index f91bb5bb..b47f0c89 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -29,6 +29,13 @@ func runMode() string { return *aPort } +// @contact.name AAStar Support +// @contact.url https://aastar.xyz +// @securityDefinitions.apikey JWT +// @in header +// @name Authorization +// @description Type 'Bearer \' to correctly set the AccessToken +// @BasePath /api func main() { port := runMode() _ = routers.SetRouters().Run(port) diff --git a/conf/appsettings.yaml b/conf/appsettings.yaml index e69de29b..65f51631 100644 --- a/conf/appsettings.yaml +++ b/conf/appsettings.yaml @@ -0,0 +1,4 @@ +jwt: + security: hello-eth-paymaster + realm: aastar + idkey: id diff --git a/conf/conf.go b/conf/conf.go index e1c96a94..46e200e1 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -9,7 +9,7 @@ import ( var once sync.Once type Conf struct { - // TODO: Add Conf Structure Here + Jwt JWT } var conf *Conf diff --git a/conf/jwt.go b/conf/jwt.go new file mode 100644 index 00000000..b25c8d1a --- /dev/null +++ b/conf/jwt.go @@ -0,0 +1,28 @@ +package conf + +import "sync" + +type JWT struct { + Security string + Realm string + IdKey string +} + +var jwt *JWT + +var onceJwt sync.Once + +// GetJwtKey represents jwt object +func GetJwtKey() *JWT { + onceJwt.Do(func() { + if jwt == nil { + j := getConf().Jwt + jwt = &JWT{ + Security: j.Security, + Realm: j.Realm, + } + } + }) + + return jwt +} diff --git a/docs/docs.go b/docs/docs.go index 5247479e..89631010 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -10,14 +10,49 @@ const docTemplate = `{ "info": { "description": "{{escape .Description}}", "title": "{{.Title}}", - "contact": {}, + "contact": { + "name": "AAStar Support", + "url": "https://aastar.xyz" + }, "version": "{{.Version}}" }, "host": "{{.Host}}", "basePath": "{{.BasePath}}", "paths": { + "/api/auth": { + "post": { + "description": "Get AccessToken By ApiKey", + "consumes": [ + "application/json" + ], + "tags": [ + "Auth" + ], + "parameters": [ + { + "description": "AccessToken Model", + "name": "credential", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/models.ClientCredential" + } + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, "/api/v1/get-support-entrypoint": { "get": { + "security": [ + { + "JWT": [] + } + ], "description": "get the support entrypoint", "consumes": [ "application/json" @@ -34,6 +69,11 @@ const docTemplate = `{ }, "/api/v1/get-support-strategy": { "get": { + "security": [ + { + "JWT": [] + } + ], "description": "get the support strategy", "consumes": [ "application/json" @@ -53,6 +93,11 @@ const docTemplate = `{ }, "/api/v1/try-pay-user-operation": { "post": { + "security": [ + { + "JWT": [] + } + ], "description": "sponsor the userOp", "consumes": [ "application/json" @@ -67,6 +112,24 @@ const docTemplate = `{ } } } + }, + "definitions": { + "models.ClientCredential": { + "type": "object", + "properties": { + "apiKey": { + "type": "string" + } + } + } + }, + "securityDefinitions": { + "JWT": { + "description": "Type 'Bearer \\\u003cTOKEN\\\u003e' to correctly set the AccessToken", + "type": "apiKey", + "name": "Authorization", + "in": "header" + } } }` diff --git a/docs/swagger.json b/docs/swagger.json index 41dd4b4c..a50f25ab 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -1,11 +1,46 @@ { "swagger": "2.0", "info": { - "contact": {} + "contact": { + "name": "AAStar Support", + "url": "https://aastar.xyz" + } }, "paths": { + "/api/auth": { + "post": { + "description": "Get AccessToken By ApiKey", + "consumes": [ + "application/json" + ], + "tags": [ + "Auth" + ], + "parameters": [ + { + "description": "AccessToken Model", + "name": "credential", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/models.ClientCredential" + } + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, "/api/v1/get-support-entrypoint": { "get": { + "security": [ + { + "JWT": [] + } + ], "description": "get the support entrypoint", "consumes": [ "application/json" @@ -22,6 +57,11 @@ }, "/api/v1/get-support-strategy": { "get": { + "security": [ + { + "JWT": [] + } + ], "description": "get the support strategy", "consumes": [ "application/json" @@ -41,6 +81,11 @@ }, "/api/v1/try-pay-user-operation": { "post": { + "security": [ + { + "JWT": [] + } + ], "description": "sponsor the userOp", "consumes": [ "application/json" @@ -55,5 +100,23 @@ } } } + }, + "definitions": { + "models.ClientCredential": { + "type": "object", + "properties": { + "apiKey": { + "type": "string" + } + } + } + }, + "securityDefinitions": { + "JWT": { + "description": "Type 'Bearer \\\u003cTOKEN\\\u003e' to correctly set the AccessToken", + "type": "apiKey", + "name": "Authorization", + "in": "header" + } } } \ No newline at end of file diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 58a335ad..ab6e309b 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,6 +1,31 @@ +definitions: + models.ClientCredential: + properties: + apiKey: + type: string + type: object info: - contact: {} + contact: + name: AAStar Support + url: https://aastar.xyz paths: + /api/auth: + post: + consumes: + - application/json + description: Get AccessToken By ApiKey + parameters: + - description: AccessToken Model + in: body + name: credential + required: true + schema: + $ref: '#/definitions/models.ClientCredential' + responses: + "200": + description: OK + tags: + - Auth /api/v1/get-support-entrypoint: get: consumes: @@ -9,6 +34,8 @@ paths: responses: "200": description: OK + security: + - JWT: [] tags: - Sponsor /api/v1/get-support-strategy: @@ -21,6 +48,8 @@ paths: responses: "200": description: OK + security: + - JWT: [] tags: - Sponsor /api/v1/try-pay-user-operation: @@ -31,6 +60,14 @@ paths: responses: "200": description: OK + security: + - JWT: [] tags: - Sponsor +securityDefinitions: + JWT: + description: Type 'Bearer \' to correctly set the AccessToken + in: header + name: Authorization + type: apiKey swagger: "2.0" diff --git a/go.mod b/go.mod index 3ca0a885..2a50609a 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,13 @@ module AAStarCommunity/EthPaymaster_BackService go 1.22.0 require ( + github.com/appleboy/gin-jwt/v2 v2.9.2 github.com/gin-contrib/cors v1.5.0 github.com/gin-gonic/gin v1.9.1 github.com/swaggo/files v1.0.1 github.com/swaggo/gin-swagger v1.6.0 github.com/swaggo/swag v1.16.3 + gorm.io/gorm v1.25.7 k8s.io/apimachinery v0.29.2 ) @@ -26,6 +28,9 @@ require ( github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.18.0 // indirect github.com/goccy/go-json v0.10.2 // indirect + github.com/golang-jwt/jwt/v4 v4.5.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.7 // indirect diff --git a/go.sum b/go.sum index 2b217352..b83ab1a2 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,9 @@ github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= -github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= -github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= +github.com/appleboy/gin-jwt/v2 v2.9.2 h1:GeS3lm9mb9HMmj7+GNjYUtpp3V1DAQ1TkUFa5poiZ7Y= +github.com/appleboy/gin-jwt/v2 v2.9.2/go.mod h1:mxGjKt9Lrx9Xusy1SrnmsCJMZG6UJwmdHN9bN27/QDw= +github.com/appleboy/gofight/v2 v2.1.2 h1:VOy3jow4vIK8BRQJoC/I9muxyYlJ2yb9ht2hZoS3rf4= +github.com/appleboy/gofight/v2 v2.1.2/go.mod h1:frW+U1QZEdDgixycTj4CygQ48yLTUhplt43+Wczp3rw= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= github.com/bytedance/sonic v1.11.1 h1:JC0+6c9FoWYYxakaoa+c5QTtJeiSZNeByOBhXtAFSn4= @@ -13,7 +15,6 @@ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpV github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0= github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -27,25 +28,12 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= -github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= -github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= -github.com/go-openapi/jsonpointer v0.19.6 h1:eCs3fxoIi3Wh6vtgmLTOjdhSpiqphQ+DaPn38N2ZdrE= -github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs= github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q= github.com/go-openapi/jsonpointer v0.20.2/go.mod h1:bHen+N0u1KEO3YlmqOjTT9Adn1RfD91Ar825/PuiRVs= -github.com/go-openapi/jsonreference v0.19.6/go.mod h1:diGHMEHg2IqXZGKxqyvWdfWU/aim5Dprw5bqpKkTvns= -github.com/go-openapi/jsonreference v0.20.2 h1:3sVjiK66+uXK/6oQ8xgcRKcFgQ5KXa2KvnJRumpMGbE= -github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En5Ap4rVB5KVcIDZG2k= github.com/go-openapi/jsonreference v0.20.4 h1:bKlDxQxQJgwpUSgOENiMPzCTBVuc7vTdXSSgNeAhojU= github.com/go-openapi/jsonreference v0.20.4/go.mod h1:5pZJyJP2MnYCpoeoMAql78cCHauHj0V9Lhc506VOpw4= -github.com/go-openapi/spec v0.20.4 h1:O8hJrt0UMnhHcluhIdUgCLRWyM2x7QkBXRvOs7m+O1M= -github.com/go-openapi/spec v0.20.4/go.mod h1:faYFR1CvsJZ0mNsmsphTMSoRrNV3TEDoAM7FOEWeq8I= github.com/go-openapi/spec v0.20.14 h1:7CBlRnw+mtjFGlPDRZmAMnq35cRzI91xj03HVyUi/Do= github.com/go-openapi/spec v0.20.14/go.mod h1:8EOhTpBoFiask8rrgwbLC3zmJfz4zsCUueRuPM6GNkw= -github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= -github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ= -github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/g= -github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= github.com/go-openapi/swag v0.22.9 h1:XX2DssF+mQKM2DHsbgZK74y/zj4mo9I99+89xUmuZCE= github.com/go-openapi/swag v0.22.9/go.mod h1:3/OXnFfnMAwBD099SwYRk7GD3xOrr1iL7d/XNLXVVwE= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= @@ -58,9 +46,15 @@ github.com/go-playground/validator/v10 v10.18.0 h1:BvolUXjp4zuvkZ5YN5t7ebzbhlUtP github.com/go-playground/validator/v10 v10.18.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg= +github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= @@ -69,19 +63,12 @@ github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa02 github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= -github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= -github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= -github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= @@ -91,19 +78,16 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= -github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= -github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= @@ -114,10 +98,14 @@ github.com/swaggo/files v1.0.1 h1:J1bVJ4XHZNq0I46UU90611i9/YzdrF7x92oX1ig5IdE= github.com/swaggo/files v1.0.1/go.mod h1:0qXmMNH6sXNf+73t65aKeB+ApmgxdnkQzVTAj2uaMUg= github.com/swaggo/gin-swagger v1.6.0 h1:y8sxvQ3E20/RCyrXeFfg60r6H0Z+SwpTjMYsMm+zy8M= github.com/swaggo/gin-swagger v1.6.0/go.mod h1:BG00cCEy294xtVpyIAHG6+e2Qzj/xKlRdOqDkvq0uzo= -github.com/swaggo/swag v1.8.12 h1:pctzkNPu0AlQP2royqX3apjKCQonAnf7KGoxeO4y64w= -github.com/swaggo/swag v1.8.12/go.mod h1:lNfm6Gg+oAq3zRJQNEMBE66LIJKM44mxFqhEEgy2its= github.com/swaggo/swag v1.16.3 h1:PnCYjPCah8FK4I26l2F/KQ4yz3sILcVUN3cTlBFA9Pg= github.com/swaggo/swag v1.16.3/go.mod h1:DImHIuOFXKpMFAQjcC7FG4m3Dg4+QuUgUzJmKjI/gRk= +github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM= +github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= @@ -131,12 +119,10 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg= golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= -golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8= +golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= @@ -145,7 +131,6 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -158,7 +143,6 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= @@ -166,25 +150,21 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA= -golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ= golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/gorm v1.25.7 h1:VsD6acwRjz2zFxGO50gPO6AkNs7KKnvfzUjHQhZDz/A= +gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= k8s.io/apimachinery v0.29.2 h1:EWGpfJ856oj11C52NRCHuU7rFDwxev48z+6DSlGNsV8= k8s.io/apimachinery v0.29.2/go.mod h1:6HVkd1FwxIagpYrHSwJlQqZI3G9LfYWRPAkUvLnXTKU= nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= diff --git a/rpc_server/api/auth.go b/rpc_server/api/auth.go new file mode 100644 index 00000000..21e39341 --- /dev/null +++ b/rpc_server/api/auth.go @@ -0,0 +1,18 @@ +package api + +import ( + "AAStarCommunity/EthPaymaster_BackService/rpc_server/middlewares" + "github.com/gin-gonic/gin" +) + +// Auth +// @Tags Auth +// @Description Get AccessToken By ApiKey +// @Accept json +// @Product json +// @Param credential body models.ClientCredential true "AccessToken Model" +// @Router /api/auth [post] +// @Success 200 +func Auth(ctx *gin.Context) { + middlewares.GinJwtMiddleware().LoginHandler(ctx) +} diff --git a/rpc_server/api/v1/get_support_entrypoint.go b/rpc_server/api/v1/get_support_entrypoint.go index f0cb381c..18e2a6cb 100644 --- a/rpc_server/api/v1/get_support_entrypoint.go +++ b/rpc_server/api/v1/get_support_entrypoint.go @@ -13,8 +13,9 @@ import ( // @Description get the support entrypoint // @Accept json // @Product json -// @Router /api/v1/get-support-entrypoint [get] +// @Router /api/v1/get-support-entrypoint [GET] // @Success 200 +// @Security JWT func GetSupportEntrypoint(c *gin.Context) { //1.TODO API validate //2. recall service diff --git a/rpc_server/api/v1/get_support_strategy.go b/rpc_server/api/v1/get_support_strategy.go index afbb2d4f..6143c786 100644 --- a/rpc_server/api/v1/get_support_strategy.go +++ b/rpc_server/api/v1/get_support_strategy.go @@ -14,7 +14,8 @@ import ( // @Accept json // @Produce json // @Success 200 -// @Router /api/v1/get-support-strategy [get] +// @Router /api/v1/get-support-strategy [GET] +// @Security JWT func GetSupportStrategy(c *gin.Context) { //1.TODO API validate //2. recall service diff --git a/rpc_server/api/v1/try_pay_user_operation.go b/rpc_server/api/v1/try_pay_user_operation.go index 9f11eacd..cc6caad9 100644 --- a/rpc_server/api/v1/try_pay_user_operation.go +++ b/rpc_server/api/v1/try_pay_user_operation.go @@ -13,8 +13,9 @@ import ( // @Description sponsor the userOp // @Accept json // @Product json -// @Router /api/v1/try-pay-user-operation [post] +// @Router /api/v1/try-pay-user-operation [POST] // @Success 200 +// @Security JWT func TryPayUserOperation(c *gin.Context) { //1.TODO API validate //2. recall service diff --git a/rpc_server/middlewares/auth.go b/rpc_server/middlewares/auth.go new file mode 100644 index 00000000..6fcd818a --- /dev/null +++ b/rpc_server/middlewares/auth.go @@ -0,0 +1,63 @@ +package middlewares + +import ( + "AAStarCommunity/EthPaymaster_BackService/conf" + jwt "github.com/appleboy/gin-jwt/v2" + "github.com/gin-gonic/gin" + "time" +) + +type ApiKey struct { + Key string `form:"apiKey" json:"apiKey" binding:"required"` +} + +var jwtMiddleware *jwt.GinJWTMiddleware + +func GinJwtMiddleware() *jwt.GinJWTMiddleware { + return jwtMiddleware +} + +func AuthHandler() gin.HandlerFunc { + m, _ := jwt.New(&jwt.GinJWTMiddleware{ + Realm: conf.GetJwtKey().Realm, + Key: []byte(conf.GetJwtKey().Security), + Timeout: time.Hour * 24, + MaxRefresh: time.Hour / 2, + IdentityKey: "jti", + PayloadFunc: func(data interface{}) jwt.MapClaims { + return jwt.MapClaims{} + }, + Authenticator: func(c *gin.Context) (interface{}, error) { + var apiKey ApiKey + if err := c.ShouldBind(&apiKey); err != nil { + return "", jwt.ErrMissingLoginValues + } + + // TODO: verify if the key is correct + return apiKey.Key, nil + + // if incorrect + //return nil, jwt.ErrFailedAuthentication + }, + Authorizator: func(data interface{}, c *gin.Context) bool { + // always return true unless the permission feature started + return true + }, + Unauthorized: func(c *gin.Context, code int, message string) { + c.JSON(code, gin.H{ + "code": code, + "message": message, + }) + }, + TokenLookup: "header: Authorization", + TokenHeadName: "Bearer", + TimeFunc: time.Now, + HTTPStatusMessageFunc: func(e error, c *gin.Context) string { + return "401 Unauthorized" + }, + }) + + jwtMiddleware = m + + return m.MiddlewareFunc() +} diff --git a/rpc_server/models/client_credential.go b/rpc_server/models/client_credential.go new file mode 100644 index 00000000..1a048c34 --- /dev/null +++ b/rpc_server/models/client_credential.go @@ -0,0 +1,5 @@ +package models + +type ClientCredential struct { + ApiKey string `json:"apiKey"` +} diff --git a/rpc_server/routers/boot.go b/rpc_server/routers/boot.go index 0c0ea9e8..051d19fc 100644 --- a/rpc_server/routers/boot.go +++ b/rpc_server/routers/boot.go @@ -39,7 +39,8 @@ func SetRouters() (routers *gin.Engine) { // use middlewares routers.Use(handlers...) - buildRouters(routers) // build http routers + // build http routers + buildRouters(routers) routers.NoRoute(func(ctx *gin.Context) { models.GetResponse().SetHttpCode(http.StatusNotFound).FailCode(ctx, http.StatusNotFound) diff --git a/rpc_server/routers/builder.go b/rpc_server/routers/builder.go index 33194c20..946040dd 100644 --- a/rpc_server/routers/builder.go +++ b/rpc_server/routers/builder.go @@ -1,23 +1,30 @@ package routers import ( + "AAStarCommunity/EthPaymaster_BackService/rpc_server/api" + "AAStarCommunity/EthPaymaster_BackService/rpc_server/middlewares" "github.com/gin-gonic/gin" ) // buildRouters Build Routers func buildRouters(router *gin.Engine) { - for _, routerMap := range RouterMaps { - for _, method := range routerMap.Methods { - if method == GET { - router.GET(routerMap.Url, routerMap.Func) - } else if method == PUT { - router.PUT(routerMap.Url, routerMap.Func) - } else if method == POST { - router.POST(routerMap.Url, routerMap.Func) - } else if method == DELETE { - router.DELETE(routerMap.Url, routerMap.Func) - } // ignore rest methods + router.POST("api/auth", api.Auth) + + router.Use(middlewares.AuthHandler()) + { + for _, routerMap := range RouterMaps { + for _, method := range routerMap.Methods { + if method == GET { + router.GET(routerMap.Url, routerMap.Func) + } else if method == PUT { + router.PUT(routerMap.Url, routerMap.Func) + } else if method == POST { + router.POST(routerMap.Url, routerMap.Func) + } else if method == DELETE { + router.DELETE(routerMap.Url, routerMap.Func) + } // ignore rest methods + } } } } diff --git a/rpc_server/routers/routers.go b/rpc_server/routers/routers.go index 47e735d0..ed860980 100644 --- a/rpc_server/routers/routers.go +++ b/rpc_server/routers/routers.go @@ -8,6 +8,6 @@ func init() { RouterMaps = make([]RouterMap, 0) RouterMaps = append(RouterMaps, RouterMap{"api/v1/try-pay-user-operation", []RestfulMethod{POST}, v1.TryPayUserOperation}) - RouterMaps = append(RouterMaps, RouterMap{"api/v1/get-support-strategy", []RestfulMethod{POST}, v1.GetSupportStrategy}) - RouterMaps = append(RouterMaps, RouterMap{"api/v1/get-support-entrypoint", []RestfulMethod{POST}, v1.GetSupportEntrypoint}) + RouterMaps = append(RouterMaps, RouterMap{"api/v1/get-support-strategy", []RestfulMethod{GET}, v1.GetSupportStrategy}) + RouterMaps = append(RouterMaps, RouterMap{"api/v1/get-support-entrypoint", []RestfulMethod{GET}, v1.GetSupportEntrypoint}) }