This repository has been archived by the owner on Sep 15, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from askuy/feature/gormandoauth
support oauth2 storage
- Loading branch information
Showing
7 changed files
with
379 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |