api.go 2.21 KB
Newer Older
1
2
3
package irmaserver

import (
4
5
6
7
	"encoding/json"
	"net/http"
	"runtime/debug"

8
9
10
11
12
	"github.com/Sirupsen/logrus"
	"github.com/mhe/gabi"
	"github.com/privacybydesign/irmago"
)

13
14
var Logger *logrus.Logger = logrus.StandardLogger()

15
16
type Configuration struct {
	IrmaConfigurationPath string
17
18
19
	PrivateKeysPath       string

	Logger *logrus.Logger
20
21
22
23
24
25

	PrivateKeys       map[irma.IssuerIdentifier]*gabi.PrivateKey
	IrmaConfiguration *irma.Configuration
}

type SessionResult struct {
Sietse Ringers's avatar
Sietse Ringers committed
26
27
28
29
30
31
	Token       string
	Status      Status
	ProofStatus irma.ProofStatus
	Disclosed   []*irma.DisclosedAttribute
	Signature   *irma.SignedMessage
	Err         *irma.RemoteError
32
33
}

Sietse Ringers's avatar
Sietse Ringers committed
34
// Status is the status of an IRMA session.
35
36
37
type Status string

const (
Sietse Ringers's avatar
Sietse Ringers committed
38
39
40
41
42
	StatusInitialized Status = "INITIALIZED" // The session has been started and is waiting for the client
	StatusConnected   Status = "CONNECTED"   // The client has retrieved the session request, we wait for its response
	StatusCancelled   Status = "CANCELLED"   // The session is cancelled, possibly due to an error
	StatusDone        Status = "DONE"        // The session has completed successfully
	StatusTimeout     Status = "TIMEOUT"     // Session timed out
43
)
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

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
}
71
72
73
74
75
76
77
78
79
80
81
82
83
84

func WriteError(w http.ResponseWriter, err Error, msg string) {
	status, bts := JsonResponse(nil, RemoteError(err, msg))
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(status)
	w.Write(bts)
}

func WriteJson(w http.ResponseWriter, object interface{}) {
	status, bts := JsonResponse(object, nil)
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(status)
	w.Write(bts)
}