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 Dec 5, 2024
1 parent 68691b4 commit e4bcb8b
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 125 deletions.
3 changes: 2 additions & 1 deletion lib/twilio-ruby/auth_strategy/token_auth_strategy.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative 'auth_strategy'
require_relative './../credential/auth_type'
require 'jwt'
module Twilio
module REST
class TokenAuthStrategy < AuthStrategy
Expand All @@ -25,7 +26,7 @@ def fetch_token
end

def token_expired?
decoded_token = JWT.decode(@token, nil, false)
decoded_token = ::JWT.decode(@token, nil, false)
exp = decoded_token[0]['exp']
Time.at(exp) < Time.now
end
Expand Down
4 changes: 4 additions & 0 deletions lib/twilio-ruby/rest/preview_iam/v1/authorize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def fetch(
})
headers = Twilio::Values.of({'Content-Type' => 'application/x-www-form-urlencoded', })





payload = @version.fetch('GET', @uri, params: params, headers: headers)
AuthorizeInstance.new(
@version,
Expand Down
4 changes: 4 additions & 0 deletions lib/twilio-ruby/rest/preview_iam/v1/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def create(

headers = Twilio::Values.of({'Content-Type' => 'application/x-www-form-urlencoded', })





payload = @version.create('POST', @uri, data: data, headers: headers)
TokenInstance.new(
@version,
Expand Down
22 changes: 13 additions & 9 deletions lib/twilio-ruby/rest/preview_iam/versionless/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def fetch

headers = Twilio::Values.of({'Content-Type' => 'application/x-www-form-urlencoded', })



headers['Accept'] = 'application/scim+json'

payload = @version.fetch('GET', @uri, headers: headers)
OrganizationInstance.new(
@version,
Expand All @@ -89,17 +93,17 @@ def resource_types
# Access the role_assignments
# @return [RoleAssignmentList]
# @return [RoleAssignmentContext] if sid was passed.
def role_assignments(role_assignment_sid=:unset)
def role_assignments(sid=:unset)

raise ArgumentError, 'role_assignment_sid cannot be nil' if role_assignment_sid.nil?
raise ArgumentError, 'sid cannot be nil' if sid.nil?

if role_assignment_sid != :unset
return RoleAssignmentContext.new(@version, @solution[:organization_sid],role_assignment_sid )
if sid != :unset
return RoleAssignmentContext.new(@version, @solution[:organization_sid],sid )
end

unless @role_assignments
@role_assignments = RoleAssignmentList.new(
@version, )
@version, organization_sid: @solution[:organization_sid], )
end

@role_assignments
Expand Down Expand Up @@ -127,12 +131,12 @@ def accounts(account_sid=:unset)
# Access the users
# @return [UserList]
# @return [UserContext] if sid was passed.
def users(user_sid=:unset)
def users(id=:unset)

raise ArgumentError, 'user_sid cannot be nil' if user_sid.nil?
raise ArgumentError, 'id cannot be nil' if id.nil?

if user_sid != :unset
return UserContext.new(@version, @solution[:organization_sid],user_sid )
if id != :unset
return UserContext.new(@version, @solution[:organization_sid],id )
end

unless @users
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,11 @@ def page(page_token: :unset, page_number: :unset, page_size: :unset)
'Page' => page_number,
'PageSize' => page_size,
})
headers = Twilio::Values.of({})



response = @version.page('GET', @uri, params: params)
response = @version.page('GET', @uri, params: params, headers: headers)

AccountPage.new(@version, response, @solution)
end
Expand Down Expand Up @@ -149,6 +152,10 @@ def fetch

headers = Twilio::Values.of({'Content-Type' => 'application/x-www-form-urlencoded', })





payload = @version.fetch('GET', @uri, headers: headers)
AccountInstance.new(
@version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Versionless < Version
class OrganizationContext < InstanceContext

class RoleAssignmentList < ListResource

class PublicApiCreateRoleAssignmentRequest
# @param [role_sid]: [String] Twilio Role Sid representing assigned role
# @param [scope]: [String] Twilio Sid representing scope of this assignment
Expand All @@ -33,13 +33,14 @@ def initialize(payload)
end
def to_json(options = {})
{
role_sid: @role_sid,
scope: @scope,
identity: @identity,
"role_sid": @role_sid,
"scope": @scope,
"identity": @identity,
}.to_json(options)
end
end


##
# Initialize the RoleAssignmentList
# @param [Version] version Version that contains the resource
Expand All @@ -49,16 +50,21 @@ def initialize(version, organization_sid: nil)
# Path Solution
@solution = { organization_sid: organization_sid }
@uri = "/#{@solution[:organization_sid]}/RoleAssignments"

end
##
# Create the RoleAssignmentInstance
# @param [PublicApiCreateRoleAssignmentRequest] public_api_create_role_assignment_request
# @param [PublicApiCreateRoleAssignmentRequest] public_api_create_role_assignment_request
# @return [RoleAssignmentInstance] Created RoleAssignmentInstance
def create(public_api_create_role_assignment_request: nil)
def create(public_api_create_role_assignment_request: nil
)

headers = Twilio::Values.of({'Content-Type' => 'application/x-www-form-urlencoded', })
headers['Content-Type'] = 'application/json'



headers['Accept'] = '*/*'
payload = @version.create('POST', @uri, headers: headers, data: public_api_create_role_assignment_request.to_json)
RoleAssignmentInstance.new(
@version,
Expand All @@ -67,21 +73,21 @@ def create(public_api_create_role_assignment_request: nil)
)
end


##
# Lists RoleAssignmentInstance records from the API as a list.
# Unlike stream(), this operation is eager and will load `limit` records into
# memory before returning.
# @param [String] identity
# @param [String] scope
# @param [String] identity
# @param [String] scope
# @param [Integer] limit Upper limit for the number of records to return. stream()
# guarantees to never return more than limit. Default is no limit
# @param [Integer] page_size Number of records to fetch per request, when
# not set will use the default value of 50 records. If no page_size is defined
# but a limit is defined, stream() will attempt to read the limit with the most
# efficient page size, i.e. min(limit, 1000)
# @return [Array] Array of up to limit results
def list(identity: :unset, scope: :unset , limit: nil, page_size: nil)
def list(identity: :unset, scope: :unset, limit: nil, page_size: nil)
self.stream(
identity: identity,
scope: scope,
Expand All @@ -94,16 +100,16 @@ def list(identity: :unset, scope: :unset , limit: nil, page_size: nil)
# Streams Instance records from the API as an Enumerable.
# This operation lazily loads records as efficiently as possible until the limit
# is reached.
# @param [String] identity
# @param [String] scope
# @param [String] identity
# @param [String] scope
# @param [Integer] limit Upper limit for the number of records to return. stream()
# guarantees to never return more than limit. Default is no limit
# @param [Integer] page_size Number of records to fetch per request, when
# not set will use the default value of 50 records. If no page_size is defined
# but a limit is defined, stream() will attempt to read the limit with the most
# efficient page size, i.e. min(limit, 1000)
# @return [Enumerable] Enumerable that will yield up to limit results
def stream(identity: :unset, scope: :unset , limit: nil, page_size: nil)
def stream(identity: :unset, scope: :unset, limit: nil, page_size: nil)
limits = @version.read_limits(limit, page_size)

page = self.page(
Expand Down Expand Up @@ -131,22 +137,25 @@ def each
##
# Retrieve a single page of RoleAssignmentInstance records from the API.
# Request is executed immediately.
# @param [String] identity
# @param [String] scope
# @param [String] identity
# @param [String] scope
# @param [String] page_token PageToken provided by the API
# @param [Integer] page_number Page Number, this value is simply for client state
# @param [Integer] page_size Number of records to return, defaults to 50
# @return [Page] Page of RoleAssignmentInstance
def page(identity: :unset, scope: :unset , page_token: :unset, page_number: :unset, page_size: :unset)
def page(identity: :unset, scope: :unset, page_token: :unset, page_number: :unset, page_size: :unset)
params = Twilio::Values.of({
'Identity' => identity,
'Scope' => scope,
'PageToken' => page_token,
'Page' => page_number,
'PageSize' => page_size,
})
headers = Twilio::Values.of({})



response = @version.page('GET', @uri, params: params)
response = @version.page('GET', @uri, params: params, headers: headers)

RoleAssignmentPage.new(@version, response, @solution)
end
Expand All @@ -163,7 +172,7 @@ def get_page(target_url)
)
RoleAssignmentPage.new(@version, response, @solution)
end



# Provide a user friendly representation
Expand All @@ -177,25 +186,27 @@ class RoleAssignmentContext < InstanceContext
##
# Initialize the RoleAssignmentContext
# @param [Version] version Version that contains the resource
# @param [String] organization_sid
# @param [String] role_assignment_sid
# @param [String] organization_sid
# @param [String] sid
# @return [RoleAssignmentContext] RoleAssignmentContext
def initialize(version, organization_sid, role_assignment_sid)
def initialize(version, organization_sid, sid)
super(version)

# Path Solution
@solution = { organization_sid: organization_sid, role_assignment_sid: role_assignment_sid, }
@uri = "/#{@solution[:organization_sid]}/RoleAssignments/#{@solution[:role_assignment_sid]}"

@solution = { organization_sid: organization_sid, sid: sid, }
@uri = "/#{@solution[:organization_sid]}/RoleAssignments/#{@solution[:sid]}"


end
##
# Delete the RoleAssignmentInstance
# @return [Boolean] True if delete succeeds, false otherwise
def delete

headers = Twilio::Values.of({'Content-Type' => 'application/x-www-form-urlencoded', })



headers['Accept'] = '*/*'
@version.delete('DELETE', @uri, headers: headers)
end

Expand Down Expand Up @@ -253,11 +264,11 @@ class RoleAssignmentInstance < InstanceResource
# resource.
# @param [String] sid The SID of the Call resource to fetch.
# @return [RoleAssignmentInstance] RoleAssignmentInstance
def initialize(version, payload , organization_sid: nil, role_assignment_sid: nil)
def initialize(version, payload , organization_sid: nil, sid: nil)
super(version)

# Marshaled Properties
@properties = {
@properties = {
'sid' => payload['sid'],
'role_sid' => payload['role_sid'],
'scope' => payload['scope'],
Expand All @@ -270,7 +281,7 @@ def initialize(version, payload , organization_sid: nil, role_assignment_sid: ni

# Context
@instance_context = nil
@params = { 'organization_sid' => organization_sid || @properties['organization_sid'] ,'role_assignment_sid' => role_assignment_sid || @properties['role_assignment_sid'] , }
@params = { 'organization_sid' => organization_sid || @properties['organization_sid'] ,'sid' => sid || @properties['sid'] , }
end

##
Expand All @@ -279,59 +290,59 @@ def initialize(version, payload , organization_sid: nil, role_assignment_sid: ni
# @return [RoleAssignmentContext] CallContext for this CallInstance
def context
unless @instance_context
@instance_context = RoleAssignmentContext.new(@version , @params['organization_sid'], @params['role_assignment_sid'])
@instance_context = RoleAssignmentContext.new(@version , @params['organization_sid'], @params['sid'])
end
@instance_context
end

##
# @return [String] Twilio Role Assignment Sid representing this role assignment
def sid
@properties['sid']
end

##
# @return [String] Twilio Role Sid representing assigned role
def role_sid
@properties['role_sid']
end

##
# @return [String] Twilio Sid representing identity of this assignment
def scope
@properties['scope']
end

##
# @return [String] Twilio Sid representing scope of this assignment
def identity
@properties['identity']
end

##
# @return [String] Twilio-specific error code
def code
@properties['code']
end

##
# @return [String] Error message
def message
@properties['message']
end

##
# @return [String] Link to Error Code References
def more_info
@properties['more_info']
end

##
# @return [String] HTTP response status code
def status
@properties['status']
end

##
# Delete the RoleAssignmentInstance
# @return [Boolean] True if delete succeeds, false otherwise
Expand Down
Loading

0 comments on commit e4bcb8b

Please sign in to comment.