user signup sessions
This commit is contained in:
77
customer.go
77
customer.go
@@ -37,42 +37,55 @@ func NewCustomer(keypadSize m.KeypadSize, nkodePolicy m.NKodePolicy) (*Customer,
|
||||
return &customer, nil
|
||||
}
|
||||
|
||||
func (c *Customer) AddNewUser(user User) error {
|
||||
_, exists := c.Users[user.Username]
|
||||
func (c *Customer) AddNewUser(username string, passcodeIdx []int, userInterface UserInterface) error {
|
||||
_, exists := c.Users[username]
|
||||
if exists {
|
||||
return errors.New(fmt.Sprintf("user: %s exists", user.Username))
|
||||
return errors.New(fmt.Sprintf("User %s already exists for customer %s exists", username, c.CustomerId.String()))
|
||||
}
|
||||
c.Users[user.Username] = user
|
||||
newKeys, err := NewUserCipherKeys(c.Attributes.KeypadSize, c.Attributes.SetVals, c.NKodePolicy.MaxNkodeLen)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
encipheredNKode, err := newKeys.EncipherNKode(passcodeIdx, c.Attributes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newUser := User{
|
||||
Username: username,
|
||||
EncipheredPasscode: *encipheredNKode,
|
||||
UserKeys: *newKeys,
|
||||
Interface: userInterface,
|
||||
}
|
||||
c.Users[username] = newUser
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Customer) ValidKeyEntryAndRenew(username string, selectedKeys []int) error {
|
||||
func (c *Customer) ValidKeyEntry(username string, selectedKeys []int) ([]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()))
|
||||
return nil, 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
|
||||
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))
|
||||
return nil, 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
|
||||
return nil, err
|
||||
}
|
||||
err = c.IsValidNKode(presumedAttrIdxVals)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = user.UserKeys.ValidPassword(user.EncipheredPasscode.Code, presumedAttrIdxVals, c.Attributes)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if user.Renew {
|
||||
// renew
|
||||
}
|
||||
return nil
|
||||
|
||||
return presumedAttrIdxVals, nil
|
||||
}
|
||||
|
||||
func (c *Customer) getPresumedAttributeIdxVals(user User, selectedKeys []int) ([]int, error) {
|
||||
@@ -107,5 +120,37 @@ func (c *Customer) IsValidNKode(passcodeAttrIdx []int) error {
|
||||
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()
|
||||
})
|
||||
|
||||
if !validIdx {
|
||||
return errors.New(fmt.Sprintf("One or more idx out of range 0-%d in IsValidNKode", c.Attributes.KeypadSize.TotalAttrs()-1))
|
||||
}
|
||||
passcodeSetVals := make([]uint64, nkodeLen)
|
||||
var err error
|
||||
for idx := range passcodeSetVals {
|
||||
attrVal := c.Attributes.AttrVals[passcodeAttrIdx[idx]]
|
||||
passcodeSetVals[idx], err = c.Attributes.GetAttrSetVal(attrVal)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Customer) GetLoginInterface(username string) ([]int, error) {
|
||||
user, exists := c.Users[username]
|
||||
if !exists {
|
||||
return nil, errors.New(fmt.Sprintf("can't get login interface for non-existant user %s in customer %s", username, c.CustomerId.String()))
|
||||
}
|
||||
err := user.Interface.PartialInterfaceShuffle()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Users[username] = user
|
||||
return user.Interface.IdxInterface, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user