登录与授权等

This commit is contained in:
2024-01-07 15:13:35 +08:00
parent 968eab9b06
commit ec548f4256
22 changed files with 248 additions and 53 deletions

View File

@@ -1,7 +1,16 @@
package common
/*
2000 用户操作错误
3000 预留
4000 数据库操作错误
5000 系统错误
*/
const (
LoginErrorCode = 2001
NoPermission = 2002
LoginExpired = 2003
LoginErrorCode = 2001
NoPermission = 2002
LoginExpired = 2003
InvalidInstance = 2004
DuplicatedValue = 4001
)

View File

@@ -69,6 +69,7 @@ func migrate() {
err = db.AutoMigrate(&entity.Update{})
err = db.AutoMigrate(&entity.Grant{})
err = db.AutoMigrate(&entity.Token{})
err = db.AutoMigrate(&entity.Metadata{})
if err != nil {
log.Logger.Fatal("关联数据表失败:", err)

View File

@@ -2,12 +2,13 @@ package entity
import (
"gorm.io/gorm"
"time"
)
// Grant Grant是授权给实例的给予实例访问权限
type Grant struct {
gorm.Model `json:"model"`
Token string `gorm:"unique;not null" json:"token,omitempty"`
TTL int `gorm:"not null;default:0" json:"ttl,omitempty"`
GrantTo uint `gorm:"not null;default:0;comment:instances(id) 授权给实例,0表示无指定(所有)" json:"grant_to,omitempty"`
Token string `gorm:"unique;not null" json:"token"`
ExpireAt time.Time `gorm:"index" json:"expire_at"`
GrantTo string `gorm:"not null;default:'';comment:instances(name)授权给实例,表示无指定(所有)" json:"grant_to"`
}

View File

@@ -6,6 +6,6 @@ import (
type Instance struct {
gorm.Model `json:"model"`
Name string `gorm:"unique;not null" json:"name,omitempty"`
UpdateURL string `gorm:"column:update_url;not null;default:'';comment:更新URL未指定使用默认" json:"update_url,omitempty"`
Name string `gorm:"unique;not null" json:"name"`
UpdateURL string `gorm:"column:update_url;not null;default:'';comment:更新URL未指定使用默认" json:"update_url"`
}

View File

@@ -0,0 +1,7 @@
package entity
type Metadata struct {
ID uint `json:"id"`
Key string `gorm:"unique;not null" json:"key"`
Value string `gorm:"not null;default:''" json:"value"`
}

View File

@@ -5,7 +5,7 @@ import "gorm.io/gorm"
// Token Token是授权给用户的给予用户登录权限
type Token struct {
gorm.Model `json:"model"`
Token string `gorm:"unique;not null" json:"token,omitempty"`
GrantTo string `gorm:"index;not null;default:''" json:"grant_to,omitempty"`
TTL int `gorm:"not null;default:0" json:"ttl,omitempty"`
Token string `gorm:"unique;not null" json:"token"`
GrantTo string `gorm:"index;not null;default:''" json:"grant_to"`
TTL int `gorm:"not null;default:0" json:"ttl"`
}

View File

@@ -4,7 +4,7 @@ import "gorm.io/gorm"
type Update struct {
gorm.Model `json:"model"`
HashID string `gorm:"index;not null" json:"hash_id,omitempty"`
Comment string `gorm:"not null;default:'';comment:更新内容或注释" json:"comment,omitempty"`
Changes string `gorm:"not null;comment:更改的文件列表逗号分隔引用files(hash_id)" json:"changes,omitempty"`
HashID string `gorm:"index;not null" json:"hash_id"`
Comment string `gorm:"not null;default:'';comment:更新内容或注释" json:"comment"`
Changes string `gorm:"not null;comment:更改的文件列表逗号分隔引用files(hash_id)" json:"changes"`
}

View File

@@ -4,7 +4,7 @@ import "gorm.io/gorm"
type User struct {
gorm.Model `json:"model"`
Username string `gorm:"unique;not null" json:"username,omitempty"`
Password string `gorm:"not null" json:"password,omitempty"`
Roles string `gorm:"not null;default:''" json:"roles,omitempty"`
Username string `gorm:"unique;not null" json:"username"`
Password string `gorm:"not null" json:"password"`
Roles string `gorm:"not null;default:''" json:"roles"`
}

View File

@@ -1,10 +0,0 @@
package param
type AuthorizeQueryParam struct {
ClientId uint `form:"client_id" binding:"required"`
ResponseType string `form:"response_type" binding:"required"`
State string `form:"state" binding:"required"`
Scope string `form:"scope" binding:"required"`
CodeChallenge string `form:"code_challenge" binding:"required"`
CodeChallengeMethod string `form:"code_challenge_method" binding:"required"`
}

12
pkg/param/instance.go Normal file
View File

@@ -0,0 +1,12 @@
package param
type AddInstanceParam struct {
Name string `json:"name" binding:"required"`
UpdateURL string `json:"update_url"`
}
type NewGrantTokenParam struct {
Target string `json:"target" binding:"required"`
TTL int `json:"ttl"`
ExpireAt string `json:"expire_at"`
}

View File

@@ -1,6 +1,6 @@
package param
type LoginParam struct {
Username string `json:"username"`
Password string `json:"password"`
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
}

View File

@@ -37,7 +37,7 @@ func (r *Result) Fail(code int, msg string) {
res := Root{
Code: code,
Msg: msg,
Data: gin.H{},
Data: nil,
}
r.ctx.JSON(http.StatusOK, res)
r.ctx.Abort()
@@ -57,14 +57,22 @@ func (r *Result) BadRequest() {
r.Fail(http.StatusBadRequest, "请求参数错误")
}
func (r *Result) BadRequestWithMsg(msg string) {
r.Fail(http.StatusBadRequest, msg)
}
func (r *Result) LoginError() {
r.Fail(common.LoginErrorCode, "账号或密码错误")
}
func (r *Result) Unauthorized() {
func (r *Result) UnLogin() {
r.Fail(http.StatusUnauthorized, "未登录")
}
func (r *Result) Unauthorized() {
r.Fail(http.StatusUnauthorized, "未授权")
}
func (r *Result) NoPermission() {
r.Fail(common.NoPermission, "权限不足")
}
@@ -72,3 +80,11 @@ func (r *Result) NoPermission() {
func (r *Result) LoginExpired() {
r.Fail(common.LoginExpired, "登录过期")
}
func (r *Result) DuplicatedValue(msg string) {
r.Fail(common.DuplicatedValue, msg)
}
func (r *Result) InvalidInstance(instName string) {
r.Fail(common.InvalidInstance, "指定的实例不存在:"+instName)
}

23
pkg/util/time.go Normal file
View File

@@ -0,0 +1,23 @@
package util
import "time"
func ToSQLTimeFormat(t time.Time) string {
return t.Format("2006-01-02 15:04:05")
}
func MustParseSQLTime(timeStr string) time.Time {
timeObj, err := time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local)
if err != nil {
panic(err)
}
return timeObj
}
func IsSQLTimeFormat(timeStr string) bool {
_, err := time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local)
if err != nil {
return false
}
return true
}

View File

@@ -1,5 +0,0 @@
package util
func GenSessionID() string {
return RandStr(32)
}