Class: RubyLLM::MCP::Auth::Token

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

Overview

Represents an OAuth 2.1 access token with expiration tracking

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token:, token_type: "Bearer", expires_in: nil, scope: nil, refresh_token: nil) ⇒ Token

Returns a new instance of Token.



28
29
30
31
32
33
34
35
# File 'lib/ruby_llm/mcp/auth.rb', line 28

def initialize(access_token:, token_type: "Bearer", expires_in: nil, scope: nil, refresh_token: nil)
  @access_token = access_token
  @token_type = token_type
  @expires_in = expires_in
  @scope = scope
  @refresh_token = refresh_token
  @expires_at = expires_in ? Time.now + expires_in : nil
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



26
27
28
# File 'lib/ruby_llm/mcp/auth.rb', line 26

def access_token
  @access_token
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



26
27
28
# File 'lib/ruby_llm/mcp/auth.rb', line 26

def expires_at
  @expires_at
end

#expires_inObject (readonly)

Returns the value of attribute expires_in.



26
27
28
# File 'lib/ruby_llm/mcp/auth.rb', line 26

def expires_in
  @expires_in
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



26
27
28
# File 'lib/ruby_llm/mcp/auth.rb', line 26

def refresh_token
  @refresh_token
end

#scopeObject (readonly)

Returns the value of attribute scope.



26
27
28
# File 'lib/ruby_llm/mcp/auth.rb', line 26

def scope
  @scope
end

#token_typeObject (readonly)

Returns the value of attribute token_type.



26
27
28
# File 'lib/ruby_llm/mcp/auth.rb', line 26

def token_type
  @token_type
end

Class Method Details

.from_h(data) ⇒ Token

Deserialize token from hash

Parameters:

  • data (Hash)

    token data

Returns:

  • (Token)

    new token instance



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ruby_llm/mcp/auth.rb', line 78

def self.from_h(data)
  token = new(
    access_token: data[:access_token] || data["access_token"],
    token_type: data[:token_type] || data["token_type"] || "Bearer",
    expires_in: data[:expires_in] || data["expires_in"],
    scope: data[:scope] || data["scope"],
    refresh_token: data[:refresh_token] || data["refresh_token"]
  )

  # Restore expires_at if present
  expires_at_str = data[:expires_at] || data["expires_at"]
  if expires_at_str
    token.instance_variable_set(:@expires_at, Time.parse(expires_at_str))
  end

  token
end

Instance Method Details

#expired?Boolean

Check if token has expired

Returns:

  • (Boolean)

    true if token is expired



39
40
41
42
43
# File 'lib/ruby_llm/mcp/auth.rb', line 39

def expired?
  return false unless @expires_at

  Time.now >= @expires_at
end

#expires_soon?Boolean

Check if token expires soon (within configured buffer) This enables proactive token refresh

Returns:

  • (Boolean)

    true if token expires within the buffer period



48
49
50
51
52
# File 'lib/ruby_llm/mcp/auth.rb', line 48

def expires_soon?
  return false unless @expires_at

  Time.now >= (@expires_at - TOKEN_REFRESH_BUFFER)
end

#to_hHash

Serialize token to hash

Returns:

  • (Hash)

    token data



64
65
66
67
68
69
70
71
72
73
# File 'lib/ruby_llm/mcp/auth.rb', line 64

def to_h
  {
    access_token: @access_token,
    token_type: @token_type,
    expires_in: @expires_in,
    scope: @scope,
    refresh_token: @refresh_token,
    expires_at: @expires_at&.iso8601
  }
end

#to_headerString

Format token for Authorization header

Returns:



56
57
58
59
60
# File 'lib/ruby_llm/mcp/auth.rb', line 56

def to_header
  token_type = @token_type.to_s.strip
  token_type = "Bearer" if token_type.empty? || token_type.casecmp("bearer").zero?
  "#{token_type} #{@access_token}"
end