Commit 393b2dbd authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Split scheme manager request handling from generic session request handling

parent c3bfa3aa
...@@ -447,12 +447,13 @@ func TestDownloadSchemeManager(t *testing.T) { ...@@ -447,12 +447,13 @@ func TestDownloadSchemeManager(t *testing.T) {
require.NotContains(t, client.Configuration.SchemeManagers, irmademo) require.NotContains(t, client.Configuration.SchemeManagers, irmademo)
// Do an add-scheme-manager-session // Do an add-scheme-manager-session
qr := &irma.Qr{ c := make(chan *irma.SessionError)
qr, err := json.Marshal(&irma.SchemeManagerRequest{
Type: irma.ActionSchemeManager, Type: irma.ActionSchemeManager,
URL: "http://localhost:48681/irma_configuration/irma-demo", URL: "http://localhost:48681/irma_configuration/irma-demo",
} })
c := make(chan *irma.SessionError) require.NoError(t, err)
client.newQrSession(qr, TestHandler{t, c, client}) client.NewSession(string(qr), TestHandler{t, c, client})
if err := <-c; err != nil { if err := <-c; err != nil {
t.Fatal(*err) t.Fatal(*err)
} }
......
...@@ -79,22 +79,24 @@ var supportedVersions = map[int][]int{ ...@@ -79,22 +79,24 @@ var supportedVersions = map[int][]int{
// Session constructors // Session constructors
// NewSession starts a new IRMA session, given (along with a handler to pass feedback to) // NewSession starts a new IRMA session, given (along with a handler to pass feedback to) a session request.
// either an irma.QR or *irma.QR; a string that contains a JSON-serialized irma.QR; // When the request is not suitable to start an IRMA session from, it calls the Failure method of the specified Handler.
// or a string that contains a serialized *irma.SignatureRequest.
// In any other case it calls the Failure method of the specified Handler.
func (client *Client) NewSession(sessionrequest string, handler Handler) SessionDismisser { func (client *Client) NewSession(sessionrequest string, handler Handler) SessionDismisser {
bts := []byte(sessionrequest) bts := []byte(sessionrequest)
// Try to deserialize it as a Qr or SignatureRequest
qr := &irma.Qr{} qr := &irma.Qr{}
if err := irma.UnmarshalValidate(bts, qr); err == nil { if err := irma.UnmarshalValidate(bts, qr); err == nil {
return client.newQrSession(qr, handler) return client.newQrSession(qr, handler)
} }
sigrequest := &irma.SignatureRequest{} schemeRequest := &irma.SchemeManagerRequest{}
if err := irma.UnmarshalValidate(bts, sigrequest); err == nil { if err := irma.UnmarshalValidate(bts, schemeRequest); err == nil {
return client.newManualSession(sigrequest, handler) return client.newSchemeSession(schemeRequest, handler)
}
sigRequest := &irma.SignatureRequest{}
if err := irma.UnmarshalValidate(bts, sigRequest); err == nil {
return client.newManualSession(sigRequest, handler)
} }
handler.Failure(&irma.SessionError{Err: errors.New("Session request could not be parsed")}) handler.Failure(&irma.SessionError{Err: errors.New("Session request could not be parsed")})
...@@ -117,6 +119,20 @@ func (client *Client) newManualSession(sigrequest *irma.SignatureRequest, handle ...@@ -117,6 +119,20 @@ func (client *Client) newManualSession(sigrequest *irma.SignatureRequest, handle
return session return session
} }
func (client *Client) newSchemeSession(qr *irma.SchemeManagerRequest, handler Handler) SessionDismisser {
session := &session{
ServerURL: qr.URL,
transport: irma.NewHTTPTransport(qr.URL),
Action: irma.ActionSchemeManager,
Handler: handler,
client: client,
}
session.Handler.StatusUpdate(session.Action, irma.StatusCommunicating)
go session.managerSession()
return session
}
// newQrSession creates and starts a new interactive IRMA session // newQrSession creates and starts a new interactive IRMA session
func (client *Client) newQrSession(qr *irma.Qr, handler Handler) SessionDismisser { func (client *Client) newQrSession(qr *irma.Qr, handler Handler) SessionDismisser {
session := &session{ session := &session{
...@@ -128,12 +144,6 @@ func (client *Client) newQrSession(qr *irma.Qr, handler Handler) SessionDismisse ...@@ -128,12 +144,6 @@ func (client *Client) newQrSession(qr *irma.Qr, handler Handler) SessionDismisse
} }
session.Handler.StatusUpdate(session.Action, irma.StatusCommunicating) session.Handler.StatusUpdate(session.Action, irma.StatusCommunicating)
// TODO move this
if session.Action == irma.ActionSchemeManager {
go session.managerSession()
return session
}
// Check if the action is one of the supported types // Check if the action is one of the supported types
switch session.Action { switch session.Action {
case irma.ActionDisclosing: case irma.ActionDisclosing:
......
...@@ -127,6 +127,8 @@ type Qr struct { ...@@ -127,6 +127,8 @@ type Qr struct {
ProtocolMaxVersion ProtocolVersion `json:"vmax"` ProtocolMaxVersion ProtocolVersion `json:"vmax"`
} }
type SchemeManagerRequest Qr
// Statuses // Statuses
const ( const (
StatusConnected = Status("connected") StatusConnected = Status("connected")
...@@ -250,21 +252,33 @@ func ParseRequestorJwt(action Action, jwt string) (RequestorJwt, error) { ...@@ -250,21 +252,33 @@ func ParseRequestorJwt(action Action, jwt string) (RequestorJwt, error) {
} }
func (qr *Qr) Validate() error { func (qr *Qr) Validate() error {
if _, err := url.ParseRequestURI(qr.URL); err != nil {
return errors.Errorf("Invalid URL: %s", err.Error())
}
if qr.URL == "" { if qr.URL == "" {
return errors.New("No URL specified") return errors.New("No URL specified")
} }
if _, err := url.ParseRequestURI(qr.URL); err != nil {
return errors.Errorf("Invalid URL: %s", err.Error())
}
switch qr.Type { switch qr.Type {
case ActionDisclosing: // nop case ActionDisclosing: // nop
case ActionIssuing: // nop case ActionIssuing: // nop
case ActionSigning: // nop case ActionSigning: // nop
case ActionSchemeManager: // nop
default: default:
return errors.New("Unsupported session type") return errors.New("Unsupported session type")
} }
return nil return nil
} }
func (smr *SchemeManagerRequest) Validate() error {
if smr.Type != ActionSchemeManager {
return errors.New("Not a scheme manager request")
}
if smr.URL == "" {
return errors.New("No URL specified")
}
if _, err := url.ParseRequestURI(smr.URL); err != nil {
return errors.Errorf("Invalid URL: %s", err.Error())
}
return nil
}
Supports Markdown
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