Class: Gle
- Inherits:
-
Object
- Object
- Gle
- Defined in:
- lib/gle.rb
Overview
Provides methods to authenticate a user with the Google OAuth flow.
Class Method Summary collapse
- .create(options = {}) ⇒ Object
- .find_by(options = {}) ⇒ Object
- .scope_for(scopes) ⇒ Object
-
.url_for(options = {}) ⇒ String
The URL where to authenticate with a Google account.
- .url_params(options) ⇒ Object
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 = {}) ⇒ Gle
constructor
A new instance of Gle.
-
#refresh_token ⇒ String
The refresh token of an authenticated Google account.
-
#revoke ⇒ Boolean
Whether the authentication was revoked.
Constructor Details
#initialize(options = {}) ⇒ Gle
Returns a new instance of Gle.
50 51 52 53 54 |
# File 'lib/gle.rb', line 50 def initialize( = {}) @tokens_body = @tokens_body[:client_id] = Yt.configuration.client_id @tokens_body[:client_secret] = Yt.configuration.client_secret end |
Class Method Details
.create(options = {}) ⇒ Object
13 14 15 |
# File 'lib/gle.rb', line 13 def self.create( = {}) new .merge(grant_type: :authorization_code) end |
.find_by(options = {}) ⇒ Object
20 21 22 |
# File 'lib/gle.rb', line 20 def self.find_by( = {}) new .merge(grant_type: :refresh_token) end |
.scope_for(scopes) ⇒ Object
103 104 105 106 107 |
# File 'lib/gle.rb', line 103 def self.scope_for(scopes) ['userinfo.email'].concat(scopes).map do |scope| "https://www.googleapis.com/auth/#{scope}" end.join(' ') end |
.url_for(options = {}) ⇒ String
Returns the URL where to authenticate with a Google account.
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/gle.rb', line 32 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 |
.url_params(options) ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/gle.rb', line 92 def self.url_params() {}.tap do |params| params[:client_id] = Yt.configuration.client_id params[:scope] = scope_for(.fetch :scopes, []) params[:access_type] = :offline params[:approval_prompt] = [:force] ? :force : :auto params[:redirect_uri] = [:redirect_uri] params[:response_type] = :code end end |
Instance Method Details
#access_token ⇒ String
Returns the access token of an authenticated Google account.
75 76 77 |
# File 'lib/gle.rb', line 75 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
87 88 |
# File 'lib/gle.rb', line 87 def access_token_was_refreshed end |
#email ⇒ String
Returns the email of an authenticated Google account.
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/gle.rb', line 62 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.
80 81 82 |
# File 'lib/gle.rb', line 80 def refresh_token tokens['refresh_token'] end |
#revoke ⇒ Boolean
Returns whether the authentication was revoked.
57 58 59 |
# File 'lib/gle.rb', line 57 def revoke !!Yt::HTTPRequest.new(revoke_params).run end |