class Yt::Auth

Provides methods to authenticate a user with the Google OAuth flow. @see developers.google.com/accounts/docs/OAuth2

Constants

VERSION

@return [String] the SemVer-compatible gem version. @see semver.org

Public Class Methods

create(options = {}) click to toggle source

@param [Hash] options the options to initialize an instance of Yt::Auth. @option options [String] :redirect_uri The URI to redirect users to

after they have completed the Google OAuth flow.

@option options [String] :code A single-use authorization code provided

by Google OAuth to obtain an access token to access Google API.
# File lib/yt/auth.rb, line 16
def self.create(options = {})
  new options.merge(grant_type: :authorization_code)
end
find_by(options = {}) click to toggle source

@param [Hash] options the options to initialize an instance of Yt::Auth. @option options [String] :refresh_token A multi-use refresh token to

obtain an access token to access Google API.
# File lib/yt/auth.rb, line 23
def self.find_by(options = {})
  new options.merge(grant_type: :refresh_token)
end
new(options = {}) click to toggle source

@param [Hash] options the options to initialize an instance of Yt::Auth. @option options [String] :grant_type @option options [String] :redirect_uri @option options [String] :code @option options [String] :refresh_token

# File lib/yt/auth.rb, line 47
def initialize(options = {})
  @tokens_body = options
  @tokens_body[:client_id] = Yt.configuration.client_id
  @tokens_body[:client_secret] = Yt.configuration.client_secret
end
url_for(options = {}) click to toggle source

@return [String] the URL where to authenticate with a Google account. @param [Hash] options the options to initialize an instance of Yt::Auth. @option options [String] :redirect_uri The URI to redirect users to

after they have completed the Google OAuth flow.

@option options [Boolean] :force whether to force users to re-authenticate

an account that was previously authenticated.

@option options [Array<String>] :scopes The list of scopes that users

are requested to authorize.
# File lib/yt/auth.rb, line 35
def self.url_for(options = {})
  host = 'accounts.google.com'
  path = '/o/oauth2/auth'
  query = URI.encode_www_form url_params(options)
  URI::HTTPS.build(host: host, path: path, query: query).to_s
end

Private Class Methods

scope_for(scopes) click to toggle source
# File lib/yt/auth.rb, line 92
def self.scope_for(scopes)
  ['userinfo.email'].concat(scopes).map do |scope|
    "https://www.googleapis.com/auth/#{scope}"
  end.join(' ')
end
url_params(options) click to toggle source
# File lib/yt/auth.rb, line 81
def self.url_params(options)
  {}.tap do |params|
    params[:client_id] = Yt.configuration.client_id
    params[:scope] = scope_for(options.fetch :scopes, [])
    params[:access_type] = :offline
    params[:approval_prompt] = options[:force] ? :force : :auto
    params[:redirect_uri] = options[:redirect_uri]
    params[:response_type] = :code
  end
end

Public Instance Methods

access_token() click to toggle source

@return [String] the access token of an authenticated Google account.

# File lib/yt/auth.rb, line 64
def access_token
  tokens['access_token']
end
access_token_was_refreshed() click to toggle source

Placeholder method that can be invoked after a refresh token is used to generate a new access token. Applications can override this method, for instance to store the new token in a database

# File lib/yt/auth.rb, line 76
def access_token_was_refreshed
end
email() click to toggle source

@return [String] the email of an authenticated Google account.

# File lib/yt/auth.rb, line 59
def email
  profile['email']
end
refresh_token() click to toggle source

@return [String] the refresh token of an authenticated Google account.

# File lib/yt/auth.rb, line 69
def refresh_token
  tokens['refresh_token']
end
revoke() click to toggle source

@return [Boolean] whether the authentication was revoked.

# File lib/yt/auth.rb, line 54
def revoke
  !!HTTPRequest.new(revoke_params).run
end

Private Instance Methods

error_message_for(body) click to toggle source
# File lib/yt/auth.rb, line 128
def error_message_for(body)
  key = @tokens_body[:grant_type].to_s.tr '_', ' '
  JSON(body)['error_description'] || "Invalid #{key}."
end
profile() click to toggle source

@return [Hash] the profile of an authenticated Google account.

# File lib/yt/auth.rb, line 104
def profile
  decoded_tokens = JWT.decode tokens['id_token'], nil, false
  decoded_tokens[0]
end
revoke_params() click to toggle source
# File lib/yt/auth.rb, line 120
def revoke_params
  {}.tap do |params|
    params[:host] = 'accounts.google.com'
    params[:path] = '/o/oauth2/revoke'
    params[:params] = {token: refresh_token || access_token}
  end
end
tokens() click to toggle source

@return [Hash] the tokens of an authenticated Google account.

# File lib/yt/auth.rb, line 99
def tokens
  @tokens ||= HTTPRequest.new(tokens_params).run.body
end
tokens_params() click to toggle source
# File lib/yt/auth.rb, line 109
def tokens_params
  {}.tap do |params|
    params[:host] = 'accounts.google.com'
    params[:path] = '/o/oauth2/token'
    params[:method] = :post
    params[:request_format] = :form
    params[:body] = @tokens_body
    params[:error_message] = ->(body) { error_message_for body }
  end
end