functional simple api
This commit is contained in:
@@ -4,20 +4,20 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"go-nkode/core/model"
|
||||
m "go-nkode/core/model"
|
||||
"go-nkode/hashset"
|
||||
py "go-nkode/py-builtin"
|
||||
"go-nkode/util"
|
||||
)
|
||||
|
||||
type Customer struct {
|
||||
CustomerId uuid.UUID
|
||||
NKodePolicy model.NKodePolicy
|
||||
CustomerId m.CustomerId
|
||||
NKodePolicy m.NKodePolicy
|
||||
Attributes CustomerAttributes
|
||||
Users map[string]User
|
||||
Users map[m.Username]User
|
||||
}
|
||||
|
||||
func NewCustomer(keypadSize model.KeypadSize, nkodePolicy model.NKodePolicy) (*Customer, error) {
|
||||
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))
|
||||
}
|
||||
@@ -30,19 +30,19 @@ func NewCustomer(keypadSize model.KeypadSize, nkodePolicy model.NKodePolicy) (*C
|
||||
return nil, err
|
||||
}
|
||||
customer := Customer{
|
||||
CustomerId: uuid.New(),
|
||||
CustomerId: m.CustomerId(uuid.New()),
|
||||
NKodePolicy: nkodePolicy,
|
||||
Attributes: *customerAttrs,
|
||||
Users: make(map[string]User),
|
||||
Users: make(map[m.Username]User),
|
||||
}
|
||||
|
||||
return &customer, nil
|
||||
}
|
||||
|
||||
func (c *Customer) AddNewUser(username string, passcodeIdx []int, userInterface UserInterface) error {
|
||||
func (c *Customer) AddNewUser(username m.Username, passcodeIdx []int, userInterface UserInterface) error {
|
||||
_, exists := c.Users[username]
|
||||
if exists {
|
||||
return errors.New(fmt.Sprintf("User %s already exists for customer %s exists", username, c.CustomerId.String()))
|
||||
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)
|
||||
if err != nil {
|
||||
@@ -62,10 +62,10 @@ func (c *Customer) AddNewUser(username string, passcodeIdx []int, userInterface
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Customer) ValidKeyEntry(username string, selectedKeys []int) ([]int, error) {
|
||||
func (c *Customer) ValidKeyEntry(username m.Username, selectedKeys []int) ([]int, error) {
|
||||
user, exists := c.Users[username]
|
||||
if !exists {
|
||||
return nil, 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 %+v", username, c.CustomerId))
|
||||
}
|
||||
|
||||
validKeys := py.All[int](selectedKeys, func(idx int) bool {
|
||||
@@ -152,10 +152,10 @@ func (c *Customer) IsValidNKode(passcodeAttrIdx []int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Customer) GetLoginInterface(username string) ([]int, error) {
|
||||
func (c *Customer) GetLoginInterface(username m.Username) ([]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()))
|
||||
return nil, errors.New(fmt.Sprintf("can't get login interface for non-existant user %s in customer %s", username, c.CustomerId))
|
||||
}
|
||||
err := user.Interface.PartialInterfaceShuffle()
|
||||
|
||||
|
||||
@@ -11,10 +11,10 @@ import (
|
||||
type CustomerAttributes struct {
|
||||
AttrVals []uint64
|
||||
SetVals []uint64
|
||||
KeypadSize model.KeypadSize
|
||||
KeypadSize m.KeypadSize
|
||||
}
|
||||
|
||||
func NewCustomerAttributes(keypadSize model.KeypadSize) (*CustomerAttributes, error) {
|
||||
func NewCustomerAttributes(keypadSize m.KeypadSize) (*CustomerAttributes, error) {
|
||||
if keypadSize.IsDispersable() {
|
||||
return nil, errors.New("number of keys must be less than the number of attributes per key to be dispersion resistant")
|
||||
}
|
||||
|
||||
@@ -2,24 +2,30 @@ package nkode
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
model2 "go-nkode/core/model"
|
||||
m "go-nkode/core/model"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewCustomerAttributes(t *testing.T) {
|
||||
keypad := model2.KeypadSize{AttrsPerKey: 10, NumbOfKeys: 5}
|
||||
func TestCustomer(t *testing.T) {
|
||||
testNewCustomerAttributes(t)
|
||||
testCustomerValidKeyEntry(t)
|
||||
testCustomerIsValidNKode(t)
|
||||
}
|
||||
|
||||
func testNewCustomerAttributes(t *testing.T) {
|
||||
keypad := m.KeypadSize{AttrsPerKey: 10, NumbOfKeys: 5}
|
||||
_, nil := NewCustomerAttributes(keypad)
|
||||
assert.NoError(t, nil)
|
||||
}
|
||||
|
||||
func TestCustomer_ValidKeyEntry(t *testing.T) {
|
||||
keypadSize := model2.KeypadSize{AttrsPerKey: 10, NumbOfKeys: 7}
|
||||
nkodePolicy := model2.NewDefaultNKodePolicy()
|
||||
func testCustomerValidKeyEntry(t *testing.T) {
|
||||
keypadSize := m.KeypadSize{AttrsPerKey: 10, NumbOfKeys: 7}
|
||||
nkodePolicy := m.NewDefaultNKodePolicy()
|
||||
customer, err := NewCustomer(keypadSize, nkodePolicy)
|
||||
assert.NoError(t, err)
|
||||
newUserInterface, err := NewUserInterface(customer.Attributes.KeypadSize)
|
||||
assert.NoError(t, err)
|
||||
username := "testing123"
|
||||
username := m.Username("testing123")
|
||||
passcodeIdx := []int{0, 1, 2, 3}
|
||||
err = customer.AddNewUser(username, passcodeIdx, *newUserInterface)
|
||||
assert.NoError(t, err)
|
||||
@@ -35,14 +41,14 @@ func TestCustomer_ValidKeyEntry(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCustomer_IsValidNKode(t *testing.T) {
|
||||
keypadSize := model2.KeypadSize{AttrsPerKey: 10, NumbOfKeys: 7}
|
||||
nkodePolicy := model2.NewDefaultNKodePolicy()
|
||||
func testCustomerIsValidNKode(t *testing.T) {
|
||||
keypadSize := m.KeypadSize{AttrsPerKey: 10, NumbOfKeys: 7}
|
||||
nkodePolicy := m.NewDefaultNKodePolicy()
|
||||
customer, err := NewCustomer(keypadSize, nkodePolicy)
|
||||
assert.NoError(t, err)
|
||||
newUserInterface, err := NewUserInterface(customer.Attributes.KeypadSize)
|
||||
assert.NoError(t, err)
|
||||
username := "testing123"
|
||||
username := m.Username("testing123")
|
||||
passcodeIdx := []int{0, 1, 2, 3}
|
||||
err = customer.AddNewUser(username, passcodeIdx, *newUserInterface)
|
||||
assert.NoError(t, err)
|
||||
|
||||
@@ -3,23 +3,22 @@ package nkode
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"go-nkode/core/model"
|
||||
m "go-nkode/core/model"
|
||||
)
|
||||
|
||||
type NKodeAPI struct {
|
||||
Customers map[uuid.UUID]Customer
|
||||
SignupSessions map[uuid.UUID]UserSignSession
|
||||
type NKodeInMemory struct {
|
||||
Customers map[m.CustomerId]Customer
|
||||
SignupSessions map[m.SessionId]UserSignSession
|
||||
}
|
||||
|
||||
func NewNKodeAPI() NKodeAPI {
|
||||
return NKodeAPI{
|
||||
Customers: make(map[uuid.UUID]Customer),
|
||||
SignupSessions: make(map[uuid.UUID]UserSignSession),
|
||||
func NewNKodeInMemory() NKodeInMemory {
|
||||
return NKodeInMemory{
|
||||
Customers: make(map[m.CustomerId]Customer),
|
||||
SignupSessions: make(map[m.SessionId]UserSignSession),
|
||||
}
|
||||
}
|
||||
|
||||
func (n *NKodeAPI) CreateNewCustomer(keypadSize model.KeypadSize, nkodePolicy model.NKodePolicy) (*uuid.UUID, error) {
|
||||
func (n *NKodeInMemory) CreateNewCustomer(keypadSize m.KeypadSize, nkodePolicy m.NKodePolicy) (*m.CustomerId, error) {
|
||||
newCustomer, err := NewCustomer(keypadSize, nkodePolicy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -28,29 +27,33 @@ func (n *NKodeAPI) CreateNewCustomer(keypadSize model.KeypadSize, nkodePolicy mo
|
||||
return &newCustomer.CustomerId, nil
|
||||
}
|
||||
|
||||
func (n *NKodeAPI) GenerateSignupInterface(customerId uuid.UUID) (*uuid.UUID, []int, error) {
|
||||
func (n *NKodeInMemory) GenerateSignupInterface(customerId m.CustomerId) (*m.GenerateSignupInterfaceResp, error) {
|
||||
customer, exists := n.Customers[customerId]
|
||||
if !exists {
|
||||
return nil, nil, errors.New(fmt.Sprintf("customer doesnt exists: %s", customerId.String()))
|
||||
return nil, errors.New(fmt.Sprintf("customer doesnt exists: %s", customerId))
|
||||
}
|
||||
|
||||
signupSession, err := NewSignupSession(customer.Attributes.KeypadSize, customer.CustomerId)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
n.SignupSessions[signupSession.SessionId] = *signupSession
|
||||
return &signupSession.SessionId, signupSession.SetInterface, nil
|
||||
resp := m.GenerateSignupInterfaceResp{
|
||||
UserInterface: signupSession.SetIdxInterface,
|
||||
SessionId: signupSession.SessionId,
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (n *NKodeAPI) SetNKode(username string, customerId uuid.UUID, keySelection []int, sessionId uuid.UUID) ([]int, error) {
|
||||
func (n *NKodeInMemory) SetNKode(username m.Username, customerId m.CustomerId, sessionId m.SessionId, keySelection m.KeySelection) (m.IdxInterface, error) {
|
||||
_, exists := n.Customers[customerId]
|
||||
if !exists {
|
||||
return nil, errors.New(fmt.Sprintf("set nkode customer id does not exist %s", customerId.String()))
|
||||
return nil, errors.New(fmt.Sprintf("set nkode customer id does not exist %s", customerId))
|
||||
}
|
||||
|
||||
session, exists := n.SignupSessions[sessionId]
|
||||
if !exists {
|
||||
return nil, errors.New(fmt.Sprintf("session id does not exist %s", sessionId.String()))
|
||||
return nil, errors.New(fmt.Sprintf("session id does not exist %s", sessionId))
|
||||
}
|
||||
confirmInterface, err := session.SetUserNKode(username, keySelection)
|
||||
if err != nil {
|
||||
@@ -60,10 +63,10 @@ func (n *NKodeAPI) SetNKode(username string, customerId uuid.UUID, keySelection
|
||||
return confirmInterface, nil
|
||||
}
|
||||
|
||||
func (n *NKodeAPI) ConfirmNKode(customerId uuid.UUID, keySelection []int, sessionId uuid.UUID) error {
|
||||
func (n *NKodeInMemory) ConfirmNKode(customerId m.CustomerId, sessionId m.SessionId, keySelection m.KeySelection) error {
|
||||
session, exists := n.SignupSessions[sessionId]
|
||||
if !exists {
|
||||
return errors.New(fmt.Sprintf("session id does not exist %s", sessionId.String()))
|
||||
return errors.New(fmt.Sprintf("session id does not exist %s", sessionId))
|
||||
}
|
||||
customer, exists := n.Customers[customerId]
|
||||
passcode, err := session.DeducePasscode(keySelection)
|
||||
@@ -74,7 +77,7 @@ func (n *NKodeAPI) ConfirmNKode(customerId uuid.UUID, keySelection []int, sessio
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = customer.AddNewUser(session.Username, passcode, session.LoginInterface)
|
||||
err = customer.AddNewUser(session.Username, passcode, session.LoginUserInterface)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -83,7 +86,7 @@ func (n *NKodeAPI) ConfirmNKode(customerId uuid.UUID, keySelection []int, sessio
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *NKodeAPI) GetLoginInterface(username string, customerId uuid.UUID) ([]int, error) {
|
||||
func (n *NKodeInMemory) GetLoginInterface(username m.Username, customerId m.CustomerId) (m.IdxInterface, error) {
|
||||
err := n.customerUserExists(username, customerId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -97,10 +100,10 @@ func (n *NKodeAPI) GetLoginInterface(username string, customerId uuid.UUID) ([]i
|
||||
return user.Interface.IdxInterface, nil
|
||||
}
|
||||
|
||||
func (n *NKodeAPI) Login(customerId uuid.UUID, username string, keySelection []int) error {
|
||||
func (n *NKodeInMemory) Login(customerId m.CustomerId, username m.Username, keySelection m.KeySelection) error {
|
||||
customer, exists := n.Customers[customerId]
|
||||
if !exists {
|
||||
return errors.New(fmt.Sprintf("customer %s does not exist", customerId.String()))
|
||||
return errors.New(fmt.Sprintf("customer %s does not exist", customerId))
|
||||
}
|
||||
user, exists := customer.Users[username]
|
||||
if !exists {
|
||||
@@ -119,10 +122,10 @@ func (n *NKodeAPI) Login(customerId uuid.UUID, username string, keySelection []i
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *NKodeAPI) customerUserExists(username string, customerId uuid.UUID) error {
|
||||
func (n *NKodeInMemory) customerUserExists(username m.Username, customerId m.CustomerId) error {
|
||||
customer, exists := n.Customers[customerId]
|
||||
if !exists {
|
||||
return errors.New(fmt.Sprintf("customer %s does not exist", customerId.String()))
|
||||
return errors.New(fmt.Sprintf("customer %s does not exist", customerId))
|
||||
}
|
||||
_, exists = customer.Users[username]
|
||||
if !exists {
|
||||
@@ -131,10 +134,10 @@ func (n *NKodeAPI) customerUserExists(username string, customerId uuid.UUID) err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *NKodeAPI) RenewAttributes(customerId uuid.UUID) error {
|
||||
func (n *NKodeInMemory) RenewAttributes(customerId m.CustomerId) error {
|
||||
customer, exists := n.Customers[customerId]
|
||||
if !exists {
|
||||
return errors.New(fmt.Sprintf("customer %s does not exist", customerId.String()))
|
||||
return errors.New(fmt.Sprintf("customer %s does not exist", customerId))
|
||||
}
|
||||
return customer.RenewKeys()
|
||||
}
|
||||
@@ -2,32 +2,34 @@ package nkode
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go-nkode/core/model"
|
||||
m "go-nkode/core/model"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNKodeAPI(t *testing.T) {
|
||||
for idx := 0; idx < 10; idx++ {
|
||||
username := "test_username"
|
||||
username := m.Username("test_username")
|
||||
passcodeLen := 4
|
||||
nkodePolicy := model.NewDefaultNKodePolicy()
|
||||
keypadSize := model.KeypadSize{AttrsPerKey: 10, NumbOfKeys: 3}
|
||||
nkodeApi := NewNKodeAPI()
|
||||
nkodePolicy := m.NewDefaultNKodePolicy()
|
||||
keypadSize := m.KeypadSize{AttrsPerKey: 10, NumbOfKeys: 3}
|
||||
nkodeApi := NewNKodeInMemory()
|
||||
customerId, err := nkodeApi.CreateNewCustomer(keypadSize, nkodePolicy)
|
||||
assert.NoError(t, err)
|
||||
sessionId, setInterface, err := nkodeApi.GenerateSignupInterface(*customerId)
|
||||
signupResponse, err := nkodeApi.GenerateSignupInterface(*customerId)
|
||||
assert.NoError(t, err)
|
||||
keypadSize = model.KeypadSize{AttrsPerKey: 3, NumbOfKeys: 3}
|
||||
setInterface := signupResponse.UserInterface
|
||||
sessionId := signupResponse.SessionId
|
||||
keypadSize = m.KeypadSize{AttrsPerKey: 3, NumbOfKeys: 3}
|
||||
userPasscode := setInterface[:passcodeLen]
|
||||
setKeySelect, err := SelectKeyByAttrIdx(setInterface, userPasscode, keypadSize)
|
||||
assert.NoError(t, err)
|
||||
confirmInterface, err := nkodeApi.SetNKode(username, *customerId, setKeySelect, *sessionId)
|
||||
confirmInterface, err := nkodeApi.SetNKode(username, *customerId, sessionId, setKeySelect)
|
||||
assert.NoError(t, err)
|
||||
confirmKeySelect, err := SelectKeyByAttrIdx(confirmInterface, userPasscode, keypadSize)
|
||||
err = nkodeApi.ConfirmNKode(*customerId, confirmKeySelect, *sessionId)
|
||||
err = nkodeApi.ConfirmNKode(*customerId, sessionId, confirmKeySelect)
|
||||
assert.NoError(t, err)
|
||||
|
||||
keypadSize = model.KeypadSize{AttrsPerKey: 10, NumbOfKeys: 3}
|
||||
keypadSize = m.KeypadSize{AttrsPerKey: 10, NumbOfKeys: 3}
|
||||
loginInterface, err := nkodeApi.GetLoginInterface(username, *customerId)
|
||||
assert.NoError(t, err)
|
||||
loginKeySelection, err := SelectKeyByAttrIdx(loginInterface, userPasscode, keypadSize)
|
||||
1
core/nkode/nkode_sqlite.go
Normal file
1
core/nkode/nkode_sqlite.go
Normal file
@@ -0,0 +1 @@
|
||||
package nkode
|
||||
@@ -3,11 +3,11 @@ package nkode
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"go-nkode/core/model"
|
||||
m "go-nkode/core/model"
|
||||
"go-nkode/util"
|
||||
)
|
||||
|
||||
func SelectKeyByAttrIdx(interfaceUser []int, passcodeIdxs []int, keypadSize model.KeypadSize) ([]int, error) {
|
||||
func SelectKeyByAttrIdx(interfaceUser []int, passcodeIdxs []int, keypadSize m.KeypadSize) ([]int, error) {
|
||||
selectedKeys := make([]int, len(passcodeIdxs))
|
||||
for idx := range passcodeIdxs {
|
||||
attrIdx := util.IndexOf[int](interfaceUser, passcodeIdxs[idx])
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Username string
|
||||
Username m.Username
|
||||
EncipheredPasscode m.EncipheredNKode
|
||||
UserKeys UserCipherKeys
|
||||
Interface UserInterface
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go-nkode/core/model"
|
||||
m "go-nkode/core/model"
|
||||
"go-nkode/util"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
@@ -18,7 +18,7 @@ type UserCipherKeys struct {
|
||||
MaxNKodeLen int
|
||||
}
|
||||
|
||||
func NewUserCipherKeys(keypadSize model.KeypadSize, setVals []uint64, maxNKodeLen int) (*UserCipherKeys, error) {
|
||||
func NewUserCipherKeys(keypadSize m.KeypadSize, setVals []uint64, maxNKodeLen int) (*UserCipherKeys, error) {
|
||||
if len(setVals) != keypadSize.AttrsPerKey {
|
||||
return nil, errors.New(fmt.Sprintf("setVals len != attrsPerKey, %d, %d", len(setVals), keypadSize.AttrsPerKey))
|
||||
}
|
||||
@@ -165,7 +165,7 @@ func (u *UserCipherKeys) DecipherMask(mask string, setVals []uint64, passcodeLen
|
||||
return passcodeSet, nil
|
||||
}
|
||||
|
||||
func (u *UserCipherKeys) EncipherNKode(passcodeAttrIdx []int, customerAttrs CustomerAttributes) (*model.EncipheredNKode, error) {
|
||||
func (u *UserCipherKeys) EncipherNKode(passcodeAttrIdx []int, customerAttrs CustomerAttributes) (*m.EncipheredNKode, error) {
|
||||
code, err := u.EncipherSaltHashCode(passcodeAttrIdx, customerAttrs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -180,7 +180,7 @@ func (u *UserCipherKeys) EncipherNKode(passcodeAttrIdx []int, customerAttrs Cust
|
||||
}
|
||||
}
|
||||
mask, err := u.EncipherMask(passcodeSet, customerAttrs)
|
||||
encipheredCode := model.EncipheredNKode{
|
||||
encipheredCode := m.EncipheredNKode{
|
||||
Code: code,
|
||||
Mask: mask,
|
||||
}
|
||||
|
||||
@@ -3,17 +3,17 @@ package nkode
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"go-nkode/core/model"
|
||||
m "go-nkode/core/model"
|
||||
"go-nkode/hashset"
|
||||
"go-nkode/util"
|
||||
)
|
||||
|
||||
type UserInterface struct {
|
||||
IdxInterface []int
|
||||
KeypadSize model.KeypadSize
|
||||
IdxInterface m.IdxInterface
|
||||
KeypadSize m.KeypadSize
|
||||
}
|
||||
|
||||
func NewUserInterface(keypadSize model.KeypadSize) (*UserInterface, error) {
|
||||
func NewUserInterface(keypadSize m.KeypadSize) (*UserInterface, error) {
|
||||
idxInterface := util.IdentityArray(keypadSize.TotalAttrs())
|
||||
userInterface := UserInterface{
|
||||
IdxInterface: idxInterface,
|
||||
|
||||
@@ -4,61 +4,61 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"go-nkode/core/model"
|
||||
m "go-nkode/core/model"
|
||||
"go-nkode/hashset"
|
||||
py_builtin "go-nkode/py-builtin"
|
||||
py "go-nkode/py-builtin"
|
||||
"go-nkode/util"
|
||||
)
|
||||
|
||||
type UserSignSession struct {
|
||||
SessionId uuid.UUID
|
||||
CustomerId uuid.UUID
|
||||
LoginInterface UserInterface
|
||||
KeypadSize model.KeypadSize
|
||||
SetInterface []int
|
||||
ConfirmInterface []int
|
||||
SetKeyEntry []int
|
||||
Username string
|
||||
SessionId m.SessionId
|
||||
CustomerId m.CustomerId
|
||||
LoginUserInterface UserInterface
|
||||
Keypad m.KeypadSize
|
||||
SetIdxInterface m.IdxInterface
|
||||
ConfirmIdxInterface m.IdxInterface
|
||||
SetKeySelection m.KeySelection
|
||||
Username m.Username
|
||||
}
|
||||
|
||||
func NewSignupSession(keypadSize model.KeypadSize, customerId uuid.UUID) (*UserSignSession, error) {
|
||||
func NewSignupSession(keypadSize m.KeypadSize, customerId m.CustomerId) (*UserSignSession, error) {
|
||||
loginInterface, err := NewUserInterface(keypadSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
signupInter, err := signupInterface(*loginInterface)
|
||||
session := UserSignSession{
|
||||
SessionId: uuid.New(),
|
||||
CustomerId: customerId,
|
||||
LoginInterface: *loginInterface,
|
||||
SetInterface: signupInter.IdxInterface,
|
||||
ConfirmInterface: nil,
|
||||
SetKeyEntry: nil,
|
||||
Username: "",
|
||||
KeypadSize: signupInter.KeypadSize,
|
||||
SessionId: m.SessionId(uuid.New()),
|
||||
CustomerId: customerId,
|
||||
LoginUserInterface: *loginInterface,
|
||||
SetIdxInterface: signupInter.IdxInterface,
|
||||
ConfirmIdxInterface: nil,
|
||||
SetKeySelection: nil,
|
||||
Username: "",
|
||||
Keypad: signupInter.KeypadSize,
|
||||
}
|
||||
|
||||
return &session, nil
|
||||
}
|
||||
|
||||
func (s *UserSignSession) DeducePasscode(confirmKeyEntry []int) ([]int, error) {
|
||||
validEntry := py_builtin.All[int](confirmKeyEntry, func(i int) bool {
|
||||
return 0 <= i && i < s.LoginInterface.KeypadSize.NumbOfKeys
|
||||
func (s *UserSignSession) DeducePasscode(confirmKeyEntry m.KeySelection) ([]int, error) {
|
||||
validEntry := py.All[int](confirmKeyEntry, func(i int) bool {
|
||||
return 0 <= i && i < s.LoginUserInterface.KeypadSize.NumbOfKeys
|
||||
})
|
||||
|
||||
if !validEntry {
|
||||
return nil, errors.New(fmt.Sprintf("Invalid Key entry. One or more key index: %#v, not in range 0-%d", confirmKeyEntry, s.LoginInterface.KeypadSize.NumbOfKeys))
|
||||
return nil, errors.New(fmt.Sprintf("Invalid Key entry. One or more key index: %#v, not in range 0-%d", confirmKeyEntry, s.LoginUserInterface.KeypadSize.NumbOfKeys))
|
||||
}
|
||||
|
||||
if s.SetInterface == nil {
|
||||
if s.SetIdxInterface == nil {
|
||||
return nil, errors.New("signup session set interface is nil")
|
||||
}
|
||||
|
||||
if s.ConfirmInterface == nil {
|
||||
if s.ConfirmIdxInterface == nil {
|
||||
return nil, errors.New("signup session confirm interface is nil")
|
||||
}
|
||||
|
||||
if s.SetKeyEntry == nil {
|
||||
if s.SetKeySelection == nil {
|
||||
return nil, errors.New("signup session set key entry is nil")
|
||||
}
|
||||
|
||||
@@ -66,16 +66,16 @@ func (s *UserSignSession) DeducePasscode(confirmKeyEntry []int) ([]int, error) {
|
||||
return nil, errors.New("signup session username is nil")
|
||||
}
|
||||
|
||||
if len(confirmKeyEntry) != len(s.SetKeyEntry) {
|
||||
return nil, errors.New(fmt.Sprintf("confirm and set key entry lenght mismatch %d != %d", len(confirmKeyEntry), len(s.SetKeyEntry)))
|
||||
if len(confirmKeyEntry) != len(s.SetKeySelection) {
|
||||
return nil, errors.New(fmt.Sprintf("confirm and set key entry lenght mismatch %d != %d", len(confirmKeyEntry), len(s.SetKeySelection)))
|
||||
}
|
||||
|
||||
passcodeLen := len(confirmKeyEntry)
|
||||
setKeyVals, err := s.getSelectedKeyVals(s.SetKeyEntry, s.SetInterface)
|
||||
setKeyVals, err := s.getSelectedKeyVals(s.SetKeySelection, s.SetIdxInterface)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
confirmKeyVals, err := s.getSelectedKeyVals(confirmKeyEntry, s.ConfirmInterface)
|
||||
confirmKeyVals, err := s.getSelectedKeyVals(confirmKeyEntry, s.ConfirmIdxInterface)
|
||||
passcode := make([]int, passcodeLen)
|
||||
|
||||
for idx := 0; idx < passcodeLen; idx++ {
|
||||
@@ -94,28 +94,28 @@ func (s *UserSignSession) DeducePasscode(confirmKeyEntry []int) ([]int, error) {
|
||||
return passcode, nil
|
||||
}
|
||||
|
||||
func (s *UserSignSession) SetUserNKode(username string, keySelection []int) ([]int, error) {
|
||||
validKeySelection := py_builtin.All[int](keySelection, func(i int) bool {
|
||||
return 0 <= i && i < s.KeypadSize.NumbOfKeys
|
||||
func (s *UserSignSession) SetUserNKode(username m.Username, keySelection m.KeySelection) (m.IdxInterface, error) {
|
||||
validKeySelection := py.All[int](keySelection, func(i int) bool {
|
||||
return 0 <= i && i < s.Keypad.NumbOfKeys
|
||||
})
|
||||
if !validKeySelection {
|
||||
return nil, errors.New(fmt.Sprintf("one or key selection is out of range 0-%d", s.KeypadSize.NumbOfKeys-1))
|
||||
return nil, errors.New(fmt.Sprintf("one or key selection is out of range 0-%d", s.Keypad.NumbOfKeys-1))
|
||||
}
|
||||
|
||||
s.SetKeyEntry = keySelection
|
||||
s.SetKeySelection = keySelection
|
||||
s.Username = username
|
||||
|
||||
setInterface := UserInterface{IdxInterface: s.SetInterface, KeypadSize: s.KeypadSize}
|
||||
setInterface := UserInterface{IdxInterface: s.SetIdxInterface, KeypadSize: s.Keypad}
|
||||
err := setInterface.DisperseInterface()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.ConfirmInterface = setInterface.IdxInterface
|
||||
return s.ConfirmInterface, nil
|
||||
s.ConfirmIdxInterface = setInterface.IdxInterface
|
||||
return s.ConfirmIdxInterface, nil
|
||||
}
|
||||
|
||||
func (s *UserSignSession) getSelectedKeyVals(keySelections []int, userInterface []int) ([][]int, error) {
|
||||
keypadInterface, err := util.ListToMatrix(userInterface, s.KeypadSize.AttrsPerKey)
|
||||
func (s *UserSignSession) getSelectedKeyVals(keySelections m.KeySelection, userInterface []int) ([][]int, error) {
|
||||
keypadInterface, err := util.ListToMatrix(userInterface, s.Keypad.AttrsPerKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -155,7 +155,7 @@ func signupInterface(baseUserInterface UserInterface) (*UserInterface, error) {
|
||||
}
|
||||
signupUserInterface := UserInterface{
|
||||
IdxInterface: util.MatrixToList(attrSetView),
|
||||
KeypadSize: model.KeypadSize{
|
||||
KeypadSize: m.KeypadSize{
|
||||
AttrsPerKey: numbOfKeys,
|
||||
NumbOfKeys: numbOfKeys,
|
||||
},
|
||||
|
||||
@@ -2,13 +2,13 @@ package nkode
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go-nkode/core/model"
|
||||
py_builtin "go-nkode/py-builtin"
|
||||
m "go-nkode/core/model"
|
||||
py "go-nkode/py-builtin"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUserCipherKeys_EncipherSaltHashCode(t *testing.T) {
|
||||
keypadSize := model.KeypadSize{AttrsPerKey: 10, NumbOfKeys: 5}
|
||||
keypadSize := m.KeypadSize{AttrsPerKey: 10, NumbOfKeys: 5}
|
||||
maxNKodeLen := 10
|
||||
customerAttrs, err := NewCustomerAttributes(keypadSize)
|
||||
assert.NoError(t, err)
|
||||
@@ -22,7 +22,7 @@ func TestUserCipherKeys_EncipherSaltHashCode(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUserCipherKeys_EncipherDecipherMask(t *testing.T) {
|
||||
keypadSize := model.KeypadSize{AttrsPerKey: 10, NumbOfKeys: 5}
|
||||
keypadSize := m.KeypadSize{AttrsPerKey: 10, NumbOfKeys: 5}
|
||||
maxNKodeLen := 10
|
||||
|
||||
customerAttrs, err := NewCustomerAttributes(keypadSize)
|
||||
@@ -48,7 +48,7 @@ func TestUserCipherKeys_EncipherDecipherMask(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUserInterface_RandomShuffle(t *testing.T) {
|
||||
keypadSize := model.KeypadSize{
|
||||
keypadSize := m.KeypadSize{
|
||||
AttrsPerKey: 10,
|
||||
NumbOfKeys: 5,
|
||||
}
|
||||
@@ -73,7 +73,7 @@ func TestUserInterface_RandomShuffle(t *testing.T) {
|
||||
func TestUserInterface_DisperseInterface(t *testing.T) {
|
||||
|
||||
for idx := 0; idx < 10000; idx++ {
|
||||
keypadSize := model.KeypadSize{AttrsPerKey: 7, NumbOfKeys: 10}
|
||||
keypadSize := m.KeypadSize{AttrsPerKey: 7, NumbOfKeys: 10}
|
||||
|
||||
userInterface, err := NewUserInterface(keypadSize)
|
||||
assert.NoError(t, err)
|
||||
@@ -92,7 +92,7 @@ func TestUserInterface_DisperseInterface(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUserInterface_PartialInterfaceShuffle(t *testing.T) {
|
||||
keypadSize := model.KeypadSize{AttrsPerKey: 7, NumbOfKeys: 10}
|
||||
keypadSize := m.KeypadSize{AttrsPerKey: 7, NumbOfKeys: 10}
|
||||
userInterface, err := NewUserInterface(keypadSize)
|
||||
assert.NoError(t, err)
|
||||
preShuffle := userInterface.IdxInterface
|
||||
@@ -105,12 +105,12 @@ func TestUserInterface_PartialInterfaceShuffle(t *testing.T) {
|
||||
shuffleCompare[idx] = val == postShuffle[idx]
|
||||
}
|
||||
|
||||
allTrue := py_builtin.All[bool](shuffleCompare, func(n bool) bool {
|
||||
allTrue := py.All[bool](shuffleCompare, func(n bool) bool {
|
||||
return n == true
|
||||
})
|
||||
assert.False(t, allTrue)
|
||||
|
||||
allFalse := py_builtin.All[bool](shuffleCompare, func(n bool) bool {
|
||||
allFalse := py.All[bool](shuffleCompare, func(n bool) bool {
|
||||
return n == false
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user