mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-18 20:00:25 +08:00
[API节点]支持HTTP API
This commit is contained in:
68
internal/db/models/api_access_token_dao.go
Normal file
68
internal/db/models/api_access_token_dao.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
"time"
|
||||
)
|
||||
|
||||
type APIAccessTokenDAO dbs.DAO
|
||||
|
||||
func NewAPIAccessTokenDAO() *APIAccessTokenDAO {
|
||||
return dbs.NewDAO(&APIAccessTokenDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeAPIAccessTokens",
|
||||
Model: new(APIAccessToken),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*APIAccessTokenDAO)
|
||||
}
|
||||
|
||||
var SharedAPIAccessTokenDAO *APIAccessTokenDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedAPIAccessTokenDAO = NewAPIAccessTokenDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// 生成AccessToken
|
||||
func (this *APIAccessTokenDAO) GenerateAccessToken(userId int64) (token string, expiresAt int64, err error) {
|
||||
// 查询以前的
|
||||
accessToken, err := this.Query().
|
||||
Attr("userId", userId).
|
||||
Find()
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
|
||||
token = rands.String(128) // TODO 增强安全性,将来使用 base64_encode(encrypt(salt+random)) 算法来代替
|
||||
expiresAt = time.Now().Unix() + 7200
|
||||
|
||||
op := NewAPIAccessTokenOperator()
|
||||
|
||||
if accessToken != nil {
|
||||
op.Id = accessToken.(*APIAccessToken).Id
|
||||
}
|
||||
|
||||
op.UserId = userId
|
||||
op.Token = token
|
||||
op.CreatedAt = time.Now().Unix()
|
||||
op.ExpiredAt = expiresAt
|
||||
err = this.Save(op)
|
||||
return
|
||||
}
|
||||
|
||||
// 查找AccessToken
|
||||
func (this *APIAccessTokenDAO) FindAccessToken(token string) (*APIAccessToken, error) {
|
||||
one, err := this.Query().
|
||||
Attr("token", token).
|
||||
Find()
|
||||
if one == nil || err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return one.(*APIAccessToken), nil
|
||||
}
|
||||
Reference in New Issue
Block a user