Sha256: 3fa398e09b0336b7bffecc79d15bedb4054d0cd8d4df147832830791e6686f05

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

require 'usos_auth_lib/version'
require 'usos_auth_lib/engine'
require 'oauth'

module UsosAuthLib
  class Configuration
    attr_accessor :api_key, :api_secret, :usos_base_url, :scopes, :redirect_path

    def initialize
      @api_key = nil
      @api_secret = nil
      @usos_base_url = nil
      @scopes = nil
      @redirect_path = nil
    end
  end

  class << self
    attr_accessor :configuration

    def configure
      self.configuration ||= Configuration.new
      yield(configuration)
    end
  end

  class UsosAuthorizer
    def initialize
      config = UsosAuthLib.configuration
      @api_key = config.api_key
      @api_secret = config.api_secret
      @usos_base_url = config.usos_base_url
      @scopes = config.scopes
    end

    def authorize(session, request)
      callback_url = "#{request.protocol}#{request.host_with_port}/callback"
      request_token = consumer.get_request_token(
        { oauth_callback: callback_url },
        { scopes: @scopes }
      )
      session[:request_token] = request_token.token
      session[:request_token_secret] = request_token.secret

      request_token.authorize_url
    end

    def access_token(session, verifier, access_token, access_token_secret)
      token = nil

      if verifier.nil?
        token = OAuth::AccessToken.new(
          consumer,
          access_token,
          access_token_secret
        )
      else
        request_token = OAuth::RequestToken.new(
          consumer,
          session.delete(:request_token),
          session.delete(:request_token_secret)
        )
        token = request_token.get_access_token(oauth_verifier: verifier)
      end

      token
    end

    private

    def consumer
      OAuth::Consumer.new(
        @api_key,
        @api_secret,
        site: @usos_base_url,
        request_token_path: '/services/oauth/request_token',
        authorize_path: '/services/oauth/authorize',
        access_token_path: '/services/oauth/access_token'
      )
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
usos_auth_lib-0.1.0 lib/usos_auth_lib.rb