Class: MCPClient::Auth::PKCE

Inherits:
Object
  • Object
show all
Defined in:
lib/mcp_client/auth.rb

Overview

PKCE (Proof Key for Code Exchange) helper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code_verifier: nil, code_challenge: nil, code_challenge_method: nil) ⇒ PKCE

Generate PKCE parameters

Parameters:

  • code_verifier (String, nil) (defaults to: nil)

    Existing code verifier (for deserialization)

  • code_challenge (String, nil) (defaults to: nil)

    Existing code challenge (for deserialization)

  • code_challenge_method (String) (defaults to: nil)

    Challenge method (default: ‘S256’)



323
324
325
326
327
# File 'lib/mcp_client/auth.rb', line 323

def initialize(code_verifier: nil, code_challenge: nil, code_challenge_method: nil)
  @code_verifier = code_verifier || generate_code_verifier
  @code_challenge = code_challenge || generate_code_challenge(@code_verifier)
  @code_challenge_method = code_challenge_method || 'S256'
end

Instance Attribute Details

#code_challengeObject (readonly)

Returns the value of attribute code_challenge.



317
318
319
# File 'lib/mcp_client/auth.rb', line 317

def code_challenge
  @code_challenge
end

#code_challenge_methodObject (readonly)

Returns the value of attribute code_challenge_method.



317
318
319
# File 'lib/mcp_client/auth.rb', line 317

def code_challenge_method
  @code_challenge_method
end

#code_verifierObject (readonly)

Returns the value of attribute code_verifier.



317
318
319
# File 'lib/mcp_client/auth.rb', line 317

def code_verifier
  @code_verifier
end

Class Method Details

.from_h(data) ⇒ PKCE

Note:

code_challenge_method is optional and defaults to ‘S256’. The code_challenge is not re-validated against code_verifier; callers are expected to provide values from a prior to_h round-trip.

Create PKCE instance from hash

Parameters:

  • data (Hash)

    Hash with PKCE parameters (symbol or string keys)

Returns:

  • (PKCE)

    New PKCE instance

Raises:

  • (ArgumentError)

    If required parameters are missing



346
347
348
349
350
351
352
353
354
355
# File 'lib/mcp_client/auth.rb', line 346

def self.from_h(data)
  verifier = data[:code_verifier] || data['code_verifier']
  challenge = data[:code_challenge] || data['code_challenge']
  method = data[:code_challenge_method] || data['code_challenge_method']

  raise ArgumentError, 'Missing code_verifier' unless verifier
  raise ArgumentError, 'Missing code_challenge' unless challenge

  new(code_verifier: verifier, code_challenge: challenge, code_challenge_method: method)
end

Instance Method Details

#to_hHash

Convert to hash for serialization

Returns:

  • (Hash)

    Hash representation



331
332
333
334
335
336
337
# File 'lib/mcp_client/auth.rb', line 331

def to_h
  {
    code_verifier: @code_verifier,
    code_challenge: @code_challenge,
    code_challenge_method: @code_challenge_method
  }
end