Class: RubyLLM::MCP::Auth::Token
- Inherits:
-
Object
- Object
- RubyLLM::MCP::Auth::Token
- Defined in:
- lib/ruby_llm/mcp/auth.rb
Overview
Represents an OAuth 2.1 access token with expiration tracking
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#expires_in ⇒ Object
readonly
Returns the value of attribute expires_in.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#token_type ⇒ Object
readonly
Returns the value of attribute token_type.
Class Method Summary collapse
-
.from_h(data) ⇒ Token
Deserialize token from hash.
Instance Method Summary collapse
-
#expired? ⇒ Boolean
Check if token has expired.
-
#expires_soon? ⇒ Boolean
Check if token expires soon (within configured buffer) This enables proactive token refresh.
-
#initialize(access_token:, token_type: "Bearer", expires_in: nil, scope: nil, refresh_token: nil) ⇒ Token
constructor
A new instance of Token.
-
#to_h ⇒ Hash
Serialize token to hash.
-
#to_header ⇒ String
Format token for Authorization header.
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_token ⇒ Object (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_at ⇒ Object (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_in ⇒ Object (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_token ⇒ Object (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 |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
26 27 28 |
# File 'lib/ruby_llm/mcp/auth.rb', line 26 def scope @scope end |
#token_type ⇒ Object (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
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
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
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_h ⇒ Hash
Serialize token to hash
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_header ⇒ String
Format token for Authorization header
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 |