Class: Clacky::Mcp::OAuth::AuthorizationManager

Inherits:
Object
  • Object
show all
Defined in:
lib/clacky/mcp/oauth/authorization_manager.rb

Defined Under Namespace

Classes: CallbackReceiver, Error, Response

Constant Summary collapse

MAX_RESPONSE_BYTES =
1_048_576

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_name:, config:, store:, requester: nil, callback: nil, clock: nil) ⇒ AuthorizationManager

Returns a new instance of AuthorizationManager.



25
26
27
28
29
30
31
32
# File 'lib/clacky/mcp/oauth/authorization_manager.rb', line 25

def initialize(server_name:, config:, store:, requester: nil, callback: nil, clock: nil)
  @server_name = server_name
  @config = config
  @store = store
  @requester = requester || method(:request)
  @callback = callback
  @clock = clock || -> { Time.now.to_i }
end

Class Method Details

.pkce_challenge(verifier) ⇒ Object



21
22
23
# File 'lib/clacky/mcp/oauth/authorization_manager.rb', line 21

def self.pkce_challenge(verifier)
  Base64.urlsafe_encode64(Digest::SHA256.digest(verifier), padding: false)
end

Instance Method Details

#login(resource_metadata_url: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/clacky/mcp/oauth/authorization_manager.rb', line 34

def (resource_metadata_url: nil)
  raise Error, "MCP server '#{@server_name}' is not configured for OAuth" unless @config.enabled?

  receiver = @callback || CallbackReceiver.new
  redirect_uri = receiver.respond_to?(:redirect_uri) ? receiver.redirect_uri : "http://127.0.0.1:52963/callback"
   = ()
  client = register_client(, redirect_uri)
  verifier = SecureRandom.urlsafe_base64(48, false)
  state = SecureRandom.urlsafe_base64(32, false)
  authorization_url = build_authorization_url(, client.fetch("client_id"), redirect_uri, verifier, state)
  callback_result = receiver.call(authorization_url, state)
  raise Error, "OAuth callback state mismatch" unless callback_result["state"].to_s == state
  raise Error, "OAuth authorization returned no code" if callback_result["code"].to_s.empty?

  token = token_request(.fetch("token_endpoint"), {
    "grant_type" => "authorization_code",
    "code" => callback_result.fetch("code"),
    "redirect_uri" => redirect_uri,
    "client_id" => client.fetch("client_id"),
    "code_verifier" => verifier
  })
  grant = normalize_token(token).merge(
    "client_id" => client.fetch("client_id"),
    "client_secret" => client["client_secret"],
    "token_endpoint_auth_method" => client["token_endpoint_auth_method"] || "none",
    "token_endpoint" => .fetch("token_endpoint"),
    "authorization_server" => .fetch("issuer"),
    "resource" => @config.resource
  )
  @store.save(grant)
ensure
  receiver.close if defined?(receiver) && receiver.respond_to?(:close)
end

#refresh(grant) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/clacky/mcp/oauth/authorization_manager.rb', line 68

def refresh(grant)
  refresh_token = grant["refresh_token"].to_s
  raise Error, "OAuth refresh token is missing; log in again" if refresh_token.empty?

  fields = {
    "grant_type" => "refresh_token",
    "refresh_token" => refresh_token,
    "client_id" => grant.fetch("client_id")
  }
  token = token_request(grant.fetch("token_endpoint"), fields,
                        client_secret: grant["client_secret"],
                        auth_method: grant["token_endpoint_auth_method"])
  updated = grant.merge(normalize_token(token))
  updated["refresh_token"] = refresh_token if token["refresh_token"].to_s.empty?
  updated
rescue KeyError
  raise Error, "stored OAuth grant is incomplete; log in again"
end