func decodeBase64(s string) ([]byte, error) {
	return base64.URLEncoding.WithPadding(base64.NoPadding).DecodeString(s)
}
copy-pasted from u2f library because the relevant API was not exported
package u2futil
import (
	"bytes"
	"crypto/sha256"
	"crypto/x509"
	"encoding/base64"
	"errors"
	"fmt"
	"github.com/function61/eventhorizon/pkg/ehevent"
	"github.com/function61/passitron/pkg/apitypes"
	"github.com/function61/passitron/pkg/domain"
	"github.com/function61/passitron/pkg/state"
	"github.com/tstranex/u2f"
	"strings"
	"time"
)
var cnFromSslCertificate = ""
func GetAppIdHostname() string {
	if cnFromSslCertificate == "" {
		panic("cnFromSslCertificate not set")
	}
	return "https://" + cnFromSslCertificate
}
func InjectCommonNameFromSslCertificate(cert *x509.Certificate) {
	cnFromSslCertificate = cert.Subject.CommonName
}
func MakeTrustedFacets() []string {
	return []string{GetAppIdHostname()}
}
func U2ftokenToRegistration(u2ftoken *state.U2FToken) *u2f.Registration {
	reg := &u2f.Registration{}
	dataDecoded, err := decodeBase64(u2ftoken.RegistrationData)
	if err != nil {
		panic(err)
	}
	if err := reg.UnmarshalBinary(dataDecoded); err != nil {
		panic(err)
	}
	return reg
}
func GrabUsersU2FTokens(userStorage *state.UserStorage) []u2f.Registration {
	regs := []u2f.Registration{}
	for _, token := range userStorage.U2FTokens() {
		regs = append(regs, *U2ftokenToRegistration(token))
	}
	return regs
}
func GrabUsersU2FTokenByKeyHandle(userStorage *state.UserStorage, keyHandle string) *state.U2FToken {
	for _, token := range userStorage.U2FTokens() {
		if token.KeyHandle == keyHandle {
			return token
		}
	}
	return nil
}
func RegisteredKeyFromRegistration(registration u2f.Registration) u2f.RegisteredKey {
	dummyChallenge, err := u2f.NewChallenge(GetAppIdHostname(), MakeTrustedFacets())
	if err != nil {
		panic(err)
	}
	dummyRegisterRequest := u2f.NewWebRegisterRequest(dummyChallenge, []u2f.Registration{registration})
	return dummyRegisterRequest.RegisteredKeys[0]
}