Class: MCPClient::Auth::Token

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

Overview

OAuth token model representing access/refresh tokens

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.

Parameters:

  • access_token (String)

    The access token

  • token_type (String) (defaults to: 'Bearer')

    Token type (default: “Bearer”)

  • expires_in (Integer, nil) (defaults to: nil)

    Token lifetime in seconds

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

    Token scope

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

    Refresh token for renewal



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_tokenObject (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_atObject (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_inObject (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_tokenObject (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

#scopeObject (readonly)

Returns the value of attribute scope.



15
16
17
# File 'lib/mcp_client/auth.rb', line 15

def scope
  @scope
end

#token_typeObject (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

Parameters:

  • data (Hash)

    Token data

Returns:

  • (Token)

    New token instance



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

Returns:

  • (Boolean)

    true if 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)

Returns:

  • (Boolean)

    true if token expires soon



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_hHash

Convert to hash for serialization

Returns:

  • (Hash)

    Hash representation



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_headerString

Convert token to authorization header value

Returns:

  • (String)

    Authorization header value



49
50
51
# File 'lib/mcp_client/auth.rb', line 49

def to_header
  "#{@token_type} #{@access_token}"
end