Class: LlmGateway::Clients::ClaudeCode::TokenManager

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_gateway/clients/claude_code/token_manager.rb

Constant Summary collapse

TOKEN_URL =
"https://api.anthropic.com/v1/oauth/token"
CLIENT_ID =
OAuthFlow::CLIENT_ID

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token: nil, refresh_token:, expires_at: nil, client_id: CLIENT_ID, client_secret: nil) ⇒ TokenManager

Returns a new instance of TokenManager.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/llm_gateway/clients/claude_code/token_manager.rb', line 17

def initialize(
  access_token: nil,
  refresh_token:,
  expires_at: nil,
  client_id: CLIENT_ID,
  client_secret: nil
)
  @access_token = access_token
  @refresh_token = refresh_token
  @expires_at = parse_expires_at(expires_at)
  @client_id = client_id
  @client_secret = client_secret
  @on_token_refresh = nil
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



14
15
16
# File 'lib/llm_gateway/clients/claude_code/token_manager.rb', line 14

def access_token
  @access_token
end

#client_idObject (readonly)

Returns the value of attribute client_id.



14
15
16
# File 'lib/llm_gateway/clients/claude_code/token_manager.rb', line 14

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



14
15
16
# File 'lib/llm_gateway/clients/claude_code/token_manager.rb', line 14

def client_secret
  @client_secret
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



14
15
16
# File 'lib/llm_gateway/clients/claude_code/token_manager.rb', line 14

def expires_at
  @expires_at
end

#on_token_refreshObject

Returns the value of attribute on_token_refresh.



15
16
17
# File 'lib/llm_gateway/clients/claude_code/token_manager.rb', line 15

def on_token_refresh
  @on_token_refresh
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



14
15
16
# File 'lib/llm_gateway/clients/claude_code/token_manager.rb', line 14

def refresh_token
  @refresh_token
end

Instance Method Details

#ensure_valid_tokenObject



37
38
39
# File 'lib/llm_gateway/clients/claude_code/token_manager.rb', line 37

def ensure_valid_token
  refresh_access_token if token_expired?
end

#refresh_access_tokenObject

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/llm_gateway/clients/claude_code/token_manager.rb', line 41

def refresh_access_token
  raise ArgumentError, "Cannot refresh token: refresh_token not provided" unless @refresh_token
  raise ArgumentError, "Cannot refresh token: client_id not provided" unless @client_id

  uri = URI(TOKEN_URL)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.read_timeout = 30
  http.open_timeout = 10

  request = Net::HTTP::Post.new(uri)
  request["Content-Type"] = "application/json"

  request_body = {
    grant_type: "refresh_token",
    client_id: @client_id,
    refresh_token: @refresh_token
  }
  request_body[:client_secret] = @client_secret if @client_secret

  request.body = request_body.to_json

  response = http.request(request)

  if response.code.to_i == 200
    data = JSON.parse(response.body)
    @access_token = data["access_token"]

    if data["refresh_token"]
      @refresh_token = data["refresh_token"]
    end

    if data["expires_in"]
      @expires_at = Time.now + data["expires_in"].to_i
    elsif data["expires_at"]
      @expires_at = Time.parse(data["expires_at"])
    end

    @on_token_refresh&.call(@access_token, @refresh_token, @expires_at)

    @access_token
  else
    error_body = begin
      JSON.parse(response.body)
    rescue StandardError
      {}
    end
    raise Errors::AuthenticationError.new(
      "Failed to refresh token: #{error_body['error'] || response.body}",
      error_body["error_code"]
    )
  end
end

#token_expired?Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/llm_gateway/clients/claude_code/token_manager.rb', line 32

def token_expired?
  return true if @expires_at.nil?
  Time.now >= @expires_at
end