forked from keyz182/go-oauth2-server
/
response.go
37 lines (31 loc) · 1.12 KB
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package response
import (
"encoding/json"
"fmt"
"net/http"
)
var realm = "go_oauth2_server"
// WriteJSON writes JSON response
func WriteJSON(w http.ResponseWriter, v interface{}, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
json.NewEncoder(w).Encode(v)
}
// NoContent writes a 204 no content response
func NoContent(w http.ResponseWriter) {
w.WriteHeader(http.StatusNoContent)
}
// Error produces a JSON error response with the following structure:
// {"error":"some error message"}
func Error(w http.ResponseWriter, err string, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
json.NewEncoder(w).Encode(map[string]string{"error": err})
}
// UnauthorizedError has to contain WWW-Authenticate header
// See http://self-issued.info/docs/draft-ietf-oauth-v2-bearer.html#rfc.section.3
func UnauthorizedError(w http.ResponseWriter, err string) {
// TODO - include error if the request contained an access token
w.Header().Set("WWW-Authenticate", fmt.Sprintf("Bearer realm=%s", realm))
Error(w, err, http.StatusUnauthorized)
}