diff --git a/getting-started/authenticating-users/Gemfile b/getting-started/authenticating-users/Gemfile index ffacdd899..b2ff410ed 100644 --- a/getting-started/authenticating-users/Gemfile +++ b/getting-started/authenticating-users/Gemfile @@ -8,3 +8,8 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" } gem "jwt", "~> 2.2" gem "sinatra", "~> 2.0" # [END getting_started_requirements] + +group :development, :test do + gem "minitest", "~> 5.16" + gem "rack-test", "~> 2.1" +end diff --git a/getting-started/authenticating-users/app.rb b/getting-started/authenticating-users/app.rb index ef40a495a..211d6b67e 100644 --- a/getting-started/authenticating-users/app.rb +++ b/getting-started/authenticating-users/app.rb @@ -60,7 +60,12 @@ def validate_assertion assertion a_header = Base64.decode64 assertion.split(".")[0] key_id = JSON.parse(a_header)["kid"] cert = OpenSSL::PKey::EC.new settings.certificates[key_id] - info = JWT.decode assertion, cert, true, algorithm: "ES256", aud: settings.audience + info = JWT.decode assertion, cert, true, + algorithm: "ES256", + aud: settings.audience, + verify_aud: true, + iss: "https://cloud.google.com/iap", + verify_iss: true [info[0]["email"], info[0]["sub"]] rescue StandardError => e puts "Failed to validate assertion: #{e}" diff --git a/getting-started/authenticating-users/test/app_test.rb b/getting-started/authenticating-users/test/app_test.rb new file mode 100644 index 000000000..37db5c0f6 --- /dev/null +++ b/getting-started/authenticating-users/test/app_test.rb @@ -0,0 +1,130 @@ +# frozen_string_literal: true + +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +ENV["RACK_ENV"] = "test" + +require "json" +require "minitest/autorun" +require "net/http" +require "openssl" +require "rack/test" + +IAP_TEST_EC_KEY = OpenSSL::PKey::EC.generate "prime256v1" +IAP_OTHER_EC_KEY = OpenSSL::PKey::EC.generate "prime256v1" +IAP_TEST_KID = "test-key-1" +IAP_EXPECTED_AUD = "/projects/123456/apps/my-project-id" +IAP_EXPECTED_ISS = "https://cloud.google.com/iap" + +fake_cert_response = Struct.new(:body).new( + JSON.generate(IAP_TEST_KID => IAP_TEST_EC_KEY.public_to_pem) +) +fake_metadata_http = Object.new +def fake_metadata_http.request req + value = req.path.end_with?("numeric-project-id") ? "123456" : "my-project-id" + Struct.new(:body).new value +end + +Net::HTTP.stub :get_response, fake_cert_response do + Net::HTTP.stub :new, fake_metadata_http do + require_relative "../app" + end +end + +describe "authenticating-users app" do + include Rack::Test::Methods + + def app + Sinatra::Application + end + + def call_validate_assertion token + result = nil + capture_io do + result = Sinatra::Application.new!.send :validate_assertion, token + end + result + end + + def build_iap_jwt(aud: IAP_EXPECTED_AUD, + iss: IAP_EXPECTED_ISS, + exp: Time.now.to_i + 300, + email: "user@example.com", + key: IAP_TEST_EC_KEY, + kid: IAP_TEST_KID) + payload = { + "aud" => aud, + "iss" => iss, + "exp" => exp, + "iat" => Time.now.to_i - 10, + "email" => email, + "sub" => "accounts.google.com:12345" + }.compact + JWT.encode payload, key, "ES256", { "kid" => kid } + end + + it "accepts a valid IAP JWT with matching audience and issuer" do + token = build_iap_jwt + assert_equal ["user@example.com", "accounts.google.com:12345"], + call_validate_assertion(token) + + header "X-Goog-Iap-Jwt-Assertion", token + get "/" + + assert_equal 200, last_response.status + assert_equal "