add cors; add username to signup interface
This commit is contained in:
@@ -2,6 +2,7 @@ package model
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"go-nkode/core/api"
|
"go-nkode/core/api"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -13,8 +14,8 @@ type NKodeHandler struct {
|
|||||||
|
|
||||||
func (h *NKodeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *NKodeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
//case api.CreateNewCustomer:
|
case api.CreateNewCustomer:
|
||||||
// h.CreateNewCustomerHandler(w, r)
|
h.CreateNewCustomerHandler(w, r)
|
||||||
case api.GenerateSignupInterface:
|
case api.GenerateSignupInterface:
|
||||||
h.GenerateSignupInterfaceHandler(w, r)
|
h.GenerateSignupInterfaceHandler(w, r)
|
||||||
case api.SetNKode:
|
case api.SetNKode:
|
||||||
@@ -43,13 +44,13 @@ func (h *NKodeHandler) CreateNewCustomerHandler(w http.ResponseWriter, r *http.R
|
|||||||
err := decodeJson(w, r, &customerPost)
|
err := decodeJson(w, r, &customerPost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
customerId, err := h.Api.CreateNewCustomer(customerPost.NKodePolicy, nil)
|
customerId, err := h.Api.CreateNewCustomer(customerPost.NKodePolicy, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
respBody := CreateNewCustomerResp{
|
respBody := CreateNewCustomerResp{
|
||||||
@@ -58,13 +59,13 @@ func (h *NKodeHandler) CreateNewCustomerHandler(w http.ResponseWriter, r *http.R
|
|||||||
respBytes, err := json.Marshal(respBody)
|
respBytes, err := json.Marshal(respBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err = w.Write(respBytes)
|
_, err = w.Write(respBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,7 +82,7 @@ func (h *NKodeHandler) GenerateSignupInterfaceHandler(w http.ResponseWriter, r *
|
|||||||
err := decodeJson(w, r, &signupPost)
|
err := decodeJson(w, r, &signupPost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
kp := KeypadDimension{
|
kp := KeypadDimension{
|
||||||
@@ -91,26 +92,26 @@ func (h *NKodeHandler) GenerateSignupInterfaceHandler(w http.ResponseWriter, r *
|
|||||||
err = kp.IsValidKeypadDimension()
|
err = kp.IsValidKeypadDimension()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
keypadSizeOutOfRange(w)
|
keypadSizeOutOfRange(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
resp, err := h.Api.GenerateSignupInterface(signupPost.CustomerId, kp)
|
resp, err := h.Api.GenerateSignupInterface(signupPost.Username, signupPost.CustomerId, kp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
respBytes, err := json.Marshal(resp)
|
respBytes, err := json.Marshal(resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = w.Write(respBytes)
|
_, err = w.Write(respBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,13 +127,13 @@ func (h *NKodeHandler) SetNKodeHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
err := decodeJson(w, r, &setNKodePost)
|
err := decodeJson(w, r, &setNKodePost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
confirmInterface, err := h.Api.SetNKode(setNKodePost.Username, setNKodePost.CustomerId, setNKodePost.SessionId, setNKodePost.KeySelection)
|
confirmInterface, err := h.Api.SetNKode(setNKodePost.CustomerId, setNKodePost.SessionId, setNKodePost.KeySelection)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
respBody := SetNKodeResp{UserInterface: confirmInterface}
|
respBody := SetNKodeResp{UserInterface: confirmInterface}
|
||||||
@@ -140,14 +141,14 @@ func (h *NKodeHandler) SetNKodeHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
respBytes, err := json.Marshal(respBody)
|
respBytes, err := json.Marshal(respBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = w.Write(respBytes)
|
_, err = w.Write(respBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,13 +165,13 @@ func (h *NKodeHandler) ConfirmNKodeHandler(w http.ResponseWriter, r *http.Reques
|
|||||||
err := decodeJson(w, r, &confirmNKodePost)
|
err := decodeJson(w, r, &confirmNKodePost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = h.Api.ConfirmNKode(confirmNKodePost.CustomerId, confirmNKodePost.SessionId, confirmNKodePost.KeySelection)
|
err = h.Api.ConfirmNKode(confirmNKodePost.CustomerId, confirmNKodePost.SessionId, confirmNKodePost.KeySelection)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,13 +188,13 @@ func (h *NKodeHandler) GetLoginInterfaceHandler(w http.ResponseWriter, r *http.R
|
|||||||
err := decodeJson(w, r, &loginInterfacePost)
|
err := decodeJson(w, r, &loginInterfacePost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
loginInterface, err := h.Api.GetLoginInterface(loginInterfacePost.Username, loginInterfacePost.CustomerId)
|
loginInterface, err := h.Api.GetLoginInterface(loginInterfacePost.Username, loginInterfacePost.CustomerId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,13 +202,13 @@ func (h *NKodeHandler) GetLoginInterfaceHandler(w http.ResponseWriter, r *http.R
|
|||||||
respBytes, err := json.Marshal(respBody)
|
respBytes, err := json.Marshal(respBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err = w.Write(respBytes)
|
_, err = w.Write(respBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,13 +225,13 @@ func (h *NKodeHandler) LoginHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
err := decodeJson(w, r, &loginPost)
|
err := decodeJson(w, r, &loginPost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = h.Api.Login(loginPost.CustomerId, loginPost.Username, loginPost.KeySelection)
|
err = h.Api.Login(loginPost.CustomerId, loginPost.Username, loginPost.KeySelection)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,14 +248,14 @@ func (h *NKodeHandler) RenewAttributesHandler(w http.ResponseWriter, r *http.Req
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = h.Api.RenewAttributes(renewAttributesPost.CustomerId)
|
err = h.Api.RenewAttributes(renewAttributesPost.CustomerId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
log.Fatal(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,13 +263,16 @@ func (h *NKodeHandler) RenewAttributesHandler(w http.ResponseWriter, r *http.Req
|
|||||||
}
|
}
|
||||||
|
|
||||||
func decodeJson(w http.ResponseWriter, r *http.Request, post any) error {
|
func decodeJson(w http.ResponseWriter, r *http.Request, post any) error {
|
||||||
|
if r.Body == nil {
|
||||||
|
invalidJson(w)
|
||||||
|
return errors.New("invalid json")
|
||||||
|
}
|
||||||
err := json.NewDecoder(r.Body).Decode(&post)
|
err := json.NewDecoder(r.Body).Decode(&post)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
internalServerErrorHandler(w)
|
internalServerErrorHandler(w)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func internalServerErrorHandler(w http.ResponseWriter) {
|
func internalServerErrorHandler(w http.ResponseWriter) {
|
||||||
@@ -285,3 +289,8 @@ func keypadSizeOutOfRange(w http.ResponseWriter) {
|
|||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
w.Write([]byte("invalid keypad dimensions"))
|
w.Write([]byte("invalid keypad dimensions"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func invalidJson(w http.ResponseWriter) {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
w.Write([]byte("invalid json"))
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,10 +16,10 @@ type GenerateSignupInterfacePost struct {
|
|||||||
CustomerId CustomerId `json:"customer_id"`
|
CustomerId CustomerId `json:"customer_id"`
|
||||||
AttrsPerKey int `json:"attrs_per_key"`
|
AttrsPerKey int `json:"attrs_per_key"`
|
||||||
NumbOfKeys int `json:"numb_of_keys"`
|
NumbOfKeys int `json:"numb_of_keys"`
|
||||||
|
Username Username `json:"username"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SetNKodePost struct {
|
type SetNKodePost struct {
|
||||||
Username Username `json:"username"`
|
|
||||||
CustomerId CustomerId `json:"customer_id"`
|
CustomerId CustomerId `json:"customer_id"`
|
||||||
KeySelection KeySelection `json:"key_selection"`
|
KeySelection KeySelection `json:"key_selection"`
|
||||||
SessionId SessionId `json:"session_id"`
|
SessionId SessionId `json:"session_id"`
|
||||||
@@ -69,8 +69,8 @@ type IdxInterface []int
|
|||||||
|
|
||||||
type NKodeAPIInterface interface {
|
type NKodeAPIInterface interface {
|
||||||
CreateNewCustomer(NKodePolicy, *CustomerId) (*CustomerId, error)
|
CreateNewCustomer(NKodePolicy, *CustomerId) (*CustomerId, error)
|
||||||
GenerateSignupInterface(CustomerId, KeypadDimension) (*GenerateSignupInterfaceResp, error)
|
GenerateSignupInterface(Username, CustomerId, KeypadDimension) (*GenerateSignupInterfaceResp, error)
|
||||||
SetNKode(Username, CustomerId, SessionId, KeySelection) (IdxInterface, error)
|
SetNKode(CustomerId, SessionId, KeySelection) (IdxInterface, error)
|
||||||
ConfirmNKode(CustomerId, SessionId, KeySelection) error
|
ConfirmNKode(CustomerId, SessionId, KeySelection) error
|
||||||
GetLoginInterface(Username, CustomerId) (IdxInterface, error)
|
GetLoginInterface(Username, CustomerId) (IdxInterface, error)
|
||||||
Login(CustomerId, Username, KeySelection) error
|
Login(CustomerId, Username, KeySelection) error
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ func (n *NKodeAPI) CreateNewCustomer(nkodePolicy m.NKodePolicy, id *m.CustomerId
|
|||||||
return &newCustomer.Id, nil
|
return &newCustomer.Id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *NKodeAPI) GenerateSignupInterface(customerId m.CustomerId, kp m.KeypadDimension) (*m.GenerateSignupInterfaceResp, error) {
|
func (n *NKodeAPI) GenerateSignupInterface(username m.Username, customerId m.CustomerId, kp m.KeypadDimension) (*m.GenerateSignupInterfaceResp, error) {
|
||||||
signupSession, err := NewSignupSession(kp, customerId)
|
signupSession, err := NewSignupSession(username, kp, customerId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -47,7 +47,7 @@ func (n *NKodeAPI) GenerateSignupInterface(customerId m.CustomerId, kp m.KeypadD
|
|||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *NKodeAPI) SetNKode(username m.Username, customerId m.CustomerId, sessionId m.SessionId, keySelection m.KeySelection) (m.IdxInterface, error) {
|
func (n *NKodeAPI) SetNKode(customerId m.CustomerId, sessionId m.SessionId, keySelection m.KeySelection) (m.IdxInterface, error) {
|
||||||
_, err := n.Db.GetCustomer(customerId)
|
_, err := n.Db.GetCustomer(customerId)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -57,7 +57,7 @@ func (n *NKodeAPI) SetNKode(username m.Username, customerId m.CustomerId, sessio
|
|||||||
if !exists {
|
if !exists {
|
||||||
return nil, errors.New(fmt.Sprintf("session id does not exist %s", sessionId))
|
return nil, errors.New(fmt.Sprintf("session id does not exist %s", sessionId))
|
||||||
}
|
}
|
||||||
confirmInterface, err := session.SetUserNKode(username, keySelection)
|
confirmInterface, err := session.SetUserNKode(keySelection)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ func testNKodeAPI(t *testing.T, db DbAccessor) {
|
|||||||
nkodeApi := NewNKodeAPI(db)
|
nkodeApi := NewNKodeAPI(db)
|
||||||
customerId, err := nkodeApi.CreateNewCustomer(nkodePolicy, nil)
|
customerId, err := nkodeApi.CreateNewCustomer(nkodePolicy, nil)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
signupResponse, err := nkodeApi.GenerateSignupInterface(*customerId, keypadSize)
|
signupResponse, err := nkodeApi.GenerateSignupInterface(username, *customerId, keypadSize)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
setInterface := signupResponse.UserInterface
|
setInterface := signupResponse.UserInterface
|
||||||
sessionId := signupResponse.SessionId
|
sessionId := signupResponse.SessionId
|
||||||
@@ -41,7 +41,7 @@ func testNKodeAPI(t *testing.T, db DbAccessor) {
|
|||||||
userPasscode := setInterface[:passcodeLen]
|
userPasscode := setInterface[:passcodeLen]
|
||||||
setKeySelect, err := m.SelectKeyByAttrIdx(setInterface, userPasscode, keypadSize)
|
setKeySelect, err := m.SelectKeyByAttrIdx(setInterface, userPasscode, keypadSize)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
confirmInterface, err := nkodeApi.SetNKode(username, *customerId, sessionId, setKeySelect)
|
confirmInterface, err := nkodeApi.SetNKode(*customerId, sessionId, setKeySelect)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
confirmKeySelect, err := m.SelectKeyByAttrIdx(confirmInterface, userPasscode, keypadSize)
|
confirmKeySelect, err := m.SelectKeyByAttrIdx(confirmInterface, userPasscode, keypadSize)
|
||||||
err = nkodeApi.ConfirmNKode(*customerId, sessionId, confirmKeySelect)
|
err = nkodeApi.ConfirmNKode(*customerId, sessionId, confirmKeySelect)
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ type UserSignSession struct {
|
|||||||
Expire int
|
Expire int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSignupSession(kp m.KeypadDimension, customerId m.CustomerId) (*UserSignSession, error) {
|
func NewSignupSession(username m.Username, kp m.KeypadDimension, customerId m.CustomerId) (*UserSignSession, error) {
|
||||||
loginInterface, err := m.NewUserInterface(&kp)
|
loginInterface, err := m.NewUserInterface(&kp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -38,7 +38,7 @@ func NewSignupSession(kp m.KeypadDimension, customerId m.CustomerId) (*UserSignS
|
|||||||
SetIdxInterface: signupInterface.IdxInterface,
|
SetIdxInterface: signupInterface.IdxInterface,
|
||||||
ConfirmIdxInterface: nil,
|
ConfirmIdxInterface: nil,
|
||||||
SetKeySelection: nil,
|
SetKeySelection: nil,
|
||||||
Username: "",
|
Username: username,
|
||||||
Kp: kp,
|
Kp: kp,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +98,7 @@ func (s *UserSignSession) DeducePasscode(confirmKeyEntry m.KeySelection) ([]int,
|
|||||||
return passcode, nil
|
return passcode, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *UserSignSession) SetUserNKode(username m.Username, keySelection m.KeySelection) (m.IdxInterface, error) {
|
func (s *UserSignSession) SetUserNKode(keySelection m.KeySelection) (m.IdxInterface, error) {
|
||||||
validKeySelection := py.All[int](keySelection, func(i int) bool {
|
validKeySelection := py.All[int](keySelection, func(i int) bool {
|
||||||
return 0 <= i && i < s.Kp.NumbOfKeys
|
return 0 <= i && i < s.Kp.NumbOfKeys
|
||||||
})
|
})
|
||||||
@@ -107,7 +107,6 @@ func (s *UserSignSession) SetUserNKode(username m.Username, keySelection m.KeySe
|
|||||||
}
|
}
|
||||||
|
|
||||||
s.SetKeySelection = keySelection
|
s.SetKeySelection = keySelection
|
||||||
s.Username = username
|
|
||||||
setKp := s.SignupKeypad()
|
setKp := s.SignupKeypad()
|
||||||
setInterface := m.UserInterface{IdxInterface: s.SetIdxInterface, Kp: &setKp}
|
setInterface := m.UserInterface{IdxInterface: s.SetIdxInterface, Kp: &setKp}
|
||||||
err := setInterface.DisperseInterface()
|
err := setInterface.DisperseInterface()
|
||||||
|
|||||||
22
main.go
22
main.go
@@ -20,7 +20,7 @@ func main() {
|
|||||||
AddDefaultCustomer(nkodeApi)
|
AddDefaultCustomer(nkodeApi)
|
||||||
handler := model.NKodeHandler{Api: &nkodeApi}
|
handler := model.NKodeHandler{Api: &nkodeApi}
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
//mux.Handle(api.CreateNewCustomer, &handler)
|
mux.Handle(api.CreateNewCustomer, &handler)
|
||||||
mux.Handle(api.GenerateSignupInterface, &handler)
|
mux.Handle(api.GenerateSignupInterface, &handler)
|
||||||
mux.Handle(api.SetNKode, &handler)
|
mux.Handle(api.SetNKode, &handler)
|
||||||
mux.Handle(api.ConfirmNKode, &handler)
|
mux.Handle(api.ConfirmNKode, &handler)
|
||||||
@@ -28,7 +28,25 @@ func main() {
|
|||||||
mux.Handle(api.Login, &handler)
|
mux.Handle(api.Login, &handler)
|
||||||
mux.Handle(api.RenewAttributes, &handler)
|
mux.Handle(api.RenewAttributes, &handler)
|
||||||
fmt.Println("Running on localhost:8080...")
|
fmt.Println("Running on localhost:8080...")
|
||||||
log.Fatal(http.ListenAndServe("localhost:8080", mux))
|
log.Fatal(http.ListenAndServe("localhost:8080", corsMiddleware(mux)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func corsMiddleware(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Set the CORS headers
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||||||
|
|
||||||
|
// Handle preflight requests
|
||||||
|
if r.Method == http.MethodOptions {
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call the next handler
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddDefaultCustomer(api nkode.NKodeAPI) {
|
func AddDefaultCustomer(api nkode.NKodeAPI) {
|
||||||
|
|||||||
22
main_test.go
22
main_test.go
@@ -16,31 +16,38 @@ func TestApi(t *testing.T) {
|
|||||||
newCustomerBody := m.NewCustomerPost{
|
newCustomerBody := m.NewCustomerPost{
|
||||||
NKodePolicy: m.NewDefaultNKodePolicy(),
|
NKodePolicy: m.NewDefaultNKodePolicy(),
|
||||||
}
|
}
|
||||||
kp := m.KeypadDefault
|
kp := m.KeypadDimension{
|
||||||
|
AttrsPerKey: 14,
|
||||||
|
NumbOfKeys: 10,
|
||||||
|
}
|
||||||
var customerResp m.CreateNewCustomerResp
|
var customerResp m.CreateNewCustomerResp
|
||||||
testApiCall(t, base+api.CreateNewCustomer, newCustomerBody, &customerResp)
|
testApiCall(t, base+api.CreateNewCustomer, newCustomerBody, &customerResp)
|
||||||
|
|
||||||
signupInterfaceBody := m.GetLoginInterfacePost{CustomerId: customerResp.CustomerId}
|
username := m.Username("test_username")
|
||||||
|
signupInterfaceBody := m.GenerateSignupInterfacePost{
|
||||||
|
CustomerId: customerResp.CustomerId,
|
||||||
|
AttrsPerKey: kp.AttrsPerKey,
|
||||||
|
NumbOfKeys: kp.NumbOfKeys,
|
||||||
|
Username: username,
|
||||||
|
}
|
||||||
var signupInterfaceResp m.GenerateSignupInterfaceResp
|
var signupInterfaceResp m.GenerateSignupInterfaceResp
|
||||||
testApiCall(t, base+api.GenerateSignupInterface, signupInterfaceBody, &signupInterfaceResp)
|
testApiCall(t, base+api.GenerateSignupInterface, signupInterfaceBody, &signupInterfaceResp)
|
||||||
|
|
||||||
username := m.Username("test_username")
|
|
||||||
passcodeLen := 4
|
passcodeLen := 4
|
||||||
setInterface := signupInterfaceResp.UserInterface
|
setInterface := signupInterfaceResp.UserInterface
|
||||||
userPasscode := setInterface[:passcodeLen]
|
userPasscode := setInterface[:passcodeLen]
|
||||||
kp = m.KeypadDimension{NumbOfKeys: kp.NumbOfKeys, AttrsPerKey: kp.NumbOfKeys}
|
kp_set := m.KeypadDimension{NumbOfKeys: kp.NumbOfKeys, AttrsPerKey: kp.NumbOfKeys}
|
||||||
setKeySelection, err := m.SelectKeyByAttrIdx(setInterface, userPasscode, kp)
|
setKeySelection, err := m.SelectKeyByAttrIdx(setInterface, userPasscode, kp_set)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
setNKodeBody := m.SetNKodePost{
|
setNKodeBody := m.SetNKodePost{
|
||||||
CustomerId: customerResp.CustomerId,
|
CustomerId: customerResp.CustomerId,
|
||||||
Username: username,
|
|
||||||
SessionId: signupInterfaceResp.SessionId,
|
SessionId: signupInterfaceResp.SessionId,
|
||||||
KeySelection: setKeySelection,
|
KeySelection: setKeySelection,
|
||||||
}
|
}
|
||||||
var setNKodeResp m.SetNKodeResp
|
var setNKodeResp m.SetNKodeResp
|
||||||
testApiCall(t, base+api.SetNKode, setNKodeBody, &setNKodeResp)
|
testApiCall(t, base+api.SetNKode, setNKodeBody, &setNKodeResp)
|
||||||
confirmInterface := setNKodeResp.UserInterface
|
confirmInterface := setNKodeResp.UserInterface
|
||||||
confirmKeySelection, err := m.SelectKeyByAttrIdx(confirmInterface, userPasscode, kp)
|
confirmKeySelection, err := m.SelectKeyByAttrIdx(confirmInterface, userPasscode, kp_set)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
confirmNKodeBody := m.ConfirmNKodePost{
|
confirmNKodeBody := m.ConfirmNKodePost{
|
||||||
CustomerId: customerResp.CustomerId,
|
CustomerId: customerResp.CustomerId,
|
||||||
@@ -57,7 +64,6 @@ func TestApi(t *testing.T) {
|
|||||||
var loginInterfaceResp m.GetLoginInterfaceResp
|
var loginInterfaceResp m.GetLoginInterfaceResp
|
||||||
testApiCall(t, base+api.GetLoginInterface, loginInterfaceBody, &loginInterfaceResp)
|
testApiCall(t, base+api.GetLoginInterface, loginInterfaceBody, &loginInterfaceResp)
|
||||||
|
|
||||||
kp = m.KeypadDefault
|
|
||||||
loginKeySelection, err := m.SelectKeyByAttrIdx(loginInterfaceResp.UserInterface, userPasscode, kp)
|
loginKeySelection, err := m.SelectKeyByAttrIdx(loginInterfaceResp.UserInterface, userPasscode, kp)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
loginBody := m.LoginPost{
|
loginBody := m.LoginPost{
|
||||||
|
|||||||
Reference in New Issue
Block a user