Skip to content

Commit

Permalink
feat: add oauth support for ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
manisha1997 committed Nov 13, 2024
1 parent d621adb commit 6fcd61d
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/twilio-ruby/base/client_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def request(host, port, method, uri, params = {}, data = {}, headers = {}, auth
@logger.debug("Request Params:#{params}")
end

auth = @credential_provider.to_auth_strategy.auth_string if not @credential_provider.nil?
auth = @credential_provider.to_auth_strategy.auth_string if @credential_provider

response = @http_client.request(
host,
Expand Down
1 change: 0 additions & 1 deletion lib/twilio-ruby/credential/auth_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module Twilio
module REST
class AuthType

BASIC = 'basic'
ORGS_TOKEN = 'orgs_token'
API_KEY = 'api_key'
Expand Down
1 change: 0 additions & 1 deletion lib/twilio-ruby/credential/credential_provider.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# frozen_string_literal: true
module Twilio
module REST
class CredentialProvider
Expand Down
9 changes: 4 additions & 5 deletions lib/twilio-ruby/credential/orgs_credential_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ module REST
class OrgsCredentialProvider < CredentialProvider
attr_accessor :grant_type, :client_id, :client_secret, :orgs_token, :auth_strategy

def initialize(client_id, client_secret, orgs_token=nil)
def initialize(client_id, client_secret, orgs_token = nil)
super(AuthType::ORGS_TOKEN)
if client_id.nil? || client_secret.nil?
raise ArgumentError, 'client_id and client_secret are required'
end
raise ArgumentError, 'client_id and client_secret are required' if client_id.nil? || client_secret.nil?

@grant_type = 'client_credentials'
@client_id = client_id
@client_secret = client_secret
Expand All @@ -28,7 +27,7 @@ def to_auth_strategy
if @auth_strategy.nil?
@auth_strategy = TokenAuthStrategy.new(@orgs_token)
end
return @auth_strategy
@auth_strategy
end
end
end
Expand Down
13 changes: 6 additions & 7 deletions lib/twilio-ruby/http/token_manager.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# frozen_string_literal: true
module Twilio
module REST
class TokenManager
attr_accessor :grant_type, :client_id, :client_secret, :code, :redirect_uri, :audience, :refresh_token, :scope
def initialize(grant_type, client_id, client_secret, code = nil, redirect_uri = nil, audience = nil, refresh_token = nil, scope = nil)
if client_id.nil? || client_secret.nil?
raise ArgumentError, 'client_id and client_secret are required'
end

def initialize(grant_type, client_id, client_secret, code = nil, redirect_uri = nil, audience = nil,
refresh_token = nil, scope = nil)
raise ArgumentError, 'client_id and client_secret are required' if client_id.nil? || client_secret.nil?
@grant_type = grant_type
@client_id = client_id
@client_secret = client_secret
Expand All @@ -18,8 +17,8 @@ def initialize(grant_type, client_id, client_secret, code = nil, redirect_uri =
end

def fetch_access_token
client = Twilio::REST::Client.new()
token_instance = client.preview_iam.v1.token.create(grant_type: @grant_type,client_id: @client_id,client_secret: @client_secret)
client = Twilio::REST::Client.new
token_instance = client.preview_iam.v1.token.create(grant_type: @grant_type, client_id: @client_id, client_secret: @client_secret)
token_instance.access_token
end
end
Expand Down

0 comments on commit 6fcd61d

Please sign in to comment.