Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Separate Username from Id #29

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions _example/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/egregors/passkey"
"github.com/go-webauthn/webauthn/webauthn"

"github.com/mstarongithub/passkey"
)

type Storage struct {
Expand Down Expand Up @@ -53,6 +55,18 @@ func (s *Storage) SaveUser(user passkey.User) {
s.users[user.WebAuthnName()] = user
}

func (s *Storage) GetUserByWebAuthnId(id []byte) passkey.User {
s.uMu.Lock()
defer s.uMu.Unlock()

// Storage implementation assumes that username == webauthn Id
if user, ok := s.users[string(id)]; ok {
return user
} else {
return nil
}
}

// -- Session storage methods --

func (s *Storage) GenSessionID() (string, error) {
Expand Down
22 changes: 17 additions & 5 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ func (p *Passkey) beginRegistration(w http.ResponseWriter, r *http.Request) {
t, err := p.sessionStore.GenSessionID()
if err != nil {
p.l.Errorf("can't generate session id: %s", err.Error())
JSONResponse(w, fmt.Sprintf("can't generate session id: %s", err.Error()), http.StatusInternalServerError)
JSONResponse(
w,
fmt.Sprintf("can't generate session id: %s", err.Error()),
http.StatusInternalServerError,
)

return
}
Expand Down Expand Up @@ -70,7 +74,7 @@ func (p *Passkey) finishRegistration(w http.ResponseWriter, r *http.Request) {
}

// TODO: username != user id? need to check
user := p.userStore.GetOrCreateUser(string(session.UserID)) // Get the user
user := p.userStore.GetUserByWebAuthnId(session.UserID) // Get the user

credential, err := p.webAuthn.FinishRegistration(user, *session, r)
if err != nil {
Expand Down Expand Up @@ -120,7 +124,11 @@ func (p *Passkey) beginLogin(w http.ResponseWriter, r *http.Request) {
t, err := p.sessionStore.GenSessionID()
if err != nil {
p.l.Errorf("can't generate session id: %s", err.Error())
JSONResponse(w, fmt.Sprintf("can't generate session id: %s", err.Error()), http.StatusInternalServerError)
JSONResponse(
w,
fmt.Sprintf("can't generate session id: %s", err.Error()),
http.StatusInternalServerError,
)

return
}
Expand All @@ -146,7 +154,7 @@ func (p *Passkey) finishLogin(w http.ResponseWriter, r *http.Request) {
session, _ := p.sessionStore.GetSession(sid.Value) // FIXME: cover invalid session

// TODO: username != user id? need to check
user := p.userStore.GetOrCreateUser(string(session.UserID)) // Get the user
user := p.userStore.GetUserByWebAuthnId(session.UserID) // Get the user

credential, err := p.webAuthn.FinishLogin(user, *session, r)
if err != nil {
Expand All @@ -173,7 +181,11 @@ func (p *Passkey) finishLogin(w http.ResponseWriter, r *http.Request) {
t, err := p.sessionStore.GenSessionID()
if err != nil {
p.l.Errorf("can't generate session id: %s", err.Error())
JSONResponse(w, fmt.Sprintf("can't generate session id: %s", err.Error()), http.StatusInternalServerError)
JSONResponse(
w,
fmt.Sprintf("can't generate session id: %s", err.Error()),
http.StatusInternalServerError,
)

return
}
Expand Down
1 change: 1 addition & 0 deletions ifaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type User interface {

type UserStore interface {
GetOrCreateUser(userID string) User
GetUserByWebAuthnId(id []byte) User
SaveUser(User)
}

Expand Down
Loading