Skip to content

Commit

Permalink
bearer: Provide method to decode signed fields
Browse files Browse the repository at this point in the history
It is in demand by apps exchanging unsigned tokens.

Signed-off-by: Leonard Lyubich <leonard@morphbits.io>
  • Loading branch information
cthulhu-rider committed Mar 20, 2024
1 parent 6f65512 commit 3efc5b3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
15 changes: 14 additions & 1 deletion bearer/bearer.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,24 @@ func (b *Token) Sign(signer neofscrypto.Signer) error {

// SignedData returns actual payload to sign.
//
// See also [Token.Sign].
// See also [Token.Sign], [Token.UnmarshalSignedData].
func (b *Token) SignedData() []byte {
return b.signedData()
}

// UnmarshalSignedData is a reverse op to [Token.SignedData].
func (b *Token) UnmarshalSignedData(data []byte) error {
var body acl.BearerTokenBody
err := body.Unmarshal(data)
if err != nil {
return fmt.Errorf("decode body: %w", err)

Check warning on line 283 in bearer/bearer.go

View check run for this annotation

Codecov / codecov/patch

bearer/bearer.go#L283

Added line #L283 was not covered by tests
}

var tok acl.BearerToken
tok.SetBody(&body)
return b.readFromV2(tok, false)
}

// VerifySignature checks if Token signature is presented and valid.
//
// Zero Token fails the check.
Expand Down
5 changes: 5 additions & 0 deletions bearer/bearer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ func TestToken_SignedData(t *testing.T) {

require.False(t, val.VerifySignature())

signedData := val.SignedData()
var dec bearer.Token
require.NoError(t, dec.UnmarshalSignedData(signedData))
require.Equal(t, val, dec)

signer := test.RandomSignerRFC6979(t)
val = bearertest.Token(t)

Expand Down

0 comments on commit 3efc5b3

Please sign in to comment.