|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../../errors" |
| 4 | +require_relative "../../server/provider" |
| 5 | + |
| 6 | +module MCP |
| 7 | + module Auth |
| 8 | + module Server |
| 9 | + module Handlers |
| 10 | + class AuthorizationRequest |
| 11 | + attr_reader :client_id, |
| 12 | + :redirect_uri, |
| 13 | + :code_challenge_method, |
| 14 | + :code_challenge, |
| 15 | + :response_type, |
| 16 | + :state, |
| 17 | + :scope |
| 18 | + |
| 19 | + def initialize( |
| 20 | + client_id: nil, |
| 21 | + redirect_uri: nil, |
| 22 | + code_challenge_method: nli, |
| 23 | + response_type: nil, |
| 24 | + code_challenge: nil, |
| 25 | + state: nil, |
| 26 | + scope: nil |
| 27 | + ) |
| 28 | + if client_id.nil? |
| 29 | + raise Errors::AuthorizationError.invalid_request("client_id must be defined") |
| 30 | + end |
| 31 | + |
| 32 | + if response_type != "code" |
| 33 | + raise Errors::AuthorizationError.new( |
| 34 | + error_code: Errors::AuthorizationError::UNSUPPORTED_RESPONSE_TYPE, |
| 35 | + message: "response_type must be 'code'", |
| 36 | + ) |
| 37 | + end |
| 38 | + |
| 39 | + if code_challenge_method != "S256" |
| 40 | + raise Errors::AuthorizationError.invalid_request("code_challenge_method must be 'S256'") |
| 41 | + end |
| 42 | + |
| 43 | + if code_challenge.nil? |
| 44 | + raise Errors::AuthorizationError.invalid_request("code_challenge must be defined") |
| 45 | + end |
| 46 | + |
| 47 | + @client_id = client_id |
| 48 | + @code_challenge = code_challenge |
| 49 | + @code_challenge_method = code_challenge_method |
| 50 | + @response_type = response_type |
| 51 | + @redirect_uri = redirect_uri |
| 52 | + @state = state |
| 53 | + @scope = scope |
| 54 | + end |
| 55 | + |
| 56 | + def scopes_array |
| 57 | + return [] if @scope.nil? |
| 58 | + |
| 59 | + @scope.split(" ") |
| 60 | + end |
| 61 | + |
| 62 | + def redirect_uri_provided? |
| 63 | + !@redirect_uri.nil? |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + class AuthorizationHandler |
| 68 | + def initialize( |
| 69 | + auth_server_provider:, |
| 70 | + request_parser: |
| 71 | + ) |
| 72 | + @auth_server_provider = auth_server_provider |
| 73 | + @request_parser = request_parser |
| 74 | + end |
| 75 | + |
| 76 | + def handle(request) |
| 77 | + # implements authorization requests for grant_type=code; |
| 78 | + # See https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.1 |
| 79 | + # For error handling, refer to https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.2.1 |
| 80 | + params_h = as_params_h(request) || {} |
| 81 | + client_info, redirect_uri, auth_error = get_client_and_redirect_uri(params_h) |
| 82 | + return bad_request_error(params_h:, auth_error:) if auth_error |
| 83 | + |
| 84 | + begin |
| 85 | + auth_request = AuthorizationRequest.new(**params_h) |
| 86 | + scopes = auth_request.scopes_array |
| 87 | + client_info.validate_scopes!(scopes) |
| 88 | + |
| 89 | + auth_params = AuthorizationParams.new( |
| 90 | + state: auth_request.state, |
| 91 | + scopes:, |
| 92 | + code_challenge: auth_request.code_challenge, |
| 93 | + redirect_uri:, |
| 94 | + redirect_uri_provided_explicitly: auth_request.redirect_uri_provided?, |
| 95 | + response_type: auth_request.response_type, |
| 96 | + ) |
| 97 | + |
| 98 | + location = @auth_server_provider.authorize(client_info:, auth_params:) |
| 99 | + headers = { |
| 100 | + "Cache-Control": "no-store", |
| 101 | + "Location": location, |
| 102 | + } |
| 103 | + [302, headers, nil] |
| 104 | + rescue => e |
| 105 | + error_code = case e |
| 106 | + in Errors::InvalidScopeError then Errors::AuthorizationError::INVALID_SCOPE |
| 107 | + in Errors::AuthorizationError then e.error_code |
| 108 | + else |
| 109 | + Errors::AuthorizationError::SERVER_ERROR |
| 110 | + end |
| 111 | + |
| 112 | + redirect_response_error( |
| 113 | + redirect_uri:, |
| 114 | + error_code:, |
| 115 | + error_description: e.message, |
| 116 | + params_h:, |
| 117 | + ) |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + private |
| 122 | + |
| 123 | + def as_params_h(request) |
| 124 | + @request_parser.get?(request) ? @request_parser.parse_query_params(request) : @request_parser.parse_body(request) |
| 125 | + end |
| 126 | + |
| 127 | + # Validates the client_id and redirect_uri parameters from the authorization request. |
| 128 | + # Returns a tuple of [client_info, redirect_uri, error] where: |
| 129 | + # - client_info is the OAuthClientInformationFull for the client if found and valid |
| 130 | + # - redirect_uri is a string derived from the params and client |
| 131 | + # - error is an AuthorizationError if validation fails, nil otherwise |
| 132 | + # |
| 133 | + # @param params_h [Hash] The authorization request parameters |
| 134 | + # @return [OAuthClientInformationFull, String, AuthorizationError] Tuple of client_info, redirect_uri, error |
| 135 | + def get_client_and_redirect_uri(params_h) |
| 136 | + client_id = params_h[:client_id] |
| 137 | + if client_id.nil? |
| 138 | + return [nil, nil, Errors::AuthorizationError.invalid_request("client_id must be defined")] |
| 139 | + end |
| 140 | + |
| 141 | + client_info = @auth_server_provider.get_client(client_id) |
| 142 | + if client_info.nil? |
| 143 | + return [nil, nil, Errors::AuthorizationError.invalid_request("client '#{client_id}' not found")] |
| 144 | + end |
| 145 | + |
| 146 | + redirect_uri = params_h[:redirect_uri] |
| 147 | + if client_info.multiple_redirect_uris? && redirect_uri.nil? |
| 148 | + return [nil, nil, Errors::AuthorizationError.invalid_request("redirect_uri must be defined because client defines multiple options")] |
| 149 | + end |
| 150 | + |
| 151 | + redirect_uri ||= client_info.redirect_uris.first |
| 152 | + if redirect_uri.nil? |
| 153 | + return [ |
| 154 | + nil, |
| 155 | + nil, |
| 156 | + Errors::AuthorizationError.new( |
| 157 | + error_code: Errors::AuthorizationError::SERVER_ERROR, message: "unable to select a redirect_uri", |
| 158 | + ), |
| 159 | + ] |
| 160 | + end |
| 161 | + |
| 162 | + unless client_info.valid_redirect_uri?(redirect_uri) |
| 163 | + return [client_info, nil, Errors::AuthorizationError.invalid_request("invalid redirect_uri")] |
| 164 | + end |
| 165 | + |
| 166 | + [client_info, redirect_uri, nil] |
| 167 | + end |
| 168 | + |
| 169 | + def redirect_response_error( |
| 170 | + redirect_uri:, |
| 171 | + error_code:, |
| 172 | + error_description:, |
| 173 | + params_h: |
| 174 | + ) |
| 175 | + end |
| 176 | + |
| 177 | + def bad_request_error( |
| 178 | + params_h:, |
| 179 | + auth_error: |
| 180 | + ) |
| 181 | + body = { error: auth_error.error_code, error_description: auth_error.message } |
| 182 | + if params_h[:state] |
| 183 | + body[:state] = params_h[:state] |
| 184 | + end |
| 185 | + |
| 186 | + [400, { "Cache-Control": "no-store" }, body] |
| 187 | + end |
| 188 | + end |
| 189 | + end |
| 190 | + end |
| 191 | + end |
| 192 | +end |
0 commit comments