Skip to content

Commit

Permalink
bugfix: customer challenge take effect
Browse files Browse the repository at this point in the history
  • Loading branch information
fanhousanbu committed Sep 1, 2024
1 parent de6e27a commit b459556
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion webauthn/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (webauthn *WebAuthn) beginLogin(userID []byte, allowedCredentials []protoco
}

session = &SessionData{
Challenge: challenge.String(),
Challenge: assertion.Response.Challenge.String(),
RelyingPartyID: assertion.Response.RelyingPartyID,
UserID: userID,
AllowedCredentialIDs: assertion.Response.GetAllowedCredentialIDs(),
Expand Down
67 changes: 67 additions & 0 deletions webauthn/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,70 @@ func TestWithLoginRelyingPartyID(t *testing.T) {
})
}
}

func TestCustomerChallenge(t *testing.T) {
customerChallenge := "hello world"
nonCustomerChallenge := make(protocol.URLEncodedBase64, 0)
testCases := []struct {
name string
have *Config
opts []LoginOption
expectedChallenge func() protocol.URLEncodedBase64
err string
}{
{
name: "NonCustomerChallenge",
have: &Config{
RPID: "https://example.com",
RPDisplayName: "Test Non-Customer Challenge",
RPOrigins: []string{"https://example.com"},
},
opts: []LoginOption{
func(opt *protocol.PublicKeyCredentialRequestOptions) {
nonCustomerChallenge = opt.Challenge
},
},
expectedChallenge: func() protocol.URLEncodedBase64 {
return nonCustomerChallenge
},
},
{
name: "CustomerChallenge",
have: &Config{
RPID: "https://example.com",
RPDisplayName: "Test Customer Challenge",
RPOrigins: []string{"https://example.com"},
},
opts: []LoginOption{
func(opt *protocol.PublicKeyCredentialRequestOptions) {
opt.Challenge = protocol.URLEncodedBase64(customerChallenge)
},
},
expectedChallenge: func() protocol.URLEncodedBase64 {
return []byte(customerChallenge)
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
w, err := New(tc.have)
assert.NoError(t, err)

user := &defaultUser{
credentials: []Credential{
{},
},
}

creation, _, err := w.BeginLogin(user, tc.opts...)
if tc.err != "" {
assert.EqualError(t, err, tc.err)
} else {
assert.NoError(t, err)
require.NotNil(t, creation)
assert.Equal(t, tc.expectedChallenge(), creation.Response.Challenge)
}
})
}
}

0 comments on commit b459556

Please sign in to comment.