-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauthhandler.go
219 lines (176 loc) · 5.76 KB
/
oauthhandler.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package mock
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
const (
bearerToken = "1/mZ1edKKACtPAb7zGlwSzvs72PvhAbGmB8K1ZrGxpcNM"
headerContentType = "Content-Type"
contentTypeApplicationJson = "application/json;charset=UTF-8"
)
type oauthHandler struct {
logger *log.Entry
}
func NewOauthHandler() *oauthHandler {
return &oauthHandler{
logger: log.WithField("Handler", "OAuth"),
}
}
type oauthResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
}
func (oh *oauthHandler) OAuthTargetHandler(w http.ResponseWriter, r *http.Request) {
err := oh.checkOauth(r)
if err != nil {
oh.logger.Error(err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
}
func (oh *oauthHandler) OAuthSpecHandler(w http.ResponseWriter, r *http.Request) {
err := oh.checkOauth(r)
if err != nil {
oh.logger.Error(err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
http.ServeFile(w, r, "spec.json")
}
func (oh *oauthHandler) checkOauth(r *http.Request) error {
headerAuthorization := r.Header.Get(AuthorizationHeader)
oAuthToken := strings.TrimPrefix(headerAuthorization, "Bearer ")
if oAuthToken != bearerToken {
return errors.New("Invalid token provided")
}
return nil
}
func (oh *oauthHandler) checkClientCredentials(r *http.Request, expectedClientId, expectedClientSecret string) error {
if expectedClientId == "" || expectedClientSecret == "" {
return fmt.Errorf("Expected credentials not provided. ClientID, ClientSecret or both not provided")
}
clientId, clientSecret, found := r.BasicAuth()
if !found {
return fmt.Errorf("basic credentials (clientId and clientSecret) not provided. Authorization header: %s", r.Header.Get(AuthorizationHeader))
}
if clientId != expectedClientId || clientSecret != expectedClientSecret {
return fmt.Errorf("Invalid credentials provided clientID: %s, clientSecret: %s", clientId, clientSecret)
}
return nil
}
func (oh *oauthHandler) checkQueryParams(r *http.Request, expectedParam, expectedParamValue string) error {
paramValue := r.URL.Query().Get(expectedParam)
if expectedParamValue != paramValue {
return fmt.Errorf("Invalid query parameter value provided")
}
return nil
}
func (oh *oauthHandler) checkHeaders(r *http.Request, expectedHeader, expectedHeaderValue string) error {
headerValue := r.Header.Get(expectedHeader)
if expectedHeaderValue != headerValue {
return errors.New("Invalid header value provided")
}
return nil
}
func (oh *oauthHandler) OAuthTokenHandler(w http.ResponseWriter, r *http.Request) {
oh.logger.Info("Handling Oauth token request")
vars := mux.Vars(r)
expectedClientId := vars["clientid"]
expectedClientSecret := vars["clientsecret"]
oh.logger.Infof("Handling OAuth secured spec request. Expected: clientID: %s, clientSecret: %s", expectedClientId, expectedClientSecret)
err := oh.checkClientCredentials(r, expectedClientId, expectedClientSecret)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
oh.logger.Error(err.Error())
return
}
err = respondWithOk(w)
if err != nil {
oh.logger.Error(err.Error())
return
}
}
func (oh *oauthHandler) OAuthTokenQueryParamsHandler(w http.ResponseWriter, r *http.Request) {
oh.logger.Info("Handling Oauth token request")
vars := mux.Vars(r)
expectedClientId := vars["clientid"]
expectedClientSecret := vars["clientsecret"]
expectedParam := vars["param"]
expectedParamValue := vars["value"]
oh.logger.Infof("Handling OAuth secured spec request. Expected: clientID: %s, clientSecret: %s, param: %s, with value: %s", expectedClientId, expectedClientSecret, expectedParam, expectedParamValue)
err := oh.checkQueryParams(r, expectedParam, expectedParamValue)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
oh.logger.Error(err.Error())
return
}
err = oh.checkClientCredentials(r, expectedClientId, expectedClientSecret)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
oh.logger.Error(err.Error())
return
}
err = respondWithOk(w)
if err != nil {
oh.logger.Error(err.Error())
return
}
}
func (oh *oauthHandler) OAuthTokenHeadersHandler(w http.ResponseWriter, r *http.Request) {
oh.logger.Info("Handling Oauth token request")
vars := mux.Vars(r)
expectedClientId := vars["clientid"]
expectedClientSecret := vars["clientsecret"]
expectedHeader := vars["header"]
expectedHeaderValue := vars["value"]
oh.logger.Infof("Handling OAuth secured spec request. Expected: clientID: %s, clientSecret: %s, header: %s, with value: %s", expectedClientId, expectedClientSecret, expectedHeader, expectedHeaderValue)
err := oh.checkHeaders(r, expectedHeader, expectedHeaderValue)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
oh.logger.Error(err.Error())
return
}
err = oh.checkClientCredentials(r, expectedClientId, expectedClientSecret)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
oh.logger.Error(err.Error())
return
}
err = respondWithOk(w)
if err != nil {
oh.logger.Error(err.Error())
return
}
}
func respondWithOk(w http.ResponseWriter) error {
oauthResponseOk := oauthResponse{
AccessToken: bearerToken,
TokenType: "Bearer",
ExpiresIn: 3600,
Scope: "",
}
var b bytes.Buffer
err := json.NewEncoder(&b).Encode(oauthResponseOk)
if err != nil {
return errors.New(fmt.Sprintf("Failed to marshall body, %s", err))
}
respond(w, http.StatusOK)
_, err = w.Write(b.Bytes())
if err != nil {
return errors.New(fmt.Sprintf("Failed to write response, %v", err))
}
return nil
}
func respond(w http.ResponseWriter, statusCode int) {
w.Header().Set(headerContentType, contentTypeApplicationJson)
w.WriteHeader(statusCode)
}