/
internalHandler.go
51 lines (42 loc) 路 1.17 KB
/
internalHandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package handlers
import (
"log"
"net/http"
"github.com/franpog859/cleanaux-backend/auth-service/internal/auth"
"github.com/franpog859/cleanaux-backend/auth-service/internal/model"
"github.com/gin-gonic/gin"
)
// InternalHandler interface
type InternalHandler interface {
Authorize(context *gin.Context)
}
type internalHandler struct {
secretKey string
}
// NewInternalHandler provides InternalHandler interface
func NewInternalHandler(secretKey string) InternalHandler {
return &internalHandler{
secretKey: secretKey,
}
}
func (ih *internalHandler) Authorize(context *gin.Context) {
authHeader := context.GetHeader(model.AuthHeaderKey)
token, err := auth.ExtractTokenFromHeader(authHeader)
if err != nil {
log.Printf("Invalid authorization header: %v", err)
context.AbortWithStatus(http.StatusBadRequest)
return
}
valid, err := auth.IsTokenValid(token, ih.secretKey)
if err != nil {
log.Printf("Error while validating token: %v", err)
context.AbortWithStatus(http.StatusUnauthorized)
return
}
if !valid {
log.Printf("Invalid authorization token: %s", token)
context.AbortWithStatus(http.StatusUnauthorized)
return
}
context.Status(http.StatusOK)
}