Class: MCP::Client::OAuth::InMemoryStorage
- Inherits:
-
Object
- Object
- MCP::Client::OAuth::InMemoryStorage
- Defined in:
- lib/mcp/client/oauth/in_memory_storage.rb
Overview
Default reference implementation of the storage contract that
Provider uses to persist OAuth state. Holds the two pieces of data
the flow saves and reads on every request:
tokens: the hash returned by the token endpoint (access_token, optionalrefresh_token,expires_in,scope, etc.).client_information: the hash returned by Dynamic Client Registration or supplied as pre-registered credentials (client_id, optionalclient_secret, optionaltoken_endpoint_auth_method). The SDK additionally stamps an"issuer"member binding the credentials to the authorization server that issued them (SEP-2352); custom storages should treat the hash as opaque and persist it as-is.
A provider without a callback_handler also keeps each pending authorization here, keyed by its state,
between the request that sends the user to the authorization server and the request that receives
the redirect (save_pending_authorization(state, pending), pending_authorization(state),
delete_pending_authorization(state)). A pending authorization holds the PKCE verifier, so custom storages
should treat it as a secret and persist it as-is. Flow#finish! touches only the entry its callback's state
names, so an authorization the user never finished stays until the storage drops it: this class drops entries
older than pending_authorization_max_age the next time one is saved, and a custom storage should expire them,
with a TTL of that age. delete_pending_authorization must remove the entry and return it in one atomic step
(Hash#delete under a mutex here; GETDEL in Redis, DELETE ... RETURNING in SQL), returning nil when
there was none: the flow redeems the code only when it gets the entry back.
This class keeps everything in process memory, so the credentials live
only for the lifetime of the Ruby process. Applications that need
persistence across restarts should supply a custom object responding to
the same four-method contract (tokens, save_tokens(t),
client_information, save_client_information(info)) and pass it via
Provider.new(storage: ...). The shape mirrors Python SDK's
TokenStorage Protocol; TypeScript's OAuthClientProvider rolls
the same responsibilities into a single object.
A web application may receive the redirect in another process, so pending authorizations
need shared storage.
Instance Attribute Summary collapse
-
#client_information ⇒ Object
Returns the value of attribute client_information.
-
#tokens ⇒ Object
Returns the value of attribute tokens.
Instance Method Summary collapse
-
#delete_pending_authorization(state) ⇒ Object
The mutex is what makes the removal and the return one step on every Ruby implementation:
Hash#deletealone is atomic only where a global interpreter lock serializes it, andFlow#finish!relies on exactly one of two callbacks racing on the samestategetting the entry back. -
#initialize(pending_authorization_max_age: Provider::DEFAULT_PENDING_AUTHORIZATION_MAX_AGE) ⇒ InMemoryStorage
constructor
A new instance of InMemoryStorage.
-
#inspect ⇒ Object
Every hash this storage holds may carry a secret (an access token, a client secret, a PKCE verifier), and
inspectis what error reporters and consoles print an object with, so it shows only whether each is present. - #pending_authorization(state) ⇒ Object
- #save_client_information(info) ⇒ Object
-
#save_pending_authorization(state, pending) ⇒ Object
Drops every pending authorization older than
pending_authorization_max_agebefore saving the new one. - #save_tokens(tokens) ⇒ Object
Constructor Details
#initialize(pending_authorization_max_age: Provider::DEFAULT_PENDING_AUTHORIZATION_MAX_AGE) ⇒ InMemoryStorage
Returns a new instance of InMemoryStorage.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mcp/client/oauth/in_memory_storage.rb', line 47 def initialize(pending_authorization_max_age: Provider::DEFAULT_PENDING_AUTHORIZATION_MAX_AGE) unless .is_a?(Integer) && .positive? raise ArgumentError, "pending_authorization_max_age must be a positive Integer number of seconds (got #{.inspect})." end @tokens = nil @client_information = nil @pending_authorizations = {} @pending_authorization_max_age = @pending_authorizations_mutex = Mutex.new end |
Instance Attribute Details
#client_information ⇒ Object
Returns the value of attribute client_information.
41 42 43 |
# File 'lib/mcp/client/oauth/in_memory_storage.rb', line 41 def client_information @client_information end |
#tokens ⇒ Object
Returns the value of attribute tokens.
41 42 43 |
# File 'lib/mcp/client/oauth/in_memory_storage.rb', line 41 def tokens @tokens end |
Instance Method Details
#delete_pending_authorization(state) ⇒ Object
The mutex is what makes the removal and the return one step on every Ruby implementation: Hash#delete
alone is atomic only where a global interpreter lock serializes it, and Flow#finish! relies on exactly
one of two callbacks racing on the same state getting the entry back.
86 87 88 |
# File 'lib/mcp/client/oauth/in_memory_storage.rb', line 86 def (state) @pending_authorizations_mutex.synchronize { @pending_authorizations.delete(state) } end |
#inspect ⇒ Object
Every hash this storage holds may carry a secret (an access token, a client secret, a PKCE verifier),
and inspect is what error reporters and consoles print an object with, so it shows only whether each is present.
92 93 94 95 96 |
# File 'lib/mcp/client/oauth/in_memory_storage.rb', line 92 def inspect pending_count = @pending_authorizations_mutex.synchronize { @pending_authorizations.size } "#<#{self.class.name} tokens=#{present_or_nil(@tokens)} client_information=#{present_or_nil(@client_information)} pending_authorizations=#{pending_count}>" end |
#pending_authorization(state) ⇒ Object
79 80 81 |
# File 'lib/mcp/client/oauth/in_memory_storage.rb', line 79 def (state) @pending_authorizations_mutex.synchronize { @pending_authorizations[state] } end |
#save_client_information(info) ⇒ Object
63 64 65 |
# File 'lib/mcp/client/oauth/in_memory_storage.rb', line 63 def save_client_information(info) @client_information = info end |
#save_pending_authorization(state, pending) ⇒ Object
Drops every pending authorization older than pending_authorization_max_age before saving the new one.
The flow refuses such an entry when it is looked up, but only a callback carrying its state looks it up,
and an authorization the user abandoned never gets one; every 401 a provider without a callback_handler meets
starts another, so without this the entries, each holding the verifier and the authorization server metadata,
would accumulate for the life of the process.
72 73 74 75 76 77 |
# File 'lib/mcp/client/oauth/in_memory_storage.rb', line 72 def (state, pending) @pending_authorizations_mutex.synchronize do @pending_authorizations[state] = pending end end |
#save_tokens(tokens) ⇒ Object
59 60 61 |
# File 'lib/mcp/client/oauth/in_memory_storage.rb', line 59 def save_tokens(tokens) @tokens = tokens end |