Skip to content

Commit

Permalink
feat: parse credential bytes (#258)
Browse files Browse the repository at this point in the history
This adds methods to parse raw byte slices instead of an io.Reader via protocol.ParseCredentialCreationResponseBytes and protocol.ParseCredentialRequestResponseBytes.
  • Loading branch information
james-d-elliott authored Jul 25, 2024
1 parent f5fa279 commit b382edc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
12 changes: 12 additions & 0 deletions protocol/assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ func ParseCredentialRequestResponseBody(body io.Reader) (par *ParsedCredentialAs
return car.Parse()
}

// ParseCredentialRequestResponseBytes is an alternative version of ParseCredentialRequestResponseBody that just takes
// a byte slice.
func ParseCredentialRequestResponseBytes(data []byte) (par *ParsedCredentialAssertionData, err error) {
var car CredentialAssertionResponse

if err = decodeBytes(data, &car); err != nil {
return nil, ErrBadRequest.WithDetails("Parse error for Assertion").WithInfo(err.Error())
}

return car.Parse()
}

// Parse validates and parses the CredentialAssertionResponse into a ParseCredentialCreationResponseBody. This receiver
// is unlikely to be expressly guaranteed under the versioning policy. Users looking for this guarantee should see
// ParseCredentialRequestResponseBody instead, and this receiver should only be used if that function is inadequate
Expand Down
12 changes: 12 additions & 0 deletions protocol/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ func ParseCredentialCreationResponseBody(body io.Reader) (pcc *ParsedCredentialC
return ccr.Parse()
}

// ParseCredentialCreationResponseBytes is an alternative version of ParseCredentialCreationResponseBody that just takes
// a byte slice.
func ParseCredentialCreationResponseBytes(data []byte) (pcc *ParsedCredentialCreationData, err error) {
var ccr CredentialCreationResponse

if err = decodeBytes(data, &ccr); err != nil {
return nil, ErrBadRequest.WithDetails("Parse error for Registration").WithInfo(err.Error())
}

return ccr.Parse()
}

// Parse validates and parses the CredentialCreationResponse into a ParsedCredentialCreationData. This receiver
// is unlikely to be expressly guaranteed under the versioning policy. Users looking for this guarantee should see
// ParseCredentialCreationResponseBody instead, and this receiver should only be used if that function is inadequate
Expand Down
17 changes: 17 additions & 0 deletions protocol/decoder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package protocol

import (
"bytes"
"encoding/json"
"errors"
"io"
Expand All @@ -21,3 +22,19 @@ func decodeBody(body io.Reader, v any) (err error) {

return nil
}

func decodeBytes(data []byte, v any) (err error) {
decoder := json.NewDecoder(bytes.NewReader(data))

if err = decoder.Decode(v); err != nil {
return err
}

_, err = decoder.Token()

if !errors.Is(err, io.EOF) {
return errors.New("The body contains trailing data")
}

return nil
}

0 comments on commit b382edc

Please sign in to comment.