This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
context.go
176 lines (144 loc) · 3.51 KB
/
context.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package hero
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
)
type responseType int
const (
responseData responseType = iota
responseRedirect
)
var (
//errNotRedirectResponse is returned when Response is not of type ResponseRedirect
errNotRedirectResponse = errors.New("Not redirect response")
)
type context struct {
Type responseType
// StatusCode is the http status code eg 200
StatusCode int
// StatusText is any text to be assoiated with the response.
StatusText string
// URL is the redirection URL
URL string
// Data is a key value pairs to be encoded in the url query.
Data map[string]interface{}
// Headers are response headers
Headers http.Header
// HasError is true when there was an error
HasError bool
// ErrID is the id of the error if HasError is true. This will be searched in the
// BaseOauthErrs
ErrID string
InternalError error
RedirectInFragment bool
Response http.ResponseWriter
}
func newContext(w http.ResponseWriter) *context {
ctx := &context{
Type: responseData,
StatusCode: http.StatusOK,
Data: make(map[string]interface{}),
Headers: make(http.Header),
Response: w,
}
ctx.Headers.Add(
"Cache-Control",
"no-cache, no-store, max-age=0, must-revalidate",
)
ctx.Headers.Add("Pragma", "no-cache")
ctx.Headers.Add("Expires", "Fri, 01 Jan 1990 00:00:00 GMT")
return ctx
}
// SetData stores key value in the context
func (ctx *context) SetData(key string, value interface{}) {
ctx.Data[key] = value
}
// ClearData deletes all stored key values.
func (ctx *context) ClearData() {
for k := range ctx.Data {
delete(ctx.Data, k)
}
}
func (ctx *context) SetError(id, desc string) {
ctx.SetErrorURI(id, desc, "", "")
}
func (ctx *context) SetErrorState(id, desc, state string) {
ctx.SetErrorURI(id, desc, "", state)
}
func (ctx *context) SetRedirect(redirURL string) {
ctx.Type = responseRedirect
ctx.URL = redirURL
}
func (ctx *context) SetRedirectFragment(hasFragment bool) {
ctx.RedirectInFragment = hasFragment
}
func (ctx *context) SetErrorURI(id, desc, uri, state string) {
if desc == "" {
desc = baseOauthErrs.Get(id)
}
ctx.HasError = true
ctx.ErrID = id
if ctx.StatusCode != http.StatusOK {
ctx.StatusText = desc
}
ctx.ClearData()
ctx.SetData(params.error, id)
ctx.SetData(params.errDesc, desc)
ctx.SetData(params.errURI, uri)
if state != "" {
ctx.SetData(params.state, state)
}
}
// GetRedirectURL returns redirect url.
func (ctx *context) GetRedirectURL() (string, error) {
if ctx.Type != responseRedirect {
return "", errNotRedirectResponse
}
link, err := url.Parse(ctx.URL)
if err != nil {
return "", err
}
q := link.Query()
for k, v := range ctx.Data {
q.Set(k, fmt.Sprint(v))
}
link.RawQuery = q.Encode()
if ctx.RedirectInFragment {
link.RawQuery = ""
link.Fragment, err = url.QueryUnescape(q.Encode())
if err != nil {
return "", err
}
}
return link.String(), nil
}
func (ctx *context) CommitJSON() error {
if ctx.InternalError != nil {
// TODO log this?
}
for k, h := range ctx.Headers {
for _, v := range h {
ctx.Response.Header().Add(k, v)
}
}
switch ctx.Type {
case responseRedirect:
link, err := ctx.GetRedirectURL()
if err != nil {
return err
}
ctx.Response.Header().Add(params.location, link)
ctx.Response.WriteHeader(http.StatusFound)
default:
ctx.Response.Header().Set("Content-Type", "application/json")
ctx.Response.WriteHeader(ctx.StatusCode)
err := json.NewEncoder(ctx.Response).Encode(ctx.Data)
if err != nil {
return err
}
}
return nil
}