-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
162 lines (133 loc) · 3.98 KB
/
proxy.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
package main
// TODO handle errors if no Refresh Token is given
// TODO ensure proper error handling
// TODO replace all ReverseProxy with a custom one
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
)
type OAuthAPI struct {
AccessTokenURL *url.URL
ClientId string
ClientSecret string
}
type Proxy struct {
HeaderName string
BasePath string
Key []byte
OAuthAPI OAuthAPI
ServeMux *http.ServeMux
}
func (p Proxy) ReverseProxyHandler() *httputil.ReverseProxy {
return &httputil.ReverseProxy{
Director: func(req *http.Request) {
start := len(p.BasePath)
req.URL.Path = req.URL.Path[start:]
req.URL.Scheme = p.OAuthAPI.AccessTokenURL.Scheme
req.URL.Host = p.OAuthAPI.AccessTokenURL.Host
req.Header.Add("Authorization", "Bearer invalid_token")
token := req.Header.Get(p.HeaderName)
if token != "" {
at, err := DecodeAccessToken(p.Key, token)
if err == nil {
req.Header.Set("Authorization", "Bearer "+at.AccessToken)
}
}
},
}
}
func (p Proxy) AuthHandler() *httputil.ReverseProxy {
return &httputil.ReverseProxy{
Director: func(req *http.Request) {
var credentials = struct {
Username string `json:"username"`
Password string `json:"password"`
}{}
err := json.NewDecoder(req.Body).Decode(&credentials)
if err != nil {
log.Println("Something is wrong with the body, should return 4xx error.")
}
req.Body.Close()
values := &url.Values{}
values.Set("client_id", p.OAuthAPI.ClientId)
values.Set("client_secret", p.OAuthAPI.ClientSecret)
values.Set("username", credentials.Username)
values.Set("password", credentials.Password)
values.Set("grant_type", "password")
req.URL = p.OAuthAPI.AccessTokenURL
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Body, req.ContentLength = createFormBodyFromValues(values)
},
Transport: &TokenTransport{Key: p.Key},
}
}
func (p Proxy) RefreshAuthHandler() *httputil.ReverseProxy {
return &httputil.ReverseProxy{
Director: func(req *http.Request) {
values := &url.Values{}
values.Set("client_id", p.OAuthAPI.ClientId)
values.Set("client_secret", p.OAuthAPI.ClientSecret)
values.Set("grant_type", "refresh_token")
values.Set("refresh_token", "invalid_token")
token := req.Header.Get(p.HeaderName)
if token != "" {
at, err := DecodeAccessToken(p.Key, token)
if err == nil {
values.Set("refresh_token", at.RefreshToken)
}
}
req.URL = p.OAuthAPI.AccessTokenURL
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Body, req.ContentLength = createFormBodyFromValues(values)
},
Transport: &TokenTransport{Key: p.Key},
}
}
func createFormBodyFromValues(values *url.Values) (io.ReadCloser, int64) {
data := values.Encode()
return ioutil.NopCloser(bytes.NewBufferString(data)), int64(len(data))
}
func (p Proxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
p.ServeMux.ServeHTTP(w, req)
}
func (p Proxy) Handle(pattern string, handler http.Handler) {
p.ServeMux.Handle(pattern, handler)
}
func (p Proxy) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
p.ServeMux.HandleFunc(pattern, handler)
}
type TokenTransport struct {
Key []byte
}
func (t *TokenTransport) RoundTrip(request *http.Request) (*http.Response, error) {
response, err := http.DefaultTransport.RoundTrip(request)
if response.StatusCode >= 400 {
response.Body = ioutil.NopCloser(bytes.NewBufferString(""))
response.ContentLength = int64(0)
response.StatusCode = 403
response.Status = "403 Forbidden"
return response, err
}
body, _ := ioutil.ReadAll(response.Body)
var at AccessToken
json.Unmarshal(body, &at)
token, err := at.Encode(t.Key)
if err != nil {
return response, err
}
as := struct {
Token string `json:"token"`
}{
Token: token,
}
newBody, _ := json.Marshal(as)
response.Body = ioutil.NopCloser(bytes.NewBuffer(newBody))
response.ContentLength = int64(len(body))
return response, err
}