Class: MCPClient::Auth::Token
- Inherits:
-
Object
- Object
- MCPClient::Auth::Token
- Defined in:
- lib/mcp_client/auth.rb
Overview
OAuth token model representing access/refresh tokens
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
Create token from hash.
Instance Method Summary collapse
-
#expired? ⇒ Boolean
Check if the token is expired.
-
#expires_soon? ⇒ Boolean
Check if the token is close to expiring (within 5 minutes).
-
#initialize(access_token:, token_type: 'Bearer', expires_in: nil, scope: nil, refresh_token: nil) ⇒ Token
constructor
A new instance of Token.
-
#to_h ⇒ Hash
Convert to hash for serialization.
-
#to_header ⇒ String
Convert token to authorization header value.
Constructor Details
#initialize(access_token:, token_type: 'Bearer', expires_in: nil, scope: nil, refresh_token: nil) ⇒ Token
Returns a new instance of Token.
22 23 24 25 26 27 28 29 |
# File 'lib/mcp_client/auth.rb', line 22 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.
15 16 17 |
# File 'lib/mcp_client/auth.rb', line 15 def access_token @access_token end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
15 16 17 |
# File 'lib/mcp_client/auth.rb', line 15 def expires_at @expires_at end |
#expires_in ⇒ Object (readonly)
Returns the value of attribute expires_in.
15 16 17 |
# File 'lib/mcp_client/auth.rb', line 15 def expires_in @expires_in end |
#refresh_token ⇒ Object (readonly)
Returns the value of attribute refresh_token.
15 16 17 |
# File 'lib/mcp_client/auth.rb', line 15 def refresh_token @refresh_token end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
15 16 17 |
# File 'lib/mcp_client/auth.rb', line 15 def scope @scope end |
#token_type ⇒ Object (readonly)
Returns the value of attribute token_type.
15 16 17 |
# File 'lib/mcp_client/auth.rb', line 15 def token_type @token_type end |
Class Method Details
.from_h(data) ⇒ Token
Create token from hash
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/mcp_client/auth.rb', line 69 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'] ) # Set expires_at if provided if (expires_at_str = data[:expires_at] || data['expires_at']) token.instance_variable_set(:@expires_at, Time.parse(expires_at_str)) end token end |
Instance Method Details
#expired? ⇒ Boolean
Check if the token is expired
33 34 35 36 37 |
# File 'lib/mcp_client/auth.rb', line 33 def expired? return false unless @expires_at Time.now >= @expires_at end |
#expires_soon? ⇒ Boolean
Check if the token is close to expiring (within 5 minutes)
41 42 43 44 45 |
# File 'lib/mcp_client/auth.rb', line 41 def expires_soon? return false unless @expires_at Time.now >= (@expires_at - 300) # 5 minutes buffer end |
#to_h ⇒ Hash
Convert to hash for serialization
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mcp_client/auth.rb', line 55 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
Convert token to authorization header value
49 50 51 |
# File 'lib/mcp_client/auth.rb', line 49 def to_header "#{@token_type} #{@access_token}" end |