|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | +require "jwt" |
| 5 | +require "openssl" |
| 6 | +require "base64" |
| 7 | +require "webmock/minitest" |
| 8 | + |
| 9 | +module MCP |
| 10 | + class Server |
| 11 | + module OAuth |
| 12 | + class JWTVerifierMiddlewareTest < Minitest::Test |
| 13 | + ISSUER = "https://as.example.com" |
| 14 | + AUDIENCE = "https://mcp.example.com/mcp" |
| 15 | + JWKS_URI = "https://as.example.com/.well-known/jwks.json" |
| 16 | + METADATA = ProtectedResourceMetadata.new(resource: AUDIENCE, authorization_servers: [ISSUER]) |
| 17 | + |
| 18 | + def setup |
| 19 | + @key = OpenSSL::PKey::EC.generate("prime256v1") |
| 20 | + @public_key = OpenSSL::PKey.read(@key.public_to_pem) |
| 21 | + @jwks = { keys: [JWT::JWK.new(@public_key, { kid: "ec-key", use: "sig", alg: "ES256" }).export] } |
| 22 | + @claims = { "iss" => ISSUER, "aud" => AUDIENCE, "exp" => Time.now.to_i + 300 } |
| 23 | + end |
| 24 | + |
| 25 | + def test_ps256_token_with_an_ec_key_returns_401 |
| 26 | + assert_ps256_rejected(key: @public_key) |
| 27 | + end |
| 28 | + |
| 29 | + def test_ps256_token_with_a_static_ec_jwks_returns_401 |
| 30 | + assert_ps256_rejected(jwks: @jwks) |
| 31 | + end |
| 32 | + |
| 33 | + def test_ps256_token_with_a_fetched_ec_jwks_returns_401 |
| 34 | + stub_request(:get, JWKS_URI).to_return(body: JSON.generate(@jwks), headers: { "content-type" => "application/json" }) |
| 35 | + |
| 36 | + assert_ps256_rejected(jwks_uri: JWKS_URI) |
| 37 | + assert_requested(:get, JWKS_URI, times: 1) |
| 38 | + end |
| 39 | + |
| 40 | + private |
| 41 | + |
| 42 | + def assert_ps256_rejected(**key_source) |
| 43 | + verifier = JWTVerifier.new(resource_metadata: METADATA, **key_source) |
| 44 | + app = mock |
| 45 | + app.expects(:call).once.returns([200, {}, ["authenticated"]]) |
| 46 | + middleware = Middleware.new(app, token_verifier: verifier, resource_metadata: METADATA) |
| 47 | + reports = [] |
| 48 | + MCP.configuration.stubs(:exception_reporter).returns(->(error, _context) { reports << error }) |
| 49 | + valid_token = JWT.encode(@claims, @key, "ES256", { kid: "ec-key" }) |
| 50 | + |
| 51 | + status, = middleware.call({ "HTTP_AUTHORIZATION" => "Bearer #{valid_token}" }) |
| 52 | + |
| 53 | + assert_equal(200, status) |
| 54 | + |
| 55 | + header = Base64.urlsafe_encode64(JSON.generate(alg: "PS256", kid: "ec-key"), padding: false) |
| 56 | + payload = Base64.urlsafe_encode64(JSON.generate(@claims), padding: false) |
| 57 | + token = "#{header}.#{payload}.AAAA" |
| 58 | + |
| 59 | + status, headers, body = middleware.call({ "HTTP_AUTHORIZATION" => "Bearer #{token}" }) |
| 60 | + |
| 61 | + assert_equal(401, status) |
| 62 | + assert_includes(headers.fetch("www-authenticate"), 'error="invalid_token"') |
| 63 | + assert_equal("invalid_token", JSON.parse(body.join).fetch("error")) |
| 64 | + assert_empty(reports) |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | +end |
0 commit comments