feat(login):基本完成用户登录,待测。

This commit is contained in:
徐涛 2022-08-12 06:32:25 +08:00
parent 8f4e0320fd
commit 1c5bcf033b
8 changed files with 265 additions and 0 deletions

52
controller/user.go Normal file
View File

@ -0,0 +1,52 @@
package controller
import (
"electricity_bill_calc/exceptions"
"electricity_bill_calc/model"
"electricity_bill_calc/response"
"electricity_bill_calc/service"
"errors"
"net/http"
"github.com/gin-gonic/gin"
)
type _UserController struct{}
var UserController _UserController
type LoginFormData struct {
Username string `form:"uname"`
Password string `form:"upass"`
Type int8 `form:"type"`
}
func (_UserController) Login(c *gin.Context) {
result := response.NewResult(c)
loginData := new(LoginFormData)
c.BindJSON(loginData)
var (
session *model.Session
err error
)
if loginData.Type == 0 {
session, err = service.UserService.ProcessEnterpriseUserLogin(loginData.Username, loginData.Password)
} else {
session, err = service.UserService.ProcessManagementUserLogin(loginData.Username, loginData.Password)
}
if err != nil {
if errors.Is(err, &exceptions.AuthenticationError{}) {
authError := err.(exceptions.AuthenticationError)
if authError.NeedReset {
result.LoginNeedReset()
return
}
result.Error(int(authError.Code), authError.Message)
return
} else {
result.Error(http.StatusInternalServerError, err.Error())
return
}
}
result.LoginSuccess(session, false)
}

21
exceptions/auth.go Normal file
View File

@ -0,0 +1,21 @@
package exceptions
import "fmt"
type AuthenticationError struct {
Code int16
Message string
NeedReset bool
}
func NewAuthenticationError(code int16, msg string) *AuthenticationError {
return &AuthenticationError{
Code: code,
Message: msg,
NeedReset: false,
}
}
func (e AuthenticationError) Error() string {
return fmt.Sprintf("[%d]%s", e.Code, e.Message)
}

1
go.mod
View File

@ -5,6 +5,7 @@ go 1.19
require ( require (
github.com/gin-gonic/gin v1.8.1 github.com/gin-gonic/gin v1.8.1
github.com/go-redis/redis/v8 v8.11.5 github.com/go-redis/redis/v8 v8.11.5
github.com/google/uuid v1.3.0
github.com/jackc/pgx/v5 v5.0.0-beta.1 github.com/jackc/pgx/v5 v5.0.0-beta.1
github.com/shopspring/decimal v1.3.1 github.com/shopspring/decimal v1.3.1
github.com/spf13/viper v1.12.0 github.com/spf13/viper v1.12.0

23
repository/abstract.go Normal file
View File

@ -0,0 +1,23 @@
package repository
func _postProcessSingle[T interface{}](instance *T, has bool, err error) (*T, error) {
if err != nil {
return nil, err
}
if has {
return instance, nil
} else {
return nil, nil
}
}
func _postProcessList[T interface{}](instance []*T, has bool, err error) ([]*T, error) {
if err != nil {
return nil, err
}
if has {
return instance, nil
} else {
return nil, nil
}
}

22
repository/user.go Normal file
View File

@ -0,0 +1,22 @@
package repository
import (
"electricity_bill_calc/global"
"electricity_bill_calc/model"
)
type _UserRepository struct{}
var UserRepo _UserRepository
func (_UserRepository) FindUserByUsername(username string) (*model.User, error) {
user := new(model.User)
has, err := global.DBConn.Where("username=?", username).Get(user)
return _postProcessSingle(user, has, err)
}
func (_UserRepository) RetreiveUserDetail(uid string) (*model.UserDetail, error) {
user := new(model.UserDetail)
has, err := global.DBConn.Where("id=?", uid).Get(user)
return _postProcessSingle(user, has, err)
}

29
response/user_response.go Normal file
View File

@ -0,0 +1,29 @@
package response
import (
"electricity_bill_calc/model"
"net/http"
)
type LoginResponse struct {
BaseResponse
NeedReset bool `json:"needReset"`
Session *model.Session `json:"session,omitempty"`
}
func (r *Result) LoginSuccess(session *model.Session, needReset bool) {
res := &LoginResponse{}
res.Code = http.StatusOK
res.Message = "用户已成功登录。"
res.NeedReset = needReset
res.Session = session
r.Ctx.JSON(http.StatusOK, res)
}
func (r *Result) LoginNeedReset() {
res := &LoginResponse{}
res.Code = http.StatusUnauthorized
res.Message = "用户凭据已失效。"
res.NeedReset = true
r.Ctx.JSON(http.StatusOK, res)
}

23
router/security.go Normal file
View File

@ -0,0 +1,23 @@
package router
import (
"electricity_bill_calc/cache"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
func AuthenticatedSession(c *gin.Context) {
auth := c.Request.Header.Get("Authorization")
if len(auth) > 0 {
token := strings.Fields(auth)[1]
session, err := cache.RetreiveSession(token)
if err != nil {
c.AbortWithStatus(http.StatusForbidden)
}
c.Set("session", session)
}
c.Next()
}

94
service/user.go Normal file
View File

@ -0,0 +1,94 @@
package service
import (
"crypto/sha512"
"electricity_bill_calc/cache"
"electricity_bill_calc/config"
"electricity_bill_calc/exceptions"
"electricity_bill_calc/model"
"electricity_bill_calc/repository"
"fmt"
"time"
"github.com/google/uuid"
)
type _UserService struct{}
var UserService _UserService
func (_UserService) ProcessEnterpriseUserLogin(username, password string) (*model.Session, error) {
user, err := repository.UserRepo.FindUserByUsername(username)
if err != nil {
return nil, err
}
if user == nil {
return nil, exceptions.NewAuthenticationError(404, "用户不存在。")
}
if user.Type != 0 {
return nil, exceptions.NewAuthenticationError(401, "用户类型不正确。")
}
hash := sha512.New512_256()
hash.Write([]byte(password))
hashedPassword := fmt.Sprintf("%x", hash.Sum(nil))
if hashedPassword != user.Password {
return nil, exceptions.NewAuthenticationError(401, "用户凭据不正确。")
}
if user.ResetNeeded {
authErr := exceptions.NewAuthenticationError(401, "用户凭据已失效。")
authErr.NeedReset = true
return nil, authErr
}
session := &model.Session{
Token: uuid.New().String(),
Uid: user.Id,
Type: user.Type,
Name: user.Username,
ExpiresAt: time.Now().Add(config.ServiceSettings.MaxSessionLife),
}
userDetial, _ := repository.UserRepo.RetreiveUserDetail(user.Id)
if userDetial != nil {
session.Name = *userDetial.Name
}
cache.CacheSession(session)
return session, nil
}
func (_UserService) ProcessManagementUserLogin(username, password string) (*model.Session, error) {
user, err := repository.UserRepo.FindUserByUsername(username)
if err != nil {
return nil, err
}
if user == nil {
return nil, exceptions.NewAuthenticationError(404, "用户不存在。")
}
if user.Type != 1 && user.Type != 2 {
return nil, exceptions.NewAuthenticationError(401, "用户类型不正确。")
}
hash := sha512.New512_256()
hash.Write([]byte(password))
hashedPassword := fmt.Sprintf("%x", hash.Sum(nil))
if hashedPassword != user.Password {
return nil, exceptions.NewAuthenticationError(401, "用户凭据不正确。")
}
if user.ResetNeeded {
authErr := exceptions.NewAuthenticationError(401, "用户凭据已失效。")
authErr.NeedReset = true
return nil, authErr
}
session := &model.Session{
Token: uuid.New().String(),
Uid: user.Id,
Type: user.Type,
Name: user.Username,
ExpiresAt: time.Now().Add(config.ServiceSettings.MaxSessionLife),
}
userDetial, _ := repository.UserRepo.RetreiveUserDetail(user.Id)
if userDetial != nil {
session.Name = *userDetial.Name
}
cache.CacheSession(session)
return session, nil
}