Skip to content

Commit

Permalink
docs: readme improvements (#157)
Browse files Browse the repository at this point in the history
This changes the variable names of the examples slightly to use the commonly
used w for the ResponseWriter and removes the clash with the WebAuthn instance. The examples can be simplified to use the convenience functions offered by the
library.

Signed-off-by: Robin Brandt <[email protected]>
  • Loading branch information
robinbrandt authored Oct 6, 2023
1 parent 270f3fb commit feda47a
Showing 1 changed file with 13 additions and 27 deletions.
40 changes: 13 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ supporting the storage and retrieval of the credential and authenticator structs
The following examples show some basic use cases of the library. For consistency sake the following variables are used
to denote specific things:

- Variable `w`: the `webauthn.WebAuthn` instance you initialize elsewhere in your code
- Variable `webAuthn`: the `webauthn.WebAuthn` instance you initialize elsewhere in your code
- Variable `datastore`: the pseudocode backend service that stores your webauthn session data and users such as PostgreSQL
- Variable `session`: the webauthn.SessionData object
- Variable `user`: your webauthn.User implementation
Expand All @@ -75,7 +75,7 @@ import (
)

var (
w *webauthn.WebAuthn
webAuthn *webauthn.WebAuthn
err error
)

Expand All @@ -87,7 +87,7 @@ func main() {
RPOrigins: []string{"https://login.go-webauthn.local"}, // The origin URLs allowed for WebAuthn requests
}

if w, err = webauthn.New(wconfig); err != nil {
if webAuthn, err = webauthn.New(wconfig); err != nil {
fmt.Println(err)
}
}
Expand All @@ -100,39 +100,32 @@ package example

func BeginRegistration(w http.ResponseWriter, r *http.Request) {
user := datastore.GetUser() // Find or create the new user
options, session, err := web.BeginRegistration(user)
options, session, err := webAuthn.BeginRegistration(user)
// handle errors if present
// store the sessionData values
JSONResponse(w, options, http.StatusOK) // return the options generated
// options.publicKey contain our registration options
}

func FinishRegistration(w http.ResponseWriter, r *http.Request) {
response, err := protocol.ParseCredentialCreationResponseBody(r.Body)
if err != nil {
// Handle Error and return.

return
}

user := datastore.GetUser() // Get the user

// Get the session data stored from the function above
session := datastore.GetSession()

credential, err := w.CreateCredential(user, session, response)
credential, err := webAuthn.FinishRegistration(user, session, r)
if err != nil {
// Handle Error and return.

return
}

// If creation was successful, store the credential object
JSONResponse(w, "Registration Success", http.StatusOK) // Handle next steps

// Pseudocode to add the user credential.
user.AddCredential(credential)
datastore.SaveUser(user)

JSONResponse(w, "Registration Success", http.StatusOK) // Handle next steps
}
```

Expand All @@ -144,7 +137,7 @@ package example
func BeginLogin(w http.ResponseWriter, r *http.Request) {
user := datastore.GetUser() // Find the user

options, session, err := w.BeginLogin(user)
options, session, err := webAuthn.BeginLogin(user)
if err != nil {
// Handle Error and return.

Expand All @@ -159,19 +152,12 @@ func BeginLogin(w http.ResponseWriter, r *http.Request) {
}

func FinishLogin(w http.ResponseWriter, r *http.Request) {
response, err := protocol.ParseCredentialRequestResponseBody(r.Body)
if err != nil {
// Handle Error and return.

return
}

user := datastore.GetUser() // Get the user

// Get the session data stored from the function above
session := datastore.GetSession()

credential, err := w.ValidateLogin(user, session, response)
credential, err := webAuthn.FinishLogin(user, session, r)
if err != nil {
// Handle Error and return.

Expand Down Expand Up @@ -200,7 +186,7 @@ import (
"github.com/go-webauthn/webauthn/webauthn"
)

var w webauthn.WebAuthn // init this in your init function
var webAuthn webauthn.WebAuthn // init this in your init function

func beginRegistration() {
// Updating the AuthenticatorSelection options.
Expand All @@ -216,7 +202,7 @@ func beginRegistration() {
conveyancePref := protocol.PreferNoAttestation

user := datastore.GetUser() // Get the user
opts, session, err := w.BeginRegistration(user, webauthn.WithAuthenticatorSelection(authSelect), webauthn.WithConveyancePreference(conveyancePref))
opts, session, err := webAuthn.BeginRegistration(user, webauthn.WithAuthenticatorSelection(authSelect), webauthn.WithConveyancePreference(conveyancePref))

// Handle next steps
}
Expand All @@ -234,7 +220,7 @@ import (
"github.com/go-webauthn/webauthn/webauthn"
)

var w webauthn.WebAuthn // init this in your init function
var webAuthn webauthn.WebAuthn // init this in your init function

func beginLogin() {
// Updating the AuthenticatorSelection options.
Expand Down Expand Up @@ -288,7 +274,7 @@ func main() {
},
}

w, err := webauthn.New(wconfig)
webAuthn, err := webauthn.New(wconfig)
if err != nil {
fmt.Println(err)
}
Expand Down

0 comments on commit feda47a

Please sign in to comment.