Skip to content

Commit b382edc

Browse files
feat: parse credential bytes (#258)
This adds methods to parse raw byte slices instead of an io.Reader via protocol.ParseCredentialCreationResponseBytes and protocol.ParseCredentialRequestResponseBytes.
1 parent f5fa279 commit b382edc

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

protocol/assertion.go

+12
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ func ParseCredentialRequestResponseBody(body io.Reader) (par *ParsedCredentialAs
7373
return car.Parse()
7474
}
7575

76+
// ParseCredentialRequestResponseBytes is an alternative version of ParseCredentialRequestResponseBody that just takes
77+
// a byte slice.
78+
func ParseCredentialRequestResponseBytes(data []byte) (par *ParsedCredentialAssertionData, err error) {
79+
var car CredentialAssertionResponse
80+
81+
if err = decodeBytes(data, &car); err != nil {
82+
return nil, ErrBadRequest.WithDetails("Parse error for Assertion").WithInfo(err.Error())
83+
}
84+
85+
return car.Parse()
86+
}
87+
7688
// Parse validates and parses the CredentialAssertionResponse into a ParseCredentialCreationResponseBody. This receiver
7789
// is unlikely to be expressly guaranteed under the versioning policy. Users looking for this guarantee should see
7890
// ParseCredentialRequestResponseBody instead, and this receiver should only be used if that function is inadequate

protocol/credential.go

+12
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ func ParseCredentialCreationResponseBody(body io.Reader) (pcc *ParsedCredentialC
8585
return ccr.Parse()
8686
}
8787

88+
// ParseCredentialCreationResponseBytes is an alternative version of ParseCredentialCreationResponseBody that just takes
89+
// a byte slice.
90+
func ParseCredentialCreationResponseBytes(data []byte) (pcc *ParsedCredentialCreationData, err error) {
91+
var ccr CredentialCreationResponse
92+
93+
if err = decodeBytes(data, &ccr); err != nil {
94+
return nil, ErrBadRequest.WithDetails("Parse error for Registration").WithInfo(err.Error())
95+
}
96+
97+
return ccr.Parse()
98+
}
99+
88100
// Parse validates and parses the CredentialCreationResponse into a ParsedCredentialCreationData. This receiver
89101
// is unlikely to be expressly guaranteed under the versioning policy. Users looking for this guarantee should see
90102
// ParseCredentialCreationResponseBody instead, and this receiver should only be used if that function is inadequate

protocol/decoder.go

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package protocol
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"errors"
67
"io"
@@ -21,3 +22,19 @@ func decodeBody(body io.Reader, v any) (err error) {
2122

2223
return nil
2324
}
25+
26+
func decodeBytes(data []byte, v any) (err error) {
27+
decoder := json.NewDecoder(bytes.NewReader(data))
28+
29+
if err = decoder.Decode(v); err != nil {
30+
return err
31+
}
32+
33+
_, err = decoder.Token()
34+
35+
if !errors.Is(err, io.EOF) {
36+
return errors.New("The body contains trailing data")
37+
}
38+
39+
return nil
40+
}

0 commit comments

Comments
 (0)