Commit 548a7249 authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Refactor returning session result

parent 9cfd42ff
......@@ -97,8 +97,12 @@ func HandleProtocolMessage(
return
}
// However we return, if the session has been cancelled by any of the handlers
// then we should inform the user by returning a SessionResult - but only if we have not
// already done this in the past, e.g. by a previous HTTP call handled by this function
defer func() {
if session.result != nil {
if session.finished() && !session.returned {
session.returned = true
result = session.result
}
}()
......@@ -123,7 +127,7 @@ func HandleProtocolMessage(
status, output = responseJson(nil, session.fail(irmaserver.ErrorMalformedInput, err.Error()))
return
}
status, output = responseJson(session.handleGetSession(min, max))
status, output = responseJson(session.handleGetRequest(min, max))
return
}
status, output = responseJson(nil, session.fail(irmaserver.ErrorInvalidRequest, ""))
......
......@@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"runtime/debug"
"time"
"github.com/mhe/gabi"
"github.com/privacybydesign/irmago"
......@@ -13,19 +14,22 @@ import (
var conf *irmaserver.Configuration
func (session *session) handleDelete() {
if !session.alive() {
if session.finished() {
return
}
session.result = &irmaserver.SessionResult{Token: session.token} // TODO what to return here?
session.markAlive()
// TODO const ProofStatusCancelled = irma.ProofStatus("CANCELLED") ?
session.result = &irmaserver.SessionResult{Token: session.token}
session.status = irmaserver.StatusCancelled
}
func (session *session) handleGetSession(min, max *irma.ProtocolVersion) (irma.SessionRequest, *irma.RemoteError) {
func (session *session) handleGetRequest(min, max *irma.ProtocolVersion) (irma.SessionRequest, *irma.RemoteError) {
var err error
session.status = irmaserver.StatusConnected
if session.version, err = chooseProtocolVersion(min, max); err != nil {
return nil, session.fail(irmaserver.ErrorProtocolVersion, "")
}
session.markAlive()
session.status = irmaserver.StatusConnected
session.request.SetVersion(session.version)
return session.request, nil
}
......@@ -35,32 +39,34 @@ func handleGetStatus(session *session) irmaserver.Status {
}
func (session *session) handlePostSignature(signature *irma.SignedMessage) (irma.ProofStatus, *irma.RemoteError) {
session.signature = signature
session.disclosed, session.proofStatus = signature.Verify(conf.IrmaConfiguration, session.request.(*irma.SignatureRequest))
session.markAlive()
session.result.Signature = signature
session.result.Disclosed, session.result.Status = signature.Verify(
conf.IrmaConfiguration, session.request.(*irma.SignatureRequest))
session.finish()
return session.proofStatus, nil
return session.result.Status, nil
}
func (session *session) handlePostProofs(proofs gabi.ProofList) (irma.ProofStatus, *irma.RemoteError) {
session.disclosed, session.proofStatus = irma.ProofList(proofs).Verify(conf.IrmaConfiguration, session.request.(*irma.DisclosureRequest))
session.markAlive()
session.result.Disclosed, session.result.Status = irma.ProofList(proofs).Verify(
conf.IrmaConfiguration, session.request.(*irma.DisclosureRequest))
session.finish()
return session.proofStatus, nil
return session.result.Status, nil
}
// Session helpers
func (session *session) alive() bool {
return session.status != irmaserver.StatusDone && session.status != irmaserver.StatusCancelled
func (session *session) finished() bool {
return session.status == irmaserver.StatusDone || session.status == irmaserver.StatusCancelled
}
func (session *session) finish() {
session.status = irmaserver.StatusDone
session.result = &irmaserver.SessionResult{
Token: session.token,
Status: session.proofStatus,
Disclosed: session.disclosed,
Signature: session.signature,
}
}
func (session *session) markAlive() {
session.lastActive = time.Now()
}
func (session *session) fail(err irmaserver.Error, message string) *irma.RemoteError {
......
......@@ -109,16 +109,16 @@ func (session *session) handlePostCommitments(commitments *gabi.IssueCommitmentM
if conf.IrmaConfiguration.SchemeManagers[schemeid].Distributed() {
proofP, err := session.getProofP(commitments, schemeid)
if err != nil {
session.fail(irmaserver.ErrorKeyshareProofMissing, err.Error())
return nil, session.fail(irmaserver.ErrorKeyshareProofMissing, err.Error())
}
proof.MergeProofP(proofP, pubkey)
}
}
// Verify all proofs and check disclosed attributes, if any, against request
session.disclosed, session.proofStatus = irma.ProofList(commitments.Proofs).VerifyAgainstDisjunctions(
session.result.Disclosed, session.result.Status = irma.ProofList(commitments.Proofs).VerifyAgainstDisjunctions(
conf.IrmaConfiguration, request.Disclose, request.Context, request.Nonce, pubkeys, false)
if session.proofStatus != irma.ProofStatusValid {
if session.result.Status != irma.ProofStatusValid {
return nil, session.fail(irmaserver.ErrorInvalidProofs, "")
}
......
......@@ -21,12 +21,9 @@ type session struct {
request irma.SessionRequest
status irmaserver.Status
active time.Time
proofStatus irma.ProofStatus
disclosed []*irma.DisclosedAttribute
signature *irma.SignedMessage
result *irmaserver.SessionResult
lastActive time.Time
returned bool
result *irmaserver.SessionResult
kssProofs map[irma.SchemeManagerIdentifier]*gabi.ProofP
}
......@@ -80,7 +77,7 @@ func (s memorySessionStore) deleteExpired() {
s.RLock()
expired := make([]string, 0, len(s.m))
for token, session := range s.m {
if session.active.Add(5 * time.Minute).Before(time.Now()) {
if session.lastActive.Add(5 * time.Minute).Before(time.Now()) {
conf.Logger.Infof("Session %s expired, deleting", token)
expired = append(expired, token)
}
......@@ -103,17 +100,21 @@ func (s memorySessionStore) deleteExpired() {
var one *big.Int = big.NewInt(1)
func newSession(action irma.Action, request irma.SessionRequest) *session {
token := newSessionToken()
s := &session{
action: action,
request: request,
status: irmaserver.StatusInitialized,
active: time.Now(),
token: newSessionToken(),
action: action,
request: request,
status: irmaserver.StatusInitialized,
lastActive: time.Now(),
token: token,
result: &irmaserver.SessionResult{
Token: token,
},
}
nonce, _ := gabi.RandomBigInt(gabi.DefaultSystemParameters[2048].Lstatzk)
request.SetNonce(nonce)
request.SetContext(one)
sessions.add(s.token, s)
sessions.add(token, s)
return s
}
......
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