Commit d8b2bd7b authored by Sietse Ringers's avatar Sietse Ringers
Browse files

refactor: renaming nonrevocation methods

parent ff930a82
...@@ -379,7 +379,7 @@ func TestRevocation(t *testing.T) { ...@@ -379,7 +379,7 @@ func TestRevocation(t *testing.T) {
req := revocationRequest() req := revocationRequest()
require.NoError(t, client.Configuration.RevocationStorage.SetRecords(req.Base())) require.NoError(t, client.Configuration.RevocationStorage.SetRecords(req.Base()))
require.NoError(t, client.PrepareNonrevocation(req)) require.NoError(t, client.NonrevPreprare(req))
// revoke cred0 // revoke cred0
require.NoError(t, revocationServer.Revoke(cred, "cred0")) require.NoError(t, revocationServer.Revoke(cred, "cred0"))
......
...@@ -57,6 +57,9 @@ type Client struct { ...@@ -57,6 +57,9 @@ type Client struct {
Configuration *irma.Configuration Configuration *irma.Configuration
irmaConfigurationPath string irmaConfigurationPath string
handler ClientHandler handler ClientHandler
jobsCancel chan struct{}
jobChan chan func()
} }
// SentryDSN should be set in the init() function // SentryDSN should be set in the init() function
...@@ -188,26 +191,19 @@ func New( ...@@ -188,26 +191,19 @@ func New(
return nil, errors.New("Too many keyshare servers") return nil, errors.New("Too many keyshare servers")
} }
reportErr := func(err error) { client.jobChan = make(chan func(), 100)
irma.Logger.Error(err) for credid, attrsets := range client.attributes {
raven.CaptureError(err, nil) for i := range attrsets {
return credid := credid // make copy of same name to capture the value for closure below
} i := i // same, see https://golang.org/doc/faq#closures_and_goroutines
client.jobChan <- func() {
defer func() { if err := client.nonrevCredPrepareCache(credid, i); err != nil {
var cred *credential client.reportError(err)
var err error
for credid, attrsets := range client.attributes {
for i := range attrsets {
if cred, err = client.credential(credid, i); err != nil {
reportErr(err)
}
if err = cred.PrepareNonrevCache(); err != nil {
reportErr(err)
} }
} }
} }
}() }
go client.startJobs()
return client, schemeMgrErr return client, schemeMgrErr
} }
...@@ -216,6 +212,43 @@ func (client *Client) Close() error { ...@@ -216,6 +212,43 @@ func (client *Client) Close() error {
return client.storage.Close() return client.storage.Close()
} }
func (client *Client) nonrevCredPrepareCache(credid irma.CredentialTypeIdentifier, index int) error {
cred, err := client.credential(credid, index)
if err != nil {
return err
}
return cred.NonrevPrepareCache()
}
func (client *Client) reportError(err error) {
irma.Logger.Error(err)
raven.CaptureError(err, nil)
}
func (client *Client) startJobs() {
if client.jobsCancel != nil { // already running
return
}
client.jobsCancel = make(chan struct{})
for {
select {
case <-client.jobsCancel:
client.jobsCancel = nil
return
case job := <-client.jobChan:
job()
}
}
}
func (client *Client) stopJobs() {
if client.jobsCancel == nil {
return
}
close(client.jobsCancel)
}
// CredentialInfoList returns a list of information of all contained credentials. // CredentialInfoList returns a list of information of all contained credentials.
func (client *Client) CredentialInfoList() irma.CredentialInfoList { func (client *Client) CredentialInfoList() irma.CredentialInfoList {
list := irma.CredentialInfoList([]*irma.CredentialInfo{}) list := irma.CredentialInfoList([]*irma.CredentialInfo{})
...@@ -721,10 +754,10 @@ func (client *Client) groupCredentials(choice *irma.DisclosureChoice) ( ...@@ -721,10 +754,10 @@ func (client *Client) groupCredentials(choice *irma.DisclosureChoice) (
return todisclose, attributeIndices, nil return todisclose, attributeIndices, nil
} }
// PrepareNonrevocation updates the revocation state for each credential in the request // NonrevPrepare updates the revocation state for each credential in the request
// requiring a nonrevocation proof, using the updates included in the request, or the remote // requiring a nonrevocation proof, using the updates included in the request, or the remote
// revocation server if those do not suffice. // revocation server if those do not suffice.
func (client *Client) PrepareNonrevocation(request irma.SessionRequest) error { func (client *Client) NonrevPreprare(request irma.SessionRequest) error {
var err error var err error
var cred *credential var cred *credential
var updated bool var updated bool
...@@ -737,7 +770,7 @@ func (client *Client) PrepareNonrevocation(request irma.SessionRequest) error { ...@@ -737,7 +770,7 @@ func (client *Client) PrepareNonrevocation(request irma.SessionRequest) error {
if cred, err = client.credential(id, i); err != nil { if cred, err = client.credential(id, i); err != nil {
return err return err
} }
if updated, err = cred.prepareNonrevocation(client.Configuration, request); err != nil { if updated, err = cred.NonrevPrepare(client.Configuration, request); err != nil {
return err return err
} }
if updated { if updated {
...@@ -750,24 +783,22 @@ func (client *Client) PrepareNonrevocation(request irma.SessionRequest) error { ...@@ -750,24 +783,22 @@ func (client *Client) PrepareNonrevocation(request irma.SessionRequest) error {
return nil return nil
} }
func (client *Client) repopulateNonrevCaches(request irma.SessionRequest) error { func (client *Client) nonrevRepopulateCaches(request irma.SessionRequest) {
var err error
var cred *credential
for id := range request.Disclosure().Identifiers().CredentialTypes { for id := range request.Disclosure().Identifiers().CredentialTypes {
typ := client.Configuration.CredentialTypes[id] typ := client.Configuration.CredentialTypes[id]
if !typ.SupportsRevocation() { if !typ.SupportsRevocation() {
continue continue
} }
for i := 0; i < len(client.attrs(id)); i++ { for i := 0; i < len(client.attrs(id)); i++ {
if cred, err = client.credential(id, i); err != nil { id := id
return err i := i
} client.jobChan <- func() {
if err = cred.PrepareNonrevCache(); err != nil { if err := client.nonrevCredPrepareCache(id, i); err != nil {
return err client.reportError(err)
}
} }
} }
} }
return nil
} }
// ProofBuilders constructs a list of proof builders for the specified attribute choice. // ProofBuilders constructs a list of proof builders for the specified attribute choice.
......
...@@ -41,18 +41,18 @@ func (cred *credential) AttributeList() *irma.AttributeList { ...@@ -41,18 +41,18 @@ func (cred *credential) AttributeList() *irma.AttributeList {
return cred.attrs return cred.attrs
} }
// prepareNonrevocation attempts to update the credential's nonrevocation witness from // NonrevPrepare attempts to update the credential's nonrevocation witness from
// 1) the session request, and then 2) the revocation server if our witness is too far out of date. // 1) the session request, and then 2) the revocation server if our witness is too far out of date.
// Returns whether or not the credential's nonrevocation state was updated. If so the caller should // Returns whether or not the credential's nonrevocation state was updated. If so the caller should
// persist the updated credential to storage. // persist the updated credential to storage.
func (cred *credential) prepareNonrevocation(conf *irma.Configuration, request irma.SessionRequest) (bool, error) { func (cred *credential) NonrevPrepare(conf *irma.Configuration, request irma.SessionRequest) (bool, error) {
credtype := cred.CredentialType().Identifier() credtype := cred.CredentialType().Identifier()
if !request.Base().RequestsRevocation(credtype) { if !request.Base().RequestsRevocation(credtype) {
return false, nil return false, nil
} }
revupdates := request.Base().RevocationUpdates[credtype] revupdates := request.Base().RevocationUpdates[credtype]
updated, err := cred.updateNonrevWitness(revupdates, conf.RevocationStorage) updated, err := cred.NonrevApplyUpdates(revupdates, conf.RevocationStorage)
if err != nil { if err != nil {
return updated, err return updated, err
} }
...@@ -68,12 +68,12 @@ func (cred *credential) prepareNonrevocation(conf *irma.Configuration, request i ...@@ -68,12 +68,12 @@ func (cred *credential) prepareNonrevocation(conf *irma.Configuration, request i
if err != nil { if err != nil {
return updated, err return updated, err
} }
return cred.updateNonrevWitness(revupdates, conf.RevocationStorage) return cred.NonrevApplyUpdates(revupdates, conf.RevocationStorage)
} }
// updateNonrevWitness updates the credential's nonrevocation witness using the specified messages, // NonrevApplyUpdates updates the credential's nonrevocation witness using the specified messages,
// if they all verify and if their indices are ahead and adjacent to that of our witness. // if they all verify and if their indices are ahead and adjacent to that of our witness.
func (cred *credential) updateNonrevWitness(messages []*irma.RevocationRecord, rs *irma.RevocationStorage) (bool, error) { func (cred *credential) NonrevApplyUpdates(messages []*irma.RevocationRecord, rs *irma.RevocationStorage) (bool, error) {
var err error var err error
var pk *revocation.PublicKey var pk *revocation.PublicKey
oldindex := cred.NonRevocationWitness.Index oldindex := cred.NonRevocationWitness.Index
...@@ -86,5 +86,5 @@ func (cred *credential) updateNonrevWitness(messages []*irma.RevocationRecord, r ...@@ -86,5 +86,5 @@ func (cred *credential) updateNonrevWitness(messages []*irma.RevocationRecord, r
} }
} }
return cred.NonRevocationWitness.Index != oldindex, cred.PrepareNonrevCache() return cred.NonRevocationWitness.Index != oldindex, cred.NonrevPrepareCache()
} }
...@@ -9,7 +9,7 @@ import ( ...@@ -9,7 +9,7 @@ import (
"strings" "strings"
"github.com/bwesterb/go-atum" "github.com/bwesterb/go-atum"
"github.com/getsentry/raven-go" raven "github.com/getsentry/raven-go"
"github.com/go-errors/errors" "github.com/go-errors/errors"
"github.com/privacybydesign/gabi" "github.com/privacybydesign/gabi"
"github.com/privacybydesign/gabi/big" "github.com/privacybydesign/gabi/big"
...@@ -77,7 +77,7 @@ type session struct { ...@@ -77,7 +77,7 @@ type session struct {
client *Client client *Client
request irma.SessionRequest request irma.SessionRequest
done bool done bool
prepRevocation chan error prepRevocation chan error // used when nonrevocation preprocessing is done
// State for issuance sessions // State for issuance sessions
issuerProofNonce *big.Int issuerProofNonce *big.Int
...@@ -138,6 +138,8 @@ func (client *Client) NewSession(sessionrequest string, handler Handler) Session ...@@ -138,6 +138,8 @@ func (client *Client) NewSession(sessionrequest string, handler Handler) Session
// newManualSession starts a manual session, given a signature request in JSON and a handler to pass messages to // newManualSession starts a manual session, given a signature request in JSON and a handler to pass messages to
func (client *Client) newManualSession(request irma.SessionRequest, handler Handler, action irma.Action) SessionDismisser { func (client *Client) newManualSession(request irma.SessionRequest, handler Handler, action irma.Action) SessionDismisser {
client.stopJobs()
session := &session{ session := &session{
Action: action, Action: action,
Handler: handler, Handler: handler,
...@@ -181,6 +183,8 @@ func (client *Client) newQrSession(qr *irma.Qr, handler Handler) SessionDismisse ...@@ -181,6 +183,8 @@ func (client *Client) newQrSession(qr *irma.Qr, handler Handler) SessionDismisse
return client.newQrSession(newqr, handler) return client.newQrSession(newqr, handler)
} }
client.stopJobs()
u, _ := url.ParseRequestURI(qr.URL) // Qr validator already checked this for errors u, _ := url.ParseRequestURI(qr.URL) // Qr validator already checked this for errors
session := &session{ session := &session{
ServerURL: qr.URL, ServerURL: qr.URL,
...@@ -309,7 +313,7 @@ func (session *session) processSessionInfo() { ...@@ -309,7 +313,7 @@ func (session *session) processSessionInfo() {
// Prepare and update all revocation state asynchroniously while the user makes her choices // Prepare and update all revocation state asynchroniously while the user makes her choices
go func() { go func() {
session.prepRevocation <- session.client.PrepareNonrevocation(session.request) session.prepRevocation <- session.client.NonrevPreprare(session.request)
}() }()
// Ask for permission to execute the session // Ask for permission to execute the session
...@@ -468,14 +472,9 @@ func (session *session) sendResponse(message interface{}) { ...@@ -468,14 +472,9 @@ func (session *session) sendResponse(message interface{}) {
session.client.handler.UpdateAttributes() session.client.handler.UpdateAttributes()
} }
session.done = true session.done = true
session.client.nonrevRepopulateCaches(session.request)
session.client.startJobs()
session.Handler.Success(string(messageJson)) session.Handler.Success(string(messageJson))
go func() {
if err := session.client.repopulateNonrevCaches(session.request); err != nil {
raven.CaptureError(err, nil)
irma.Logger.Error(err)
}
}()
} }
// managerSession performs a "session" in which a new scheme manager is added (asking for permission first). // managerSession performs a "session" in which a new scheme manager is added (asking for permission first).
...@@ -652,11 +651,14 @@ func panicToError(e interface{}) *irma.SessionError { ...@@ -652,11 +651,14 @@ func panicToError(e interface{}) *irma.SessionError {
} }
// Idempotently send DELETE to remote server, returning whether or not we did something // Idempotently send DELETE to remote server, returning whether or not we did something
// TODO this function does more, rename
func (session *session) delete() bool { func (session *session) delete() bool {
if !session.done { if !session.done {
if session.IsInteractive() { if session.IsInteractive() {
session.transport.Delete() session.transport.Delete()
} }
session.client.nonrevRepopulateCaches(session.request)
session.client.startJobs()
session.done = true session.done = true
return true return true
} }
......
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