Skip to content

Commit 6ef55b8

Browse files
authored
Merge pull request #1 from westonsteimel/post-form
use http.PostForm in place of http.Post
2 parents c4ae16b + fe0fd36 commit 6ef55b8

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

proxy.go

+36-35
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,37 @@ package main
55

66
import (
77
"encoding/json"
8-
"log"
98
"io/ioutil"
9+
"log"
1010
"net/http"
1111
"net/http/httputil"
1212
"net/url"
1313
"os"
14-
"strings"
1514
"time"
1615
)
1716

1817
var (
19-
auth_endpoint_url string
20-
auth_client_id string
21-
auth_client_secret string
22-
auth_scope string
18+
auth_endpoint_url string
19+
auth_client_id string
20+
auth_client_secret string
21+
auth_scope string
2322
proxy_downstream_url string
24-
proxy_port string
25-
access_token string
26-
token_type string
27-
token_refresh_time time.Time
28-
api_key string
29-
api_key_header string
23+
proxy_port string
24+
access_token string
25+
token_type string
26+
token_refresh_time time.Time
27+
api_key string
28+
api_key_header string
3029
)
3130

32-
// Structure for storing results from a
31+
// Structure for storing results from a
3332
type AuthReponse struct {
3433
AccessToken string `json:"access_token"`
35-
ExpiresIn int `json:"expires_in"`
36-
TokenType string `json:"token_type"`
34+
ExpiresIn int `json:"expires_in"`
35+
TokenType string `json:"token_type"`
3736
}
3837

39-
// Proxies the incoming request to the downstream, adding Authorization
38+
// Proxies the incoming request to the downstream, adding Authorization
4039
// header and optional API Key header
4140
func handleRequestAndRedirect(res http.ResponseWriter, req *http.Request) {
4241
url, err := url.Parse(proxy_downstream_url)
@@ -52,7 +51,7 @@ func handleRequestAndRedirect(res http.ResponseWriter, req *http.Request) {
5251
req.URL.Scheme = url.Scheme
5352
req.Host = url.Host
5453

55-
req.Header.Set("Authorization", token_type + " " + access_token)
54+
req.Header.Set("Authorization", token_type+" "+access_token)
5655

5756
if api_key != "" {
5857
req.Header.Set(api_key_header, api_key)
@@ -63,32 +62,34 @@ func handleRequestAndRedirect(res http.ResponseWriter, req *http.Request) {
6362
}
6463

6564
// Gets (or refreshes) the access token using a jittered backed-off retry
66-
func getOuath2AuthAccessToken(){
67-
request_body := "grant_type=client_credentials&client_id=" + auth_client_id + "&client_secret=" + auth_client_secret
65+
func getOuath2AuthAccessToken() {
66+
request_body := url.Values{
67+
"grant_type": {"client_credentials"},
68+
"client_id": {auth_client_id},
69+
"client_secret": {auth_client_secret},
70+
}
6871
if auth_scope != "" {
69-
request_body = request_body + "&scope=" + auth_scope
72+
request_body.Set("scope", auth_scope)
7073
}
7174

72-
request_body_reader := strings.NewReader(request_body)
73-
7475
retry_number := -1
7576

7677
for {
7778
retry_number++
78-
79-
if retry_number > 5 {
79+
80+
if retry_number > 5 {
8081
log.Print("Failed to acquire access token; exiting")
8182
break
8283
} else if retry_number > 0 {
83-
seconds_to_wait := retry_number * retry_number + 1
84+
seconds_to_wait := retry_number*retry_number + 1
8485
log.Printf("Failed to aquired token; awaiting retry #%v in %v seconds", retry_number, seconds_to_wait)
85-
retry_time := time.Duration(seconds_to_wait) * time.Second
86+
retry_time := time.Duration(seconds_to_wait) * time.Second
8687
time.Sleep(retry_time)
8788
log.Printf("Retry #%v", retry_number)
8889
}
8990

9091
log.Printf("Sending authentication request via POST to %s", auth_endpoint_url)
91-
resp, err := http.Post(auth_endpoint_url, "application/x-www-form-urlencoded", request_body_reader)
92+
resp, err := http.PostForm(auth_endpoint_url, request_body)
9293

9394
if err != nil {
9495
log.Print(err)
@@ -107,7 +108,7 @@ func getOuath2AuthAccessToken(){
107108
log.Print(err)
108109
continue
109110
}
110-
111+
111112
//TODO: Error handling on unmarshalling the JSON payload
112113
auth_response := AuthReponse{}
113114
err = json.Unmarshal(body, &auth_response)
@@ -123,8 +124,8 @@ func getOuath2AuthAccessToken(){
123124

124125
access_token = auth_response.AccessToken
125126
token_type = auth_response.TokenType
126-
expires := auth_response.ExpiresIn - ( 60 * 5 )
127-
token_refresh_time = time.Now().UTC().Add(time.Second * time.Duration(expires))
127+
expires := auth_response.ExpiresIn - (60 * 5)
128+
token_refresh_time = time.Now().UTC().Add(time.Second * time.Duration(expires))
128129

129130
log.Print("Access token updated")
130131
log.Printf("Token refresh scheduled at %s", token_refresh_time)
@@ -141,7 +142,7 @@ func handleTokenRefresh() {
141142
getOuath2AuthAccessToken()
142143
}
143144
time.Sleep(30 * time.Second)
144-
}
145+
}
145146
}
146147

147148
// Retrieves a named environment variable. validates that required
@@ -154,11 +155,11 @@ func getEnvironmentVariable(key string, required bool, secret bool, fallback str
154155
log.Printf("%s=%s", key, value)
155156
}
156157
return value
157-
}
158+
}
158159

159160
if required {
160161
log.Fatalf("Environment variable %s must be supplied", key)
161-
}
162+
}
162163

163164
if fallback != "" {
164165
log.Printf("%s=%s (Default Value)", key, fallback)
@@ -177,7 +178,7 @@ func initVariables() {
177178
api_key = getEnvironmentVariable("PROXY_API_KEY", false, true, "")
178179
if api_key != "" {
179180
api_key_header = getEnvironmentVariable("PROXY_API_KEY_HEADER", false, false, "x-api-key")
180-
}
181+
}
181182
}
182183

183184
// Main program entrypoint
@@ -204,4 +205,4 @@ func main() {
204205
if err := http.ListenAndServe(listen_address, nil); err != nil {
205206
panic(err)
206207
}
207-
}
208+
}

0 commit comments

Comments
 (0)