implement svg interface in login and signup

This commit is contained in:
2024-09-13 15:18:36 -05:00
parent 8ba7ae206f
commit 3013e74bc5
32 changed files with 515 additions and 359 deletions

View File

@@ -28,6 +28,8 @@ func (h *NKodeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.LoginHandler(w, r)
case api.RenewAttributes:
h.RenewAttributesHandler(w, r)
case api.RandomSvgInterface:
h.RandomSvgInterfaceHandler(w, r)
default:
w.WriteHeader(http.StatusNotFound)
_, err := w.Write([]byte("404 not found"))
@@ -77,6 +79,7 @@ func (h *NKodeHandler) GenerateSignupInterfaceHandler(w http.ResponseWriter, r *
methodNotAllowed(w)
return
}
log.Print("signup interface")
var signupPost GenerateSignupInterfacePost
err := decodeJson(w, r, &signupPost)
@@ -95,7 +98,7 @@ func (h *NKodeHandler) GenerateSignupInterfaceHandler(w http.ResponseWriter, r *
log.Println(err)
return
}
resp, err := h.Api.GenerateSignupInterface(signupPost.Username, signupPost.CustomerId, kp)
resp, err := h.Api.GenerateSignupInterface(signupPost.Username, CustomerId(signupPost.CustomerId), kp)
if err != nil {
internalServerErrorHandler(w)
log.Println(err)
@@ -130,7 +133,7 @@ func (h *NKodeHandler) SetNKodeHandler(w http.ResponseWriter, r *http.Request) {
log.Println(err)
return
}
confirmInterface, err := h.Api.SetNKode(setNKodePost.CustomerId, setNKodePost.SessionId, setNKodePost.KeySelection)
confirmInterface, err := h.Api.SetNKode(CustomerId(setNKodePost.CustomerId), SessionId(setNKodePost.SessionId), setNKodePost.KeySelection)
if err != nil {
internalServerErrorHandler(w)
log.Println(err)
@@ -168,7 +171,7 @@ func (h *NKodeHandler) ConfirmNKodeHandler(w http.ResponseWriter, r *http.Reques
log.Println(err)
return
}
err = h.Api.ConfirmNKode(confirmNKodePost.CustomerId, confirmNKodePost.SessionId, confirmNKodePost.KeySelection)
err = h.Api.ConfirmNKode(CustomerId(confirmNKodePost.CustomerId), SessionId(confirmNKodePost.SessionId), confirmNKodePost.KeySelection)
if err != nil {
internalServerErrorHandler(w)
log.Println(err)
@@ -191,15 +194,14 @@ func (h *NKodeHandler) GetLoginInterfaceHandler(w http.ResponseWriter, r *http.R
log.Println(err)
return
}
loginInterface, err := h.Api.GetLoginInterface(loginInterfacePost.Username, loginInterfacePost.CustomerId)
loginInterface, err := h.Api.GetLoginInterface(loginInterfacePost.Username, CustomerId(loginInterfacePost.CustomerId))
if err != nil {
internalServerErrorHandler(w)
log.Println(err)
return
}
respBody := GetLoginInterfaceResp{UserInterface: loginInterface}
respBytes, err := json.Marshal(respBody)
respBytes, err := json.Marshal(loginInterface)
if err != nil {
internalServerErrorHandler(w)
log.Println(err)
@@ -228,7 +230,7 @@ func (h *NKodeHandler) LoginHandler(w http.ResponseWriter, r *http.Request) {
log.Println(err)
return
}
err = h.Api.Login(loginPost.CustomerId, loginPost.Username, loginPost.KeySelection)
err = h.Api.Login(CustomerId(loginPost.CustomerId), loginPost.Username, loginPost.KeySelection)
if err != nil {
internalServerErrorHandler(w)
log.Println(err)
@@ -252,7 +254,35 @@ func (h *NKodeHandler) RenewAttributesHandler(w http.ResponseWriter, r *http.Req
return
}
err = h.Api.RenewAttributes(renewAttributesPost.CustomerId)
err = h.Api.RenewAttributes(CustomerId(renewAttributesPost.CustomerId))
if err != nil {
internalServerErrorHandler(w)
log.Println(err)
return
}
w.WriteHeader(http.StatusOK)
}
func (h *NKodeHandler) RandomSvgInterfaceHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
methodNotAllowed(w)
}
svgs, err := h.Api.RandomSvgInterface()
if err != nil {
internalServerErrorHandler(w)
log.Println(err)
return
}
respBody := RandomSvgInterfaceResp{Svgs: svgs}
respBytes, err := json.Marshal(respBody)
if err != nil {
internalServerErrorHandler(w)
log.Println(err)
return
}
_, err = w.Write(respBytes)
if err != nil {
internalServerErrorHandler(w)
log.Println(err)

View File

@@ -8,42 +8,46 @@ type SetNKodeResp struct {
UserInterface []int `json:"user_interface"`
}
type RandomSvgInterfaceResp struct {
Svgs []string `json:"svgs"`
}
type NewCustomerPost struct {
NKodePolicy NKodePolicy `json:"nkode_policy"`
}
type GenerateSignupInterfacePost struct {
CustomerId CustomerId `json:"customer_id"`
AttrsPerKey int `json:"attrs_per_key"`
NumbOfKeys int `json:"numb_of_keys"`
Username Username `json:"username"`
CustomerId uuid.UUID `json:"customer_id"`
AttrsPerKey int `json:"attrs_per_key"`
NumbOfKeys int `json:"numb_of_keys"`
Username Username `json:"username"`
}
type SetNKodePost struct {
CustomerId CustomerId `json:"customer_id"`
CustomerId uuid.UUID `json:"customer_id"`
KeySelection KeySelection `json:"key_selection"`
SessionId SessionId `json:"session_id"`
SessionId uuid.UUID `json:"session_id"`
}
type ConfirmNKodePost struct {
CustomerId CustomerId `json:"customer_id"`
CustomerId uuid.UUID `json:"customer_id"`
KeySelection KeySelection `json:"key_selection"`
SessionId SessionId `json:"session_id"`
SessionId uuid.UUID `json:"session_id"`
}
type GetLoginInterfacePost struct {
Username Username `json:"username"`
CustomerId CustomerId `json:"customer_id"`
Username Username `json:"username"`
CustomerId uuid.UUID `json:"customer_id"`
}
type LoginPost struct {
CustomerId CustomerId `json:"customer_id"`
CustomerId uuid.UUID `json:"customer_id"`
Username Username `json:"username"`
KeySelection KeySelection `json:"key_selection"`
}
type RenewAttributesPost struct {
CustomerId CustomerId `json:"customer_id"`
CustomerId uuid.UUID `json:"customer_id"`
}
type CreateNewCustomerResp struct {
@@ -51,12 +55,14 @@ type CreateNewCustomerResp struct {
}
type GenerateSignupInterfaceResp struct {
SessionId SessionId `json:"session_id"`
UserInterface IdxInterface `json:"user_interface"`
SessionId SessionId `json:"session_id"`
UserIdxInterface IdxInterface `json:"user_interface"`
SvgInterface []string `json:"svg_interface"`
}
type GetLoginInterfaceResp struct {
UserInterface IdxInterface `json:"user_interface"`
UserIdxInterface IdxInterface `json:"user_interface"`
SvgInterface []string `json:"svg_interface"`
}
type KeySelection []int
@@ -66,15 +72,18 @@ type UserId uuid.UUID
type Username string
type IdxInterface []int
type SvgIdInterface []int
type NKodeAPIInterface interface {
CreateNewCustomer(NKodePolicy, *CustomerId) (*CustomerId, error)
GenerateSignupInterface(Username, CustomerId, KeypadDimension) (*GenerateSignupInterfaceResp, error)
SetNKode(CustomerId, SessionId, KeySelection) (IdxInterface, error)
ConfirmNKode(CustomerId, SessionId, KeySelection) error
GetLoginInterface(Username, CustomerId) (IdxInterface, error)
GetLoginInterface(Username, CustomerId) (*GetLoginInterfaceResp, error)
Login(CustomerId, Username, KeySelection) error
RenewAttributes(CustomerId) error
RandomSvgInterface() ([]string, error)
GetSvgStringInterface(idInterface SvgIdInterface) ([]string, error)
}
type EncipheredNKode struct {

View File

@@ -10,29 +10,29 @@ type User struct {
Username Username
EncipheredPasscode EncipheredNKode
Kp KeypadDimension
UserKeys UserCipherKeys
CipherKeys UserCipherKeys
Interface UserInterface
Renew bool
}
func (u *User) DecipherMask(setVals []uint64, passcodeLen int) ([]uint64, error) {
return u.UserKeys.DecipherMask(u.EncipheredPasscode.Mask, setVals, passcodeLen)
return u.CipherKeys.DecipherMask(u.EncipheredPasscode.Mask, setVals, passcodeLen)
}
func (u *User) RenewKeys(setXor []uint64, attrXor []uint64) error {
u.Renew = true
var err error
u.UserKeys.SetKey, err = util.XorLists(setXor[:u.Kp.AttrsPerKey], u.UserKeys.SetKey)
u.CipherKeys.SetKey, err = util.XorLists(setXor[:u.Kp.AttrsPerKey], u.CipherKeys.SetKey)
if err != nil {
panic(err)
}
u.UserKeys.AlphaKey, err = util.XorLists(attrXor[:u.Kp.TotalAttrs()], u.UserKeys.AlphaKey)
u.CipherKeys.AlphaKey, err = util.XorLists(attrXor[:u.Kp.TotalAttrs()], u.CipherKeys.AlphaKey)
return err
}
func (u *User) RefreshPasscode(passcodeAttrIdx []int, customerAttributes CustomerAttributes) error {
setVals, err := customerAttributes.SetValsForKp(u.Kp)
newKeys, err := NewUserCipherKeys(&u.Kp, setVals, u.UserKeys.MaxNKodeLen)
newKeys, err := NewUserCipherKeys(&u.Kp, setVals, u.CipherKeys.MaxNKodeLen)
if err != nil {
return err
}
@@ -42,7 +42,7 @@ func (u *User) RefreshPasscode(passcodeAttrIdx []int, customerAttributes Custome
return err
}
u.UserKeys = *newKeys
u.CipherKeys = *newKeys
u.EncipheredPasscode = *encipheredPasscode
u.Renew = false
return nil

View File

@@ -9,13 +9,16 @@ import (
type UserInterface struct {
IdxInterface IdxInterface
SvgId SvgIdInterface
Kp *KeypadDimension
}
func NewUserInterface(kp *KeypadDimension) (*UserInterface, error) {
func NewUserInterface(kp *KeypadDimension, svgId SvgIdInterface) (*UserInterface, error) {
idxInterface := util.IdentityArray(kp.TotalAttrs())
userInterface := UserInterface{
IdxInterface: idxInterface,
SvgId: svgId,
Kp: kp,
}
err := userInterface.RandomShuffle()

View File

@@ -59,7 +59,8 @@ func TestUserInterface_RandomShuffle(t *testing.T) {
AttrsPerKey: 10,
NumbOfKeys: 8,
}
userInterface, err := NewUserInterface(&kp)
mockSvgInterface := make(SvgIdInterface, kp.TotalAttrs())
userInterface, err := NewUserInterface(&kp, mockSvgInterface)
assert.NoError(t, err)
userInterfaceCopy := make([]int, len(userInterface.IdxInterface))
copy(userInterfaceCopy, userInterface.IdxInterface)
@@ -81,8 +82,8 @@ func TestUserInterface_DisperseInterface(t *testing.T) {
for idx := 0; idx < 10000; idx++ {
kp := KeypadDimension{AttrsPerKey: 7, NumbOfKeys: 10}
userInterface, err := NewUserInterface(&kp)
mockSvgInterface := make(SvgIdInterface, kp.TotalAttrs())
userInterface, err := NewUserInterface(&kp, mockSvgInterface)
assert.NoError(t, err)
preDispersion, err := userInterface.AttributeAdjacencyGraph()
assert.NoError(t, err)
@@ -100,7 +101,8 @@ func TestUserInterface_DisperseInterface(t *testing.T) {
func TestUserInterface_PartialInterfaceShuffle(t *testing.T) {
kp := KeypadDimension{AttrsPerKey: 7, NumbOfKeys: 10}
userInterface, err := NewUserInterface(&kp)
mockSvgInterface := make(SvgIdInterface, kp.TotalAttrs())
userInterface, err := NewUserInterface(&kp, mockSvgInterface)
assert.NoError(t, err)
preShuffle := userInterface.IdxInterface
err = userInterface.PartialInterfaceShuffle()