transport.go 4.17 KB
Newer Older
1
package irmago
2
3
4
5

import (
	"bytes"
	"encoding/json"
6
	"fmt"
7
8
9
10
11
12
13
	"io"
	"io/ioutil"
	"net/http"
	"strings"
	"time"
)

Sietse Ringers's avatar
Sietse Ringers committed
14
// HTTPTransport sends and receives JSON messages to a HTTP server.
15
16
17
18
19
type HTTPTransport struct {
	Server string
	client *http.Client
}

20
21
22
23
24
25
26
27
28
29
30
31
// ErrorCode are session errors.
type ErrorCode string

// Error is a protocol error.
type Error struct {
	Err error
	ErrorCode
	*ApiError
	Info   string
	Status int
}

Sietse Ringers's avatar
Sietse Ringers committed
32
// ApiError is an error message returned by the API server on errors.
33
34
type ApiError struct {
	Status      int    `json:"status"`
35
	ErrorName   string `json:"error"`
36
37
38
39
40
	Description string `json:"description"`
	Message     string `json:"message"`
	Stacktrace  string `json:"stacktrace"`
}

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Protocol errors
const (
	// Protocol version not supported
	ErrorProtocolVersionNotSupported = ErrorCode("versionNotSupported")
	// Error in HTTP communication
	ErrorTransport = ErrorCode("httpError")
	// Invalid client JWT in first IRMA message
	ErrorInvalidJWT = ErrorCode("invalidJwt")
	// Unkown session type (not disclosing, signing, or issuing)
	ErrorUnknownAction = ErrorCode("unknownAction")
	// Crypto error during calculation of our response (second IRMA message)
	ErrorCrypto = ErrorCode("cryptoResponseError")
	// Server rejected our response (second IRMA message)
	ErrorRejected = ErrorCode("rejectedByServer")
	// (De)serializing of a message failed
	ErrorSerialization = ErrorCode("serializationError")
)

func (e *Error) Error() string {
	if e.Err != nil {
		return fmt.Sprintf("%s: %s", string(e.ErrorCode), e.Err.Error())
	}
	return string(e.ErrorCode)
}

Sietse Ringers's avatar
Sietse Ringers committed
66
// NewHTTPTransport returns a new HTTPTransport.
67
68
69
70
71
72
73
74
75
76
77
78
79
func NewHTTPTransport(serverURL string) *HTTPTransport {
	url := serverURL
	if !strings.HasSuffix(url, "/") {
		url += "/"
	}
	return &HTTPTransport{
		Server: url,
		client: &http.Client{
			Timeout: time.Second * 5,
		},
	}
}

Sietse Ringers's avatar
Sietse Ringers committed
80
func (transport *HTTPTransport) request(url string, method string, result interface{}, object interface{}) *Error {
81
82
83
84
85
86
87
	if method != http.MethodPost && method != http.MethodGet {
		panic("Unsupported HTTP method " + method)
	}
	if method == http.MethodGet && object != nil {
		panic("Cannot GET and also post an object")
	}

88
	var isstr bool
89
90
	var reader io.Reader
	if object != nil {
91
92
93
94
95
96
		var objstr string
		if objstr, isstr = object.(string); isstr {
			reader = bytes.NewBuffer([]byte(objstr))
		} else {
			marshaled, err := json.Marshal(object)
			if err != nil {
97
				return &Error{Err: err, ErrorCode: ErrorSerialization}
Sietse Ringers's avatar
Sietse Ringers committed
98
				//return &TransportError{Err: err.Error()}
99
			}
100
			//fmt.Printf("POST: %s\n", string(marshaled))
101
			reader = bytes.NewBuffer(marshaled)
102
103
104
105
106
		}
	}

	req, err := http.NewRequest(method, transport.Server+url, reader)
	if err != nil {
107
		return &Error{Err: err, ErrorCode: ErrorTransport}
108
109
110
111
	}

	req.Header.Set("User-Agent", "irmago")
	if object != nil {
112
113
114
115
116
		if isstr {
			req.Header.Set("Content-Type", "text/plain; charset=UTF-8")
		} else {
			req.Header.Set("Content-Type", "application/json; charset=UTF-8")
		}
117
118
119
120
	}

	res, err := transport.client.Do(req)
	if err != nil {
121
		return &Error{Err: err, ErrorCode: ErrorTransport}
122
123
124
125
	}

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
126
		return &Error{Err: err, Status: res.StatusCode}
127
128
129
130
131
	}
	if res.StatusCode != 200 {
		apierr := &ApiError{}
		json.Unmarshal(body, apierr)
		if apierr.ErrorName == "" { // Not an ApiErrorMessage
Sietse Ringers's avatar
Sietse Ringers committed
132
			return &Error{ErrorCode: ErrorTransport, Status: res.StatusCode}
133
		}
134
		//fmt.Printf("ERROR: %+v\n", apierr)
Sietse Ringers's avatar
Sietse Ringers committed
135
		return &Error{ErrorCode: ErrorTransport, Status: res.StatusCode, ApiError: apierr}
136
137
	}

138
	//fmt.Printf("RESPONSE: %s\n", string(body))
139
140
	err = json.Unmarshal(body, result)
	if err != nil {
141
		return &Error{Err: err, Status: res.StatusCode}
142
143
144
145
146
	}

	return nil
}

Sietse Ringers's avatar
Sietse Ringers committed
147
// Post sends the object to the server and parses its response into result.
Sietse Ringers's avatar
Sietse Ringers committed
148
func (transport *HTTPTransport) Post(url string, result interface{}, object interface{}) *Error {
149
150
151
	return transport.request(url, http.MethodPost, result, object)
}

Sietse Ringers's avatar
Sietse Ringers committed
152
// Get performs a GET request and parses the server's response into result.
Sietse Ringers's avatar
Sietse Ringers committed
153
func (transport *HTTPTransport) Get(url string, result interface{}) *Error {
154
155
156
	return transport.request(url, http.MethodGet, result, nil)
}

Sietse Ringers's avatar
Sietse Ringers committed
157
// Delete performs a DELETE.
158
159
160
func (transport *HTTPTransport) Delete(url string) {
	// TODO
}