Просмотр исходного кода

Use a struct for the Wire DPoP token

Herman Slatman 2 лет назад
Родитель
Сommit
231f03ae28
5 измененных файлов с 79 добавлено и 48 удалено
  1. 1 1
      acme/api/wire_integration_test.go
  2. 23 23
      acme/challenge.go
  3. 0 0
      acme/challenge_test.go
  4. 7 7
      acme/db.go
  5. 48 17
      acme/db/nosql/wire.go

+ 1 - 1
acme/api/wire_integration_test.go

@@ -370,7 +370,7 @@ MCowBQYDK2VwAyEA5c+4NKZSNQcR1T8qN6SjwgdPZQ0Ge12Ylx/YeGAJ35k=
 		require.NoError(t, err)
 
 		// TODO(hs): move these to a more appropriate place and/or provide more realistic value
-		err = db.CreateDpopToken(ctx, order.ID, map[string]any{"fake-dpop": "dpop-value"})
+		err = db.CreateDpopToken(ctx, order.ID, &acme.WireDpopToken{Challenge: "challenge", Handle: "handle", Team: "wire"})
 		require.NoError(t, err)
 		err = db.CreateOidcToken(ctx, order.ID, map[string]any{"fake-oidc": "oidc-value"})
 		require.NoError(t, err)

+ 23 - 23
acme/challenge.go

@@ -517,7 +517,7 @@ func wireDPOP01Validate(ctx context.Context, ch *Challenge, db DB, accountJWK *j
 	}
 
 	order := orders[len(orders)-1]
-	if err := db.CreateDpopToken(ctx, order, map[string]any(*dpop)); err != nil {
+	if err := db.CreateDpopToken(ctx, order, dpop); err != nil {
 		return WrapErrorISE(err, "failed storing DPoP token")
 	}
 
@@ -525,20 +525,28 @@ func wireDPOP01Validate(ctx context.Context, ch *Challenge, db DB, accountJWK *j
 }
 
 type wireCnf struct {
-	Kid string `json:"kid"`
+	Kid string `json:"kid,omitempty"`
 }
 
 type wireAccessToken struct {
 	jose.Claims
-	Challenge  string  `json:"chal"`
-	Cnf        wireCnf `json:"cnf"`
-	Proof      string  `json:"proof"`
-	ClientID   string  `json:"client_id"`
-	APIVersion int     `json:"api_version"`
-	Scope      string  `json:"scope"`
+	Challenge  string  `json:"chal,omitempty"`
+	Cnf        wireCnf `json:"cnf,omitempty"`
+	Proof      string  `json:"proof,omitempty"`
+	ClientID   string  `json:"client_id,omitempty"`
+	APIVersion int     `json:"api_version,omitempty"`
+	Scope      string  `json:"scope,omitempty"`
 }
 
-type wireDpopToken map[string]any
+type WireDpopToken struct {
+	jose.Claims
+	Nonce     string `json:"nonce,omitempty"`
+	Method    string `json:"htm,omitempty"`
+	URL       string `json:"htu,omitempty"`
+	Challenge string `json:"chal,omitempty"`
+	Handle    string `json:"handle,omitempty"`
+	Team      string `json:"team,omitempty"`
+}
 
 type wireVerifyParams struct {
 	token     string
@@ -551,7 +559,7 @@ type wireVerifyParams struct {
 	t         time.Time
 }
 
-func parseAndVerifyWireAccessToken(v wireVerifyParams) (*wireAccessToken, *wireDpopToken, error) {
+func parseAndVerifyWireAccessToken(v wireVerifyParams) (*wireAccessToken, *WireDpopToken, error) {
 	jwt, err := jose.ParseSigned(v.token)
 	if err != nil {
 		return nil, nil, fmt.Errorf("failed parsing token: %w", err)
@@ -583,25 +591,17 @@ func parseAndVerifyWireAccessToken(v wireVerifyParams) (*wireAccessToken, *wireD
 	if err != nil {
 		return nil, nil, fmt.Errorf("invalid Wire DPoP token: %w", err)
 	}
-	var dpopToken wireDpopToken
+	var dpopToken WireDpopToken
 	if err := dpopJWT.Claims(v.dpopKey, &dpopToken); err != nil {
 		return nil, nil, fmt.Errorf("failed validating Wire DPoP token claims: %w", err)
 	}
 
-	challenge, ok := dpopToken["chal"].(string)
-	if !ok {
-		return nil, nil, fmt.Errorf("invalid challenge in Wire DPoP token")
-	}
-	if challenge != v.chToken {
-		return nil, nil, fmt.Errorf("invalid Wire DPoP challenge %q", challenge)
+	if dpopToken.Challenge != v.chToken {
+		return nil, nil, fmt.Errorf("invalid Wire DPoP challenge %q", dpopToken.Challenge)
 	}
 
-	handle, ok := dpopToken["handle"].(string)
-	if !ok {
-		return nil, nil, fmt.Errorf("invalid handle in Wire DPoP token")
-	}
-	if handle != v.wireID.Handle {
-		return nil, nil, fmt.Errorf("invalid Wire client handle %q", handle)
+	if dpopToken.Handle != v.wireID.Handle {
+		return nil, nil, fmt.Errorf("invalid Wire client handle %q", dpopToken.Handle)
 	}
 
 	return &accessToken, &dpopToken, nil

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
acme/challenge_test.go


+ 7 - 7
acme/db.go

@@ -56,8 +56,8 @@ type DB interface {
 
 	// TODO(hs): put in a different interface
 	GetAllOrdersByAccountID(ctx context.Context, accountID string) ([]string, error)
-	CreateDpopToken(ctx context.Context, orderID string, dpop map[string]interface{}) error
-	GetDpopToken(ctx context.Context, orderID string) (map[string]interface{}, error)
+	CreateDpopToken(ctx context.Context, orderID string, dpop *WireDpopToken) error
+	GetDpopToken(ctx context.Context, orderID string) (*WireDpopToken, error)
 	CreateOidcToken(ctx context.Context, orderID string, idToken map[string]interface{}) error
 	GetOidcToken(ctx context.Context, orderID string) (map[string]interface{}, error)
 }
@@ -126,8 +126,8 @@ type MockDB struct {
 	MockUpdateOrder          func(ctx context.Context, o *Order) error
 
 	MockGetAllOrdersByAccountID func(ctx context.Context, accountID string) ([]string, error)
-	MockGetDpopToken            func(ctx context.Context, orderID string) (map[string]interface{}, error)
-	MockCreateDpopToken         func(ctx context.Context, orderID string, dpop map[string]interface{}) error
+	MockGetDpopToken            func(ctx context.Context, orderID string) (*WireDpopToken, error)
+	MockCreateDpopToken         func(ctx context.Context, orderID string, dpop *WireDpopToken) error
 	MockGetOidcToken            func(ctx context.Context, orderID string) (map[string]interface{}, error)
 	MockCreateOidcToken         func(ctx context.Context, orderID string, idToken map[string]interface{}) error
 
@@ -416,17 +416,17 @@ func (m *MockDB) GetAllOrdersByAccountID(ctx context.Context, accountID string)
 }
 
 // GetDpop retrieves a DPoP from the database.
-func (m *MockDB) GetDpopToken(ctx context.Context, orderID string) (map[string]any, error) {
+func (m *MockDB) GetDpopToken(ctx context.Context, orderID string) (*WireDpopToken, error) {
 	if m.MockGetDpopToken != nil {
 		return m.MockGetDpopToken(ctx, orderID)
 	} else if m.MockError != nil {
 		return nil, m.MockError
 	}
-	return m.MockRet1.(map[string]any), m.MockError
+	return m.MockRet1.(*WireDpopToken), m.MockError
 }
 
 // CreateDpop creates DPoP resources and saves them to the DB.
-func (m *MockDB) CreateDpopToken(ctx context.Context, orderID string, dpop map[string]any) error {
+func (m *MockDB) CreateDpopToken(ctx context.Context, orderID string, dpop *WireDpopToken) error {
 	if m.MockCreateDpopToken != nil {
 		return m.MockCreateDpopToken(ctx, orderID, dpop)
 	}

+ 48 - 17
acme/db/nosql/wire.go

@@ -9,12 +9,24 @@ import (
 	"github.com/pkg/errors"
 	"github.com/smallstep/certificates/acme"
 	"github.com/smallstep/nosql"
+	"go.step.sm/crypto/jose"
 )
 
 type dbDpopToken struct {
-	ID        string    `json:"id"`
-	Content   []byte    `json:"content"`
-	CreatedAt time.Time `json:"createdAt"`
+	ID        string            `json:"id"` // jti
+	Issuer    string            `json:"iss"`
+	Subject   string            `json:"sub"`
+	Audience  jose.Audience     `json:"aud,omitempty"`
+	Expiry    *jose.NumericDate `json:"exp,omitempty"`
+	NotBefore *jose.NumericDate `json:"nbf,omitempty"`
+	IssuedAt  *jose.NumericDate `json:"iat,omitempty"`
+	Nonce     string            `json:"nonce"`
+	Method    string            `json:"htm"`
+	URL       string            `json:"htu"`
+	Challenge string            `json:"chal"`
+	Handle    string            `json:"handle"`
+	Team      string            `json:"team"`
+	CreatedAt time.Time         `json:"createdAt"`
 }
 
 // getDBDpopToken retrieves and unmarshals an DPoP type from the database.
@@ -33,30 +45,49 @@ func (db *DB) getDBDpopToken(_ context.Context, orderID string) (*dbDpopToken, e
 	return d, nil
 }
 
-// GetDpopToken retrieves an DPoP from the database.
-func (db *DB) GetDpopToken(ctx context.Context, orderID string) (map[string]any, error) {
+// GetDpopToken retrieves a DPoP from the database.
+func (db *DB) GetDpopToken(ctx context.Context, orderID string) (*acme.WireDpopToken, error) {
 	dbDpop, err := db.getDBDpopToken(ctx, orderID)
 	if err != nil {
 		return nil, err
 	}
 
-	dpop := make(map[string]any)
-	err = json.Unmarshal(dbDpop.Content, &dpop)
-
-	return dpop, err
+	return &acme.WireDpopToken{
+		Claims: jose.Claims{
+			ID:        dbDpop.ID,
+			Issuer:    dbDpop.Issuer,
+			Subject:   dbDpop.Subject,
+			Audience:  dbDpop.Audience,
+			Expiry:    dbDpop.Expiry,
+			NotBefore: dbDpop.NotBefore,
+			IssuedAt:  dbDpop.IssuedAt,
+		},
+		Nonce:     dbDpop.Nonce,
+		Method:    dbDpop.Method,
+		URL:       dbDpop.URL,
+		Challenge: dbDpop.Challenge,
+		Handle:    dbDpop.Handle,
+		Team:      dbDpop.Team,
+	}, nil
 }
 
 // CreateDpopToken creates DPoP resources and saves them to the DB.
-func (db *DB) CreateDpopToken(ctx context.Context, orderID string, dpop map[string]any) error {
-	content, err := json.Marshal(dpop)
-	if err != nil {
-		return err
-	}
-
+func (db *DB) CreateDpopToken(ctx context.Context, orderID string, dpop *acme.WireDpopToken) error {
 	now := clock.Now()
 	dbDpop := &dbDpopToken{
-		ID:        orderID,
-		Content:   content,
+		ID:        dpop.ID,
+		Issuer:    dpop.Issuer,
+		Subject:   dpop.Subject,
+		Audience:  dpop.Audience,
+		Expiry:    dpop.Expiry,
+		NotBefore: dpop.NotBefore,
+		IssuedAt:  dpop.IssuedAt,
+		Nonce:     dpop.Nonce,
+		Method:    dpop.Method,
+		URL:       dpop.URL,
+		Challenge: dpop.Challenge,
+		Handle:    dpop.Handle,
+		Team:      dpop.Team,
 		CreatedAt: now,
 	}
 	if err := db.save(ctx, orderID, dbDpop, nil, "dpop", wireDpopTokenTable); err != nil {

Некоторые файлы не были показаны из-за большого количества измененных файлов