gin funn and testing
This commit is contained in:
62
security/controller/auth.go
Normal file
62
security/controller/auth.go
Normal file
@ -0,0 +1,62 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"gin-server/lib/messages"
|
||||
"gin-server/security"
|
||||
"github.com/astaxie/beego/validation"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gin-server/security/service"
|
||||
"gin-server/lib/app"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type auth struct {
|
||||
Username string `valid:"Required; MaxSize(50)"`
|
||||
Password string `valid:"Required; MaxSize(50)"`
|
||||
}
|
||||
|
||||
// @Summary Get Auth
|
||||
// @Produce json
|
||||
// @Param username query string true "userName"
|
||||
// @Param password query string true "password"
|
||||
// @Success 200 {object} app.Response
|
||||
// @Failure 500 {object} app.Response
|
||||
// @Router /auth [get]
|
||||
func GetAuth(c *gin.Context) {
|
||||
appG := app.Gin{C: c}
|
||||
valid := validation.Validation{}
|
||||
|
||||
username := c.Query("username")
|
||||
password := c.Query("password")
|
||||
|
||||
a := auth{Username: username, Password: password}
|
||||
ok, _ := valid.Valid(&a)
|
||||
|
||||
if !ok {
|
||||
app.MarkErrors(valid.Errors)
|
||||
appG.Response(http.StatusBadRequest, messages.INVALID_PARAMS, nil)
|
||||
return
|
||||
}
|
||||
|
||||
authService := service.Auth{Username: username, Password: password}
|
||||
isExist, err := authService.Check()
|
||||
if err != nil {
|
||||
appG.Response(http.StatusInternalServerError, messages.ERROR_AUTH_CHECK_TOKEN_FAIL, nil)
|
||||
return
|
||||
}
|
||||
|
||||
if !isExist {
|
||||
appG.Response(http.StatusUnauthorized, messages.ERROR_AUTH, nil)
|
||||
return
|
||||
}
|
||||
|
||||
token, err := security.GenerateToken(username, password)
|
||||
if err != nil {
|
||||
appG.Response(http.StatusInternalServerError, messages.ERROR_AUTH_TOKEN, nil)
|
||||
return
|
||||
}
|
||||
|
||||
appG.Response(http.StatusOK, messages.SUCCESS, map[string]string{
|
||||
"token": token,
|
||||
})
|
||||
}
|
61
security/jwt.go
Normal file
61
security/jwt.go
Normal file
@ -0,0 +1,61 @@
|
||||
package security
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"time"
|
||||
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
)
|
||||
|
||||
var jwtSecret []byte
|
||||
|
||||
type Claims struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
jwt.StandardClaims
|
||||
}
|
||||
|
||||
// EncodeMD5 md5 encryption
|
||||
func EncodeMD5(value string) string {
|
||||
m := md5.New()
|
||||
m.Write([]byte(value))
|
||||
|
||||
return hex.EncodeToString(m.Sum(nil))
|
||||
}
|
||||
|
||||
// GenerateToken generate tokens used for auth
|
||||
func GenerateToken(username, password string) (string, error) {
|
||||
nowTime := time.Now()
|
||||
expireTime := nowTime.Add(3 * time.Hour)
|
||||
|
||||
claims := Claims{
|
||||
EncodeMD5(username),
|
||||
EncodeMD5(password),
|
||||
jwt.StandardClaims{
|
||||
ExpiresAt: expireTime.Unix(),
|
||||
Issuer: "gin-server",
|
||||
},
|
||||
}
|
||||
|
||||
tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
token, err := tokenClaims.SignedString(jwtSecret)
|
||||
|
||||
return token, err
|
||||
}
|
||||
|
||||
// ParseToken parsing token
|
||||
func ParseToken(token string) (*Claims, error) {
|
||||
tokenClaims, err := jwt.ParseWithClaims(token, &Claims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
return jwtSecret, nil
|
||||
})
|
||||
|
||||
if tokenClaims != nil {
|
||||
if claims, ok := tokenClaims.Claims.(*Claims); ok && tokenClaims.Valid {
|
||||
return claims, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
46
security/middleware/jwt/jwt.go
Normal file
46
security/middleware/jwt/jwt.go
Normal file
@ -0,0 +1,46 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gin-server/lib/messages"
|
||||
"gin-server/security"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// JWT is jwt middleware
|
||||
func JWT() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var code int
|
||||
var data interface{}
|
||||
|
||||
code = messages.SUCCESS
|
||||
token := c.Query("token")
|
||||
if token == "" {
|
||||
code = messages.INVALID_PARAMS
|
||||
} else {
|
||||
_, err := security.ParseToken(token)
|
||||
if err != nil {
|
||||
switch err.(*jwt.ValidationError).Errors {
|
||||
case jwt.ValidationErrorExpired:
|
||||
code = messages.ERROR_AUTH_CHECK_TOKEN_TIMEOUT
|
||||
default:
|
||||
code = messages.ERROR_AUTH_CHECK_TOKEN_FAIL
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if code != messages.SUCCESS {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{
|
||||
"code": code,
|
||||
"msg": messages.GetMsg(code),
|
||||
"data": data,
|
||||
})
|
||||
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
12
security/service/auth_service.go
Normal file
12
security/service/auth_service.go
Normal file
@ -0,0 +1,12 @@
|
||||
package service
|
||||
|
||||
import "gin-server/models"
|
||||
|
||||
type Auth struct {
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
func (a *Auth) Check() (bool, error) {
|
||||
return models.CheckAuth(a.Username, a.Password)
|
||||
}
|
Reference in New Issue
Block a user