mirror of
https://github.com/FlourishingWorld/hk4e.git
synced 2026-02-12 15:22:25 +08:00
init commit
This commit is contained in:
65
water/controller/auth_controller.go
Normal file
65
water/controller/auth_controller.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
providerApiEntity "flswld.com/annie-user-api/entity"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (c *Controller) login(context *gin.Context) {
|
||||
json := make(map[string]string)
|
||||
err := context.BindJSON(&json)
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"code": 10003,
|
||||
"msg": "参数错误",
|
||||
})
|
||||
return
|
||||
}
|
||||
accessToken, refreshToken, err := c.service.LoginAuth(&providerApiEntity.User{Username: json["username"], Password: json["password"]})
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"code": 10002,
|
||||
"msg": "用户名或密码错误",
|
||||
})
|
||||
return
|
||||
}
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"code": 0,
|
||||
"msg": "",
|
||||
"access_token": accessToken,
|
||||
"refresh_token": refreshToken,
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Controller) refreshToken(context *gin.Context) {
|
||||
json := make(map[string]string)
|
||||
err := context.BindJSON(&json)
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"code": 10003,
|
||||
"msg": "参数错误",
|
||||
})
|
||||
return
|
||||
}
|
||||
accessToken, err := c.service.RefreshToken(json["refresh_token"])
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"code": 10004,
|
||||
"msg": "刷新access_token失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"code": 0,
|
||||
"msg": "",
|
||||
"access_token": accessToken,
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Controller) authTest(context *gin.Context) {
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"code": 0,
|
||||
"msg": "认证测试成功",
|
||||
})
|
||||
}
|
||||
72
water/controller/controller.go
Normal file
72
water/controller/controller.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"flswld.com/common/config"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"water/service"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
service *service.Service
|
||||
}
|
||||
|
||||
func NewController(service *service.Service) (r *Controller) {
|
||||
r = new(Controller)
|
||||
r.service = service
|
||||
go r.registerRouter()
|
||||
return r
|
||||
}
|
||||
|
||||
func (c *Controller) getAccessToken(context *gin.Context) string {
|
||||
accessToken := context.GetHeader("Authorization")
|
||||
divIndex := strings.Index(accessToken, " ")
|
||||
if divIndex > 0 {
|
||||
payload := accessToken[divIndex+1:]
|
||||
return payload
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// access_token鉴权
|
||||
func (c *Controller) authorize() gin.HandlerFunc {
|
||||
return func(context *gin.Context) {
|
||||
valid := c.service.VerifyAccessToken(c.getAccessToken(context))
|
||||
if valid == true {
|
||||
// 验证通过
|
||||
context.Next()
|
||||
return
|
||||
}
|
||||
// 验证不通过
|
||||
context.Abort()
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"code": "10001",
|
||||
"msg": "没有访问权限",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) registerRouter() {
|
||||
if config.CONF.Logger.Level == "DEBUG" {
|
||||
gin.SetMode(gin.DebugMode)
|
||||
} else {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
engine := gin.Default()
|
||||
// 非认证接口
|
||||
engine.POST("/auth/login", c.login)
|
||||
engine.POST("/auth/refresh", c.refreshToken)
|
||||
engine.Use(c.authorize())
|
||||
// 认证接口
|
||||
engine.GET("/auth/test", c.authTest)
|
||||
// 获取内存token
|
||||
//engine.POST("/oauth/token", c.getMemoryToken)
|
||||
// 验证内存token
|
||||
//engine.POST("/oauth/check_token", c.checkMemoryToken)
|
||||
port := strconv.FormatInt(int64(config.CONF.HttpPort), 10)
|
||||
portStr := ":" + port
|
||||
_ = engine.Run(portStr)
|
||||
}
|
||||
44
water/controller/memory_token_controller.go
Normal file
44
water/controller/memory_token_controller.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"flswld.com/logger"
|
||||
"github.com/gin-gonic/gin"
|
||||
"water/entity"
|
||||
)
|
||||
|
||||
// 获取token
|
||||
func (c *Controller) getMemoryToken(context *gin.Context) {
|
||||
login := new(entity.Login)
|
||||
err := context.ShouldBindJSON(login)
|
||||
if err != nil {
|
||||
logger.LOG.Error("[controller:getMemoryToken] context.ShouldBindJSON() fail: %v", err)
|
||||
return
|
||||
}
|
||||
auth, token := c.service.GetMemoryToken(login)
|
||||
if auth {
|
||||
context.JSON(200, gin.H{
|
||||
"code": 0,
|
||||
"token": token,
|
||||
})
|
||||
} else {
|
||||
context.JSON(200, gin.H{
|
||||
"code": 401,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 验证token
|
||||
func (c *Controller) checkMemoryToken(context *gin.Context) {
|
||||
token := new(entity.MemoryToken)
|
||||
err := context.ShouldBindJSON(token)
|
||||
if err != nil {
|
||||
logger.LOG.Error("[controller:checkMemoryToken] context.ShouldBindJSON() fail: %v", err)
|
||||
return
|
||||
}
|
||||
valid, uid := c.service.CheckMemoryToken(token.Token)
|
||||
context.JSON(200, gin.H{
|
||||
"code": 0,
|
||||
"valid": valid,
|
||||
"uid": uid,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user