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

Move HTTP helper functions to other package for reuse

parent 392daf80
package irmaserver
import (
"encoding/json"
"net/http"
"runtime/debug"
"github.com/Sirupsen/logrus"
"github.com/mhe/gabi"
"github.com/privacybydesign/irmago"
)
var Logger *logrus.Logger = logrus.StandardLogger()
type Configuration struct {
IrmaConfigurationPath string
PrivateKeysPath string
......@@ -35,3 +41,30 @@ const (
StatusDone Status = "DONE" // The session has completed successfully
StatusTimeout Status = "TIMEOUT" // Session timed out
)
func RemoteError(err Error, message string) *irma.RemoteError {
stack := string(debug.Stack())
Logger.Errorf("Error: %d %s %s\n%s", err.Status, err.Type, message, stack)
return &irma.RemoteError{
Status: err.Status,
Description: err.Description,
ErrorName: string(err.Type),
Message: message,
Stacktrace: stack,
}
}
func JsonResponse(v interface{}, err *irma.RemoteError) (int, []byte) {
msg := v
status := http.StatusOK
if err != nil {
msg = err
status = err.Status
}
b, e := json.Marshal(msg)
if e != nil {
Logger.Error("Failed to serialize response:", e.Error())
return http.StatusInternalServerError, nil
}
return status, b
}
......@@ -24,6 +24,7 @@ func Initialize(configuration *irmaserver.Configuration) error {
conf.Logger.Level = logrus.DebugLevel
conf.Logger.Formatter = &logrus.TextFormatter{}
}
irmaserver.Logger = conf.Logger
if conf.IrmaConfiguration == nil {
var err error
......@@ -129,7 +130,7 @@ func HandleProtocolMessage(
matches := pattern.FindStringSubmatch(path)
if len(matches) != 3 {
conf.Logger.Warnf("Invalid URL: %s", path)
status, output = responseJson(nil, getError(irmaserver.ErrorInvalidRequest, ""))
status, output = irmaserver.JsonResponse(nil, irmaserver.RemoteError(irmaserver.ErrorInvalidRequest, ""))
return
}
......@@ -139,7 +140,7 @@ func HandleProtocolMessage(
session := sessions.get(token)
if session == nil {
conf.Logger.Warnf("Session not found: %s", token)
status, output = responseJson(nil, getError(irmaserver.ErrorSessionUnknown, ""))
status, output = irmaserver.JsonResponse(nil, irmaserver.RemoteError(irmaserver.ErrorSessionUnknown, ""))
return
}
session.Lock()
......@@ -169,53 +170,53 @@ func HandleProtocolMessage(
min := &irma.ProtocolVersion{}
max := &irma.ProtocolVersion{}
if err := json.Unmarshal([]byte(h.Get(irma.MinVersionHeader)), min); err != nil {
status, output = responseJson(nil, session.fail(irmaserver.ErrorMalformedInput, err.Error()))
status, output = irmaserver.JsonResponse(nil, session.fail(irmaserver.ErrorMalformedInput, err.Error()))
return
}
if err := json.Unmarshal([]byte(h.Get(irma.MaxVersionHeader)), max); err != nil {
status, output = responseJson(nil, session.fail(irmaserver.ErrorMalformedInput, err.Error()))
status, output = irmaserver.JsonResponse(nil, session.fail(irmaserver.ErrorMalformedInput, err.Error()))
return
}
status, output = responseJson(session.handleGetRequest(min, max))
status, output = irmaserver.JsonResponse(session.handleGetRequest(min, max))
return
}
status, output = responseJson(nil, session.fail(irmaserver.ErrorInvalidRequest, ""))
status, output = irmaserver.JsonResponse(nil, session.fail(irmaserver.ErrorInvalidRequest, ""))
return
default:
if method != http.MethodPost {
status, output = responseJson(nil, session.fail(irmaserver.ErrorInvalidRequest, ""))
status, output = irmaserver.JsonResponse(nil, session.fail(irmaserver.ErrorInvalidRequest, ""))
return
}
if verb == "commitments" && session.action == irma.ActionIssuing {
commitments := &gabi.IssueCommitmentMessage{}
if err := irma.UnmarshalValidate(message, commitments); err != nil {
status, output = responseJson(nil, session.fail(irmaserver.ErrorMalformedInput, ""))
status, output = irmaserver.JsonResponse(nil, session.fail(irmaserver.ErrorMalformedInput, ""))
return
}
status, output = responseJson(session.handlePostCommitments(commitments))
status, output = irmaserver.JsonResponse(session.handlePostCommitments(commitments))
return
}
if verb == "proofs" && session.action == irma.ActionDisclosing {
proofs := gabi.ProofList{}
if err := irma.UnmarshalValidate(message, &proofs); err != nil {
status, output = responseJson(nil, session.fail(irmaserver.ErrorMalformedInput, ""))
status, output = irmaserver.JsonResponse(nil, session.fail(irmaserver.ErrorMalformedInput, ""))
return
}
status, output = responseJson(session.handlePostProofs(proofs))
status, output = irmaserver.JsonResponse(session.handlePostProofs(proofs))
return
}
if verb == "proofs" && session.action == irma.ActionSigning {
signature := &irma.SignedMessage{}
if err := irma.UnmarshalValidate(message, signature); err != nil {
status, output = responseJson(nil, session.fail(irmaserver.ErrorMalformedInput, ""))
status, output = irmaserver.JsonResponse(nil, session.fail(irmaserver.ErrorMalformedInput, ""))
return
}
status, output = responseJson(session.handlePostSignature(signature))
status, output = irmaserver.JsonResponse(session.handlePostSignature(signature))
return
}
status, output = responseJson(nil, session.fail(irmaserver.ErrorInvalidRequest, ""))
status, output = irmaserver.JsonResponse(nil, session.fail(irmaserver.ErrorInvalidRequest, ""))
return
}
}
......@@ -25,7 +25,7 @@ func (session *session) handleDelete() {
func (session *session) handleGetRequest(min, max *irma.ProtocolVersion) (irma.SessionRequest, *irma.RemoteError) {
if session.status != irmaserver.StatusInitialized {
return nil, getError(irmaserver.ErrorUnexpectedRequest, "Session already started")
return nil, irmaserver.RemoteError(irmaserver.ErrorUnexpectedRequest, "Session already started")
}
session.markAlive()
......@@ -41,7 +41,7 @@ func (session *session) handleGetRequest(min, max *irma.ProtocolVersion) (irma.S
func (session *session) handlePostSignature(signature *irma.SignedMessage) (*irma.ProofStatus, *irma.RemoteError) {
if session.status != irmaserver.StatusConnected {
return nil, getError(irmaserver.ErrorUnexpectedRequest, "Session not yet started or already finished")
return nil, irmaserver.RemoteError(irmaserver.ErrorUnexpectedRequest, "Session not yet started or already finished")
}
session.markAlive()
......@@ -65,7 +65,7 @@ func (session *session) handlePostSignature(signature *irma.SignedMessage) (*irm
func (session *session) handlePostProofs(proofs gabi.ProofList) (*irma.ProofStatus, *irma.RemoteError) {
if session.status != irmaserver.StatusConnected {
return nil, getError(irmaserver.ErrorUnexpectedRequest, "Session not yet started or already finished")
return nil, irmaserver.RemoteError(irmaserver.ErrorUnexpectedRequest, "Session not yet started or already finished")
}
session.markAlive()
......@@ -88,7 +88,7 @@ func (session *session) handlePostProofs(proofs gabi.ProofList) (*irma.ProofStat
func (session *session) handlePostCommitments(commitments *gabi.IssueCommitmentMessage) ([]*gabi.IssueSignatureMessage, *irma.RemoteError) {
if session.status != irmaserver.StatusConnected {
return nil, getError(irmaserver.ErrorUnexpectedRequest, "Session not yet started or already finished")
return nil, irmaserver.RemoteError(irmaserver.ErrorUnexpectedRequest, "Session not yet started or already finished")
}
session.markAlive()
......
package backend
import (
"encoding/json"
"net/http"
"runtime/debug"
"strconv"
"time"
......@@ -32,41 +29,12 @@ func (session *session) setStatus(status irmaserver.Status) {
}
func (session *session) fail(err irmaserver.Error, message string) *irma.RemoteError {
rerr := getError(err, message)
rerr := irmaserver.RemoteError(err, message)
session.setStatus(irmaserver.StatusCancelled)
session.result = &irmaserver.SessionResult{Err: rerr, Token: session.token, Status: irmaserver.StatusCancelled}
return rerr
}
// Output helpers
func getError(err irmaserver.Error, message string) *irma.RemoteError {
stack := string(debug.Stack())
conf.Logger.Errorf("Error: %d %s %s\n%s", err.Status, err.Type, message, stack)
return &irma.RemoteError{
Status: err.Status,
Description: err.Description,
ErrorName: string(err.Type),
Message: message,
Stacktrace: stack,
}
}
func responseJson(v interface{}, err *irma.RemoteError) (int, []byte) {
msg := v
status := http.StatusOK
if err != nil {
msg = err
status = err.Status
}
b, e := json.Marshal(msg)
if e != nil {
conf.Logger.Error("Failed to serialize response:", e.Error())
return http.StatusInternalServerError, nil
}
return status, b
}
// Issuance helpers
func validateIssuanceRequest(request *irma.IssuanceRequest) error {
......
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