release: v0.6.1
This commit is contained in:
commit
877eef7f07
118 files changed
+46315
No files matched your search
@@ -0,0 +1,56 @@
|
||||
package auth_middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/EvolutionAPI/evolution-go/pkg/config"
|
||||
instance_service "github.com/EvolutionAPI/evolution-go/pkg/instance/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Middleware interface {
|
||||
Auth(ctx *gin.Context)
|
||||
AuthAdmin(ctx *gin.Context)
|
||||
}
|
||||
|
||||
type middleware struct {
|
||||
config *config.Config
|
||||
instanceService instance_service.InstanceService
|
||||
}
|
||||
|
||||
func (m middleware) Auth(ctx *gin.Context) {
|
||||
token := ctx.GetHeader("apikey")
|
||||
if token == "" {
|
||||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "not authorized"})
|
||||
return
|
||||
}
|
||||
|
||||
instance, err := m.instanceService.GetInstanceByToken(token)
|
||||
if err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "not authorized"})
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Set("instance", instance)
|
||||
|
||||
ctx.Next()
|
||||
}
|
||||
|
||||
func (m middleware) AuthAdmin(ctx *gin.Context) {
|
||||
token := ctx.GetHeader("apikey")
|
||||
if token == "" {
|
||||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "not authorized"})
|
||||
return
|
||||
}
|
||||
|
||||
if token != m.config.GlobalApiKey {
|
||||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "not authorized"})
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Next()
|
||||
}
|
||||
|
||||
func NewMiddleware(config *config.Config, instanceService instance_service.InstanceService) *middleware {
|
||||
return &middleware{config: config, instanceService: instanceService}
|
||||
}
|
||||
Reference in new issue
Block a user