From 11c7f161081ffe59747a8bb772a1820da32d722c Mon Sep 17 00:00:00 2001 From: Arnaud Mimart <33665250+amimart@users.noreply.github.com> Date: Fri, 9 Aug 2024 09:08:59 +0200 Subject: [PATCH] refactor: make it look better --- auth/jwt/http.go | 3 ++- auth/jwt/jwt.go | 3 ++- auth/proxy.go | 1 + http/server.go | 7 +++---- provider/storage/http.go | 25 +++++++++++++------------ provider/storage/proxy.go | 9 ++++++--- 6 files changed, 27 insertions(+), 21 deletions(-) diff --git a/auth/jwt/http.go b/auth/jwt/http.go index f73521d..4fd7ec6 100644 --- a/auth/jwt/http.go +++ b/auth/jwt/http.go @@ -3,9 +3,10 @@ package jwt import ( "context" "errors" - "github.com/axone-protocol/axone-sdk/auth" "io" "net/http" + + "github.com/axone-protocol/axone-sdk/auth" ) func (f *Factory) HTTPAuthHandler(proxy auth.Proxy) http.Handler { diff --git a/auth/jwt/jwt.go b/auth/jwt/jwt.go index 8f5adce..cc8b78a 100644 --- a/auth/jwt/jwt.go +++ b/auth/jwt/jwt.go @@ -2,9 +2,10 @@ package jwt import ( "fmt" - "github.com/axone-protocol/axone-sdk/auth" "time" + "github.com/axone-protocol/axone-sdk/auth" + "github.com/golang-jwt/jwt" "github.com/google/uuid" ) diff --git a/auth/proxy.go b/auth/proxy.go index 01ed4cc..0b7557d 100644 --- a/auth/proxy.go +++ b/auth/proxy.go @@ -3,6 +3,7 @@ package auth import ( "context" "fmt" + "github.com/axone-protocol/axone-sdk/dataverse" ) diff --git a/http/server.go b/http/server.go index 7c5e850..dac3a59 100644 --- a/http/server.go +++ b/http/server.go @@ -1,8 +1,9 @@ package http import ( - "github.com/gorilla/mux" "net/http" + + "github.com/gorilla/mux" ) type Server struct { @@ -20,9 +21,7 @@ func NewServer(addr string, opts ...Option) *Server { router: router, } - for _, opt := range opts { - opt(s) - } + WithOptions(opts...)(s) return s } diff --git a/provider/storage/http.go b/provider/storage/http.go index bfdda62..39b6d3d 100644 --- a/provider/storage/http.go +++ b/provider/storage/http.go @@ -2,28 +2,29 @@ package storage import ( "context" + "io" + "net/http" + "time" + "github.com/axone-protocol/axone-sdk/auth" "github.com/axone-protocol/axone-sdk/auth/jwt" axonehttp "github.com/axone-protocol/axone-sdk/http" "github.com/gorilla/mux" - "io" - "net/http" - "time" ) -func (s *Proxy) HTTPConfigurator(jwtSecretKey []byte, jwtTTL time.Duration) axonehttp.Option { - jwtFactory := jwt.NewFactory(jwtSecretKey, s.key.DID, jwtTTL) +func (p *Proxy) HTTPConfigurator(jwtSecretKey []byte, jwtTTL time.Duration) axonehttp.Option { + jwtFactory := jwt.NewFactory(jwtSecretKey, p.key.DID, jwtTTL) return axonehttp.WithOptions( - axonehttp.WithRoute(http.MethodGet, "/authenticate", jwtFactory.HTTPAuthHandler(s)), - axonehttp.WithRoute(http.MethodGet, "/{path}}", jwtFactory.VerifyHTTPMiddleware(s.HTTPReadHandler())), - axonehttp.WithRoute(http.MethodPost, "/{path}}", jwtFactory.VerifyHTTPMiddleware(s.HTTPStoreHandler())), + axonehttp.WithRoute(http.MethodGet, "/authenticate", jwtFactory.HTTPAuthHandler(p)), + axonehttp.WithRoute(http.MethodGet, "/{path}", jwtFactory.VerifyHTTPMiddleware(p.HTTPReadHandler())), + axonehttp.WithRoute(http.MethodPost, "/{path}", jwtFactory.VerifyHTTPMiddleware(p.HTTPStoreHandler())), ) } -func (s *Proxy) HTTPReadHandler() auth.AuthenticatedHandler { +func (p *Proxy) HTTPReadHandler() auth.AuthenticatedHandler { return func(id *auth.Identity, writer http.ResponseWriter, request *http.Request) { - resource, err := s.Read(context.Background(), id, mux.Vars(request)["path"]) + resource, err := p.Read(context.Background(), id, mux.Vars(request)["path"]) if err != nil { // ... return @@ -34,9 +35,9 @@ func (s *Proxy) HTTPReadHandler() auth.AuthenticatedHandler { } } -func (s *Proxy) HTTPStoreHandler() auth.AuthenticatedHandler { +func (p *Proxy) HTTPStoreHandler() auth.AuthenticatedHandler { return func(id *auth.Identity, writer http.ResponseWriter, request *http.Request) { - vc, err := s.Store(context.Background(), id, mux.Vars(request)["path"], request.Body) + vc, err := p.Store(context.Background(), id, mux.Vars(request)["path"], request.Body) if err != nil { // ... return diff --git a/provider/storage/proxy.go b/provider/storage/proxy.go index f3f313c..9305225 100644 --- a/provider/storage/proxy.go +++ b/provider/storage/proxy.go @@ -5,14 +5,17 @@ import ( "context" "errors" "fmt" + "io" + "github.com/axone-protocol/axone-sdk/auth" "github.com/axone-protocol/axone-sdk/dataverse" "github.com/axone-protocol/axone-sdk/keys" - "io" ) -const readAction = "read" -const storeAction = "store" +const ( + readAction = "read" + storeAction = "store" +) type Proxy struct { key *keys.Key