-
Notifications
You must be signed in to change notification settings - Fork 28
/
result_token.go
72 lines (61 loc) · 1.62 KB
/
result_token.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
38
39
40
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
66
67
68
69
70
71
72
package handler
import (
"encoding/json"
"net/http"
"github.com/authgear/authgear-server/pkg/lib/oauth/protocol"
)
type (
tokenResultOK struct {
Response protocol.TokenResponse
}
tokenResultError struct {
StatusCode int
InternalError bool
Response protocol.ErrorResponse
}
tokenResultEmpty struct{}
)
func (t tokenResultOK) WriteResponse(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
rw.Header().Set("Cache-Control", "no-store")
rw.Header().Set("Pragma", "no-cache")
rw.WriteHeader(http.StatusOK)
encoder := json.NewEncoder(rw)
err := encoder.Encode(t.Response)
if err != nil {
http.Error(rw, err.Error(), 500)
}
}
func (t tokenResultOK) IsInternalError() bool {
return false
}
func (t tokenResultError) WriteResponse(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
rw.Header().Set("Cache-Control", "no-store")
rw.Header().Set("Pragma", "no-cache")
if t.InternalError {
rw.WriteHeader(http.StatusInternalServerError)
} else {
if t.StatusCode == 0 {
rw.WriteHeader(http.StatusBadRequest)
} else {
rw.WriteHeader(t.StatusCode)
}
}
encoder := json.NewEncoder(rw)
err := encoder.Encode(t.Response)
if err != nil {
http.Error(rw, err.Error(), 500)
}
}
func (t tokenResultError) IsInternalError() bool {
return t.InternalError
}
func (t tokenResultEmpty) WriteResponse(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Cache-Control", "no-store")
rw.Header().Set("Pragma", "no-cache")
rw.WriteHeader(http.StatusOK)
}
func (t tokenResultEmpty) IsInternalError() bool {
return false
}