118 lines
3.8 KiB
Go
118 lines
3.8 KiB
Go
package webui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
)
|
|
|
|
func (s *Server) registerPasskeyRoutes(public, protected *http.ServeMux) {
|
|
public.HandleFunc("POST /api/auth/passkey/begin", func(w http.ResponseWriter, r *http.Request) { s.handlePasskeyBegin(w, r, false) })
|
|
public.HandleFunc("POST /api/auth/passkey/finish", func(w http.ResponseWriter, r *http.Request) { s.handlePasskeyFinish(w, r, false) })
|
|
protected.HandleFunc("POST /api/account/passkeys/begin", func(w http.ResponseWriter, r *http.Request) { s.handlePasskeyBegin(w, r, true) })
|
|
protected.HandleFunc("POST /api/account/passkeys/finish", func(w http.ResponseWriter, r *http.Request) { s.handlePasskeyFinish(w, r, true) })
|
|
protected.HandleFunc("GET /api/account/passkeys", s.handlePasskeyList)
|
|
protected.HandleFunc("DELETE /api/account/passkeys/{id}", s.handlePasskeyDelete)
|
|
}
|
|
|
|
func (s *Server) passkeyCookie(registration bool) string {
|
|
if registration {
|
|
return s.cookies().passkeyRegistration
|
|
}
|
|
return s.cookies().passkeyLogin
|
|
}
|
|
|
|
func (s *Server) handlePasskeyBegin(w http.ResponseWriter, r *http.Request, registration bool) {
|
|
if !s.authorizeAuthenticationMutation(w, r) {
|
|
return
|
|
}
|
|
if registration && !s.requireFreshAccount(w, r) {
|
|
return
|
|
}
|
|
store, ok := s.Accounts.(identity.Passkeys)
|
|
if !ok {
|
|
http.Error(w, "passkeys unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
value, err := store.BeginPasskey(r.Context(), registration)
|
|
if err != nil {
|
|
s.writeAccountError(w, err)
|
|
return
|
|
}
|
|
scheme, _ := s.expectedOrigin(r)
|
|
http.SetCookie(w, &http.Cookie{Name: s.passkeyCookie(registration), Value: value.Token, Path: "/api/", HttpOnly: true, Secure: scheme == "https", SameSite: http.SameSiteStrictMode, MaxAge: 300})
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
writeJSON(w, json.RawMessage(value.Options))
|
|
}
|
|
|
|
func (s *Server) handlePasskeyFinish(w http.ResponseWriter, r *http.Request, registration bool) {
|
|
if !s.authorizeAuthenticationMutation(w, r) {
|
|
return
|
|
}
|
|
if registration && !s.requireFreshAccount(w, r) {
|
|
return
|
|
}
|
|
store, ok := s.Accounts.(identity.Passkeys)
|
|
if !ok {
|
|
http.Error(w, "passkeys unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
cookie, err := r.Cookie(s.passkeyCookie(registration))
|
|
if err != nil {
|
|
http.Error(w, "passkey request expired", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
http.SetCookie(w, &http.Cookie{Name: s.passkeyCookie(registration), Path: "/api/", HttpOnly: true, SameSite: http.SameSiteStrictMode, MaxAge: -1})
|
|
var request struct {
|
|
Credential json.RawMessage `json:"credential"`
|
|
Name string `json:"name"`
|
|
}
|
|
if !decodeAccountRequest(w, r, &request) {
|
|
return
|
|
}
|
|
value, err := store.FinishPasskey(r.Context(), cookie.Value, request.Credential, registration, request.Name)
|
|
if err != nil {
|
|
s.writeAccountError(w, err)
|
|
return
|
|
}
|
|
if !registration {
|
|
s.clearProviderCookie(w)
|
|
s.setAccountCookie(w, r, value)
|
|
}
|
|
writeJSON(w, map[string]any{"user": value.Identity})
|
|
}
|
|
|
|
func (s *Server) handlePasskeyList(w http.ResponseWriter, r *http.Request) {
|
|
store, ok := s.Accounts.(identity.Passkeys)
|
|
if !ok {
|
|
http.Error(w, "passkeys unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
values, err := store.ListPasskeys(r.Context())
|
|
if err != nil {
|
|
s.writeAccountError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, map[string]any{"passkeys": values})
|
|
}
|
|
|
|
func (s *Server) handlePasskeyDelete(w http.ResponseWriter, r *http.Request) {
|
|
if !s.requireFreshAccount(w, r) {
|
|
return
|
|
}
|
|
if !s.authorizeAuthenticationMutation(w, r) {
|
|
return
|
|
}
|
|
store, ok := s.Accounts.(identity.Passkeys)
|
|
if !ok {
|
|
http.Error(w, "passkeys unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
if err := store.DeletePasskey(r.Context(), r.PathValue("id")); err != nil {
|
|
s.writeAccountError(w, err)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|