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')



361
362
363
364
365
# File 'lib/mcp_client/auth.rb', line 361

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.



355
356
357
# File 'lib/mcp_client/auth.rb', line 355

def code_challenge
  @code_challenge
end

#code_challenge_methodObject (readonly)

Returns the value of attribute code_challenge_method.



355
356
357
# File 'lib/mcp_client/auth.rb', line 355

def code_challenge_method
  @code_challenge_method
end

#code_verifierObject (readonly)

Returns the value of attribute code_verifier.



355
356
357
# File 'lib/mcp_client/auth.rb', line 355

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



384
385
386
387
388
389
390
391
392
393
# File 'lib/mcp_client/auth.rb', line 384

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



369
370
371
372
373
374
375
# File 'lib/mcp_client/auth.rb', line 369

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