@@ -5,38 +5,37 @@ package main
5
5
6
6
import (
7
7
"encoding/json"
8
- "log"
9
8
"io/ioutil"
9
+ "log"
10
10
"net/http"
11
11
"net/http/httputil"
12
12
"net/url"
13
13
"os"
14
- "strings"
15
14
"time"
16
15
)
17
16
18
17
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
23
22
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
30
29
)
31
30
32
- // Structure for storing results from a
31
+ // Structure for storing results from a
33
32
type AuthReponse struct {
34
33
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"`
37
36
}
38
37
39
- // Proxies the incoming request to the downstream, adding Authorization
38
+ // Proxies the incoming request to the downstream, adding Authorization
40
39
// header and optional API Key header
41
40
func handleRequestAndRedirect (res http.ResponseWriter , req * http.Request ) {
42
41
url , err := url .Parse (proxy_downstream_url )
@@ -52,7 +51,7 @@ func handleRequestAndRedirect(res http.ResponseWriter, req *http.Request) {
52
51
req .URL .Scheme = url .Scheme
53
52
req .Host = url .Host
54
53
55
- req .Header .Set ("Authorization" , token_type + " " + access_token )
54
+ req .Header .Set ("Authorization" , token_type + " " + access_token )
56
55
57
56
if api_key != "" {
58
57
req .Header .Set (api_key_header , api_key )
@@ -63,32 +62,34 @@ func handleRequestAndRedirect(res http.ResponseWriter, req *http.Request) {
63
62
}
64
63
65
64
// 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
+ }
68
71
if auth_scope != "" {
69
- request_body = request_body + "& scope=" + auth_scope
72
+ request_body . Set ( " scope" , auth_scope )
70
73
}
71
74
72
- request_body_reader := strings .NewReader (request_body )
73
-
74
75
retry_number := - 1
75
76
76
77
for {
77
78
retry_number ++
78
-
79
- if retry_number > 5 {
79
+
80
+ if retry_number > 5 {
80
81
log .Print ("Failed to acquire access token; exiting" )
81
82
break
82
83
} else if retry_number > 0 {
83
- seconds_to_wait := retry_number * retry_number + 1
84
+ seconds_to_wait := retry_number * retry_number + 1
84
85
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
86
87
time .Sleep (retry_time )
87
88
log .Printf ("Retry #%v" , retry_number )
88
89
}
89
90
90
91
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 )
92
93
93
94
if err != nil {
94
95
log .Print (err )
@@ -107,7 +108,7 @@ func getOuath2AuthAccessToken(){
107
108
log .Print (err )
108
109
continue
109
110
}
110
-
111
+
111
112
//TODO: Error handling on unmarshalling the JSON payload
112
113
auth_response := AuthReponse {}
113
114
err = json .Unmarshal (body , & auth_response )
@@ -123,8 +124,8 @@ func getOuath2AuthAccessToken(){
123
124
124
125
access_token = auth_response .AccessToken
125
126
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 ))
128
129
129
130
log .Print ("Access token updated" )
130
131
log .Printf ("Token refresh scheduled at %s" , token_refresh_time )
@@ -141,7 +142,7 @@ func handleTokenRefresh() {
141
142
getOuath2AuthAccessToken ()
142
143
}
143
144
time .Sleep (30 * time .Second )
144
- }
145
+ }
145
146
}
146
147
147
148
// Retrieves a named environment variable. validates that required
@@ -154,11 +155,11 @@ func getEnvironmentVariable(key string, required bool, secret bool, fallback str
154
155
log .Printf ("%s=%s" , key , value )
155
156
}
156
157
return value
157
- }
158
+ }
158
159
159
160
if required {
160
161
log .Fatalf ("Environment variable %s must be supplied" , key )
161
- }
162
+ }
162
163
163
164
if fallback != "" {
164
165
log .Printf ("%s=%s (Default Value)" , key , fallback )
@@ -177,7 +178,7 @@ func initVariables() {
177
178
api_key = getEnvironmentVariable ("PROXY_API_KEY" , false , true , "" )
178
179
if api_key != "" {
179
180
api_key_header = getEnvironmentVariable ("PROXY_API_KEY_HEADER" , false , false , "x-api-key" )
180
- }
181
+ }
181
182
}
182
183
183
184
// Main program entrypoint
@@ -204,4 +205,4 @@ func main() {
204
205
if err := http .ListenAndServe (listen_address , nil ); err != nil {
205
206
panic (err )
206
207
}
207
- }
208
+ }
0 commit comments