Merge pull request #93 from nspcc-dev/add-x-headers-to-request-data

Add X-headers to request data
This commit is contained in:
Leonard Lyubich 2020-06-18 15:53:32 +03:00 committed by GitHub
commit 42956686f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 122 additions and 1 deletions

View file

@ -4,6 +4,10 @@ import (
"io" "io"
) )
type extHdrWrapper struct {
msg *RequestExtendedHeader_KV
}
type extHdrSrcWrapper struct { type extHdrSrcWrapper struct {
extHdrSrc ExtendedHeadersSource extHdrSrc ExtendedHeadersSource
} }
@ -84,3 +88,44 @@ func (m *RequestExtendedHeader_KV) SetV(v string) {
func (m *RequestExtendedHeader) SetHeaders(v []RequestExtendedHeader_KV) { func (m *RequestExtendedHeader) SetHeaders(v []RequestExtendedHeader_KV) {
m.Headers = v m.Headers = v
} }
func wrapExtendedHeaderKV(msg *RequestExtendedHeader_KV) extHdrWrapper {
return extHdrWrapper{
msg: msg,
}
}
// Key returns the result of K field getter.
//
// If message is nil, empty string returns.
func (m extHdrWrapper) Key() string {
if m.msg != nil {
return m.msg.GetK()
}
return ""
}
// Value returns the result of V field getter.
//
// If message is nil, empty string returns.
func (m extHdrWrapper) Value() string {
if m.msg != nil {
return m.msg.GetV()
}
return ""
}
// ExtendedHeaders composes ExtendedHeader list from the Headers field getter result.
func (m RequestExtendedHeader) ExtendedHeaders() []ExtendedHeader {
hs := m.GetHeaders()
res := make([]ExtendedHeader, 0, len(hs))
for i := range hs {
res = append(res, wrapExtendedHeaderKV(&hs[i]))
}
return res
}

View file

@ -51,3 +51,49 @@ func TestRequestExtendedHeader_SetHeaders(t *testing.T) {
require.Equal(t, hdrs, s.GetHeaders()) require.Equal(t, hdrs, s.GetHeaders())
} }
func TestExtHdrWrapper(t *testing.T) {
s := wrapExtendedHeaderKV(nil)
require.Empty(t, s.Key())
require.Empty(t, s.Value())
msg := new(RequestExtendedHeader_KV)
s = wrapExtendedHeaderKV(msg)
key := "key"
msg.SetK(key)
require.Equal(t, key, s.Key())
val := "val"
msg.SetV(val)
require.Equal(t, val, s.Value())
}
func TestRequestExtendedHeader_ExtendedHeaders(t *testing.T) {
var (
k1, v1 = "key1", "value1"
k2, v2 = "key2", "value2"
h1 = new(RequestExtendedHeader_KV)
h2 = new(RequestExtendedHeader_KV)
)
h1.SetK(k1)
h1.SetV(v1)
h2.SetK(k2)
h2.SetV(v2)
s := new(RequestExtendedHeader)
s.SetHeaders([]RequestExtendedHeader_KV{
*h1, *h2,
})
xHdrs := s.ExtendedHeaders()
require.Len(t, xHdrs, 2)
require.Equal(t, k1, xHdrs[0].Key())
require.Equal(t, v1, xHdrs[0].Value())
require.Equal(t, k2, xHdrs[1].Key())
require.Equal(t, v2, xHdrs[1].Value())
}

View file

@ -212,6 +212,7 @@ func SignRequestData(key *ecdsa.PrivateKey, src RequestSignedData) error {
NewSignedBearerToken( NewSignedBearerToken(
src.GetBearerToken(), src.GetBearerToken(),
), ),
ExtendedHeadersSignedData(src),
) )
if err != nil { if err != nil {
return err return err
@ -237,6 +238,7 @@ func VerifyRequestData(src RequestVerifyData) error {
NewVerifiedBearerToken( NewVerifiedBearerToken(
src.GetBearerToken(), src.GetBearerToken(),
), ),
ExtendedHeadersSignedData(src),
) )
if err != nil { if err != nil {
return err return err

View file

@ -20,6 +20,8 @@ type testSignedDataSrc struct {
token SessionToken token SessionToken
bearer BearerToken bearer BearerToken
extHdrs []ExtendedHeader
} }
type testSignedDataReader struct { type testSignedDataReader struct {
@ -60,6 +62,10 @@ func (s testSignedDataSrc) GetBearerToken() BearerToken {
return s.bearer return s.bearer
} }
func (s testSignedDataSrc) ExtendedHeaders() []ExtendedHeader {
return s.extHdrs
}
func (s testSignedDataReader) SignedDataSize() int { func (s testSignedDataReader) SignedDataSize() int {
return len(s.data) return len(s.data)
} }
@ -262,7 +268,7 @@ func TestVerifySignatureWithKey(t *testing.T) {
require.Error(t, VerifySignatureWithKey(&sk.PublicKey, src)) require.Error(t, VerifySignatureWithKey(&sk.PublicKey, src))
} }
func TestSignVerifyDataWithSessionToken(t *testing.T) { func TestSignVerifyRequestData(t *testing.T) {
// sign with empty RequestSignedData // sign with empty RequestSignedData
require.EqualError(t, require.EqualError(t,
SignRequestData(nil, nil), SignRequestData(nil, nil),
@ -282,18 +288,27 @@ func TestSignVerifyDataWithSessionToken(t *testing.T) {
bearer = wrapBearerTokenMsg(new(BearerTokenMsg)) bearer = wrapBearerTokenMsg(new(BearerTokenMsg))
bearerEpoch = uint64(8) bearerEpoch = uint64(8)
extHdrKey = "key"
extHdr = new(RequestExtendedHeader_KV)
) )
token.SetVerb(initVerb) token.SetVerb(initVerb)
bearer.SetExpirationEpoch(bearerEpoch) bearer.SetExpirationEpoch(bearerEpoch)
extHdr.SetK(extHdrKey)
// create test data with token // create test data with token
src := &testSignedDataSrc{ src := &testSignedDataSrc{
data: testData(t, 10), data: testData(t, 10),
token: token, token: token,
bearer: bearer, bearer: bearer,
extHdrs: []ExtendedHeader{
wrapExtendedHeaderKV(extHdr),
},
} }
// create test private key // create test private key
@ -338,6 +353,18 @@ func TestSignVerifyDataWithSessionToken(t *testing.T) {
// ascertain that verification is passed // ascertain that verification is passed
require.NoError(t, VerifyRequestData(src)) require.NoError(t, VerifyRequestData(src))
// break the extended header
extHdr.SetK(extHdrKey + "1")
// ascertain that verification is failed
require.Error(t, VerifyRequestData(src))
// restore the extended header
extHdr.SetK(extHdrKey)
// ascertain that verification is passed
require.NoError(t, VerifyRequestData(src))
// wrap to data reader // wrap to data reader
rdr := &testSignedDataReader{ rdr := &testSignedDataReader{
testSignedDataSrc: src, testSignedDataSrc: src,

View file

@ -255,6 +255,7 @@ type RequestData interface {
SignedDataSource SignedDataSource
SessionTokenSource SessionTokenSource
BearerTokenSource BearerTokenSource
ExtendedHeadersSource
} }
// RequestSignedData is an interface of request information with signature write access. // RequestSignedData is an interface of request information with signature write access.