Class: Yt::Auth
- Inherits:
-
Object
- Object
- Yt::Auth
- 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.
'1.1.0'
Class Method Summary collapse
- .create(options = {}) ⇒ Object
- .find_by(options = {}) ⇒ Object
-
.url_for(options = {}) ⇒ String
The URL where to authenticate with a Google account.
Instance Method Summary collapse
-
#access_token ⇒ String
The access token of an authenticated Google account.
-
#access_token_was_refreshed ⇒ Object
Placeholder method that can be invoked after a refresh token is used to generate a new access token.
-
#email ⇒ String
The email of an authenticated Google account.
-
#initialize(options = {}) ⇒ Auth
constructor
A new instance of Auth.
-
#refresh_token ⇒ String
The refresh token of an authenticated Google account.
-
#revoke ⇒ Boolean
Whether the authentication was revoked.
Constructor Details
Class Method Details
.create(options = {}) ⇒ Object
16 17 18 |
# File 'lib/yt/auth.rb', line 16 def self.create( = {}) new .merge(grant_type: :authorization_code) end |
.find_by(options = {}) ⇒ Object
23 24 25 |
# File 'lib/yt/auth.rb', line 23 def self.find_by( = {}) new .merge(grant_type: :refresh_token) end |
.url_for(options = {}) ⇒ String
Returns the URL where to authenticate with a Google account.
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/yt/auth.rb', line 35 def self.url_for( = {}) if Yt.configuration.mock_auth_error [:redirect_uri] + '?error=' + Yt.configuration.mock_auth_error elsif Yt.configuration.mock_auth_email [:redirect_uri] + '?code=mock-email' else host = 'accounts.google.com' path = '/o/oauth2/auth' query = URI.encode_www_form url_params() URI::HTTPS.build(host: host, path: path, query: query).to_s end end |
Instance Method Details
#access_token ⇒ String
Returns the access token of an authenticated Google account.
78 79 80 |
# File 'lib/yt/auth.rb', line 78 def access_token tokens['access_token'] end |
#access_token_was_refreshed ⇒ Object
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
90 91 |
# File 'lib/yt/auth.rb', line 90 def access_token_was_refreshed end |
#email ⇒ String
Returns the email of an authenticated Google account.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/yt/auth.rb', line 65 def email if Yt.configuration.mock_auth_email if Yt.configuration.mock_auth_email.eql? 'invalid-email' raise Yt::HTTPError, 'Malformed auth code' else Yt.configuration.mock_auth_email end else profile['email'] end end |
#refresh_token ⇒ String
Returns the refresh token of an authenticated Google account.
83 84 85 |
# File 'lib/yt/auth.rb', line 83 def refresh_token tokens['refresh_token'] end |
#revoke ⇒ Boolean
Returns whether the authentication was revoked.
60 61 62 |
# File 'lib/yt/auth.rb', line 60 def revoke !!HTTPRequest.new(revoke_params).run end |