Sha256: ddaf5160ecb5d5ccc7d54847ecb26eb675251392ad9379dfcac30551a8a73178

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 KB

Contents

module Lionel
  class GoogleAuthentication
    include Configurable

    attr_accessor :access_token
    attr_writer :client
    config_accessor :google_client_id, :google_client_secret

    def data
      raise "No access token" unless access_token
      {
        google_token: access_token.token,
        google_refresh_token: access_token.refresh_token,
        google_client_id: google_client_id,
        google_client_secret: google_client_secret
      }
    end

    def retrieve_access_token(authorization_code)
      @access_token = client.auth_code.get_token(authorization_code,
        :redirect_uri => "urn:ietf:wg:oauth:2.0:oob")
    end

    def refresh
      return false unless refresh_token

      current_token = OAuth2::AccessToken.from_hash(client,
          {:refresh_token => refresh_token, :expires_at => 36000})
      @access_token = current_token.refresh! # returns new access_token
    end

    def authorize_url
      client.auth_code.authorize_url(
        :redirect_uri => "urn:ietf:wg:oauth:2.0:oob",
        :scope =>
            "https://docs.google.com/feeds/ " +
            "https://docs.googleusercontent.com/ " +
            "https://spreadsheets.google.com/feeds/")
    end

    def api_console_url
      "https://code.google.com/apis/console"
    end

    def client
      @client ||= OAuth2::Client.new(google_client_id, google_client_secret,
        :site => "https://accounts.google.com",
        :token_url => "/o/oauth2/token",
        :authorize_url => "/o/oauth2/auth")
    end

    private

    def refresh_token
      @refresh_token || configuration.google_refresh_token
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lionel_richie-0.1.5.1 lib/lionel/google_authentication.rb
lionel_richie-0.1.5 lib/lionel/google_authentication.rb
lionel_richie-0.1.4 lib/lionel/google_authentication.rb