Skip to content

Commit

Permalink
Add library call to irma server to subscribe on statuses using channels
Browse files Browse the repository at this point in the history
  • Loading branch information
ivard committed Sep 4, 2020
1 parent 656dcaf commit 15ee246
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
22 changes: 5 additions & 17 deletions irma/cmd/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"regexp"
"strconv"
"sync"
"time"

"github.com/go-errors/errors"
"github.com/privacybydesign/irmago"
Expand Down Expand Up @@ -133,21 +132,10 @@ func libraryRequest(

if binding {
// Listen for session status
statuschan := make(chan irma.ServerStatus)
go func() {
var status irma.ServerStatus
for {
newStatus := irmaServer.GetSessionResult(requestorToken).Status
if newStatus != status {
status = newStatus
statuschan <- status
if status.Finished() {
return
}
}
time.Sleep(500 * time.Millisecond)
}
}()
statuschan, err := irmaServer.GetSessionStatus(string(requestorToken), true)
if err != nil {
return nil, errors.WrapPrefix(err, "Failed to start listening for session statuses", 0)
}

_, err = handleBinding(sessionOptions, statuschan, func() error {
return irmaServer.BindingCompleted(requestorToken)
Expand Down Expand Up @@ -309,7 +297,7 @@ func handleBinding(options *irma.SessionOptions, statusChan chan irma.ServerStat
}
return status, nil
case err := <-errorChan:
return status, err
return "", err
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions server/irmaserver/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,27 @@ func (s *Server) SubscribeServerSentEvents(w http.ResponseWriter, r *http.Reques
s.serverSentEvents.ServeHTTP(w, r)
return nil
}

// GetSessionStatus retrieves a channel on which the current session status of the specified
// IRMA session can be retrieved.
func GetSessionStatus(token string, requestor bool) (chan irma.ServerStatus, error) {
return s.GetSessionStatus(token, requestor)
}
func (s *Server) GetSessionStatus(token string, requestor bool) (chan irma.ServerStatus, error) {
var session *session
if requestor {
session = s.sessions.get(irma.RequestorToken(token))
} else {
session = s.sessions.clientGet(irma.ClientToken(token))
}
if session == nil {
return nil, server.LogError(errors.Errorf("can't get session status of unknown session %s", token))
}

statusChan := make(chan irma.ServerStatus)
go func() {
statusChan <- session.status
}()
session.statusChannels = append(session.statusChannels, statusChan)
return statusChan, nil
}
11 changes: 11 additions & 0 deletions server/irmaserver/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ func (session *session) setStatus(status irma.ServerStatus) {
}

func (session *session) onUpdate() {
// Perform sending to channels async to make sure session handling
// will not hang if channel is not (immediately) read.
for _, statusChan := range session.statusChannels {
go func(statusChan chan irma.ServerStatus, status irma.ServerStatus) {
statusChan <- status
if status.Finished() {
close(statusChan)
}
}(statusChan, session.status)
}

if session.sse == nil {
return
}
Expand Down
11 changes: 6 additions & 5 deletions server/irmaserver/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ type session struct {
request irma.SessionRequest
legacyCompatible bool // if the request is convertible to pre-condiscon format

options irma.SessionOptions
status irma.ServerStatus
prevStatus irma.ServerStatus
sse *sse.Server
responseCache responseCache
options irma.SessionOptions
status irma.ServerStatus
prevStatus irma.ServerStatus
sse *sse.Server
statusChannels []chan irma.ServerStatus
responseCache responseCache

clientAuth irma.ClientAuthorization
lastActive time.Time
Expand Down

0 comments on commit 15ee246

Please sign in to comment.