update authorized.go, database.go, middleware.go, project.go, response.go, role.go, service.go
This commit is contained in:
1 parent
53257427c9
commit
b6133df436
7 files changed
+226
No files matched your search
@@ -0,0 +1,30 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"esproject/pkg/util"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
authorizationHeader = "Authorization"
|
||||
userCtx = "UserInfo"
|
||||
userHeader = "UserHeader"
|
||||
)
|
||||
|
||||
func (h *Handler) userIdentity(c *gin.Context) {
|
||||
header := c.GetHeader(authorizationHeader)
|
||||
if header == "" {
|
||||
util.NewErrorResponse(c, http.StatusUnauthorized, "empty auth header")
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.services.Authorization.Authorize(header)
|
||||
if err != nil {
|
||||
util.NewErrorResponse(c, http.StatusUnauthorized, err.Error())
|
||||
}
|
||||
|
||||
c.Set(userCtx, user.Id)
|
||||
c.Set(userHeader, header)
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"esproject"
|
||||
"fmt"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Host string
|
||||
Port string
|
||||
Username string
|
||||
Password string
|
||||
DBName string
|
||||
}
|
||||
|
||||
func NewDB(cfg Config) (*gorm.DB, error) {
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
||||
cfg.Username,
|
||||
cfg.Password,
|
||||
cfg.Host,
|
||||
cfg.Port,
|
||||
cfg.DBName)
|
||||
|
||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = db.Set("gorm:table_options", "CHARSET=utf8mb4").AutoMigrate(&esproject.Project{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = db.Set("gorm:table_options", "CHARSET=utf8mb4").AutoMigrate(&esproject.ProjectInvite{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"esproject"
|
||||
)
|
||||
|
||||
type Authorization interface {
|
||||
Authorize(header string) (*esproject.AuthorizedUser, error)
|
||||
AuthorizeById(id int64) (*esproject.AuthorizedUser, error)
|
||||
AuthorizeAndUpdatePicture(header string, picUrl string) (*esproject.AuthorizedUser, error)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
Authorization
|
||||
}
|
||||
|
||||
func NewService() *Service {
|
||||
return &Service{
|
||||
Authorization: NewAuthService(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type error struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func NewErrorResponse(c *gin.Context, statusCode int, message string) {
|
||||
logrus.Error(message)
|
||||
c.AbortWithStatusJSON(statusCode, error{Message: message})
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"esproject"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func MatchRole(c *gin.Context, role string) bool {
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", fmt.Sprintf("https://auth.easystartup.su/api/user/self"), nil)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", c.GetHeader("Authorization"))
|
||||
req.Header.Add("Accept", "application/json")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
err := Body.Close()
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
}(resp.Body)
|
||||
|
||||
// Check if the response status is 200 OK
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return false
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
var result esproject.AuthorizedUser
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return result.Access == role
|
||||
}
|
||||
Reference in new issue
Block a user