Skip to content

Commit

Permalink
rpc: use goware/validation for origin validation & matching
Browse files Browse the repository at this point in the history
  • Loading branch information
patrislav committed Mar 22, 2024
1 parent ca1f28b commit 09ce6cd
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 36 deletions.
18 changes: 10 additions & 8 deletions proto/authenticator.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions proto/authenticator.ridl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ struct Tenant
- oidcProviders: []OpenIdProvider
+ go.field.name = OIDCProviders
- allowedOrigins: []string
+ go.field.type = validation.Origins
+ go.type.import = github.com/goware/validation
- updatedAt: timestamp

struct TenantData
Expand Down Expand Up @@ -81,6 +83,8 @@ struct TenantData
+ json = kmsKeys
- allowedOrigins: []string
+ json = allowedOrigins
+ go.field.type = validation.Origins
+ go.type.import = github.com/goware/validation

struct MiniSequenceContext
- factory: string
Expand Down
18 changes: 10 additions & 8 deletions proto/clients/authenticator.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions proto/clients/authenticator.gen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable */
// sequence-waas-authenticator v0.1.0 3f8465a3a233581e35b46bda830d8b31e2e71bde
// sequence-waas-authenticator v0.1.0 72a0165f606ce0df3a64b01fece4b59c6b05fd10
// --
// Code generated by [email protected] with typescript generator. DO NOT EDIT.
//
Expand All @@ -12,7 +12,7 @@ export const WebRPCVersion = "v1"
export const WebRPCSchemaVersion = "v0.1.0"

// Schema hash generated from your RIDL schema
export const WebRPCSchemaHash = "3f8465a3a233581e35b46bda830d8b31e2e71bde"
export const WebRPCSchemaHash = "72a0165f606ce0df3a64b01fece4b59c6b05fd10"

//
// Types
Expand Down
24 changes: 14 additions & 10 deletions rpc/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/0xsequence/waas-authenticator/proto"
"github.com/0xsequence/waas-authenticator/rpc/attestation"
"github.com/0xsequence/waas-authenticator/rpc/crypto"
"github.com/goware/validation"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -57,8 +58,9 @@ func (s *RPC) CreateTenant(
return nil, "", fmt.Errorf("invalid oidcProviders: %w", err)
}

if len(allowedOrigins) == 0 {
return nil, "", fmt.Errorf("at least one allowed origin is required")
origins, err := validation.NewOrigins(allowedOrigins...)
if err != nil {
return nil, "", fmt.Errorf("invalid allowedOrigins: %w", err)
}

wallet, err := ethwallet.NewWalletFromRandomEntropy()
Expand Down Expand Up @@ -111,7 +113,7 @@ func (s *RPC) CreateTenant(
WaasAccessToken: waasAccessToken,
OIDCProviders: oidcProviders,
KMSKeys: s.Config.KMS.DefaultSessionKeys,
AllowedOrigins: allowedOrigins,
AllowedOrigins: origins,
}

encryptedKey, algorithm, ciphertext, err := crypto.EncryptData(ctx, att, s.Config.KMS.TenantKeys[0], tenantData)
Expand Down Expand Up @@ -168,12 +170,13 @@ func (s *RPC) UpdateTenant(
return nil, fmt.Errorf("invalid oidcProviders: %w", err)
}

if len(allowedOrigins) == 0 {
return nil, fmt.Errorf("at least one allowed origin is required")
origins, err := validation.NewOrigins(allowedOrigins...)
if err != nil {
return nil, fmt.Errorf("invalid allowedOrigins: %w", err)
}

tntData.OIDCProviders = oidcProviders
tntData.AllowedOrigins = allowedOrigins
tntData.AllowedOrigins = origins

encryptedKey, algorithm, ciphertext, err := crypto.EncryptData(ctx, att, s.Config.KMS.TenantKeys[0], tntData)
if err != nil {
Expand All @@ -190,10 +193,11 @@ func (s *RPC) UpdateTenant(
}

retTenant := &proto.Tenant{
ProjectID: tnt.ProjectID,
Version: tnt.Version,
OIDCProviders: tntData.OIDCProviders,
UpdatedAt: tnt.CreatedAt,
ProjectID: tnt.ProjectID,
Version: tnt.Version,
OIDCProviders: tntData.OIDCProviders,
AllowedOrigins: tntData.AllowedOrigins,
UpdatedAt: tnt.CreatedAt,
}
return retTenant, nil
}
Expand Down
10 changes: 6 additions & 4 deletions rpc/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/0xsequence/nitrocontrol/enclave"
"github.com/0xsequence/waas-authenticator/data"
"github.com/0xsequence/waas-authenticator/proto"
"github.com/goware/validation"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -53,7 +54,7 @@ func TestRPC_GetTenant(t *testing.T) {
require.NoError(t, err)
assert.NotEmpty(t, tnt)
assert.Equal(t, uint64(1), tnt.ProjectID)
assert.Equal(t, []string{"http://localhost"}, tnt.AllowedOrigins)
assert.Equal(t, validation.Origins{"http://localhost"}, tnt.AllowedOrigins)
})

t.Run("MissingTenant", func(t *testing.T) {
Expand Down Expand Up @@ -115,11 +116,12 @@ func TestRPC_CreateTenant(t *testing.T) {
assert.ErrorContains(t, err, "invalid oidcProviders")
})

t.Run("NoAllowedOrigins", func(t *testing.T) {
tnt, code, err := c.CreateTenant(ctx, 2, "WAAS_ACCESS_TOKEN", validOidcProviders, nil)
t.Run("InvalidOrigin", func(t *testing.T) {
invalidOrigins := []string{"localhost"}
tnt, code, err := c.CreateTenant(ctx, 2, "WAAS_ACCESS_TOKEN", validOidcProviders, invalidOrigins)
assert.Nil(t, tnt)
assert.Empty(t, code)
assert.ErrorContains(t, err, "at least one allowed origin is required")
assert.ErrorContains(t, err, "invalid allowedOrigins")
})

t.Run("Success", func(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion rpc/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
dynamodbtypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/aws/aws-sdk-go-v2/service/kms"
kmstypes "github.com/aws/aws-sdk-go-v2/service/kms/types"
"github.com/goware/validation"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/jwt"
Expand Down Expand Up @@ -461,7 +462,7 @@ func newTenant(t *testing.T, enc *enclave.Enclave, issuer string) (*data.Tenant,
{Issuer: issuer, Audience: []string{"audience"}},
{Issuer: "https://" + strings.TrimPrefix(issuer, "http://"), Audience: []string{"audience"}},
},
AllowedOrigins: []string{"http://localhost"},
AllowedOrigins: validation.Origins{"http://localhost"},
KMSKeys: []string{"SessionKey"},
}

Expand Down
5 changes: 2 additions & 3 deletions rpc/tenant/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -56,8 +55,8 @@ func Middleware(tenants *data.TenantTable, tenantKeys []string) func(http.Handle
}

origin := r.Header.Get("origin")
if origin != "" && len(tntData.AllowedOrigins) > 0 {
if !slices.Contains(tntData.AllowedOrigins, origin) {
if origin != "" {
if !tntData.AllowedOrigins.MatchAny(origin) {
proto.RespondWithError(w, fmt.Errorf("origin not allowed"))
return
}
Expand Down

0 comments on commit 09ce6cd

Please sign in to comment.