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

Refactor returning session result

parent 9cfd42ff
...@@ -97,8 +97,12 @@ func HandleProtocolMessage( ...@@ -97,8 +97,12 @@ func HandleProtocolMessage(
return 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() { defer func() {
if session.result != nil { if session.finished() && !session.returned {
session.returned = true
result = session.result result = session.result
} }
}() }()
...@@ -123,7 +127,7 @@ func HandleProtocolMessage( ...@@ -123,7 +127,7 @@ func HandleProtocolMessage(
status, output = responseJson(nil, session.fail(irmaserver.ErrorMalformedInput, err.Error())) status, output = responseJson(nil, session.fail(irmaserver.ErrorMalformedInput, err.Error()))
return return
} }
status, output = responseJson(session.handleGetSession(min, max)) status, output = responseJson(session.handleGetRequest(min, max))
return return
} }
status, output = responseJson(nil, session.fail(irmaserver.ErrorInvalidRequest, "")) status, output = responseJson(nil, session.fail(irmaserver.ErrorInvalidRequest, ""))
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"runtime/debug" "runtime/debug"
"time"
"github.com/mhe/gabi" "github.com/mhe/gabi"
"github.com/privacybydesign/irmago" "github.com/privacybydesign/irmago"
...@@ -13,19 +14,22 @@ import ( ...@@ -13,19 +14,22 @@ import (
var conf *irmaserver.Configuration var conf *irmaserver.Configuration
func (session *session) handleDelete() { func (session *session) handleDelete() {
if !session.alive() { if session.finished() {
return 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 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 var err error
session.status = irmaserver.StatusConnected
if session.version, err = chooseProtocolVersion(min, max); err != nil { if session.version, err = chooseProtocolVersion(min, max); err != nil {
return nil, session.fail(irmaserver.ErrorProtocolVersion, "") return nil, session.fail(irmaserver.ErrorProtocolVersion, "")
} }
session.markAlive()
session.status = irmaserver.StatusConnected
session.request.SetVersion(session.version) session.request.SetVersion(session.version)
return session.request, nil return session.request, nil
} }
...@@ -35,32 +39,34 @@ func handleGetStatus(session *session) irmaserver.Status { ...@@ -35,32 +39,34 @@ func handleGetStatus(session *session) irmaserver.Status {
} }
func (session *session) handlePostSignature(signature *irma.SignedMessage) (irma.ProofStatus, *irma.RemoteError) { func (session *session) handlePostSignature(signature *irma.SignedMessage) (irma.ProofStatus, *irma.RemoteError) {
session.signature = signature session.markAlive()
session.disclosed, session.proofStatus = signature.Verify(conf.IrmaConfiguration, session.request.(*irma.SignatureRequest)) session.result.Signature = signature
session.result.Disclosed, session.result.Status = signature.Verify(
conf.IrmaConfiguration, session.request.(*irma.SignatureRequest))
session.finish() session.finish()
return session.proofStatus, nil return session.result.Status, nil
} }
func (session *session) handlePostProofs(proofs gabi.ProofList) (irma.ProofStatus, *irma.RemoteError) { 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() session.finish()
return session.proofStatus, nil return session.result.Status, nil
} }
// Session helpers // Session helpers
func (session *session) alive() bool { func (session *session) finished() bool {
return session.status != irmaserver.StatusDone && session.status != irmaserver.StatusCancelled return session.status == irmaserver.StatusDone || session.status == irmaserver.StatusCancelled
} }
func (session *session) finish() { func (session *session) finish() {
session.status = irmaserver.StatusDone session.status = irmaserver.StatusDone
session.result = &irmaserver.SessionResult{ }
Token: session.token,
Status: session.proofStatus, func (session *session) markAlive() {
Disclosed: session.disclosed, session.lastActive = time.Now()
Signature: session.signature,
}
} }
func (session *session) fail(err irmaserver.Error, message string) *irma.RemoteError { func (session *session) fail(err irmaserver.Error, message string) *irma.RemoteError {
......
...@@ -109,16 +109,16 @@ func (session *session) handlePostCommitments(commitments *gabi.IssueCommitmentM ...@@ -109,16 +109,16 @@ func (session *session) handlePostCommitments(commitments *gabi.IssueCommitmentM
if conf.IrmaConfiguration.SchemeManagers[schemeid].Distributed() { if conf.IrmaConfiguration.SchemeManagers[schemeid].Distributed() {
proofP, err := session.getProofP(commitments, schemeid) proofP, err := session.getProofP(commitments, schemeid)
if err != nil { if err != nil {
session.fail(irmaserver.ErrorKeyshareProofMissing, err.Error()) return nil, session.fail(irmaserver.ErrorKeyshareProofMissing, err.Error())
} }
proof.MergeProofP(proofP, pubkey) proof.MergeProofP(proofP, pubkey)
} }
} }
// Verify all proofs and check disclosed attributes, if any, against request // 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) 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, "") return nil, session.fail(irmaserver.ErrorInvalidProofs, "")
} }
......
...@@ -21,12 +21,9 @@ type session struct { ...@@ -21,12 +21,9 @@ type session struct {
request irma.SessionRequest request irma.SessionRequest
status irmaserver.Status status irmaserver.Status
active time.Time lastActive time.Time
returned bool
proofStatus irma.ProofStatus result *irmaserver.SessionResult
disclosed []*irma.DisclosedAttribute
signature *irma.SignedMessage
result *irmaserver.SessionResult
kssProofs map[irma.SchemeManagerIdentifier]*gabi.ProofP kssProofs map[irma.SchemeManagerIdentifier]*gabi.ProofP
} }
...@@ -80,7 +77,7 @@ func (s memorySessionStore) deleteExpired() { ...@@ -80,7 +77,7 @@ func (s memorySessionStore) deleteExpired() {
s.RLock() s.RLock()
expired := make([]string, 0, len(s.m)) expired := make([]string, 0, len(s.m))
for token, session := range 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) conf.Logger.Infof("Session %s expired, deleting", token)
expired = append(expired, token) expired = append(expired, token)
} }
...@@ -103,17 +100,21 @@ func (s memorySessionStore) deleteExpired() { ...@@ -103,17 +100,21 @@ func (s memorySessionStore) deleteExpired() {
var one *big.Int = big.NewInt(1) var one *big.Int = big.NewInt(1)
func newSession(action irma.Action, request irma.SessionRequest) *session { func newSession(action irma.Action, request irma.SessionRequest) *session {
token := newSessionToken()
s := &session{ s := &session{
action: action, action: action,
request: request, request: request,
status: irmaserver.StatusInitialized, status: irmaserver.StatusInitialized,
active: time.Now(), lastActive: time.Now(),
token: newSessionToken(), token: token,
result: &irmaserver.SessionResult{
Token: token,
},
} }
nonce, _ := gabi.RandomBigInt(gabi.DefaultSystemParameters[2048].Lstatzk) nonce, _ := gabi.RandomBigInt(gabi.DefaultSystemParameters[2048].Lstatzk)
request.SetNonce(nonce) request.SetNonce(nonce)
request.SetContext(one) request.SetContext(one)
sessions.add(s.token, s) sessions.add(token, s)
return s return s
} }
......
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