refactor user defined keypad
This commit is contained in:
@@ -17,15 +17,8 @@ type Customer struct {
|
||||
Users map[m.Username]User
|
||||
}
|
||||
|
||||
func NewCustomer(keypadSize m.KeypadSize, nkodePolicy m.NKodePolicy) (*Customer, error) {
|
||||
if keypadSize.TotalAttrs() < nkodePolicy.DistinctAttributes {
|
||||
return nil, errors.New(fmt.Sprintf("incompadible nkode policy and keypad size TotalAttrs: %d < DistinctAttributes: %d", keypadSize.TotalAttrs(), nkodePolicy.DistinctAttributes))
|
||||
}
|
||||
|
||||
if keypadSize.AttrsPerKey < nkodePolicy.DistinctSets {
|
||||
return nil, errors.New(fmt.Sprintf("incompadible nkode policy and keypad size AttrPerKey: %d < DistinctSets: %d", keypadSize.AttrsPerKey, nkodePolicy.DistinctSets))
|
||||
}
|
||||
customerAttrs, err := NewCustomerAttributes(keypadSize)
|
||||
func NewCustomer(nkodePolicy m.NKodePolicy) (*Customer, error) {
|
||||
customerAttrs, err := NewCustomerAttributes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -39,12 +32,16 @@ func NewCustomer(keypadSize m.KeypadSize, nkodePolicy m.NKodePolicy) (*Customer,
|
||||
return &customer, nil
|
||||
}
|
||||
|
||||
func (c *Customer) AddNewUser(username m.Username, passcodeIdx []int, userInterface UserInterface) error {
|
||||
func (c *Customer) AddNewUser(username m.Username, passcodeIdx []int, ui UserInterface, kp m.KeypadDimension) error {
|
||||
_, exists := c.Users[username]
|
||||
if exists {
|
||||
return errors.New(fmt.Sprintf("User %s already exists for customer %+v exists", username, c.CustomerId))
|
||||
}
|
||||
newKeys, err := NewUserCipherKeys(c.Attributes.KeypadSize, c.Attributes.SetVals, c.NKodePolicy.MaxNkodeLen)
|
||||
setVals, err := c.Attributes.SetVals(kp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newKeys, err := NewUserCipherKeys(&kp, setVals, c.NKodePolicy.MaxNkodeLen)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -56,7 +53,8 @@ func (c *Customer) AddNewUser(username m.Username, passcodeIdx []int, userInterf
|
||||
Username: username,
|
||||
EncipheredPasscode: *encipheredNKode,
|
||||
UserKeys: *newKeys,
|
||||
Interface: userInterface,
|
||||
Interface: ui,
|
||||
Kp: kp,
|
||||
}
|
||||
c.Users[username] = newUser
|
||||
return nil
|
||||
@@ -69,20 +67,24 @@ func (c *Customer) ValidKeyEntry(username m.Username, selectedKeys []int) ([]int
|
||||
}
|
||||
|
||||
validKeys := py.All[int](selectedKeys, func(idx int) bool {
|
||||
return 0 <= idx && idx < c.Attributes.KeypadSize.NumbOfKeys
|
||||
return 0 <= idx && idx < user.Kp.NumbOfKeys
|
||||
})
|
||||
if !validKeys {
|
||||
return nil, errors.New(fmt.Sprintf("one or more keys not in range 0-%d", c.Attributes.KeypadSize.NumbOfKeys-1))
|
||||
return nil, errors.New(fmt.Sprintf("one or more keys not in range 0-%d", user.Kp.NumbOfKeys-1))
|
||||
}
|
||||
presumedAttrIdxVals, err := c.getPresumedAttributeIdxVals(user, selectedKeys)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = c.IsValidNKode(presumedAttrIdxVals)
|
||||
err = c.IsValidNKode(user.Kp, presumedAttrIdxVals)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = user.UserKeys.ValidPassword(user.EncipheredPasscode.Code, presumedAttrIdxVals, c.Attributes)
|
||||
attrVals, err := c.Attributes.AttrVals(user.Kp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = user.UserKeys.ValidPassword(user.EncipheredPasscode.Code, presumedAttrIdxVals, attrVals)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -96,7 +98,12 @@ func (c *Customer) getPresumedAttributeIdxVals(user User, selectedKeys []int) ([
|
||||
if passcodeLen < c.NKodePolicy.MinNkodeLen || passcodeLen > c.NKodePolicy.MaxNkodeLen {
|
||||
return nil, errors.New(fmt.Sprintf("Invalid passcode length of %d. Passcode length must be in range %d-%d", passcodeLen, c.NKodePolicy.MinNkodeLen, c.NKodePolicy.MaxNkodeLen))
|
||||
}
|
||||
passcodeSetVals, err := user.DecipherMask(c.Attributes.SetVals, passcodeLen)
|
||||
|
||||
setVals, err := c.Attributes.SetVals(user.Kp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
passcodeSetVals, err := user.DecipherMask(setVals, passcodeLen)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -117,24 +124,28 @@ func (c *Customer) getPresumedAttributeIdxVals(user User, selectedKeys []int) ([
|
||||
return presumedAttrIdxVals, nil
|
||||
}
|
||||
|
||||
func (c *Customer) IsValidNKode(passcodeAttrIdx []int) error {
|
||||
func (c *Customer) IsValidNKode(kp m.KeypadDimension, passcodeAttrIdx []int) error {
|
||||
nkodeLen := len(passcodeAttrIdx)
|
||||
if nkodeLen < c.NKodePolicy.MinNkodeLen {
|
||||
return errors.New(fmt.Sprintf("NKode length %d is too short. Minimum nKode length is %d", nkodeLen, c.NKodePolicy.MinNkodeLen))
|
||||
}
|
||||
|
||||
validIdx := py.All[int](passcodeAttrIdx, func(i int) bool {
|
||||
return i >= 0 && i < c.Attributes.KeypadSize.TotalAttrs()
|
||||
return i >= 0 && i < kp.TotalAttrs()
|
||||
})
|
||||
|
||||
if !validIdx {
|
||||
return errors.New(fmt.Sprintf("One or more idx out of range 0-%d in IsValidNKode", c.Attributes.KeypadSize.TotalAttrs()-1))
|
||||
return errors.New(fmt.Sprintf("One or more idx out of range 0-%d in IsValidNKode", kp.TotalAttrs()-1))
|
||||
}
|
||||
passcodeSetVals := make(hashset.Set[uint64])
|
||||
passcodeAttrVals := make(hashset.Set[uint64])
|
||||
attrVals, err := c.Attributes.AttrVals(kp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for idx := 0; idx < nkodeLen; idx++ {
|
||||
attrVal := c.Attributes.AttrVals[passcodeAttrIdx[idx]]
|
||||
setVal, err := c.Attributes.GetAttrSetVal(attrVal)
|
||||
attrVal := attrVals[passcodeAttrIdx[idx]]
|
||||
setVal, err := c.Attributes.GetAttrSetVal(attrVal, kp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -167,20 +178,36 @@ func (c *Customer) GetLoginInterface(username m.Username) ([]int, error) {
|
||||
}
|
||||
|
||||
func (c *Customer) RenewKeys() error {
|
||||
oldAttrs := make([]uint64, c.Attributes.KeypadSize.TotalAttrs())
|
||||
oldSets := make([]uint64, c.Attributes.KeypadSize.AttrsPerKey)
|
||||
copy(oldAttrs, c.Attributes.AttrVals)
|
||||
copy(oldSets, c.Attributes.SetVals)
|
||||
oldAttrs := make([]uint64, m.KeypadMax.TotalAttrs())
|
||||
oldSets := make([]uint64, m.KeypadMax.AttrsPerKey)
|
||||
allAttrVals, err := c.Attributes.AttrVals(m.KeypadMax)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
allSetVals, err := c.Attributes.AttrVals(m.KeypadMax)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
copy(oldAttrs, allAttrVals)
|
||||
copy(oldSets, allSetVals)
|
||||
|
||||
err := c.Attributes.Renew()
|
||||
err = c.Attributes.Renew()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
attrsXor, err := util.XorLists(oldAttrs, c.Attributes.AttrVals)
|
||||
allAttrVals, err = c.Attributes.AttrVals(m.KeypadMax)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
setXor, err := util.XorLists(oldSets, c.Attributes.SetVals)
|
||||
allSetVals, err = c.Attributes.AttrVals(m.KeypadMax)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
attrsXor, err := util.XorLists(oldAttrs, allAttrVals)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
setXor, err := util.XorLists(oldSets, allSetVals)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user