Class: Ask::MCP::Auth::OAuth

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, client_secret: nil, token_url:, auth_url: nil, redirect_uri: nil, scopes: []) ⇒ OAuth

Returns a new instance of OAuth.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ask/mcp/auth/oauth.rb', line 9

def initialize(client_id:, client_secret: nil, token_url:, auth_url: nil,
               redirect_uri: nil, scopes: [])
  @client_id = client_id
  @client_secret = client_secret
  @token_url = token_url
  @auth_url = auth_url
  @redirect_uri = redirect_uri
  @scopes = scopes
  @access_token = nil
  @refresh_token = nil
  @expires_at = nil
end

Instance Attribute Details

#auth_urlObject (readonly)

Returns the value of attribute auth_url.



7
8
9
# File 'lib/ask/mcp/auth/oauth.rb', line 7

def auth_url
  @auth_url
end

#client_idObject (readonly)

Returns the value of attribute client_id.



7
8
9
# File 'lib/ask/mcp/auth/oauth.rb', line 7

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



7
8
9
# File 'lib/ask/mcp/auth/oauth.rb', line 7

def client_secret
  @client_secret
end

#token_urlObject (readonly)

Returns the value of attribute token_url.



7
8
9
# File 'lib/ask/mcp/auth/oauth.rb', line 7

def token_url
  @token_url
end

Instance Method Details

#apply(headers = {}) ⇒ Object



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

def apply(headers = {})
  headers.merge("Authorization" => "Bearer #{@access_token}")
end

#authenticate!Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/ask/mcp/auth/oauth.rb', line 30

def authenticate!
  if @client_secret
    authenticate_client_credentials
  elsif @auth_url
    authenticate_authorization_code
  else
    raise AuthError, "No authentication method available"
  end
  self
end

#authenticated?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/ask/mcp/auth/oauth.rb', line 22

def authenticated?
  !@access_token.nil? && !expired?
end

#refresh!Object

Raises:



41
42
43
44
45
# File 'lib/ask/mcp/auth/oauth.rb', line 41

def refresh!
  raise AuthError, "No refresh token available" unless @refresh_token
  perform_token_refresh
  self
end