module Yt::Associations::Authenticable
@private
Public Class Methods
new(options = {})
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 25 def initialize(options = {}) @access_token = options[:access_token] @refresh_token = options[:refresh_token] @device_code = options[:device_code] @expires_at = options[:expires_at] @authorization_code = options[:authorization_code] @redirect_uri = options[:redirect_uri] @force = options[:force] @scopes = options[:scopes] @authentication = options[:authentication] end
Public Instance Methods
access_token_was_refreshed()
click to toggle source
Invoked when the access token is refreshed.
# File lib/yt/associations/has_authentication.rb, line 83 def access_token_was_refreshed # Apps using Yt can override this method to handle this event, for # instance to store the newly generated access token in the database. end
auth()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 37 def auth self end
authentication()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 41 def authentication @authentication = current_authentication @authentication ||= use_refresh_token! if @refresh_token @authentication ||= use_authorization_code! if @authorization_code @authentication ||= use_device_code! if @device_code @authentication ||= raise_missing_authentication! end
authentication_url()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 49 def authentication_url host = 'accounts.google.com' path = '/o/oauth2/auth' query = authentication_url_params.to_param URI::HTTPS.build(host: host, path: path, query: query).to_s end
refreshed_access_token?()
click to toggle source
Obtains a new access token. Returns true if the new access token is different from the previous one
# File lib/yt/associations/has_authentication.rb, line 58 def refreshed_access_token? old_access_token = authentication.access_token @authentication = @access_token = @refreshed_authentications = nil if old_access_token != authentication.access_token access_token_was_refreshed true else false end end
revoke_access()
click to toggle source
Revoke access given to the application. Returns true if the access was correctly revoked. @see developers.google.com/identity/protocols/OAuth2WebServer#tokenrevoke
# File lib/yt/associations/has_authentication.rb, line 73 def revoke_access revocations.first! @authentication = @access_token = @refreshed_authentications = nil true rescue Errors::RequestError => e raise unless e.reasons.include? 'invalid_token' false end
Private Instance Methods
authentication_scope()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 196 def authentication_scope @scopes.map do |scope| "https://www.googleapis.com/auth/#{scope}" end.join(' ') if @scopes.is_a?(Array) end
authentication_url_params()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 184 def authentication_url_params {}.tap do |params| params[:client_id] = client_id params[:scope] = authentication_scope params[:redirect_uri] = @redirect_uri params[:response_type] = :code params[:access_type] = :offline params[:approval_prompt] = @force ? :force : :auto # params[:include_granted_scopes] = true end end
client_id()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 237 def client_id Yt.configuration.client_id end
client_secret()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 241 def client_secret Yt.configuration.client_secret end
current_authentication()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 90 def current_authentication @authentication ||= Yt::Authentication.new current_data if @access_token @authentication unless @authentication.nil? || @authentication.expired? end
current_data()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 95 def current_data {}.tap do |data| data['access_token'] = @access_token data['expires_at'] = @expires_at data['refresh_token'] = @refresh_token end end
device_code_authentication_params()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 221 def device_code_authentication_params {}.tap do |params| params[:client_id] = client_id params[:client_secret] = client_secret params[:code] = @device_code params[:grant_type] = 'http://oauth.net/grant_type/device/1.0' end end
device_code_authentications()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 166 def device_code_authentications Collections::Authentications.of(self).tap do |auth| auth.auth_params = device_code_authentication_params end end
device_flow_params()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 230 def device_flow_params {}.tap do |params| params[:client_id] = client_id params[:scope] = authentication_scope end end
device_flows()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 172 def device_flows @device_flows ||= Collections::DeviceFlows.of(self).tap do |auth| auth.auth_params = device_flow_params end end
new_authentication_params()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 202 def new_authentication_params {}.tap do |params| params[:client_id] = client_id params[:client_secret] = client_secret params[:code] = @authorization_code params[:redirect_uri] = @redirect_uri params[:grant_type] = :authorization_code end end
new_authentications()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 154 def new_authentications @new_authentications ||= Collections::Authentications.of(self).tap do |auth| auth.auth_params = new_authentication_params end end
pending_device_code_message()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 136 def pending_device_code_message @device_flow ||= device_flows.first! @device_code ||= @device_flow.device_code {}.tap do |params| params[:scopes] = @scopes params[:user_code] = @device_flow.user_code params[:verification_url] = @device_flow.verification_url end end
raise_missing_authentication!()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 127 def raise_missing_authentication! error_message = case when @redirect_uri && @scopes then missing_authorization_code_message when @scopes then pending_device_code_message else {} end raise Errors::MissingAuth, error_message end
refreshed_authentication_params()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 212 def refreshed_authentication_params {}.tap do |params| params[:client_id] = client_id params[:client_secret] = client_secret params[:refresh_token] = @refresh_token params[:grant_type] = :refresh_token end end
refreshed_authentications()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 160 def refreshed_authentications @refreshed_authentications ||= Collections::Authentications.of(self).tap do |auth| auth.auth_params = refreshed_authentication_params end end
revocations()
click to toggle source
# File lib/yt/associations/has_authentication.rb, line 178 def revocations @revocations ||= Collections::Revocations.of(self).tap do |auth| auth.auth_params = {token: @refresh_token || @access_token} end end
use_device_code!()
click to toggle source
Tries to obtain an access token using the device code (which must be confirmed by the user with the user_code). On failure, raise an error.
# File lib/yt/associations/has_authentication.rb, line 121 def use_device_code! device_code_authentications.first!.tap do |auth| raise Errors::MissingAuth, pending_device_code_message if auth.pending? end end
use_refresh_token!()
click to toggle source
Tries to obtain an access token using the refresh token (which can be used multiple times). On failure, raise an error.
# File lib/yt/associations/has_authentication.rb, line 113 def use_refresh_token! refreshed_authentications.first! rescue Errors::NoItems => error raise Errors::Unauthorized, error.to_param end