Sha256: b2cfe1cd119901c6af969b9143045b488d5b343ec5050972b314c094f858758b

Contents?: true

Size: 1.85 KB

Versions: 11

Compression:

Stored size: 1.85 KB

Contents

require 'rubygems'
require 'bundler/setup'

require 'oauth2'

module RubyApp

  module Elements

    module Mobile

      module Documents

        module Authentication

          module OAuth
            require 'ruby_app'
            require 'ruby_app/elements/mobile/document'

            class AuthenticationDocument < RubyApp::Elements::Mobile::Document

              template_path(:all, File.dirname(__FILE__))

              def initialize(access_key, secret_key, scopes, options)
                super()

                self.loaded do |element, event|
                  unless @client
                    RubyApp::Log.debug("OAUTH     scopes=#{scopes.inspect}")
                    RubyApp::Log.debug("OAUTH     options=#{options.inspect}")
                    @client = ::OAuth2::Client.new(access_key, secret_key, options)
                    url = @client.auth_code.authorize_url(:redirect_uri => RubyApp::Request.url,
                                                          :scope        => scopes.is_a?(Array) ? scopes.join(',') : scopes)
                    RubyApp::Log.debug("OAUTH     --> #{url.inspect}")
                    event.go(url)
                  else
                    RubyApp::Log.debug("OAUTH     <-- #{RubyApp::Request.url.inspect}")
                    code = RubyApp::Request.query['code']
                    access_token = @client.auth_code.get_token(code, :redirect_uri => RubyApp::Request.url)
                    RubyApp::Log.debug("OAUTH     token=#{access_token.token.inspect}")
                    self.process_token(access_token)
                    self.hide(event)
                  end
                end

              end

              def process_token(token)
                RubyApp::Session.identity = self.create_identity_from_token(token)
              end

            end

          end

        end

      end

    end

  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
RubyApp-0.6.49 lib/ruby_app/elements/mobile/documents/authentication/o_auth/authentication_document.rb
RubyApp-0.6.48 lib/ruby_app/elements/mobile/documents/authentication/o_auth/authentication_document.rb
RubyApp-0.6.47 lib/ruby_app/elements/mobile/documents/authentication/o_auth/authentication_document.rb
RubyApp-0.6.46 lib/ruby_app/elements/mobile/documents/authentication/o_auth/authentication_document.rb
RubyApp-0.6.45 lib/ruby_app/elements/mobile/documents/authentication/o_auth/authentication_document.rb
RubyApp-0.6.44 lib/ruby_app/elements/mobile/documents/authentication/o_auth/authentication_document.rb
RubyApp-0.6.43 lib/ruby_app/elements/mobile/documents/authentication/o_auth/authentication_document.rb
RubyApp-0.6.42 lib/ruby_app/elements/mobile/documents/authentication/o_auth/authentication_document.rb
RubyApp-0.6.41 lib/ruby_app/elements/mobile/documents/authentication/o_auth/authentication_document.rb
RubyApp-0.6.40 lib/ruby_app/elements/mobile/documents/authentication/o_auth/authentication_document.rb
RubyApp-0.6.39 lib/ruby_app/elements/mobile/documents/authentication/o_auth/authentication_document.rb