登录功能完成

This commit is contained in:
2022-10-04 00:36:01 +08:00
parent 26278707bb
commit 968eab9b06
31 changed files with 1022 additions and 36 deletions

57
internal/service/token.go Normal file
View File

@@ -0,0 +1,57 @@
package service
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"mc-client-updater-server/pkg/dao"
"mc-client-updater-server/pkg/dao/entity"
"mc-client-updater-server/pkg/result"
"time"
)
type TokenService struct {
ctx *gin.Context
}
func NewTokenService(c *gin.Context) *TokenService {
return &TokenService{ctx: c}
}
func (s *TokenService) VerifyToken(token string) (*entity.Token, bool) {
res := result.NewResult(s.ctx)
// 是否存在
tokenRow := s.getToken(token)
if tokenRow == nil {
res.Unauthorized()
return nil, false
}
// 是否过期
if tokenRow.TTL != 0 && int(time.Since(tokenRow.CreatedAt).Seconds()) > tokenRow.TTL {
res.LoginExpired()
return tokenRow, false
}
return tokenRow, true
}
func (s *TokenService) getToken(token string) *entity.Token {
tokenRow := entity.Token{Token: token}
tx := dao.DB().Last(&tokenRow)
if tx.Error == gorm.ErrRecordNotFound {
return nil
}
return &tokenRow
}
func (s *TokenService) getTokenByUsername(username string) *entity.Token {
tokenRow := entity.Token{GrantTo: username}
tx := dao.DB().First(&tokenRow)
if tx.Error == gorm.ErrRecordNotFound {
return nil
}
return &tokenRow
}
func (s *TokenService) AddToken(tokenObj *entity.Token) error {
tx := dao.DB().Create(tokenObj)
return tx.Error
}

78
internal/service/user.go Normal file
View File

@@ -0,0 +1,78 @@
package service
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"mc-client-updater-server/pkg/dao"
"mc-client-updater-server/pkg/dao/entity"
"mc-client-updater-server/pkg/password"
"mc-client-updater-server/pkg/result"
"mc-client-updater-server/pkg/token"
"mc-client-updater-server/pkg/util"
"strings"
)
type UserService struct {
ctx *gin.Context
}
func NewUserService(c *gin.Context) *UserService {
return &UserService{ctx: c}
}
func (s *UserService) Login(username, pwd string) {
res := result.NewResult(s.ctx)
user := entity.User{Username: username}
tx := dao.DB().First(&user)
if tx.Error != nil {
switch tx.Error {
case gorm.ErrRecordNotFound:
res.LoginError()
return
default:
res.LoginError()
return
}
}
// 验证密码
ok, err := password.Password().Verify(pwd, user.Password)
if err != nil || !ok {
res.LoginError()
return
}
tokenStr := token.NewToken(user.Username)
tokenResult := &entity.Token{
Token: tokenStr,
GrantTo: user.Username,
TTL: 86400,
}
tokenSrv := NewTokenService(s.ctx)
err = tokenSrv.AddToken(tokenResult)
if err != nil {
res.InternalServerError("创建登录记录时发生意外错误")
return
}
res.Success(tokenResult)
}
func (s *UserService) hasRole(role string, user *entity.User) bool {
roles := strings.Split(user.Roles, ",")
return util.InStringSlice(roles, role)
}
func (s *UserService) getUserByUsername(name string) *entity.User {
user := entity.User{Username: name}
tx := dao.DB().First(&user)
if tx.Error != nil {
return nil
}
return &user
}
func (s *UserService) JudgeRoleByToken(role string, token *entity.Token) bool {
user := s.getUserByUsername(token.GrantTo)
return s.hasRole(role, user)
}