From d13cec973d84c2a84ef70eaddd1c3d91bc7cc520 Mon Sep 17 00:00:00 2001
From: Benjamin Wenger <ben@elixxir.ioo>
Date: Mon, 27 Jun 2022 12:56:29 -0700
Subject: [PATCH] implemented basic API improvemt for auth callbacks

---
 cmd/callbacks.go | 14 +++++-----
 cmd/root.go      |  4 +--
 xxdk/e2e.go      | 66 ++++++++++++++++++++++++++++++++++++++----------
 3 files changed, 59 insertions(+), 25 deletions(-)

diff --git a/cmd/callbacks.go b/cmd/callbacks.go
index 139006223..d5d75a321 100644
--- a/cmd/callbacks.go
+++ b/cmd/callbacks.go
@@ -26,20 +26,18 @@ import (
 type authCallbacks struct {
 	autoConfirm bool
 	confCh      chan *id.ID
-	client      *xxdk.E2e
 }
 
-func makeAuthCallbacks(client *xxdk.E2e, autoConfirm bool) *authCallbacks {
+func makeAuthCallbacks(autoConfirm bool) *authCallbacks {
 	return &authCallbacks{
 		autoConfirm: autoConfirm,
 		confCh:      make(chan *id.ID, 10),
-		client:      client,
 	}
 }
 
 func (a *authCallbacks) Request(requestor contact.Contact,
 	receptionID receptionID.EphemeralIdentity,
-	round rounds.Round) {
+	round rounds.Round, client *xxdk.E2e) {
 	msg := fmt.Sprintf("Authentication channel request from: %s\n",
 		requestor.ID)
 	jww.INFO.Printf(msg)
@@ -48,9 +46,9 @@ func (a *authCallbacks) Request(requestor contact.Contact,
 		jww.INFO.Printf("Channel Request: %s",
 			requestor.ID)
 		if viper.GetBool("verify-sends") { // Verify message sends were successful
-			acceptChannelVerified(a.client, requestor.ID)
+			acceptChannelVerified(client, requestor.ID)
 		} else {
-			acceptChannel(a.client, requestor.ID)
+			acceptChannel(client, requestor.ID)
 		}
 
 		a.confCh <- requestor.ID
@@ -60,14 +58,14 @@ func (a *authCallbacks) Request(requestor contact.Contact,
 
 func (a *authCallbacks) Confirm(requestor contact.Contact,
 	receptionID receptionID.EphemeralIdentity,
-	round rounds.Round) {
+	round rounds.Round, client *xxdk.E2e) {
 	jww.INFO.Printf("Channel Confirmed: %s", requestor.ID)
 	a.confCh <- requestor.ID
 }
 
 func (a *authCallbacks) Reset(requestor contact.Contact,
 	receptionID receptionID.EphemeralIdentity,
-	round rounds.Round) {
+	round rounds.Round, client *xxdk.E2e) {
 	msg := fmt.Sprintf("Authentication channel reset from: %s\n",
 		requestor.ID)
 	jww.INFO.Printf(msg)
diff --git a/cmd/root.go b/cmd/root.go
index ac0e48756..0f0a42ad9 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -670,7 +670,7 @@ func initClient() *xxdk.E2e {
 		jww.FATAL.Panicf("%+v", err)
 	}
 
-	authCbs = makeAuthCallbacks(nil,
+	authCbs = makeAuthCallbacks(
 		viper.GetBool("unsafe-channel-creation"))
 
 	client, err := xxdk.LoginLegacy(baseclient, authCbs)
@@ -678,8 +678,6 @@ func initClient() *xxdk.E2e {
 		jww.FATAL.Panicf("%+v", err)
 	}
 
-	authCbs.client = client
-
 	if protoUser := viper.GetString("protoUserOut"); protoUser != "" {
 
 		jsonBytes, err := client.ConstructProtoUserFile()
diff --git a/xxdk/e2e.go b/xxdk/e2e.go
index 0a58b8af5..0bba070b0 100644
--- a/xxdk/e2e.go
+++ b/xxdk/e2e.go
@@ -12,10 +12,13 @@ import (
 	"github.com/pkg/errors"
 	jww "github.com/spf13/jwalterweatherman"
 	"gitlab.com/elixxir/client/auth"
+	"gitlab.com/elixxir/client/cmix/identity/receptionID"
+	"gitlab.com/elixxir/client/cmix/rounds"
 	"gitlab.com/elixxir/client/e2e"
 	"gitlab.com/elixxir/client/e2e/rekey"
 	"gitlab.com/elixxir/client/storage/user"
 	"gitlab.com/elixxir/client/storage/versioned"
+	"gitlab.com/elixxir/crypto/contact"
 	"gitlab.com/elixxir/crypto/cyclic"
 	"gitlab.com/elixxir/crypto/diffieHellman"
 	"gitlab.com/elixxir/ekv"
@@ -34,16 +37,25 @@ type E2e struct {
 	e2eIdentity ReceptionIdentity
 }
 
+type AuthCallbacks interface {
+	Request(partner contact.Contact, receptionID receptionID.EphemeralIdentity,
+		round rounds.Round, e2e *E2e)
+	Confirm(partner contact.Contact, receptionID receptionID.EphemeralIdentity,
+		round rounds.Round, e2e *E2e)
+	Reset(partner contact.Contact, receptionID receptionID.EphemeralIdentity,
+		round rounds.Round, e2e *E2e)
+}
+
 // Login creates a new E2e backed by the xxdk.Cmix persistent versioned.KV
 // It bundles a Cmix object with a ReceptionIdentity object
 // and initializes the auth.State and e2e.Handler objects
-func Login(client *Cmix, callbacks auth.Callbacks,
+func Login(client *Cmix, callbacks AuthCallbacks,
 	identity ReceptionIdentity) (m *E2e, err error) {
 	return login(client, callbacks, identity, client.GetStorage().GetKV())
 }
 
 // LoginEphemeral creates a new E2e backed by a totally ephemeral versioned.KV
-func LoginEphemeral(client *Cmix, callbacks auth.Callbacks,
+func LoginEphemeral(client *Cmix, callbacks AuthCallbacks,
 	identity ReceptionIdentity) (m *E2e, err error) {
 	return login(client, callbacks, identity, versioned.NewKV(ekv.MakeMemstore()))
 }
@@ -52,7 +64,7 @@ func LoginEphemeral(client *Cmix, callbacks auth.Callbacks,
 // Uses the pre-generated transmission ID used by xxdk.Cmix.
 // This function is designed to maintain backwards compatibility with previous
 // xx messenger designs and should not be used for other purposes.
-func LoginLegacy(client *Cmix, callbacks auth.Callbacks) (m *E2e, err error) {
+func LoginLegacy(client *Cmix, callbacks AuthCallbacks) (m *E2e, err error) {
 	m = &E2e{
 		Cmix:   client,
 		backup: &Container{},
@@ -70,9 +82,14 @@ func LoginLegacy(client *Cmix, callbacks auth.Callbacks) (m *E2e, err error) {
 			"the e2e processies")
 	}
 
+	acw := &authCallbacksAdapter{
+		ac:  callbacks,
+		e2e: m,
+	}
+
 	m.auth, err = auth.NewState(client.GetStorage().GetKV(), client.GetCmix(),
 		m.e2e, client.GetRng(), client.GetEventReporter(),
-		auth.GetDefaultParams(), callbacks, m.backup.TriggerBackup)
+		auth.GetDefaultParams(), acw, m.backup.TriggerBackup)
 	if err != nil {
 		return nil, err
 	}
@@ -131,7 +148,7 @@ func LoginWithNewBaseNDF_UNSAFE(storageDir string, password []byte,
 // JSON containing the cryptographic primitives. This is designed for
 // some specific deployment procedures and is generally unsafe.
 func LoginWithProtoClient(storageDir string, password []byte,
-	protoClientJSON []byte, newBaseNdf string,
+	protoClientJSON []byte, newBaseNdf string, callbacks AuthCallbacks,
 	params Params) (*E2e, error) {
 	jww.INFO.Printf("LoginWithProtoClient()")
 
@@ -166,18 +183,12 @@ func LoginWithProtoClient(storageDir string, password []byte,
 
 	c.network.AddIdentity(c.GetUser().ReceptionID, time.Time{}, true)
 
-	// FIXME: The callbacks need to be set, so I suppose we would need to
-	//        either set them via a special type or add them
-	//        to the login call?
-	if err != nil {
-		return nil, err
-	}
 	err = c.registerFollower()
 	if err != nil {
 		return nil, err
 	}
 
-	return Login(c, nil, ReceptionIdentity{
+	return Login(c, callbacks, ReceptionIdentity{
 		ID:            protoUser.ReceptionID,
 		RSAPrivatePem: protoUser.ReceptionRSA,
 		Salt:          protoUser.ReceptionSalt,
@@ -186,7 +197,7 @@ func LoginWithProtoClient(storageDir string, password []byte,
 }
 
 // login creates a new xxdk.E2e backed by the given versioned.KV
-func login(client *Cmix, callbacks auth.Callbacks,
+func login(client *Cmix, callbacks AuthCallbacks,
 	identity ReceptionIdentity, kv *versioned.KV) (m *E2e, err error) {
 
 	// Verify the passed-in ReceptionIdentity matches its properties
@@ -228,9 +239,14 @@ func login(client *Cmix, callbacks auth.Callbacks,
 			"the e2e processies")
 	}
 
+	acw := &authCallbacksAdapter{
+		ac:  callbacks,
+		e2e: m,
+	}
+
 	m.auth, err = auth.NewState(kv, client.GetCmix(),
 		m.e2e, client.GetRng(), client.GetEventReporter(),
-		auth.GetDefaultTemporaryParams(), callbacks, m.backup.TriggerBackup)
+		auth.GetDefaultTemporaryParams(), acw, m.backup.TriggerBackup)
 	if err != nil {
 		return nil, err
 	}
@@ -396,3 +412,25 @@ func (m *E2e) DeleteContact(partnerId *id.ID) error {
 
 	return nil
 }
+
+// Adapter type to make the xxdk auth callbacks type compatible with the
+// auth.callbacks
+type authCallbacksAdapter struct {
+	ac  AuthCallbacks
+	e2e *E2e
+}
+
+func (aca *authCallbacksAdapter) Request(partner contact.Contact,
+	receptionID receptionID.EphemeralIdentity, round rounds.Round) {
+	aca.ac.Request(partner, receptionID, round, aca.e2e)
+}
+
+func (aca *authCallbacksAdapter) Confirm(partner contact.Contact,
+	receptionID receptionID.EphemeralIdentity, round rounds.Round) {
+	aca.ac.Confirm(partner, receptionID, round, aca.e2e)
+}
+
+func (aca *authCallbacksAdapter) Reset(partner contact.Contact,
+	receptionID receptionID.EphemeralIdentity, round rounds.Round) {
+	aca.ac.Reset(partner, receptionID, round, aca.e2e)
+}
-- 
GitLab