Commit 7b1d8560 authored by Koen van Ingen's avatar Koen van Ingen
Browse files

Some duplicated code cleanup

parent fd1c52e7
...@@ -2,8 +2,8 @@ package irmaclient ...@@ -2,8 +2,8 @@ package irmaclient
import ( import (
"fmt" "fmt"
"github.com/privacybydesign/irmago"
"testing" "testing"
"github.com/credentials/irmago"
) )
type ManualSessionHandler struct { type ManualSessionHandler struct {
...@@ -51,15 +51,15 @@ func TestManualKeyShareSession(t *testing.T) { ...@@ -51,15 +51,15 @@ func TestManualKeyShareSession(t *testing.T) {
} }
} }
func (sh *ManualSessionHandler) Success(irmaAction irma.Action, result string) { func (sh *ManualSessionHandler) Success(irmaAction irma.Action, result string) {
fmt.Println("Result: " + result)
sh.c <- nil sh.c <- nil
} }
func (sh *ManualSessionHandler) UnsatisfiableRequest(irmaAction irma.Action, missingAttributes irma.AttributeDisjunctionList) { sh.t.Fail() } func (sh *ManualSessionHandler) UnsatisfiableRequest(irmaAction irma.Action, missingAttributes irma.AttributeDisjunctionList) {
sh.t.Fail()
}
// Done in irma bridge? // Done in irma bridge?
func (sh *ManualSessionHandler) StatusUpdate(irmaAction irma.Action, status irma.Status) { } func (sh *ManualSessionHandler) StatusUpdate(irmaAction irma.Action, status irma.Status) {}
func (sh *ManualSessionHandler) RequestPin(remainingAttempts int, ph PinHandler) { func (sh *ManualSessionHandler) RequestPin(remainingAttempts int, ph PinHandler) {
ph(true, "12345") ph(true, "12345")
} }
...@@ -70,10 +70,18 @@ func (sh *ManualSessionHandler) RequestSignaturePermission(request irma.Signatur ...@@ -70,10 +70,18 @@ func (sh *ManualSessionHandler) RequestSignaturePermission(request irma.Signatur
// These handlers should not be called, fail test if they are called // These handlers should not be called, fail test if they are called
func (sh *ManualSessionHandler) Cancelled(irmaAction irma.Action) { sh.t.Fail() } func (sh *ManualSessionHandler) Cancelled(irmaAction irma.Action) { sh.t.Fail() }
func (sh *ManualSessionHandler) MissingKeyshareEnrollment(manager irma.SchemeManagerIdentifier) { sh.t.Fail() } func (sh *ManualSessionHandler) MissingKeyshareEnrollment(manager irma.SchemeManagerIdentifier) {
func (sh *ManualSessionHandler) RequestIssuancePermission(request irma.IssuanceRequest, issuerName string, ph PermissionHandler) { sh.t.Fail() } sh.t.Fail()
func (sh *ManualSessionHandler) RequestSchemeManagerPermission(manager *irma.SchemeManager, callback func(proceed bool)) { sh.t.Fail() } }
func (sh *ManualSessionHandler) RequestVerificationPermission(request irma.DisclosureRequest, verifierName string, ph PermissionHandler) { sh.t.Fail() } func (sh *ManualSessionHandler) RequestIssuancePermission(request irma.IssuanceRequest, issuerName string, ph PermissionHandler) {
sh.t.Fail()
}
func (sh *ManualSessionHandler) RequestSchemeManagerPermission(manager *irma.SchemeManager, callback func(proceed bool)) {
sh.t.Fail()
}
func (sh *ManualSessionHandler) RequestVerificationPermission(request irma.DisclosureRequest, verifierName string, ph PermissionHandler) {
sh.t.Fail()
}
func (sh *ManualSessionHandler) Failure(irmaAction irma.Action, err *irma.SessionError) { func (sh *ManualSessionHandler) Failure(irmaAction irma.Action, err *irma.SessionError) {
fmt.Println(err.Err) fmt.Println(err.Err)
sh.t.Fail() sh.t.Fail()
......
package irmaclient package irmaclient
import ( import (
"encoding/json"
"fmt" "fmt"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"encoding/json"
"math/big" "math/big"
...@@ -46,9 +46,12 @@ type SessionDismisser interface { ...@@ -46,9 +46,12 @@ type SessionDismisser interface {
Dismiss() Dismiss()
} }
// baseSession contains methods generic to both manual and interactive sessions
type baseSession interface { type baseSession interface {
sendResponse(message interface{}) getBuilders() (gabi.ProofBuilderList, error)
cancel(action irma.Action) getProof() (interface{}, error)
panicFailure()
checkKeyshareEnrollment() bool
} }
type session struct { type session struct {
...@@ -59,6 +62,7 @@ type session struct { ...@@ -59,6 +62,7 @@ type session struct {
choice *irma.DisclosureChoice choice *irma.DisclosureChoice
client *Client client *Client
downloaded *irma.IrmaIdentifierSet
irmaSession irma.IrmaSession irmaSession irma.IrmaSession
} }
...@@ -71,8 +75,6 @@ type interactiveSession struct { ...@@ -71,8 +75,6 @@ type interactiveSession struct {
info *irma.SessionInfo info *irma.SessionInfo
jwt irma.RequestorJwt jwt irma.RequestorJwt
transport *irma.HTTPTransport transport *irma.HTTPTransport
choice *irma.DisclosureChoice
downloaded *irma.IrmaIdentifierSet
done bool done bool
} }
...@@ -125,6 +127,64 @@ func calcVersion(qr *irma.Qr) (string, error) { ...@@ -125,6 +127,64 @@ func calcVersion(qr *irma.Qr) (string, error) {
return "", fmt.Errorf("No supported protocol version between %s and %s", qr.ProtocolVersion, qr.ProtocolMaxVersion) return "", fmt.Errorf("No supported protocol version between %s and %s", qr.ProtocolVersion, qr.ProtocolMaxVersion)
} }
func (session *session) getBuilders() (gabi.ProofBuilderList, error) {
var builders gabi.ProofBuilderList
var err error
switch session.Action {
case irma.ActionSigning:
fallthrough
case irma.ActionDisclosing:
builders, err = session.client.ProofBuilders(session.choice)
case irma.ActionIssuing:
builders, err = session.client.IssuanceProofBuilders(session.irmaSession.(*irma.IssuanceRequest))
}
return builders, err
}
func (session *session) getProof() (interface{}, error) {
var message interface{}
var err error
switch session.Action {
case irma.ActionSigning:
message, err = session.client.Proofs(session.choice, session.irmaSession, true)
case irma.ActionDisclosing:
message, err = session.client.Proofs(session.choice, session.irmaSession, false)
case irma.ActionIssuing:
message, err = session.client.IssueCommitments(session.irmaSession.(*irma.IssuanceRequest))
}
return message, err
}
// Check if we are enrolled into all involved keyshare servers
func (session *session) checkKeyshareEnrollment() bool {
for id := range session.irmaSession.Identifiers().SchemeManagers {
manager, ok := session.client.Configuration.SchemeManagers[id]
if !ok {
session.Handler.Failure(session.Action, &irma.SessionError{ErrorType: irma.ErrorUnknownSchemeManager, Info: id.String()})
return false
}
distributed := manager.Distributed()
_, enrolled := session.client.keyshareServers[id]
if distributed && !enrolled {
session.Handler.MissingKeyshareEnrollment(id)
return false
}
}
return true
}
func (session *session) panicFailure() {
if e := recover(); e != nil {
if session.Handler != nil {
session.Handler.Failure(session.Action, panicToError(e))
}
}
}
func parseSigrequestJSON(sigrequestJSONString string) (*irma.SignatureRequest, error) { func parseSigrequestJSON(sigrequestJSONString string) (*irma.SignatureRequest, error) {
sigrequestJSON := []byte(sigrequestJSONString) sigrequestJSON := []byte(sigrequestJSONString)
sigrequest := &irma.SignatureRequest{} sigrequest := &irma.SignatureRequest{}
...@@ -135,36 +195,28 @@ func parseSigrequestJSON(sigrequestJSONString string) (*irma.SignatureRequest, e ...@@ -135,36 +195,28 @@ func parseSigrequestJSON(sigrequestJSONString string) (*irma.SignatureRequest, e
// Start a manual session // Start a manual session
func (client *Client) NewManualSession(sigrequestJSONString string, handler Handler) { func (client *Client) NewManualSession(sigrequestJSONString string, handler Handler) {
sigrequest, err := parseSigrequestJSON(sigrequestJSONString); sigrequest, err := parseSigrequestJSON(sigrequestJSONString)
if err != nil { if err != nil {
handler.Failure(irma.ActionUnknown, &irma.SessionError{Err: err}) handler.Failure(irma.ActionUnknown, &irma.SessionError{Err: err})
return return
} }
session := &manualSession{} session := &manualSession{
// TODO: for some reason: we can't define these above in constructor? session: session{
session.Action = irma.ActionSigning// TODO hardcoded for now Action: irma.ActionSigning, // TODO hardcoded for now
session.Handler = handler Handler: handler,
session.client = client // TODO hardcoded for now client: client,
session.Version = irma.Version("2") // TODO hardcoded for now Version: irma.Version("2"), // TODO hardcoded for now
session.irmaSession = sigrequest irmaSession: sigrequest,
},
}
session.Handler.StatusUpdate(session.Action, irma.StatusManualStarted) session.Handler.StatusUpdate(session.Action, irma.StatusManualStarted)
// Check if we are enrolled into all involved keyshare servers // Check if we are enrolled into all involved keyshare servers
for id := range session.irmaSession.Identifiers().SchemeManagers { if !session.checkKeyshareEnrollment() {
manager, ok := session.client.Configuration.SchemeManagers[id]
if !ok {
session.Handler.Failure(session.Action, &irma.SessionError{ErrorType: irma.ErrorUnknownSchemeManager, Info: id.String()})
return
}
distributed := manager.Distributed()
_, enrolled := session.client.keyshareServers[id]
if distributed && !enrolled {
session.Handler.MissingKeyshareEnrollment(id)
return return
} }
}
candidates, missing := session.client.CheckSatisfiability(session.irmaSession.ToDisclose()) candidates, missing := session.client.CheckSatisfiability(session.irmaSession.ToDisclose())
if len(missing) > 0 { if len(missing) > 0 {
...@@ -178,59 +230,29 @@ func (client *Client) NewManualSession(sigrequestJSONString string, handler Hand ...@@ -178,59 +230,29 @@ func (client *Client) NewManualSession(sigrequestJSONString string, handler Hand
callback := PermissionHandler(func(proceed bool, choice *irma.DisclosureChoice) { callback := PermissionHandler(func(proceed bool, choice *irma.DisclosureChoice) {
session.choice = choice session.choice = choice
session.irmaSession.SetDisclosureChoice(choice) session.irmaSession.SetDisclosureChoice(choice)
fmt.Println("Starting session.do()")
go session.do(proceed) go session.do(proceed)
}) })
session.Handler.RequestSignaturePermission( session.Handler.RequestSignaturePermission(
*session.irmaSession.(*irma.SignatureRequest), "IRMA Signature App", callback) *session.irmaSession.(*irma.SignatureRequest), "E-mail request", callback)
fmt.Println(session)
} }
func (session *manualSession) do(proceed bool) { func (session *manualSession) do(proceed bool) {
defer func() { defer session.panicFailure()
if e := recover(); e != nil {
if session.Handler != nil {
session.Handler.Failure(session.Action, panicToError(e))
}
}
}()
if !proceed { if !proceed {
session.cancel() session.Handler.Cancelled(session.Action)
return return
} }
session.Handler.StatusUpdate(session.Action, irma.StatusCommunicating)
if !session.irmaSession.Identifiers().Distributed(session.client.Configuration) { if !session.irmaSession.Identifiers().Distributed(session.client.Configuration) {
var message interface{} message, err := session.getProof()
var err error
switch session.Action {
case irma.ActionSigning:
message, err = session.client.Proofs(session.choice, session.irmaSession, true)
case irma.ActionDisclosing:
message, err = session.client.Proofs(session.choice, session.irmaSession, false)
case irma.ActionIssuing:
message, err = session.client.IssueCommitments(session.irmaSession.(*irma.IssuanceRequest))
}
if err != nil { if err != nil {
session.Handler.Failure(session.Action, &irma.SessionError{ErrorType: irma.ErrorCrypto, Err: err}) session.Handler.Failure(session.Action, &irma.SessionError{ErrorType: irma.ErrorCrypto, Err: err})
return return
} }
session.sendResponse(message) session.sendResponse(message)
} else { } else {
var builders gabi.ProofBuilderList builders, err := session.getBuilders()
var err error
switch session.Action {
case irma.ActionSigning:
fallthrough
case irma.ActionDisclosing:
builders, err = session.client.ProofBuilders(session.choice)
case irma.ActionIssuing:
builders, err = session.client.IssuanceProofBuilders(session.irmaSession.(*irma.IssuanceRequest))
}
if err != nil { if err != nil {
session.Handler.Failure(session.Action, &irma.SessionError{ErrorType: irma.ErrorCrypto, Err: err}) session.Handler.Failure(session.Action, &irma.SessionError{ErrorType: irma.ErrorCrypto, Err: err})
} }
...@@ -252,13 +274,13 @@ func (client *Client) NewSession(qr *irma.Qr, handler Handler) SessionDismisser ...@@ -252,13 +274,13 @@ func (client *Client) NewSession(qr *irma.Qr, handler Handler) SessionDismisser
session := &interactiveSession{ session := &interactiveSession{
ServerURL: qr.URL, ServerURL: qr.URL,
transport: irma.NewHTTPTransport(qr.URL), transport: irma.NewHTTPTransport(qr.URL),
session: session{
Action: irma.Action(qr.Type),
Handler: handler,
client: client,
},
} }
// TODO: for some reason: we can't define these above in constructor?
session.Action = irma.Action(qr.Type)
session.Handler = handler
session.client = client
if session.Action == irma.ActionSchemeManager { if session.Action == irma.ActionSchemeManager {
go session.managerSession() go session.managerSession()
return session return session
...@@ -295,13 +317,7 @@ func (client *Client) NewSession(qr *irma.Qr, handler Handler) SessionDismisser ...@@ -295,13 +317,7 @@ func (client *Client) NewSession(qr *irma.Qr, handler Handler) SessionDismisser
// start retrieves the first message in the IRMA protocol, checks if we can perform // start retrieves the first message in the IRMA protocol, checks if we can perform
// the request, and informs the user of the outcome. // the request, and informs the user of the outcome.
func (session *interactiveSession) start() { func (session *interactiveSession) start() {
defer func() { defer session.panicFailure()
if e := recover(); e != nil {
if session.Handler != nil {
session.Handler.Failure(session.Action, panicToError(e))
}
}
}()
session.Handler.StatusUpdate(session.Action, irma.StatusCommunicating) session.Handler.StatusUpdate(session.Action, irma.StatusCommunicating)
...@@ -331,20 +347,9 @@ func (session *interactiveSession) start() { ...@@ -331,20 +347,9 @@ func (session *interactiveSession) start() {
} }
// Check if we are enrolled into all involved keyshare servers // Check if we are enrolled into all involved keyshare servers
for id := range session.irmaSession.Identifiers().SchemeManagers { if !session.checkKeyshareEnrollment() {
manager, ok := session.client.Configuration.SchemeManagers[id]
if !ok {
session.fail(&irma.SessionError{ErrorType: irma.ErrorUnknownSchemeManager, Info: id.String()})
return return
} }
distributed := manager.Distributed()
_, enrolled := session.client.keyshareServers[id]
if distributed && !enrolled {
session.delete()
session.Handler.MissingKeyshareEnrollment(id)
return
}
}
// Download missing credential types/issuers/public keys from the scheme manager // Download missing credential types/issuers/public keys from the scheme manager
if session.downloaded, err = session.client.Configuration.Download(session.irmaSession.Identifiers()); err != nil { if session.downloaded, err = session.client.Configuration.Download(session.irmaSession.Identifiers()); err != nil {
...@@ -398,13 +403,7 @@ func (session *interactiveSession) start() { ...@@ -398,13 +403,7 @@ func (session *interactiveSession) start() {
} }
func (session *interactiveSession) do(proceed bool) { func (session *interactiveSession) do(proceed bool) {
defer func() { defer session.panicFailure()
if e := recover(); e != nil {
if session.Handler != nil {
session.Handler.Failure(session.Action, panicToError(e))
}
}
}()
if !proceed { if !proceed {
session.cancel() session.cancel()
...@@ -413,38 +412,17 @@ func (session *interactiveSession) do(proceed bool) { ...@@ -413,38 +412,17 @@ func (session *interactiveSession) do(proceed bool) {
session.Handler.StatusUpdate(session.Action, irma.StatusCommunicating) session.Handler.StatusUpdate(session.Action, irma.StatusCommunicating)
if !session.irmaSession.Identifiers().Distributed(session.client.Configuration) { if !session.irmaSession.Identifiers().Distributed(session.client.Configuration) {
var message interface{} message, err := session.getProof()
var err error
switch session.Action {
case irma.ActionSigning:
message, err = session.client.Proofs(session.choice, session.irmaSession, true)
case irma.ActionDisclosing:
message, err = session.client.Proofs(session.choice, session.irmaSession, false)
case irma.ActionIssuing:
message, err = session.client.IssueCommitments(session.irmaSession.(*irma.IssuanceRequest))
}
if err != nil { if err != nil {
session.fail(&irma.SessionError{ErrorType: irma.ErrorCrypto, Err: err}) session.fail(&irma.SessionError{ErrorType: irma.ErrorCrypto, Err: err})
return return
} }
session.sendResponse(message) session.sendResponse(message)
} else { } else {
var builders gabi.ProofBuilderList builders, err := session.getBuilders()
var err error
switch session.Action {
case irma.ActionSigning:
fallthrough
case irma.ActionDisclosing:
builders, err = session.client.ProofBuilders(session.choice)
case irma.ActionIssuing:
builders, err = session.client.IssuanceProofBuilders(session.irmaSession.(*irma.IssuanceRequest))
}
if err != nil { if err != nil {
session.fail(&irma.SessionError{ErrorType: irma.ErrorCrypto, Err: err}) session.fail(&irma.SessionError{ErrorType: irma.ErrorCrypto, Err: err})
} }
startKeyshareSession( startKeyshareSession(
session, session,
session.Handler, session.Handler,
...@@ -508,17 +486,11 @@ func (session *manualSession) KeyshareError(err error) { ...@@ -508,17 +486,11 @@ func (session *manualSession) KeyshareError(err error) {
session.Handler.Failure(session.Action, serr) session.Handler.Failure(session.Action, serr)
} }
func (session *interactiveSession) KeysharePin() { func (session *session) KeysharePin() {
session.Handler.StatusUpdate(session.Action, irma.StatusConnected)
}
func (session *manualSession) KeysharePin() {
session.Handler.StatusUpdate(session.Action, irma.StatusConnected) session.Handler.StatusUpdate(session.Action, irma.StatusConnected)
} }
func (session *interactiveSession) KeysharePinOK() { func (session *session) KeysharePinOK() {
session.Handler.StatusUpdate(session.Action, irma.StatusCommunicating)
}
func (session *manualSession) KeysharePinOK() {
session.Handler.StatusUpdate(session.Action, irma.StatusCommunicating) session.Handler.StatusUpdate(session.Action, irma.StatusCommunicating)
} }
...@@ -665,10 +637,6 @@ func (session *interactiveSession) cancel() { ...@@ -665,10 +637,6 @@ func (session *interactiveSession) cancel() {
} }
} }
func (session *manualSession) cancel() {
session.Handler.Cancelled(session.Action)
}
func (session *interactiveSession) Dismiss() { func (session *interactiveSession) Dismiss() {
session.cancel() session.cancel()
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment