Skip to content

Commit d5eddd2

Browse files
Provide API to set the parameters of Challenge Response Config (#18)
* API to set the Parameters of Challenge Response Config * Incorporating review comments * Incorporating review comments
1 parent f4c1756 commit d5eddd2

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

verification/challengeresponse.go

+55
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"log"
1212
"net/http"
13+
"net/url"
1314
"time"
1415

1516
"github.com/veraison/apiclient/common"
@@ -44,6 +45,60 @@ type ChallengeResponseSession struct {
4445
Result json.RawMessage `json:"result"`
4546
}
4647

48+
// SetNonce sets the Nonce supplied by the user
49+
func (cfg *ChallengeResponseConfig) SetNonce(nonce []byte) error {
50+
if len(nonce) == 0 {
51+
return errors.New("no nonce supplied")
52+
}
53+
cfg.Nonce = nonce
54+
return nil
55+
}
56+
57+
// SetNonceSz sets the nonce size supplied by the user
58+
func (cfg *ChallengeResponseConfig) SetNonceSz(nonceSz uint) error {
59+
if nonceSz == 0 {
60+
return errors.New("zero nonce size supplied")
61+
}
62+
cfg.NonceSz = nonceSz
63+
return nil
64+
}
65+
66+
// SetEvidenceBuilder sets the Evidence Builder callback supplied by the user
67+
func (cfg *ChallengeResponseConfig) SetEvidenceBuilder(evidenceBuilder EvidenceBuilder) error {
68+
if evidenceBuilder == nil {
69+
return errors.New("no evidence builder supplied")
70+
}
71+
cfg.EvidenceBuilder = evidenceBuilder
72+
return nil
73+
}
74+
75+
// SetSessionURI sets the New Session URI supplied by the user
76+
func (cfg *ChallengeResponseConfig) SetSessionURI(uri string) error {
77+
u, err := url.Parse(uri)
78+
if err != nil {
79+
return fmt.Errorf("malformed session URI: %w", err)
80+
}
81+
if !u.IsAbs() {
82+
return errors.New("the supplied session URI is not in absolute form")
83+
}
84+
cfg.NewSessionURI = uri
85+
return nil
86+
}
87+
88+
// SetClient sets the HTTP(s) client connection configuration
89+
func (cfg *ChallengeResponseConfig) SetClient(client *common.Client) error {
90+
if client == nil {
91+
return errors.New("no client supplied")
92+
}
93+
cfg.Client = client
94+
return nil
95+
}
96+
97+
// SetDeleteSession sets the DeleteSession parameter using the supplied val
98+
func (cfg *ChallengeResponseConfig) SetDeleteSession(val bool) {
99+
cfg.DeleteSession = val
100+
}
101+
47102
// Run implements the challenge-response protocol FSM invoking the user
48103
// callback. On success, the received Attestation Result is returned.
49104
func (cfg ChallengeResponseConfig) Run() ([]byte, error) {

verification/challengeresponse_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ import (
1515

1616
var (
1717
testNonce []byte = []byte{0xde, 0xad, 0xbe, 0xef}
18+
testNonceSz uint = 32
1819
testEvidence []byte = []byte{0x0e, 0x0d, 0x0e}
1920

2021
testBaseURI = "http://veraison.example"
2122
testRelSessionURI = "/challenge-response/v1/session/1"
2223
testSessionURI = testBaseURI + testRelSessionURI
2324
testNewSessionURI = testBaseURI + "/challenge-response/v1/newSession"
25+
testBadURI = `http://veraison.example:80challenge-response/v1/session/1`
2426
)
2527

2628
type testEvidenceBuilder struct{}
@@ -32,6 +34,79 @@ func (testEvidenceBuilder) BuildEvidence(
3234
return testEvidence, "application/my-evidence-media-type", nil
3335
}
3436

37+
func TestChallengeResponseConfig_SetNonce_ok(t *testing.T) {
38+
cfg := ChallengeResponseConfig{}
39+
err := cfg.SetNonce(testNonce)
40+
assert.NoError(t, err)
41+
}
42+
43+
func TestChallengeResponseConfig_SetNonce_nil_nonce(t *testing.T) {
44+
cfg := ChallengeResponseConfig{}
45+
expectedErr := `no nonce supplied`
46+
var nonce []byte
47+
err := cfg.SetNonce(nonce)
48+
assert.EqualError(t, err, expectedErr)
49+
}
50+
51+
func TestChallengeResponseConfig_SetNonceSz_ok(t *testing.T) {
52+
cfg := ChallengeResponseConfig{}
53+
err := cfg.SetNonceSz(testNonceSz)
54+
assert.NoError(t, err)
55+
}
56+
57+
func TestChallengeResponseConfig_SetNonceSz_zero_noncesz(t *testing.T) {
58+
cfg := ChallengeResponseConfig{}
59+
expectedErr := `zero nonce size supplied`
60+
err := cfg.SetNonceSz(0)
61+
assert.EqualError(t, err, expectedErr)
62+
}
63+
func TestChallengeResponseConfig_SetClient_ok(t *testing.T) {
64+
cfg := ChallengeResponseConfig{}
65+
client := common.NewClient()
66+
err := cfg.SetClient(client)
67+
assert.NoError(t, err)
68+
}
69+
70+
func TestChallengeResponseConfig_SetClient_nil_client(t *testing.T) {
71+
cfg := ChallengeResponseConfig{}
72+
expectedErr := `no client supplied`
73+
err := cfg.SetClient(nil)
74+
assert.EqualError(t, err, expectedErr)
75+
}
76+
77+
func TestChallengeResponseConfig_SetSessionURI_ok(t *testing.T) {
78+
cfg := ChallengeResponseConfig{}
79+
err := cfg.SetSessionURI(testSessionURI)
80+
assert.NoError(t, err)
81+
}
82+
83+
func TestChallengeResponseConfig_SetSessionURI_not_absolute(t *testing.T) {
84+
cfg := ChallengeResponseConfig{}
85+
expectedErr := `the supplied session URI is not in absolute form`
86+
err := cfg.SetSessionURI("veraison.example/challenge-response/v1/session/1")
87+
assert.EqualError(t, err, expectedErr)
88+
}
89+
90+
func TestChallengeResponseConfig_SetSessionURI_bad_uri(t *testing.T) {
91+
cfg := ChallengeResponseConfig{}
92+
expectedErr := `malformed session URI: parse "http://veraison.example:80challenge-response/v1/session/1": invalid port ":80challenge-response" after host`
93+
err := cfg.SetSessionURI(testBadURI)
94+
assert.EqualError(t, err, expectedErr)
95+
}
96+
97+
func TestChallengeResponseConfig_SetEvidenceBuilder_ok(t *testing.T) {
98+
cfg := ChallengeResponseConfig{}
99+
err := cfg.SetEvidenceBuilder(testEvidenceBuilder{})
100+
assert.NoError(t, err)
101+
}
102+
103+
func TestChallengeResponseConfig_SetEvidenceBuilder_no_ok(t *testing.T) {
104+
cfg := ChallengeResponseConfig{}
105+
expectedErr := `no evidence builder supplied`
106+
err := cfg.SetEvidenceBuilder(nil)
107+
assert.EqualError(t, err, expectedErr)
108+
}
109+
35110
func TestChallengeResponseConfig_NewSession_ok(t *testing.T) {
36111
newSessionCreatedBody := `
37112
{

0 commit comments

Comments
 (0)