Skip to content

Commit

Permalink
feat: oauth2(client_credential mode) support with swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
fanhousanbu committed Mar 2, 2024
1 parent ab0cb73 commit d5846fb
Show file tree
Hide file tree
Showing 18 changed files with 345 additions and 61 deletions.
7 changes: 7 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 \<TOKEN\>' to correctly set the AccessToken
// @BasePath /api
func main() {
port := runMode()
_ = routers.SetRouters().Run(port)
Expand Down
4 changes: 4 additions & 0 deletions conf/appsettings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
jwt:
security: hello-eth-paymaster
realm: aastar
idkey: id
2 changes: 1 addition & 1 deletion conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
var once sync.Once

type Conf struct {
// TODO: Add Conf Structure Here
Jwt JWT
}

var conf *Conf
Expand Down
28 changes: 28 additions & 0 deletions conf/jwt.go
Original file line number Diff line number Diff line change
@@ -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
}
65 changes: 64 additions & 1 deletion docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -34,6 +69,11 @@ const docTemplate = `{
},
"/api/v1/get-support-strategy": {
"get": {
"security": [
{
"JWT": []
}
],
"description": "get the support strategy",
"consumes": [
"application/json"
Expand All @@ -53,6 +93,11 @@ const docTemplate = `{
},
"/api/v1/try-pay-user-operation": {
"post": {
"security": [
{
"JWT": []
}
],
"description": "sponsor the userOp",
"consumes": [
"application/json"
Expand All @@ -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"
}
}
}`

Expand Down
65 changes: 64 additions & 1 deletion docs/swagger.json
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -22,6 +57,11 @@
},
"/api/v1/get-support-strategy": {
"get": {
"security": [
{
"JWT": []
}
],
"description": "get the support strategy",
"consumes": [
"application/json"
Expand All @@ -41,6 +81,11 @@
},
"/api/v1/try-pay-user-operation": {
"post": {
"security": [
{
"JWT": []
}
],
"description": "sponsor the userOp",
"consumes": [
"application/json"
Expand All @@ -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"
}
}
}
39 changes: 38 additions & 1 deletion docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -9,6 +34,8 @@ paths:
responses:
"200":
description: OK
security:
- JWT: []
tags:
- Sponsor
/api/v1/get-support-strategy:
Expand All @@ -21,6 +48,8 @@ paths:
responses:
"200":
description: OK
security:
- JWT: []
tags:
- Sponsor
/api/v1/try-pay-user-operation:
Expand All @@ -31,6 +60,14 @@ paths:
responses:
"200":
description: OK
security:
- JWT: []
tags:
- Sponsor
securityDefinitions:
JWT:
description: Type 'Bearer \<TOKEN\>' to correctly set the AccessToken
in: header
name: Authorization
type: apiKey
swagger: "2.0"
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
Expand Down
Loading

0 comments on commit d5846fb

Please sign in to comment.