Sha256: a6ac3306e9131e21581ac032d84b1883bd49bc6f420c26920afe946104e8da6f

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

module Shutl
  module  Auth
    class Shutl::Error        < ::StandardError; end
    class InvalidUrl          < Shutl::Error; end
    class InvalidCredentials  < Shutl::Error; end
    class InternalServerError < Shutl::Error; end

    def access_token!
      access_token_response!.access_token
    end

    def access_token_response!
      c = client

      Shutl::NetworkRetry.retry "Authentication Service Error" do
        handling_exceptions { c.access_token! }
     end
    end

    private

    def handling_exceptions
      yield
    rescue Rack::OAuth2::Client::Error => e
      case e.message
      when /The client identifier provided is invalid, the client failed to authenticate, the client did not include its credentials, provided multiple client credentials, or used unsupported credentials type\./
        raise_invalid_credentials
      else
        raise_internal_server_error e
      end
    end

    def client
      check uri

      Rack::OAuth2::Client.new \
        identifier:     Shutl::Auth.client_id,
        secret:         Shutl::Auth.client_secret,
        token_endpoint: '/token',
        host:           uri.host,
        port:           uri.port,
        scheme:         uri.scheme
    end


    def uri
      URI Shutl::Auth.url
    rescue
      raise_invalid_uri
    end

    def check uri
      return uri if uri and uri.host and uri.scheme
      raise_invalid_uri

    rescue
      raise_invalid_uri
    end

    def raise_invalid_uri
      raise Shutl::Auth::InvalidUrl, "Please set value of Shutl::Auth.url"
    end

    def raise_invalid_credentials
      raise Shutl::Auth::InvalidCredentials, "Invalid credentials set, please see https://github.com/shutl/shutl_auth/blob/master/README.md"
    end

    def raise_internal_server_error e
      Shutl.notify e
      raise Shutl::Auth::InternalServerError
    end

    extend self
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shutl_auth-0.8.1 lib/shutl/auth/access_token_request.rb
shutl_auth-0.8.0 lib/shutl/auth/access_token_request.rb