Skip to content

Commit

Permalink
Add Enterprise connect auth url
Browse files Browse the repository at this point in the history
  • Loading branch information
dmbento committed Sep 27, 2023
1 parent 77ed581 commit 09a785f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
39 changes: 39 additions & 0 deletions lib/cronofy/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def initialize(options = {})
@client_credentials_missing = blank?(client_id) || blank?(client_secret)

@auth_client = OAuth2::Client.new(client_id, client_secret, site: ::Cronofy.app_url(data_center), connection_opts: { headers: { "User-Agent" => "Cronofy Ruby #{::Cronofy::VERSION}" } })
@auth_enterprise_connect_client = OAuth2::Client.new(client_id, client_secret, site: ::Cronofy.app_url(data_center) + '/enterprise_connect', connection_opts: { headers: { "User-Agent" => "Cronofy Ruby #{::Cronofy::VERSION}" } })
@api_client = OAuth2::Client.new(client_id, client_secret, site: ::Cronofy.api_url(data_center), connection_opts: { headers: { "User-Agent" => "Cronofy Ruby #{::Cronofy::VERSION}" } })

set_access_token(access_token, refresh_token) if access_token || refresh_token
Expand Down Expand Up @@ -53,6 +54,44 @@ def user_auth_link(redirect_uri, options = {})
@auth_client.auth_code.authorize_url(params)
end

# Internal: generate a URL for authorizing the application with Cronofy
#
# redirect_uri - A String specifing the URI to return the user to once they
# have completed the authorization steps.
# options - The Hash options used to refine the selection
# (default: {}):
# :scope - Array or String of scopes describing the access to
# request from the user to the Enterprise Connect
# account (required).
# :delegated_scope - Array or String of scopes describing the access to
# request from the Enterprise Connect to the users
# calendars (required).
# :state - Array of states to retain during the OAuth
# authorization process (optional).
#
# See http://www.cronofy.com/developers/api#authorization for reference.
#
# Returns the URL as a String.
def enterprise_connect_auth_link(redirect_uri, options = {})
raise ArgumentError.new(":scope is required") unless options[:scope]
raise ArgumentError.new(":delegated_scope is required") unless options[:delegated_scope]

params = options.merge(redirect_uri: redirect_uri, response_type: 'code')

# Reformat params as needed
params.delete(:state) if params[:state].nil?

if params[:scope].respond_to?(:join)
params[:scope] = params[:scope].join(' ')
end

if params[:delegated_scope].respond_to?(:join)
params[:delegated_scope] = params[:delegated_scope].join(' ')
end

@auth_enterprise_connect_client.auth_code.authorize_url(params)
end

def get_token_from_code(code, redirect_uri)
do_request do
@access_token = @auth_client.auth_code.get_token(code, redirect_uri: redirect_uri)
Expand Down
40 changes: 37 additions & 3 deletions lib/cronofy/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class Client
delete_event
}.freeze

# Public: The scope to request if none is explicitly specified by the
# caller.
DEFAULT_OAUTH_ENTERPRISE_SCOPE = %w{
service_account/accounts/manage
service_account/resources/manage
}.freeze

# Public: Initialize a new Cronofy::Client.
#
# options - A Hash of options used to initialize the client (default: {}):
Expand Down Expand Up @@ -677,6 +684,33 @@ def user_auth_link(redirect_url, options = {})
@auth.user_auth_link(redirect_url, options)
end

# Public: Generates a URL to send the user to in order to perform the OAuth
# 2.0 authorization process for an enterprise.
#
# redirect_uri - A String specifing the URI to return the user to once they
# have completed the authorization steps.
# options - The Hash options used to refine the selection
# (default: {}):
# :scope - Array or String of scopes describing the access to
# request from the user to the Enterprise Connect
# account (required).
# :delegated_scope - Array or String of scopes describing the access to
# request from the Enterprise Connect to the users
# calendars (required).
# :state - Array of states to retain during the OAuth
# authorization process (optional).
#
# See http://www.cronofy.com/developers/api#authorization for reference.
#
# Returns the URL as a String.
def enterprise_connect_auth_link(redirect_url, options = {})
options = {
scope: DEFAULT_OAUTH_ENTERPRISE_SCOPE,
delegated_scope: DEFAULT_OAUTH_SCOPE
}.merge(options)
@auth.enterprise_connect_auth_link(redirect_url, options)
end

# Public: Retrieves the OAuth credentials authorized for the given code and
# redirect URL pair.
#
Expand Down Expand Up @@ -1119,7 +1153,7 @@ def real_time_scheduling(args = {})
end

# Public: Gets the status of a Real-Time Scheduling link.
#
#
# Provide one of the following arguments to identify the link:
# id - A String uniquely identifying the link, returned on initial
# creation
Expand Down Expand Up @@ -1147,8 +1181,8 @@ def get_real_time_scheduling_status(args = {})
end

# Public: Disables a Real-Time Scheduling link.
#
# id - A String uniquely identifying the link, returned
#
# id - A String uniquely identifying the link, returned
# on initial creation
# display_message - A message to display to visitors of the disabled link
#
Expand Down

0 comments on commit 09a785f

Please sign in to comment.