Sha256: 94f85fea80c5374e049ba4b817d7a91e798ee80a809a8ec58bd88ed76022cd7c

Contents?: true

Size: 1.39 KB

Versions: 4

Compression:

Stored size: 1.39 KB

Contents

module Shutl
  module Auth
    class Authenticator

      def initialize(options = {})
        Shutl::Auth.logger.debug "building a new authenticator"
        self.cache_key      = options.fetch(:cache_key) { :access_token }
        self.client_id      = options.fetch(:client_id)
        self.client_secret  = options.fetch(:client_secret)
        self.url            = options.fetch(:url)
      end

      def access_token
        return read_token if read_token
        set_token request_access_token
      end

      def authenticated_request &blk
        begin
          yield
        rescue Shutl::UnauthorizedAccess => e
          Shutl::Auth.logger.debug "Shutl::UnauthorizedAccess - resetting access token"
          set_token nil
          access_token
          yield
        end
      end

      def request_access_token
        return read_token if read_token
        Shutl::Auth.logger.debug "request_access_token: cached? #{!!read_token}"

        AccessTokenRequest.new(opts).access_token
      end

      private

      attr_accessor :cache_key, :client_id, :client_secret, :url

      def read_token
        Shutl::Auth.cache.read(cache_key)
      end

      def set_token(token)
        Shutl::Auth.cache.write(cache_key, token)
      end

      def opts
        {
          client_id:     client_id,
          client_secret: client_secret,
          url:           url
        }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
shutl_auth-0.9.3 lib/shutl/auth/authenticator.rb
shutl_auth-0.9.2 lib/shutl/auth/authenticator.rb
shutl_auth-0.9.1 lib/shutl/auth/authenticator.rb
shutl_auth-0.9.0 lib/shutl/auth/authenticator.rb