Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions internal/controller/httpapi/v1/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package v1

import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"time"
Expand All @@ -15,7 +17,10 @@ import (
"github.com/device-management-toolkit/console/pkg/consoleerrors"
)

var ErrLogin = consoleerrors.CreateConsoleError("LoginHandler")
var (
ErrLogin = consoleerrors.CreateConsoleError("LoginHandler")
ErrUnexpectedSigningMethod = errors.New("unexpected signing method")
)

type LoginRoute struct {
Config *config.Config
Expand Down Expand Up @@ -99,11 +104,17 @@ func (lr LoginRoute) JWTAuthMiddleware() gin.HandlerFunc {
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid access token"})
c.Abort()

return
}
} else {
claims := &jwt.MapClaims{}

token, err := jwt.ParseWithClaims(tokenString, claims, func(_ *jwt.Token) (interface{}, error) {
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("%w: %v", ErrUnexpectedSigningMethod, token.Header["alg"])
}

return []byte(lr.Config.JWTKey), nil
})

Expand Down
10 changes: 9 additions & 1 deletion internal/controller/ws/v1/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package v1

import (
"compress/flate"
"errors"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
Expand All @@ -13,6 +15,8 @@ import (
"github.com/device-management-toolkit/console/pkg/logger"
)

var ErrUnexpectedSigningMethod = errors.New("unexpected signing method")

type RedirectRoutes struct {
d devices.Feature
l logger.Interface
Expand Down Expand Up @@ -41,7 +45,11 @@ func (r *RedirectRoutes) websocketHandler(c *gin.Context) {

claims := &jwt.MapClaims{}

token, err := jwt.ParseWithClaims(tokenString, claims, func(_ *jwt.Token) (interface{}, error) {
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("%w: %v", ErrUnexpectedSigningMethod, token.Header["alg"])
}

return []byte(config.ConsoleConfig.JWTKey), nil
})

Expand Down
Loading