From 6fcd61d1c03021d1641ff43d97ecde04f3a8057d Mon Sep 17 00:00:00 2001 From: manisha1997 Date: Wed, 13 Nov 2024 13:13:11 +0530 Subject: [PATCH] feat: add oauth support for ruby --- lib/twilio-ruby/base/client_base.rb | 2 +- lib/twilio-ruby/credential/auth_type.rb | 1 - lib/twilio-ruby/credential/credential_provider.rb | 1 - .../credential/orgs_credential_provider.rb | 9 ++++----- lib/twilio-ruby/http/token_manager.rb | 13 ++++++------- 5 files changed, 11 insertions(+), 15 deletions(-) diff --git a/lib/twilio-ruby/base/client_base.rb b/lib/twilio-ruby/base/client_base.rb index 32346540d..b0904c135 100644 --- a/lib/twilio-ruby/base/client_base.rb +++ b/lib/twilio-ruby/base/client_base.rb @@ -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, diff --git a/lib/twilio-ruby/credential/auth_type.rb b/lib/twilio-ruby/credential/auth_type.rb index 5a6757c5a..837c4d1cb 100644 --- a/lib/twilio-ruby/credential/auth_type.rb +++ b/lib/twilio-ruby/credential/auth_type.rb @@ -3,7 +3,6 @@ module Twilio module REST class AuthType - BASIC = 'basic' ORGS_TOKEN = 'orgs_token' API_KEY = 'api_key' diff --git a/lib/twilio-ruby/credential/credential_provider.rb b/lib/twilio-ruby/credential/credential_provider.rb index ab5544c3a..71d1f29ea 100644 --- a/lib/twilio-ruby/credential/credential_provider.rb +++ b/lib/twilio-ruby/credential/credential_provider.rb @@ -1,4 +1,3 @@ -# frozen_string_literal: true module Twilio module REST class CredentialProvider diff --git a/lib/twilio-ruby/credential/orgs_credential_provider.rb b/lib/twilio-ruby/credential/orgs_credential_provider.rb index cf2d2dd01..c709a95a6 100644 --- a/lib/twilio-ruby/credential/orgs_credential_provider.rb +++ b/lib/twilio-ruby/credential/orgs_credential_provider.rb @@ -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 @@ -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 diff --git a/lib/twilio-ruby/http/token_manager.rb b/lib/twilio-ruby/http/token_manager.rb index 1d10e2ae8..5eda22d4c 100644 --- a/lib/twilio-ruby/http/token_manager.rb +++ b/lib/twilio-ruby/http/token_manager.rb @@ -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 @@ -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