Move jwt keys to database

Moved code for it to one place
Updated to chi v5
This commit is contained in:
Jamie Curnow
2023-05-26 14:50:41 +10:00
parent 29990110b1
commit ceb62fb0f2
12 changed files with 168 additions and 139 deletions

View File

@ -13,7 +13,7 @@ import (
"npm/internal/logger"
"npm/internal/util"
"github.com/go-chi/jwtauth"
"github.com/go-chi/jwtauth/v5"
)
// DecodeAuth decodes an auth header
@ -29,7 +29,7 @@ func DecodeAuth() func(http.Handler) http.Handler {
}
tokenAuth := jwtauth.New("RS256", privateKey, publicKey)
return jwtauth.Verifier(tokenAuth)
return jwtauth.Verify(tokenAuth, jwtauth.TokenFromHeader)
}
// Enforce is a authentication middleware to enforce access from the
@ -44,13 +44,14 @@ func Enforce(permission string) func(http.Handler) http.Handler {
token, claims, err := jwtauth.FromContext(ctx)
if err != nil {
logger.Debug("EnforceError: %+v", err)
h.ResultErrorJSON(w, r, http.StatusUnauthorized, err.Error(), nil)
return
}
userID := uint(claims["uid"].(float64))
_, enabled := user.IsEnabled(userID)
if token == nil || !token.Valid || !enabled {
if token == nil || !enabled {
h.ResultErrorJSON(w, r, http.StatusUnauthorized, "Unauthorised", nil)
return
}

View File

@ -5,7 +5,7 @@ import (
"net/http"
"strings"
"github.com/go-chi/chi"
"github.com/go-chi/chi/v5"
)
var methodMap = []string{

View File

@ -12,8 +12,8 @@ import (
"npm/internal/logger"
"npm/internal/serverevents"
"github.com/go-chi/chi"
chiMiddleware "github.com/go-chi/chi/middleware"
"github.com/go-chi/chi/v5"
chiMiddleware "github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
)