api.go 2.46 KB
Newer Older
Sietse Ringers's avatar
Sietse Ringers committed
1
package server
2
3

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
	IssuerPrivateKeysPath string
18

19
	Logger *logrus.Logger `json:"-"`
20

21
22
	IssuerPrivateKeys map[irma.IssuerIdentifier]*gabi.PrivateKey `json:"-"`
	IrmaConfiguration *irma.Configuration                        `json:"-"`
23
24
25
}

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

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

const (
Sietse Ringers's avatar
Sietse Ringers committed
39
40
41
42
43
	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
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
71

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
}
72
73

func WriteError(w http.ResponseWriter, err Error, msg string) {
74
	WriteResponse(w, nil, RemoteError(err, msg))
75
76
77
}

func WriteJson(w http.ResponseWriter, object interface{}) {
78
79
80
81
82
	WriteResponse(w, object, nil)
}

func WriteResponse(w http.ResponseWriter, object interface{}, rerr *irma.RemoteError) {
	status, bts := JsonResponse(object, rerr)
83
84
85
86
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(status)
	w.Write(bts)
}
87
88
89
90
91
92

func WriteString(w http.ResponseWriter, str string) {
	w.Header().Set("Content-Type", "text/plain")
	w.WriteHeader(http.StatusOK)
	w.Write([]byte(str))
}