Skip to content

Commit

Permalink
feat: add access_token for the developer #45
Browse files Browse the repository at this point in the history
  • Loading branch information
saltbo committed Jul 12, 2021
1 parent e2f8750 commit 4f4d061
Show file tree
Hide file tree
Showing 35 changed files with 6,417 additions and 203 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ generate: ## generate the static assets
build: mod ## Build the binary file
go build -v -o build/bin/$(PROJECT) $(MAINFILE)

swag:
swag init -g internal/app/api/router.go --exclude client --parseDependency --parseDepth 1 --output internal/docs

install:
# 复制二进制文件
# 复制默认配置文件
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,25 @@ go 1.16
//replace github.com/saltbo/gopkg => /opt/works/gopkg

require (
github.com/NetEase-Object-Storage/nos-golang-sdk v0.0.0-20191125093154-335c2b73bf6b
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/aliyun/aliyun-oss-go-sdk v2.1.6+incompatible
github.com/aws/aws-sdk-go v1.34.14
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/gin-gonic/gin v1.6.3
github.com/go-oauth2/oauth2/v4 v4.3.0
github.com/google/uuid v1.1.1
github.com/saltbo/gopkg v0.0.0-20200905151036-32195ea0b27b
github.com/satori/go.uuid v1.2.0
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.7.1
github.com/storyicon/grbac v0.0.0-20200224041032-a0461737df7e
github.com/stretchr/testify v1.6.1
github.com/swaggo/swag v1.7.0
github.com/tencentyun/cos-go-sdk-v5 v0.7.18
github.com/upyun/go-sdk/v3 v3.0.2
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
gorm.io/driver/mysql v1.0.3
Expand Down
316 changes: 148 additions & 168 deletions go.sum

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions internal/app/api/matter.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ func (rs *FileResource) findAll(c *gin.Context) {
ginutil.JSONList(c, list, total)
}

// create godoc
// @Tags Matters
// @Summary 创建文件
// @Description 创建文件
// @Accept json
// @Produce json
// @Security OAuth2Application[matter, admin]
// @Param body body bind.BodyMatter true "参数"
// @Success 200 {object} httputil.JSONResponse{data=model.User}
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /matters [post]
func (rs *FileResource) create(c *gin.Context) {
p := new(bind.BodyMatter)
if err := c.ShouldBindJSON(p); err != nil {
Expand Down
23 changes: 23 additions & 0 deletions internal/app/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,36 @@ package api
import (
"github.com/gin-gonic/gin"
"github.com/saltbo/gopkg/ginutil"

_ "github.com/saltbo/zpan/internal/docs"
)

// @title zpan
// @description zpan apis
// @version 1.0.0

// @BasePath /api/
// @securitydefinitions.oauth2.application OAuth2Application
// @scope.matter Grants matter access and write
// @scope.admin Grants read and write access to administrative information
// @tokenUrl /api/tokens
// @name Authorization

// @contact.name API Support
// @contact.url http://zpan.space
// @contact.email saltbo@foxmail.com

// @license.name GPL 3.0
// @license.url https://github.com/saltbo/zpan/blob/master/LICENSE

func SetupRoutes(ge *gin.Engine) {
ginutil.SetupSwagger(ge)

apiRouter := ge.Group("/api")
ginutil.SetupResource(apiRouter,
NewOptionResource(),
NewUserResource(),
NewUserKeyResource(),
NewTokenResource(),
NewStorageResource(),
NewFileResource(),
Expand Down
38 changes: 34 additions & 4 deletions internal/app/api/token.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package api

import (
"log"

"github.com/gin-gonic/gin"
"github.com/go-oauth2/oauth2/v4/errors"
"github.com/go-oauth2/oauth2/v4/manage"
"github.com/go-oauth2/oauth2/v4/server"
"github.com/go-oauth2/oauth2/v4/store"
"github.com/saltbo/gopkg/ginutil"
_ "github.com/saltbo/gopkg/httputil"

Expand All @@ -12,10 +18,28 @@ import (

type TokenResource struct {
sUser *service.User

srv *server.Server
}

func NewTokenResource() *TokenResource {
uk := service.NewUserKey()
uk.LoadExistClient()
manager := manage.NewManager()
manager.MapAccessGenerate(uk)
manager.MapClientStorage(uk.ClientStore())
manager.MustTokenStorage(store.NewMemoryTokenStore())

srv := server.NewDefaultServer(manager)
srv.SetAllowGetAccessRequest(true)
srv.SetClientInfoHandler(server.ClientBasicHandler)
srv.SetInternalErrorHandler(func(err error) (re *errors.Response) {
log.Println("Internal Error:", err.Error())
return
})

return &TokenResource{
srv: srv,
sUser: service.NewUser(),
}
}
Expand All @@ -26,7 +50,7 @@ func (rs *TokenResource) Register(router *gin.RouterGroup) {
}

// create godoc
// @Tags v1/Tokens
// @Tags Tokens
// @Summary 登录/密码重置
// @Description 用于账户登录和申请密码重置
// @Accept json
Expand All @@ -35,8 +59,14 @@ func (rs *TokenResource) Register(router *gin.RouterGroup) {
// @Success 200 {object} httputil.JSONResponse
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /v1/tokens [post]
// @Router /tokens [post]
func (rs *TokenResource) create(c *gin.Context) {
// support gen oauth2 access_token
if _, _, ok := c.Request.BasicAuth(); ok {
rs.srv.HandleTokenRequest(c.Writer, c.Request)
return
}

p := new(bind.BodyToken)
if err := c.ShouldBindJSON(p); err != nil {
ginutil.JSONBadRequest(c, err)
Expand Down Expand Up @@ -67,15 +97,15 @@ func (rs *TokenResource) create(c *gin.Context) {
}

// delete godoc
// @Tags v1/Tokens
// @Tags Tokens
// @Summary 退出登录
// @Description 用户状态登出
// @Accept json
// @Produce json
// @Success 200 {object} httputil.JSONResponse
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /v1/tokens [delete]
// @Router /tokens [delete]
func (rs *TokenResource) delete(c *gin.Context) {
authed.TokenCookieSet(c, "", 1)
authed.RoleCookieSet(c, "", 1)
Expand Down
53 changes: 29 additions & 24 deletions internal/app/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ func (rs *UserResource) Register(router *gin.RouterGroup) {
}

// create godoc
// @Tags v1/Users
// @Tags Users
// @Summary 用户注册
// @Description 注册一个用户
// @Accept json
// @Produce json
// @Param body body bind.BodyUser true "参数"
// @Param body body bind.BodyUserCreation true "参数"
// @Success 200 {object} httputil.JSONResponse{data=model.User}
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /v1/users [post]
// @Router /users [post]
func (rs *UserResource) create(c *gin.Context) {
p := new(bind.BodyUserCreation)
if err := c.ShouldBindJSON(p); err != nil {
Expand Down Expand Up @@ -84,7 +84,7 @@ func (rs *UserResource) create(c *gin.Context) {
}

// patch godoc
// @Tags v1/Users
// @Tags Users
// @Summary 更新一项用户信息
// @Description 用于账户激活和密码重置
// @Accept json
Expand All @@ -94,7 +94,7 @@ func (rs *UserResource) create(c *gin.Context) {
// @Success 200 {object} httputil.JSONResponse
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /v1/users/{email} [patch]
// @Router /users/{email} [patch]
func (rs *UserResource) patch(c *gin.Context) {
p := new(bind.BodyUserPatch)
if err := c.ShouldBindJSON(p); err != nil {
Expand Down Expand Up @@ -122,16 +122,17 @@ func (rs *UserResource) patch(c *gin.Context) {
}

// findAll godoc
// @Tags v1/Users
// @Tags Users
// @Summary 用户列表
// @Description 获取用户列表信息
// @Accept json
// @Produce json
// @Security OAuth2Application[admin]
// @Param query query bind.QueryUser true "参数"
// @Success 200 {object} httputil.JSONResponse{data=gin.H{list=[]model.UserFormats,total=int64}}
// @Success 200 {object} httputil.JSONResponse{data=gin.H{list=[]model.User,total=int64}}
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /v1/users [get]
// @Router /users [get]
func (rs *UserResource) findAll(c *gin.Context) {
p := new(bind.QueryUser)
if err := c.BindQuery(p); err != nil {
Expand All @@ -155,7 +156,7 @@ func (rs *UserResource) findAll(c *gin.Context) {
}

// find godoc
// @Tags v1/Users
// @Tags Users
// @Summary 用户查询
// @Description 获取一个用户的公开信息
// @Accept json
Expand All @@ -164,7 +165,7 @@ func (rs *UserResource) findAll(c *gin.Context) {
// @Success 200 {object} httputil.JSONResponse{data=model.UserProfile}
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /v1/users/{username} [get]
// @Router /users/{username} [get]
func (rs *UserResource) find(c *gin.Context) {
user, exist := rs.dUser.UsernameExist(c.Param("username"))
if !exist {
Expand All @@ -176,17 +177,18 @@ func (rs *UserResource) find(c *gin.Context) {
}

// updateStorage godoc
// @Tags v1/Users
// @Tags Users
// @Summary 修改某一个用户的存储空间
// @Description 修改某一个用户的存储空间
// @Accept json
// @Produce json
// @Security OAuth2Application[admin]
// @Param username path string true "用户名"
// @Param body body bind.BodyUserPassword true "参数"
// @Success 200 {object} httputil.JSONResponse
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /v1/users/{username}/storage [put]
// @Router /users/{username}/storage [put]
func (rs *UserResource) updateStorage(c *gin.Context) {
p := new(bind.BodyUserStorage)
if err := c.ShouldBindJSON(p); err != nil {
Expand All @@ -209,17 +211,18 @@ func (rs *UserResource) updateStorage(c *gin.Context) {
}

// updateStatus godoc
// @Tags v1/Users
// @Tags Users
// @Summary 修改某一个用户的状态
// @Description 修改某一个用户的状态
// @Accept json
// @Produce json
// @Security OAuth2Application[admin]
// @Param username path string true "用户名"
// @Param body body bind.BodyUserStatus true "参数"
// @Success 200 {object} httputil.JSONResponse
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /v1/users/{username}/storage [put]
// @Router /users/{username}/status [put]
func (rs *UserResource) updateStatus(c *gin.Context) {
p := new(bind.BodyUserStatus)
if err := c.ShouldBindJSON(p); err != nil {
Expand All @@ -242,17 +245,18 @@ func (rs *UserResource) updateStatus(c *gin.Context) {
}

// resetPassword godoc
// @Tags v1/Users
// @Tags Users
// @Summary 重置某一个用户的密码
// @Description 重置某一个用户的密码
// @Accept json
// @Produce json
// @Security OAuth2Application[admin]
// @Param username path string true "用户名"
// @Param body body bind.BodyUserStatus true "参数"
// @Success 200 {object} httputil.JSONResponse
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /v1/users/{username}/password [put]
// @Router /users/{username}/password [put]
func (rs *UserResource) resetPassword(c *gin.Context) {
p := new(bind.BodyUserPasswordReset)
if err := c.ShouldBindJSON(p); err != nil {
Expand All @@ -276,16 +280,17 @@ func (rs *UserResource) resetPassword(c *gin.Context) {
}

// remove godoc
// @Tags v1/Users
// @Tags Users
// @Summary 删除某一个用户
// @Description 删除某一个用户
// @Accept json
// @Produce json
// @Security OAuth2Application[admin]
// @Param username path string true "用户名"
// @Success 200 {object} httputil.JSONResponse
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /v1/users/{username} [delete]
// @Router /users/{username} [delete]
func (rs *UserResource) remove(c *gin.Context) {
user, err := rs.dUser.FindByUsername(c.Param("username"))
if err != nil {
Expand All @@ -302,15 +307,15 @@ func (rs *UserResource) remove(c *gin.Context) {
}

// profile godoc
// @Tags v1/Users
// @Tags Users
// @Summary 当前登录用户信息
// @Description 获取已登录用户的详细信息
// @Accept json
// @Produce json
// @Success 200 {object} httputil.JSONResponse{data=gin.H{user=model.User,profile=model.UserProfile}}
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /v1/user [get]
// @Router /user [get]
func (rs *UserResource) userMe(c *gin.Context) {
user, err := rs.dUser.Find(authed.UidGet(c))
if err != nil {
Expand All @@ -322,7 +327,7 @@ func (rs *UserResource) userMe(c *gin.Context) {
}

// updatePassword godoc
// @Tags v1/Users
// @Tags Users
// @Summary 修改登录用户密码
// @Description 修改登录用户密码
// @Accept json
Expand All @@ -331,7 +336,7 @@ func (rs *UserResource) userMe(c *gin.Context) {
// @Success 200 {object} httputil.JSONResponse
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /v1/user/password [put]
// @Router /user/password [put]
func (rs *UserResource) updatePassword(c *gin.Context) {
p := new(bind.BodyUserPassword)
if err := c.ShouldBindJSON(p); err != nil {
Expand All @@ -349,7 +354,7 @@ func (rs *UserResource) updatePassword(c *gin.Context) {
}

// updateProfile godoc
// @Tags v1/Users
// @Tags Users
// @Summary 修改个人信息
// @Description 更新用户的个人信息
// @Accept json
Expand All @@ -358,7 +363,7 @@ func (rs *UserResource) updatePassword(c *gin.Context) {
// @Success 200 {object} httputil.JSONResponse
// @Failure 400 {object} httputil.JSONResponse
// @Failure 500 {object} httputil.JSONResponse
// @Router /v1/user/profile [put]
// @Router /user/profile [put]
func (rs *UserResource) updateProfile(c *gin.Context) {
p := new(bind.BodyUserProfile)
if err := c.ShouldBindJSON(p); err != nil {
Expand Down
Loading

0 comments on commit 4f4d061

Please sign in to comment.