Sha256: 0ed72469c93fb865ffe569e4ad9d3922e26af70a475ea22a4ab2028842a0df0c

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

require 'httparty'

# Handles Google authentication
#
# @!visibility private
class GoogleAuthenticator < BaseAuthenticator

  PROVIDER = 'google'.freeze
  TOKEN_URL = 'https://www.googleapis.com/oauth2/v3/token'.freeze
  PROFILE_URL = 'https://www.googleapis.com/plus/v1/people/me/openIdConnect?access_token=%{access_token}'.freeze

  private

    def connect_login_to_account(login, user)
      login.update_attributes!(uid: user[:sub], provider: PROVIDER)
    end

    def create_login_from_account(user)
      login_attributes = {
        identification: user[:email],
        uid: user[:sub],
        provider: PROVIDER
      }

      Login.create!(login_attributes)
    end

    def access_token
      response = HTTParty.post(TOKEN_URL, token_options)
      response.parsed_response['access_token']
    end

    def get_user(access_token)
      @google_user ||= begin
        get_request(user_url(access_token)).parsed_response.symbolize_keys
      end
    end

    def user_url(access_token)
      PROFILE_URL % { access_token: access_token }
    end

    def token_options
      @token_options ||= {
        body: {
          code: @auth_code,
          client_id: RailsApiAuth.google_client_id,
          client_secret: RailsApiAuth.google_client_secret,
          redirect_uri: RailsApiAuth.google_redirect_uri,
          grant_type: 'authorization_code'
        }
      }
    end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rails_api_auth-0.0.8 app/services/google_authenticator.rb
rails_api_auth-0.0.7 app/services/google_authenticator.rb
rails_api_auth-0.0.6 app/services/google_authenticator.rb
rails_api_auth-0.0.5 app/services/google_authenticator.rb
rails_api_auth-0.0.4 app/services/google_authenticator.rb