Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #108 from askuy/feature/gormandoauth
Browse files Browse the repository at this point in the history
support oauth2 storage
  • Loading branch information
askuy authored May 31, 2021
2 parents f1c06f9 + 9426859 commit adb305c
Show file tree
Hide file tree
Showing 7 changed files with 379 additions and 1 deletion.
3 changes: 2 additions & 1 deletion eoauth2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module github.com/gotomicro/ego-component/eoauth2
go 1.16

require (
github.com/gotomicro/ego v0.4.1
github.com/gotomicro/ego v0.5.6
github.com/gotomicro/ego-component/egorm v0.2.1 // indirect
github.com/pborman/uuid v1.2.1
go.uber.org/zap v1.15.0
)
134 changes: 134 additions & 0 deletions eoauth2/go.sum

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions eoauth2/storage/access.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package storage

import (
"context"
"fmt"
"time"

"github.com/gotomicro/ego-component/egorm"
"gorm.io/gorm"
)

type Access struct {
Id int `gorm:"not null;primary_key;AUTO_INCREMENT" json:"id" form:"id"` // FormID
Client string `gorm:"not null" json:"client" form:"client"` // client
Authorize string `gorm:"not null" json:"authorize" form:"authorize"` // authorize
Previous string `gorm:"not null" json:"previous" form:"previous"` // previous
AccessToken string `gorm:"not null" json:"accessToken" form:"accessToken"` // access_token
RefreshToken string `gorm:"not null" json:"refreshToken" form:"refreshToken"` // refresh_token
ExpiresIn int `gorm:"not null" json:"expiresIn" form:"expiresIn"` // expires_in
Scope string `gorm:"not null" json:"scope" form:"scope"` // scope
RedirectUri string `gorm:"not null" json:"redirectUri" form:"redirectUri"` // redirect_uri
Extra string `gorm:"not null;type:longtext" json:"extra" form:"extra"` // extra
Ctime int64 `gorm:"not null" json:"ctime" form:"ctime"` // 创建时间
}

func (t *Access) TableName() string {
return "access"
}

// AccessCreate insert a new Access into database and returns
// last inserted Id on success.
func AccessCreate(ctx context.Context, db *gorm.DB, data *Access) (err error) {
data.Ctime = time.Now().Unix()
if err = db.WithContext(ctx).Create(data).Error; err != nil {
err = fmt.Errorf("AccessCreate, err: %w", err)
return
}
return
}

// AccessDeleteX Delete的扩展方法,根据Cond删除一条或多条记录。如果有delete_time则软删除,否则硬删除。
func AccessDeleteX(ctx context.Context, db *gorm.DB, conds egorm.Conds) (err error) {
sql, binds := egorm.BuildQuery(conds)
if err = db.WithContext(ctx).Table("access").Where(sql, binds...).Delete(&Access{}).Error; err != nil {
err = fmt.Errorf("AccessDeleteX, err: %w", err)
return
}
return
}

// AccessInfoX Info的扩展方法,根据Cond查询单条记录
func AccessInfoX(ctx context.Context, db *gorm.DB, conds egorm.Conds) (resp Access, err error) {
sql, binds := egorm.BuildQuery(conds)
if err = db.WithContext(ctx).Table("access").Where(sql, binds...).First(&resp).Error; err != nil {
err = fmt.Errorf("AccessInfoX, err: %w", err)
return
}
return
}
39 changes: 39 additions & 0 deletions eoauth2/storage/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package storage

import (
"context"
"fmt"

"github.com/gotomicro/ego-component/egorm"
)

type App struct {
Aid int `gorm:"not null;primary_key;AUTO_INCREMENT" json:"aid" form:"aid"` // 应用id
ClientId string `gorm:"not null" json:"clientId" form:"clientId"` // 客户端
Name string `gorm:"not null" json:"name" form:"name"` // 名称
Secret string `gorm:"not null" json:"secret" form:"secret"` // 秘钥
RedirectUri string `gorm:"not null" json:"redirectUri" form:"redirectUri"` // 跳转地址
Url string `gorm:"not null" json:"url" form:"url"` // 访问地址
Extra string `gorm:"not null;type:longtext" json:"extra" form:"extra"` // 额外信息
CntCall int `gorm:"not null" json:"cntCall" form:"cntCall"` // 调用次数
State int `gorm:"not null" json:"state" form:"state"` // 状态
Ctime int64 `gorm:"not null" json:"ctime" form:"ctime"` // 创建时间
Utime int64 `gorm:"not null" json:"utime" form:"utime"` // 更新时间
Dtime int64 `gorm:"not null" json:"dtime" form:"dtime"` // 删除时间

}

func (t *App) TableName() string {
return "app"
}

// AppInfoX Info的扩展方法,根据Cond查询单条记录
func AppInfoX(ctx context.Context, db *egorm.Component, conds egorm.Conds) (resp App, err error) {
conds["dtime"] = 0
sql, binds := egorm.BuildQuery(conds)
if err = db.WithContext(ctx).Table("app").Where(sql, binds...).First(&resp).Error; err != nil {
err = fmt.Errorf("AccessDeleteX, err: %w", err)
return
}
return
}
58 changes: 58 additions & 0 deletions eoauth2/storage/authorize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package storage

import (
"context"
"fmt"
"time"

"github.com/gotomicro/ego-component/egorm"
"gorm.io/gorm"
)

type Authorize struct {
Id int `gorm:"not null;primary_key;AUTO_INCREMENT" json:"id" form:"id"` // FormID
Client string `gorm:"not null" json:"client" form:"client"` // 客户端
Code string `gorm:"not null" json:"code" form:"code"` // 状态码
ExpiresIn int32 `gorm:"not null" json:"expiresIn" form:"expiresIn"` // 过期时间
Scope string `gorm:"not null" json:"scope" form:"scope"` // 范围
RedirectUri string `gorm:"not null" json:"redirectUri" form:"redirectUri"` // 跳转地址
State string `gorm:"not null" json:"state" form:"state"` // 状态
Extra string `gorm:"not null;type:longtext" json:"extra" form:"extra"` // 额外信息
Ctime int64 `gorm:"not null" json:"ctime" form:"ctime"` // 创建时间
}

func (t *Authorize) TableName() string {
return "authorize"
}

// AuthorizeCreate insert a new Authorize into database and returns
// last inserted Id on success.
func AuthorizeCreate(ctx context.Context, db *gorm.DB, data *Authorize) (err error) {
data.Ctime = time.Now().Unix()
if err = db.WithContext(ctx).Create(data).Error; err != nil {
err = fmt.Errorf("AuthorizeCreate, err: %w", err)
return
}
return
}

func AuthorizeDeleteX(ctx context.Context, db *gorm.DB, conds egorm.Conds) (err error) {
sql, binds := egorm.BuildQuery(conds)

if err = db.WithContext(ctx).Table("authorize").Where(sql, binds...).Delete(&Authorize{}).Error; err != nil {
err = fmt.Errorf("AuthorizeDeleteX, err: %w", err)
return
}

return
}

// AuthorizeInfoX Info的扩展方法,根据Cond查询单条记录
func AuthorizeInfoX(ctx context.Context, db *egorm.Component, conds egorm.Conds) (resp Authorize, err error) {
sql, binds := egorm.BuildQuery(conds)
if err = db.WithContext(ctx).Table("authorize").Where(sql, binds...).First(&resp).Error; err != nil {
err = fmt.Errorf("AuthorizeInfoX, err: %w", err)
return
}
return
}
40 changes: 40 additions & 0 deletions eoauth2/storage/expires.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package storage

import (
"context"
"fmt"

"github.com/gotomicro/ego-component/egorm"
"gorm.io/gorm"
)

type Expires struct {
Id int `gorm:"not null;primary_key;AUTO_INCREMENT" json:"id" form:"id"` // 客户端
Token string `gorm:"not null" json:"token" form:"token"` // token
ExpiresAt int64 `gorm:"not null" json:"expiresAt" form:"expiresAt"` // 过期时间

}

func (t *Expires) TableName() string {
return "expires"
}

// ExpiresCreate insert a new Expires into database and returns
// last inserted Id on success.
func ExpiresCreate(ctx context.Context, db *gorm.DB, data *Expires) (err error) {
if err = db.WithContext(ctx).Create(data).Error; err != nil {
err = fmt.Errorf("ExpiresCreate, err: %w", err)
return
}
return
}

// ExpiresDeleteX Delete的扩展方法,根据Cond删除一条或多条记录。如果有delete_time则软删除,否则硬删除。
func ExpiresDeleteX(ctx context.Context, db *gorm.DB, conds egorm.Conds) (err error) {
sql, binds := egorm.BuildQuery(conds)
if err = db.WithContext(ctx).Table("expires").Where(sql, binds...).Delete(&Expires{}).Error; err != nil {
err = fmt.Errorf("ExpiresDeleteX, err: %w", err)
return
}
return
}
47 changes: 47 additions & 0 deletions eoauth2/storage/refresh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package storage

import (
"context"
"fmt"

"github.com/gotomicro/ego-component/egorm"
"gorm.io/gorm"
)

type Refresh struct {
Id int `gorm:"not null;primary_key;AUTO_INCREMENT" json:"id" form:"id"` // FormID
Token string `gorm:"not null" json:"token" form:"token"` // token
Access string `gorm:"not null" json:"access" form:"access"` // access
}

func (t *Refresh) TableName() string {
return "refresh"
}

func RefreshCreate(ctx context.Context, db *gorm.DB, data *Refresh) (err error) {
if err = db.WithContext(ctx).Create(data).Error; err != nil {
err = fmt.Errorf("RefreshCreate, err: %w", err)
return
}
return
}

// RefreshDeleteX Delete的扩展方法,根据Cond删除一条或多条记录。如果有delete_time则软删除,否则硬删除。
func RefreshDeleteX(ctx context.Context, db *gorm.DB, conds egorm.Conds) (err error) {
sql, binds := egorm.BuildQuery(conds)
if err = db.WithContext(ctx).Table("refresh").Where(sql, binds...).Delete(&Refresh{}).Error; err != nil {
err = fmt.Errorf("RefreshDeleteX, err: %w", err)
return
}
return
}

// RefreshInfoX Info的扩展方法,根据Cond查询单条记录
func RefreshInfoX(ctx context.Context, db *gorm.DB, conds egorm.Conds) (resp Refresh, err error) {
sql, binds := egorm.BuildQuery(conds)
if err = db.WithContext(ctx).Table("refresh").Where(sql, binds...).First(&resp).Error; err != nil {
err = fmt.Errorf("RefreshInfoX, err: %w", err)
return
}
return
}

0 comments on commit adb305c

Please sign in to comment.