sgx-jmp
created pr with
sgx-jmp/access-token-endpoint.1
commented
Changes are available at https://tangled.org/mrry.band/sgx-jmp on branch access-token-endpoint.
cmds
checkout latest patchset:
ssh pr.pico.sh pull sgx-jmp:access-token-endpoint | git am -3checkout specific patchset revision:
ssh pr.pico.sh pull sgx-jmp:access-token-endpoint [rev] | git am -3add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh sgx-jmp:access-token-endpointadd comment to patch request:
ssh pr.pico.sh comment sgx-jmp:access-token-endpoint "lgtm!"
Patchset
sgx-jmp/access-token-endpoint.1
Add endpoint to get bandwidth access token
Eric Roberts
2026-11-11T18:27:33ZSo our proxied endpoints can use it.
Semantic diff summary
0 added,
0 modified,
0 signature changed,
0 removed
across 0 analyzed files
(2 files skipped: unsupported file type)
+54
-0
test/test_web.rb
#
| ... | ... | @@ -171,6 +171,60 @@ class WebTest < Minitest::Test | |
| 171 | 171 | Web.app | |
| 172 | 172 | end | |
| 173 | 173 | ||
| 174 | + | def test_bandwidth_auth_loopback | |
| 175 | + | client_id = "test-client" | |
| 176 | + | client_secret = "test-secret" | |
| 177 | + | config = Bandwidth::Configuration.new | |
| 178 | + | config.client_id = client_id | |
| 179 | + | config.client_secret = client_secret | |
| 180 | + | ||
| 181 | + | stub_request( | |
| 182 | + | :post, "https://api.bandwidth.com/api/v1/oauth2/token" | |
| 183 | + | ).with( | |
| 184 | + | basic_auth: [client_id, client_secret], | |
| 185 | + | body: "grant_type=client_credentials" | |
| 186 | + | ).to_return(body: { access_token: "test-token", expires_in: 3600 }.to_json) | |
| 187 | + | ||
| 188 | + | Bandwidth::Configuration.stub(:default, config) do | |
| 189 | + | ["127.0.0.1", "::1"].each do |ip| | |
| 190 | + | get "/bandwidth-auth", {}, "REMOTE_ADDR" => ip | |
| 191 | + | assert_equal 204, last_response.status | |
| 192 | + | assert_equal "Bearer test-token", last_response.headers["Authorization"] | |
| 193 | + | assert_equal "no-store", last_response.headers["Cache-Control"] | |
| 194 | + | assert_empty last_response.body | |
| 195 | + | end | |
| 196 | + | end | |
| 197 | + | end | |
| 198 | + | ||
| 199 | + | def test_bandwidth_auth_rejects_remote_and_forwarded_loopback | |
| 200 | + | Bandwidth::Configuration.stub(:default, -> { flunk "Requested token" }) do | |
| 201 | + | get "/bandwidth-auth", {}, | |
| 202 | + | "REMOTE_ADDR" => "192.0.2.1", "HTTP_X_FORWARDED_FOR" => "127.0.0.1" | |
| 203 | + | assert_equal 403, last_response.status | |
| 204 | + | assert_nil last_response.headers["Authorization"] | |
| 205 | + | end | |
| 206 | + | end | |
| 207 | + | ||
| 208 | + | def test_bandwidth_auth_failure | |
| 209 | + | config = Bandwidth::Configuration.new | |
| 210 | + | config.access_token_getter = -> { raise "something went wrong" } | |
| 211 | + | ||
| 212 | + | Bandwidth::Configuration.stub(:default, config) do | |
| 213 | + | get "/bandwidth-auth", {}, "REMOTE_ADDR" => "127.0.0.1" | |
| 214 | + | assert_equal 502, last_response.status | |
| 215 | + | assert_nil last_response.headers["Authorization"] | |
| 216 | + | assert_empty last_response.body | |
| 217 | + | end | |
| 218 | + | end | |
| 219 | + | ||
| 220 | + | def test_bandwidth_auth_missing_token | |
| 221 | + | Bandwidth::Configuration.stub(:default, Bandwidth::Configuration.new) do | |
| 222 | + | get "/bandwidth-auth", {}, "REMOTE_ADDR" => "127.0.0.1" | |
| 223 | + | assert_equal 502, last_response.status | |
| 224 | + | assert_nil last_response.headers["Authorization"] | |
| 225 | + | end | |
| 226 | + | end | |
| 227 | + | ||
| 174 | 228 | def test_outbound_forwards | |
| 175 | 229 | CustomerPlan::DB.expect( | |
| 176 | 230 | :query_one, {}, [String, "customerid"] |
+22
-0
web.rb
#
| ... | ... | @@ -258,6 +259,27 @@ class Web < Roda | |
| 258 | 259 | end | |
| 259 | 260 | ||
| 260 | 261 | route do |r| | |
| 262 | + | r.get "bandwidth-auth" do | |
| 263 | + | unless ["127.0.0.1", "::1"].include?(r.env["REMOTE_ADDR"]) | |
| 264 | + | r.halt [403, response.headers, []] | |
| 265 | + | end | |
| 266 | + | response["Cache-Control"] = "no-store" | |
| 267 | + | config = Bandwidth::Configuration.default | |
| 268 | + | authorization = config.oauth_bearer_token | |
| 269 | + | if authorization.nil? | |
| 270 | + | log_error("Bandwidth token unavailable: client credentials missing") | |
| 271 | + | r.halt [502, response.headers, []] | |
| 272 | + | end | |
| 273 | + | ||
| 274 | + | response["Authorization"] = authorization | |
| 275 | + | response.status = 204 | |
| 276 | + | "" | |
| 277 | + | rescue StandardError => e | |
| 278 | + | log_error(e) | |
| 279 | + | response.status = 502 | |
| 280 | + | "" | |
| 281 | + | end | |
| 282 | + | ||
| 261 | 283 | r.get "healthcheck" do | |
| 262 | 284 | "OK" | |
| 263 | 285 | end |