Class: Yt::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/yt/auth.rb,
lib/yt/auth/version.rb

Overview

Provides methods to authenticate a user with the Google OAuth flow.

Constant Summary collapse

VERSION =

Returns the SemVer-compatible gem version.

Returns:

  • (String)

    the SemVer-compatible gem version.

See Also:

'0.3.1'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Auth

Returns a new instance of Auth.

Parameters:

  • options (Hash) (defaults to: {})

    the options to initialize an instance of Yt::Auth.

Options Hash (options):

  • :grant_type (String)
  • :redirect_uri (String)
  • :code (String)
  • :refresh_token (String)


47
48
49
50
51
# 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

Class Method Details

.create(options = {}) ⇒ Object

Parameters:

  • options (Hash) (defaults to: {})

    the options to initialize an instance of Yt::Auth.

Options Hash (options):

  • :redirect_uri (String)

    The URI to redirect users to after they have completed the Google OAuth flow.

  • :code (String)

    A single-use authorization code provided by Google OAuth to obtain an access token to access Google API.



16
17
18
# File 'lib/yt/auth.rb', line 16

def self.create(options = {})
  new options.merge(grant_type: :authorization_code)
end

.find_by(options = {}) ⇒ Object

Parameters:

  • options (Hash) (defaults to: {})

    the options to initialize an instance of Yt::Auth.

Options Hash (options):

  • :refresh_token (String)

    A multi-use refresh token to obtain an access token to access Google API.



23
24
25
# File 'lib/yt/auth.rb', line 23

def self.find_by(options = {})
  new options.merge(grant_type: :refresh_token)
end

.url_for(options = {}) ⇒ String

Returns the URL where to authenticate with a Google account.

Parameters:

  • options (Hash) (defaults to: {})

    the options to initialize an instance of Yt::Auth.

Options Hash (options):

  • :redirect_uri (String)

    The URI to redirect users to after they have completed the Google OAuth flow.

  • :force (Boolean)

    whether to force users to re-authenticate an account that was previously authenticated.

  • :scopes (Array<String>)

    The list of scopes that users are requested to authorize.

Returns:

  • (String)

    the URL where to authenticate with a Google account.



35
36
37
38
39
40
# 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

Instance Method Details

#access_tokenString

Returns the access token of an authenticated Google account.

Returns:

  • (String)

    the access token of an authenticated Google account.



64
65
66
# File 'lib/yt/auth.rb', line 64

def access_token
  tokens['access_token']
end

#access_token_was_refreshedObject

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



76
77
# File 'lib/yt/auth.rb', line 76

def access_token_was_refreshed
end

#emailString

Returns the email of an authenticated Google account.

Returns:

  • (String)

    the email of an authenticated Google account.



59
60
61
# File 'lib/yt/auth.rb', line 59

def email
  profile['email']
end

#refresh_tokenString

Returns the refresh token of an authenticated Google account.

Returns:

  • (String)

    the refresh token of an authenticated Google account.



69
70
71
# File 'lib/yt/auth.rb', line 69

def refresh_token
  tokens['refresh_token']
end

#revokeBoolean

Returns whether the authentication was revoked.

Returns:

  • (Boolean)

    whether the authentication was revoked.



54
55
56
# File 'lib/yt/auth.rb', line 54

def revoke
  !!HTTPRequest.new(revoke_params).run
end