112 lines
3.3 KiB
Go
112 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"github.com/google/uuid"
|
|
m "go-nkode/models"
|
|
py "go-nkode/py-builtin"
|
|
)
|
|
|
|
type Customer struct {
|
|
CustomerId uuid.UUID
|
|
NKodePolicy m.NKodePolicy
|
|
Attributes CustomerAttributes
|
|
Users map[string]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)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
customer := Customer{
|
|
CustomerId: uuid.New(),
|
|
NKodePolicy: nkodePolicy,
|
|
Attributes: *customerAttrs,
|
|
Users: make(map[string]User),
|
|
}
|
|
|
|
return &customer, nil
|
|
}
|
|
|
|
func (c *Customer) AddNewUser(user User) error {
|
|
_, exists := c.Users[user.Username]
|
|
if exists {
|
|
return errors.New(fmt.Sprintf("user: %s exists", user.Username))
|
|
}
|
|
c.Users[user.Username] = user
|
|
return nil
|
|
}
|
|
|
|
func (c *Customer) ValidKeyEntryAndRenew(username string, selectedKeys []int) error {
|
|
user, exists := c.Users[username]
|
|
if !exists {
|
|
return errors.New(fmt.Sprintf("user %s does not exist for customer %s", username, c.CustomerId.String()))
|
|
}
|
|
|
|
validKeys := py.All[int](selectedKeys, func(idx int) bool {
|
|
return 0 <= idx && idx < c.Attributes.keypadSize.NumbOfKeys
|
|
})
|
|
if !validKeys {
|
|
return errors.New(fmt.Sprintf("one or more keys not in range 0-%d", c.Attributes.keypadSize.NumbOfKeys-1))
|
|
}
|
|
presumedAttrIdxVals, err := c.getPresumedAttributeIdxVals(user, selectedKeys)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = user.UserKeys.ValidPassword(user.EncipheredPasscode.Code, presumedAttrIdxVals, c.Attributes)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if user.Renew {
|
|
// renew
|
|
}
|
|
return nil
|
|
|
|
}
|
|
|
|
func (c *Customer) getPresumedAttributeIdxVals(user User, selectedKeys []int) ([]int, error) {
|
|
|
|
passcodeLen := len(selectedKeys)
|
|
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)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
presumedAttrIdxVals := make([]int, passcodeLen)
|
|
|
|
for idx := range presumedAttrIdxVals {
|
|
keyNumb := selectedKeys[idx]
|
|
setIdx, err := c.Attributes.IndexOfSet(passcodeSetVals[idx])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
selectedAttrIdx, err := user.Interface.GetAttrIdxByKeyNumbSetIdx(setIdx, keyNumb)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
presumedAttrIdxVals[idx] = selectedAttrIdx
|
|
}
|
|
return presumedAttrIdxVals, nil
|
|
}
|
|
|
|
func (c *Customer) IsValidNKode(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))
|
|
}
|
|
return nil
|
|
}
|