Skip to content

Commit

Permalink
refactor: make it look better
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Aug 9, 2024
1 parent ba0c9ce commit 11c7f16
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 21 deletions.
3 changes: 2 additions & 1 deletion auth/jwt/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion auth/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
1 change: 1 addition & 0 deletions auth/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package auth
import (
"context"
"fmt"

"github.com/axone-protocol/axone-sdk/dataverse"
)

Expand Down
7 changes: 3 additions & 4 deletions http/server.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package http

import (
"github.com/gorilla/mux"
"net/http"

"github.com/gorilla/mux"
)

type Server struct {
Expand All @@ -20,9 +21,7 @@ func NewServer(addr string, opts ...Option) *Server {
router: router,
}

for _, opt := range opts {
opt(s)
}
WithOptions(opts...)(s)

return s
}
Expand Down
25 changes: 13 additions & 12 deletions provider/storage/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions provider/storage/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 11c7f16

Please sign in to comment.