Skip to content

Commit b8630ae

Browse files
docs: add function alternate use hints (#446)
This adds documentation on alternative use cases to help uses find the appropriate functions for their intended purpose.
1 parent 9956038 commit b8630ae

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

webauthn/login.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ func (webauthn *WebAuthn) FinishLogin(user User, session SessionData, response *
209209
// FinishDiscoverableLogin takes the response from the client and validate it against the handler and stored session data.
210210
// The handler helps to find out which user must be used to validate the response. This is a function defined in your
211211
// business code that will retrieve the user from your persistent data.
212+
//
213+
// As with all Finish functions this function requires a *http.Request but you can perform the same steps with the
214+
// protocol.ParseCredentialRequestResponseBody or protocol.ParseCredentialRequestResponseBytes which require an
215+
// io.Reader or byte array respectively, you can also use an arbitrary *protocol.ParsedCredentialAssertionData which is
216+
// returned from all of these functions i.e. by implementing a custom parser. The DiscoverableUserHandler, *SessionData,
217+
// and *protocol.ParsedCredentialAssertionData can then be used with the ValidatePasskeyLogin function.
212218
func (webauthn *WebAuthn) FinishDiscoverableLogin(handler DiscoverableUserHandler, session SessionData, response *http.Request) (credential *Credential, err error) {
213219
var parsedResponse *protocol.ParsedCredentialAssertionData
214220

@@ -222,6 +228,12 @@ func (webauthn *WebAuthn) FinishDiscoverableLogin(handler DiscoverableUserHandle
222228
// FinishPasskeyLogin takes the response from the client and validate it against the handler and stored session data.
223229
// The handler helps to find out which user must be used to validate the response. This is a function defined in your
224230
// business code that will retrieve the user from your persistent data.
231+
//
232+
// As with all Finish functions this function requires a *http.Request but you can perform the same steps with the
233+
// protocol.ParseCredentialRequestResponseBody or protocol.ParseCredentialRequestResponseBytes which require an
234+
// io.Reader or byte array respectively, you can also use an arbitrary *protocol.ParsedCredentialAssertionData which is
235+
// returned from all of these functions i.e. by implementing a custom parser. The DiscoverableUserHandler, *SessionData,
236+
// and *protocol.ParsedCredentialAssertionData can then be used with the ValidatePasskeyLogin function.
225237
func (webauthn *WebAuthn) FinishPasskeyLogin(handler DiscoverableUserHandler, session SessionData, response *http.Request) (user User, credential *Credential, err error) {
226238
var parsedResponse *protocol.ParsedCredentialAssertionData
227239

@@ -233,6 +245,9 @@ func (webauthn *WebAuthn) FinishPasskeyLogin(handler DiscoverableUserHandler, se
233245
}
234246

235247
// ValidateLogin takes a parsed response and validates it against the user credentials and session data.
248+
//
249+
// If you wish to skip performing the step required to parse the *protocol.ParsedCredentialAssertionData and
250+
// you're using net/http then you can use FinishLogin instead.
236251
func (webauthn *WebAuthn) ValidateLogin(user User, session SessionData, parsedResponse *protocol.ParsedCredentialAssertionData) (credential *Credential, err error) {
237252
if !bytes.Equal(user.WebAuthnID(), session.UserID) {
238253
return nil, protocol.ErrBadRequest.WithDetails("ID mismatch for User and Session")
@@ -247,6 +262,9 @@ func (webauthn *WebAuthn) ValidateLogin(user User, session SessionData, parsedRe
247262

248263
// ValidateDiscoverableLogin is an overloaded version of ValidateLogin that allows for discoverable credentials.
249264
//
265+
// If you wish to skip performing the step required to parse the *protocol.ParsedCredentialAssertionData and
266+
// you're using net/http then you can use FinishDiscoverableLogin instead.
267+
//
250268
// Note: this is just a backwards compatibility layer over ValidatePasskeyLogin which returns more information.
251269
func (webauthn *WebAuthn) ValidateDiscoverableLogin(handler DiscoverableUserHandler, session SessionData, parsedResponse *protocol.ParsedCredentialAssertionData) (credential *Credential, err error) {
252270
_, credential, err = webauthn.ValidatePasskeyLogin(handler, session, parsedResponse)
@@ -255,6 +273,9 @@ func (webauthn *WebAuthn) ValidateDiscoverableLogin(handler DiscoverableUserHand
255273
}
256274

257275
// ValidatePasskeyLogin is an overloaded version of ValidateLogin that allows for passkey credentials.
276+
//
277+
// If you wish to skip performing the step required to parse the *protocol.ParsedCredentialAssertionData and
278+
// you're using net/http then you can use FinishPasskeyLogin instead.
258279
func (webauthn *WebAuthn) ValidatePasskeyLogin(handler DiscoverableUserHandler, session SessionData, parsedResponse *protocol.ParsedCredentialAssertionData) (user User, credential *Credential, err error) {
259280
if len(session.UserID) != 0 {
260281
return nil, nil, protocol.ErrBadRequest.WithDetails("Session was not initiated as a client-side discoverable login")
@@ -275,7 +296,7 @@ func (webauthn *WebAuthn) ValidatePasskeyLogin(handler DiscoverableUserHandler,
275296
return user, credential, nil
276297
}
277298

278-
// ValidateLogin takes a parsed response and validates it against the user credentials and session data.
299+
// validateLogin takes a parsed response and validates it against the user credentials and session data.
279300
func (webauthn *WebAuthn) validateLogin(user User, session SessionData, parsedResponse *protocol.ParsedCredentialAssertionData) (*Credential, error) {
280301
// Step 1. If the allowCredentials option was given when this authentication ceremony was initiated,
281302
// verify that credential.id identifies one of the public key credentials that were listed in

webauthn/registration.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,13 @@ func WithRegistrationRelyingPartyName(name string) RegistrationOption {
212212

213213
// FinishRegistration takes the response from the authenticator and client and verify the credential against the user's
214214
// credentials and session data.
215-
func (webauthn *WebAuthn) FinishRegistration(user User, session SessionData, request *http.Request) (*Credential, error) {
215+
//
216+
// As with all Finish functions this function requires a *http.Request but you can perform the same steps with the
217+
// protocol.ParseCredentialCreationResponseBody or protocol.ParseCredentialCreationResponseBytes which require an
218+
// io.Reader or byte array respectively, you can also use an arbitrary *protocol.ParsedCredentialCreationData which is
219+
// returned from all of these functions i.e. by implementing a custom parser. The User, *SessionData, and
220+
// *protocol.ParsedCredentialCreationData can then be used with the CreateCredential function.
221+
func (webauthn *WebAuthn) FinishRegistration(user User, session SessionData, request *http.Request) (credential *Credential, err error) {
216222
parsedResponse, err := protocol.ParseCredentialCreationResponse(request)
217223
if err != nil {
218224
return nil, err
@@ -222,6 +228,9 @@ func (webauthn *WebAuthn) FinishRegistration(user User, session SessionData, req
222228
}
223229

224230
// CreateCredential verifies a parsed response against the user's credentials and session data.
231+
//
232+
// If you wish to skip performing the step required to parse the *protocol.ParsedCredentialCreationData and
233+
// you're using net/http then you can use FinishRegistration instead.
225234
func (webauthn *WebAuthn) CreateCredential(user User, session SessionData, parsedResponse *protocol.ParsedCredentialCreationData) (credential *Credential, err error) {
226235
if !bytes.Equal(user.WebAuthnID(), session.UserID) {
227236
return nil, protocol.ErrBadRequest.WithDetails("ID mismatch for User and Session")

0 commit comments

Comments
 (0)