Class: Cronofy::Auth
- Inherits:
-
Object
- Object
- Cronofy::Auth
- Defined in:
- lib/cronofy/auth.rb
Overview
Internal: Class for dealing with authentication and authorization issues.
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#api_client ⇒ Object
readonly
Returns the value of attribute api_client.
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
Instance Method Summary collapse
-
#application_calendar(application_calendar_id) ⇒ Object
Internal: Obtains access to an application calendar.
- #get_token_from_code(code, redirect_uri) ⇒ Object
-
#initialize(options = {}) ⇒ Auth
constructor
A new instance of Auth.
-
#refresh! ⇒ Object
Internal: Refreshes the access token.
-
#revoke! ⇒ Object
Internal: Revokes the refresh token and corresponding access tokens.
-
#revoke_by_sub(sub) ⇒ Object
Internal: Revokes an authorization by the sub.
-
#revoke_by_token(token) ⇒ Object
Internal: Revokes an authorization via the token.
- #set_access_token(token, refresh_token) ⇒ Object
- #set_access_token_from_auth_token(auth_token) ⇒ Object
- #set_api_key(client_secret) ⇒ Object
-
#user_auth_link(redirect_uri, options = {}) ⇒ Object
Internal: generate a URL for authorizing the application with Cronofy.
Constructor Details
#initialize(options = {}) ⇒ Auth
Returns a new instance of Auth.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/cronofy/auth.rb', line 10 def initialize( = {}) access_token = [:access_token] client_id = [:client_id] client_secret = [:client_secret] data_center = [:data_center] refresh_token = [:refresh_token] @client_credentials_missing = blank?(client_id) || blank?(client_secret) @auth_client = OAuth2::Client.new(client_id, client_secret, site: ::Cronofy.app_url(data_center), auth_scheme: :request_body, connection_opts: { headers: { "User-Agent" => "Cronofy Ruby #{::Cronofy::VERSION}" } }) @api_client = OAuth2::Client.new(client_id, client_secret, site: ::Cronofy.api_url(data_center), auth_scheme: :request_body, connection_opts: { headers: { "User-Agent" => "Cronofy Ruby #{::Cronofy::VERSION}" } }) set_access_token(access_token, refresh_token) if access_token || refresh_token set_api_key(client_secret) if client_secret end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
6 7 8 |
# File 'lib/cronofy/auth.rb', line 6 def access_token @access_token end |
#api_client ⇒ Object (readonly)
Returns the value of attribute api_client.
8 9 10 |
# File 'lib/cronofy/auth.rb', line 8 def api_client @api_client end |
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
7 8 9 |
# File 'lib/cronofy/auth.rb', line 7 def api_key @api_key end |
Instance Method Details
#application_calendar(application_calendar_id) ⇒ Object
Internal: Obtains access to an application calendar
application_calendar_id - A String to identify the application calendar
which is to be accessed.
Returns Hash of token elements to allow client to update in local store for user
Raises Cronofy::CredentialsMissingError if no credentials available.
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/cronofy/auth.rb', line 88 def application_calendar(application_calendar_id) do_request do body = { client_id: @api_client.id, client_secret: @api_client.secret, application_calendar_id: application_calendar_id, } @response = @api_client.request(:post, "/v1/application_calendars", body: body) Credentials.new(OAuth2::AccessToken.from_hash(@api_client, @response.parsed)) end end |
#get_token_from_code(code, redirect_uri) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/cronofy/auth.rb', line 56 def get_token_from_code(code, redirect_uri) do_request do @access_token = @auth_client.auth_code.get_token(code, redirect_uri: redirect_uri) Credentials.new(@access_token) end end |
#refresh! ⇒ Object
Internal: Refreshes the access token
Returns Hash of token elements to allow client to update in local store for user
Raises Cronofy::CredentialsMissingError if no credentials available.
69 70 71 72 73 74 75 76 77 |
# File 'lib/cronofy/auth.rb', line 69 def refresh! raise CredentialsMissingError.new("No credentials to refresh") unless access_token raise CredentialsMissingError.new("No refresh_token provided") unless access_token.refresh_token do_request do @access_token = access_token.refresh! Credentials.new(@access_token) end end |
#revoke! ⇒ Object
Internal: Revokes the refresh token and corresponding access tokens.
Returns nothing.
Raises Cronofy::CredentialsMissingError if no credentials available.
118 119 120 121 122 123 124 |
# File 'lib/cronofy/auth.rb', line 118 def revoke! raise CredentialsMissingError.new("No credentials to revoke") unless access_token token = access_token.refresh_token || access_token.token revoke_by_token(token) @access_token = nil end |
#revoke_by_sub(sub) ⇒ Object
Internal: Revokes an authorization by the sub
Returns nothing.
Raises Cronofy::CredentialsMissingError if no credentials available.
131 132 133 |
# File 'lib/cronofy/auth.rb', line 131 def revoke_by_sub(sub) do_revoke(sub: sub) end |
#revoke_by_token(token) ⇒ Object
Internal: Revokes an authorization via the token
Returns nothing.
Raises Cronofy::CredentialsMissingError if no credentials available.
140 141 142 |
# File 'lib/cronofy/auth.rb', line 140 def revoke_by_token(token) do_revoke(token: token) end |
#set_access_token(token, refresh_token) ⇒ Object
105 106 107 |
# File 'lib/cronofy/auth.rb', line 105 def set_access_token(token, refresh_token) @access_token = OAuth2::AccessToken.new(@api_client, token, refresh_token: refresh_token) end |
#set_access_token_from_auth_token(auth_token) ⇒ Object
101 102 103 |
# File 'lib/cronofy/auth.rb', line 101 def set_access_token_from_auth_token(auth_token) set_access_token(auth_token.token, auth_token.refresh_token) end |
#set_api_key(client_secret) ⇒ Object
109 110 111 |
# File 'lib/cronofy/auth.rb', line 109 def set_api_key(client_secret) @api_key = ApiKey.new(@api_client, client_secret) end |
#user_auth_link(redirect_uri, options = {}) ⇒ Object
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 users calendars
(required).
:state - Array of states to retain during the OAuth
authorization process (optional).
See docs.cronofy.com/developers/api/authorization/request-authorization/ for reference.
Returns the URL as a String.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/cronofy/auth.rb', line 41 def user_auth_link(redirect_uri, = {}) raise ArgumentError.new(":scope is required") unless [:scope] params = .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 @auth_client.auth_code.(params) end |