Fix jwt middleware
This commit is contained in:
parent
eae3cbc7bb
commit
dac315db59
1 changed files with 25 additions and 1 deletions
|
@ -47,9 +47,13 @@
|
||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/golang-jwt/jwt"
|
||||||
|
|
||||||
"code.vikunja.io/api/pkg/config"
|
"code.vikunja.io/api/pkg/config"
|
||||||
"code.vikunja.io/api/pkg/db"
|
"code.vikunja.io/api/pkg/db"
|
||||||
"code.vikunja.io/api/pkg/log"
|
"code.vikunja.io/api/pkg/log"
|
||||||
|
@ -257,7 +261,27 @@ func registerAPIRoutes(a *echo.Group) {
|
||||||
|
|
||||||
// ===== Routes with Authetication =====
|
// ===== Routes with Authetication =====
|
||||||
// Authetification
|
// Authetification
|
||||||
a.Use(middleware.JWT([]byte(config.ServiceJWTSecret.GetString())))
|
a.Use(middleware.JWTWithConfig(middleware.JWTConfig{
|
||||||
|
// Custom parse function to make the middleware work with the github.com/golang-jwt/jwt package.
|
||||||
|
// See https://github.com/labstack/echo/pull/1916#issuecomment-878046299
|
||||||
|
ParseTokenFunc: func(auth string, c echo.Context) (interface{}, error) {
|
||||||
|
keyFunc := func(t *jwt.Token) (interface{}, error) {
|
||||||
|
if t.Method.Alg() != "HS256" {
|
||||||
|
return nil, fmt.Errorf("unexpected jwt signing method=%v", t.Header["alg"])
|
||||||
|
}
|
||||||
|
return []byte(config.ServiceJWTSecret.GetString()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
token, err := jwt.Parse(auth, keyFunc)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !token.Valid {
|
||||||
|
return nil, errors.New("invalid token")
|
||||||
|
}
|
||||||
|
return token, nil
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
// Rate limit
|
// Rate limit
|
||||||
setupRateLimit(a, config.RateLimitKind.GetString())
|
setupRateLimit(a, config.RateLimitKind.GetString())
|
||||||
|
|
Loading…
Reference in a new issue