-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
24 changed files
with
2,183 additions
and
9 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
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,38 @@ | ||
require_relative 'auth_strategy' | ||
require_relative './../credential/auth_type' | ||
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
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 |
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 OrgTokenManager | ||
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 |
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,15 @@ | ||
module Twilio | ||
module REST | ||
class PreviewIam < PreviewIamBase | ||
def account | ||
self.organizations.accounts | ||
end | ||
def role_assignment | ||
self.organizations.role_assignments | ||
end | ||
def user | ||
self.organizations.users | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.