Skip to content

Commit 80d04a6

Browse files
committed
Process to retrieve identity token from GCP
1 parent c6f95f0 commit 80d04a6

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# typed: strict
2+
# frozen_string_literal: true
3+
4+
module ShopifyAPI
5+
module Auth
6+
module IdToken
7+
class GoogleIdToken
8+
METADATA_IDENTITY_URL = "http://metadata/computeMetadata/v1/instance/service-accounts/default/identity"
9+
10+
class << self
11+
extend T::Sig
12+
13+
sig { params(shop: String).returns(T.nilable(String)) }
14+
def request(shop:)
15+
response = HTTParty.get(
16+
"#{METADATA_IDENTITY_URL}?audience=#{shop}&format=full",
17+
headers: {
18+
"Metadata-Flavor" => "Google",
19+
},
20+
)
21+
22+
return unless response.success?
23+
24+
response.body
25+
end
26+
end
27+
end
28+
end
29+
end
30+
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# typed: true
2+
# frozen_string_literal: true
3+
4+
require_relative "../../test_helper"
5+
6+
module ShopifyAPITest
7+
module Auth
8+
module IdToken
9+
class GoogleIdTokenTest < Test::Unit::TestCase
10+
def setup
11+
super
12+
@sample_jwt = "eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJ0ZXN0LXNob3AubXlzaG9waWZ5LmlvIn0.CnoHeW_Z0oMKQcBPqezcbFyWLlkioPKPz8X0wchUkeQ"
13+
end
14+
15+
def test_successful_requests_returns_id_token
16+
stub_request(:get, "http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=test-shop.myshopify.io&format=full")
17+
.with(headers: { "Metadata-Flavor" => "Google" })
18+
.to_return(body: @sample_jwt, status: 200)
19+
20+
assert_equal(@sample_jwt, ShopifyAPI::Auth::IdToken::GoogleIdToken.request(shop: "test-shop.myshopify.io"))
21+
end
22+
23+
def test_unsuccessful_requests_returns_nil
24+
stub_request(:get, "http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=test-shop.myshopify.io&format=full")
25+
.with(headers: { "Metadata-Flavor" => "Google" })
26+
.to_return(status: 404)
27+
28+
assert_nil(ShopifyAPI::Auth::IdToken::GoogleIdToken.request(shop: "test-shop.myshopify.io"))
29+
end
30+
end
31+
end
32+
end
33+
end

0 commit comments

Comments
 (0)