-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
33 changed files
with
2,296 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'rspec/matchers' | ||
require 'twilio-ruby' | ||
require './lib/twilio-ruby/credential/client_credential_provider' | ||
|
||
describe 'Cluster Test' do | ||
before(:each) do | ||
@account_sid = ENV['TWILIO_ACCOUNT_SID'] | ||
@client_secret = ENV['TWILIO_CLIENT_SECRET'] | ||
@client_id = ENV['TWILIO_CLIENT_ID'] | ||
@message_sid = ENV['TWILIO_MESSAGE_SID'] | ||
@credential = Twilio::REST::ClientCredentialProvider.new(@client_id, @client_secret) | ||
@client = Twilio::REST::Client.new(@account_sid).credential_provider(@credential) | ||
end | ||
|
||
it 'can fetch a message' do | ||
response = @client.messages(@message_sid).fetch | ||
expect(response).to_not be_nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'rspec/matchers' | ||
require 'twilio-ruby' | ||
require './lib/twilio-ruby/credential/client_credential_provider' | ||
|
||
describe 'Cluster Test' do | ||
before(:each) do | ||
@client_secret = ENV['TWILIO_ORGS_CLIENT_SECRET'] | ||
@client_id = ENV['TWILIO_ORGS_CLIENT_ID'] | ||
@org_sid = ENV['TWILIO_ORG_SID'] | ||
@user_sid = ENV['TWILIO_USER_SID'] | ||
@account_sid = ENV['TWILIO_ORGS_ACCOUNT_SID'] | ||
@credential = Twilio::REST::ClientCredentialProvider.new(@client_id, @client_secret) | ||
@client = Twilio::REST::Client.new.credential_provider(@credential) | ||
end | ||
|
||
it 'can list accounts' do | ||
response = @client.preview_iam.organizations(@org_sid).accounts.list | ||
expect(response).to_not be_nil | ||
end | ||
|
||
it 'can fetch specific account' do | ||
response = @client.preview_iam.organizations(@org_sid).accounts(@account_sid).fetch | ||
expect(response).to_not be_nil | ||
end | ||
|
||
it 'can access role assignments list' do | ||
response = @client.preview_iam.organizations(@org_sid).role_assignments.list(scope: @account_sid) | ||
expect(response).to_not be_nil | ||
end | ||
|
||
it 'can get user list' do | ||
response = @client.preview_iam.organizations(@org_sid).users.list | ||
expect(response).to_not be_nil | ||
end | ||
|
||
it 'can get single user' do | ||
response = @client.preview_iam.organizations(@org_sid).users(@user_sid).fetch | ||
expect(response).to_not be_nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'twilio-ruby' | ||
require 'twilio-ruby/credential/client_credential_provider' | ||
|
||
credential_provider = Twilio::REST::ClientCredentialProvider.new(ENV['CLIENT_ID'], ENV['CLIENT_SECRET']) | ||
# passing account sid is not mandatory | ||
client = Twilio::REST::Client.new(ENV['ACCOUNT_SID']).credential_provider(credential_provider) | ||
|
||
# send messages | ||
client.messages.create( | ||
from: ENV['TWILIO_PHONE_NUMBER'], | ||
to: ENV['PHONE_NUMBER'], | ||
body: 'Hello from Ruby!' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Twilio | ||
module REST | ||
class AuthStrategy | ||
attr_accessor :auth_type | ||
|
||
def initialize(auth_type) | ||
@auth_type = auth_type | ||
end | ||
|
||
def auth_string | ||
raise NotImplementedError, 'Subclasses must implement this method' | ||
end | ||
|
||
def requires_authentication | ||
raise NotImplementedError, 'Subclasses must implement this method' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Twilio | ||
module REST | ||
class NoAuthStrategy < AuthStrategy | ||
def initialize | ||
super(AuthType::NONE) | ||
end | ||
|
||
def auth_string | ||
'' | ||
end | ||
|
||
def requires_authentication | ||
false | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require_relative 'auth_strategy' | ||
require_relative './../credential/auth_type' | ||
require 'jwt' | ||
module Twilio | ||
module REST | ||
class TokenAuthStrategy < AuthStrategy | ||
attr_accessor :token_manager, :token, :lock | ||
|
||
def initialize(token_manager) | ||
super(AuthType::ORGS_TOKEN) | ||
@token = nil | ||
@token_manager = token_manager | ||
@lock = Mutex.new | ||
end | ||
|
||
def auth_string | ||
token = fetch_token | ||
"Bearer #{token}" | ||
end | ||
|
||
def fetch_token | ||
@lock.synchronize do | ||
@token = @token_manager.fetch_access_token if @token.nil? || token_expired? || @token == '' | ||
return @token | ||
end | ||
end | ||
|
||
def token_expired? | ||
decoded_token = ::JWT.decode(@token, nil, false) | ||
exp = decoded_token[0]['exp'] | ||
Time.at(exp) < Time.now | ||
end | ||
|
||
def requires_authentication | ||
true | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module Twilio | ||
module REST | ||
class AuthType | ||
BASIC = 'basic' | ||
ORGS_TOKEN = 'orgs_token' | ||
API_KEY = 'api_key' | ||
NOAUTH = 'noauth' | ||
CLIENT_CREDENTIALS = 'client_credentials' | ||
|
||
def to_s | ||
name | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require_relative 'credential_provider' | ||
require_relative 'auth_type' | ||
require_relative './../http/client_token_manager' | ||
require_relative './../auth_strategy/token_auth_strategy' | ||
module Twilio | ||
module REST | ||
class ClientCredentialProvider < CredentialProvider | ||
attr_accessor :grant_type, :client_id, :client_secret, :orgs_token, :auth_strategy | ||
|
||
def initialize(client_id, client_secret, orgs_token = nil) | ||
super(AuthType::ORGS_TOKEN) | ||
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 | ||
@orgs_token = orgs_token | ||
@auth_strategy = nil | ||
end | ||
|
||
def to_auth_strategy | ||
@orgs_token = ClientTokenManager.new(@grant_type, @client_id, @client_secret) if @orgs_token.nil? | ||
@auth_strategy = TokenAuthStrategy.new(@orgs_token) if @auth_strategy.nil? | ||
@auth_strategy | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Twilio | ||
module REST | ||
class CredentialProvider | ||
attr_accessor :auth_type | ||
|
||
def initialize(auth_type) | ||
@auth_type = auth_type | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'credential_provider' | ||
require_relative 'auth_type' | ||
require_relative './../http/org_token_manager' | ||
require_relative './../auth_strategy/token_auth_strategy' | ||
module Twilio | ||
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) | ||
super(AuthType::ORGS_TOKEN) | ||
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 | ||
@orgs_token = orgs_token | ||
@auth_strategy = nil | ||
end | ||
|
||
def to_auth_strategy | ||
@orgs_token = OrgTokenManager.new(@grant_type, @client_id, @client_secret) if @orgs_token.nil? | ||
@auth_strategy = TokenAuthStrategy.new(@orgs_token) if @auth_strategy.nil? | ||
@auth_strategy | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module Twilio | ||
module REST | ||
class ClientTokenManager | ||
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) | ||
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 | ||
@code = code | ||
@redirect_uri = redirect_uri | ||
@audience = audience | ||
@refresh_token = refresh_token | ||
@scope = scope | ||
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) | ||
token_instance.access_token | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.