Commit 50f6eaa4 authored by ale's avatar ale

Use a custom u2f.Registration wrapper type that serializes to JSON

Establish a standardized serialization format for u2f registration
keys: base64-encoded raw registration data, encoded as a JSON
string. This format decodes transparently to a Go []byte slice, but it
needs explicit base64 decoding in Python.
parent 2dc1ea31
......@@ -12,7 +12,6 @@ import (
umdb "git.autistici.org/id/usermetadb"
"github.com/pquerna/otp/totp"
"github.com/sethvargo/go-password/password"
"github.com/tstranex/u2f"
)
// RequestBase contains parameters shared by all request types.
......@@ -904,8 +903,8 @@ func (s *AccountService) CreateUser(ctx context.Context, tx TX, req *CreateUserR
type UpdateUserRequest struct {
RequestBase
Lang string `json:"lang,omitempty"`
U2FRegistrations []*u2f.Registration `json:"u2f_registrations,omitempty"`
Lang string `json:"lang,omitempty"`
U2FRegistrations []*U2FRegistration `json:"u2f_registrations,omitempty"`
}
// Validate the request.
......
......@@ -46,7 +46,7 @@ type User struct {
AppSpecificPasswords []*AppSpecificPasswordInfo `json:"app_specific_passwords,omitempty"`
U2FRegistrations []*u2f.Registration `json:"u2f_registrations,omitempty"`
U2FRegistrations []*U2FRegistration `json:"u2f_registrations,omitempty"`
Resources []*Resource `json:"resources,omitempty"`
}
......@@ -407,3 +407,32 @@ func getHostingDir(path, siteRoot string) string {
}
return path
}
// U2FRegistration is just a thin wrapper for u2f.Registration that
// supports serialization and deserialization to JSON. The serialized
// format is simply the raw registration data (base64-encoded for
// transport).
type U2FRegistration struct {
*u2f.Registration
}
// MarshalJSON implements the json.Marshaler interface.
func (r *U2FRegistration) MarshalJSON() ([]byte, error) {
data, err := r.MarshalBinary()
if err != nil {
return nil, err
}
return json.Marshal(data)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (r *U2FRegistration) UnmarshalJSON(data []byte) error {
if data == nil {
return nil
}
var b []byte
if err := json.Unmarshal(data, &b); err != nil {
return err
}
return r.UnmarshalBinary(b)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment