|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "json" |
| 4 | +require "uri" |
| 5 | + |
| 6 | +module MCP |
| 7 | + class Client |
| 8 | + module OAuth |
| 9 | + # RFC 8693 token exchange against an enterprise identity provider, turning an IdP-issued ID token into |
| 10 | + # an Identity Assertion Authorization Grant (ID-JAG) per the MCP Enterprise Managed Authorization extension (SEP-990). |
| 11 | + # The returned ID-JAG is an opaque assertion the client then presents to the MCP authorization server with |
| 12 | + # the RFC 7523 `jwt-bearer` grant (see `CrossAppAccessProvider`). |
| 13 | + # Mirrors `requestJwtAuthorizationGrant` in the TypeScript SDK. |
| 14 | + # |
| 15 | + # - https://github.com/modelcontextprotocol/modelcontextprotocol/issues/990 |
| 16 | + # - https://www.rfc-editor.org/rfc/rfc8693 |
| 17 | + module IDJAGTokenExchange |
| 18 | + # Raised when the identity provider's token exchange fails or returns something other than an ID-JAG. |
| 19 | + class ExchangeError < StandardError; end |
| 20 | + |
| 21 | + GRANT_TYPE = "urn:ietf:params:oauth:grant-type:token-exchange" |
| 22 | + ID_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:id_token" |
| 23 | + ID_JAG_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:id-jag" |
| 24 | + |
| 25 | + class << self |
| 26 | + # Exchanges `id_token` for an ID-JAG at the IdP's token endpoint and returns the assertion string. |
| 27 | + # |
| 28 | + # @param token_endpoint [String] The identity provider's token endpoint. |
| 29 | + # @param id_token [String] The IdP-issued ID token (the subject token). |
| 30 | + # @param client_id [String] The client's identifier at the IdP. |
| 31 | + # @param audience [String] The MCP authorization server's issuer identifier. |
| 32 | + # @param resource [String] The canonical MCP server URL (RFC 8707). |
| 33 | + # @param http_client [Object, nil] Faraday-compatible client; built lazily by default. |
| 34 | + def request(token_endpoint:, id_token:, client_id:, audience:, resource:, http_client: nil) |
| 35 | + http_client ||= default_http_client |
| 36 | + |
| 37 | + response = begin |
| 38 | + http_client.post(token_endpoint) do |req| |
| 39 | + req.headers["Content-Type"] = "application/x-www-form-urlencoded" |
| 40 | + req.headers["Accept"] = "application/json" |
| 41 | + req.body = URI.encode_www_form( |
| 42 | + "grant_type" => GRANT_TYPE, |
| 43 | + "subject_token" => id_token, |
| 44 | + "subject_token_type" => ID_TOKEN_TYPE, |
| 45 | + "requested_token_type" => ID_JAG_TOKEN_TYPE, |
| 46 | + "audience" => audience, |
| 47 | + "resource" => resource, |
| 48 | + "client_id" => client_id, |
| 49 | + ) |
| 50 | + end |
| 51 | + rescue Faraday::Error => e |
| 52 | + raise ExchangeError, "Token exchange request to #{token_endpoint} failed: #{e.class}: #{e.message}." |
| 53 | + end |
| 54 | + |
| 55 | + if response.status < 200 || response.status >= 300 |
| 56 | + raise ExchangeError, "Identity provider token exchange returned status #{response.status}." |
| 57 | + end |
| 58 | + |
| 59 | + parse_id_jag(response) |
| 60 | + end |
| 61 | + |
| 62 | + private |
| 63 | + |
| 64 | + def parse_id_jag(response) |
| 65 | + body = response.body.is_a?(String) ? response.body : response.body.to_s |
| 66 | + parsed = begin |
| 67 | + JSON.parse(body) |
| 68 | + rescue JSON::ParserError => e |
| 69 | + raise ExchangeError, "Failed to parse token exchange response: #{e.message}." |
| 70 | + end |
| 71 | + |
| 72 | + unless parsed.is_a?(Hash) |
| 73 | + raise ExchangeError, "Token exchange response is not a JSON object (got #{parsed.class})." |
| 74 | + end |
| 75 | + |
| 76 | + issued_token_type = parsed["issued_token_type"] |
| 77 | + unless issued_token_type == ID_JAG_TOKEN_TYPE |
| 78 | + raise ExchangeError, |
| 79 | + "Token exchange did not issue an ID-JAG " \ |
| 80 | + "(expected issued_token_type #{ID_JAG_TOKEN_TYPE.inspect}, got #{issued_token_type.inspect})." |
| 81 | + end |
| 82 | + |
| 83 | + assertion = parsed["access_token"] |
| 84 | + if assertion.nil? || assertion.to_s.empty? |
| 85 | + raise ExchangeError, "Token exchange response is missing `access_token`." |
| 86 | + end |
| 87 | + |
| 88 | + assertion |
| 89 | + end |
| 90 | + |
| 91 | + def default_http_client |
| 92 | + require "faraday" |
| 93 | + Faraday.new do |faraday| |
| 94 | + faraday.headers["Accept"] = "application/json" |
| 95 | + end |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | +end |
0 commit comments