36 lines
719 B
Go
36 lines
719 B
Go
package handler
|
|
|
|
import (
|
|
"esauth/pkg/util"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const (
|
|
authorizationHeader = "Authorization"
|
|
userCtx = "userId"
|
|
)
|
|
|
|
func (h *Handler) userIdentity(c *gin.Context) {
|
|
header := c.GetHeader(authorizationHeader)
|
|
if header == "" {
|
|
util.NewErrorResponse(c, http.StatusUnauthorized, "empty auth header")
|
|
return
|
|
}
|
|
|
|
headerParts := strings.Split(header, " ")
|
|
if len(headerParts) != 2 {
|
|
util.NewErrorResponse(c, http.StatusUnauthorized, "invalid auth header")
|
|
return
|
|
}
|
|
|
|
userId, err := h.services.Authorization.ParseToken(headerParts[1])
|
|
if err != nil {
|
|
util.NewErrorResponse(c, http.StatusUnauthorized, err.Error())
|
|
}
|
|
|
|
c.Set(userCtx, userId)
|
|
}
|