Sha256: 5be4b9c26004769d58ebeb2ca92d6865254a0550b10255afd4f03ba8ca4b1ed6

Contents?: true

Size: 1.64 KB

Versions: 7

Compression:

Stored size: 1.64 KB

Contents

require 'open-uri'
module Uricp

  class OrbitAuth
    
    AuthenticationFailure = Class.new(ArgumentError)

    def initialize(auth_uri, auth_id, auth_key)
      auth_uri.open(
        'X-Auth-User' => auth_id,
	'X-Auth-Key' => auth_key,
	'Range' => 'bytes=0-0'
      ) do |uri|
        @storage_url = uri.meta['x-storage-url']
	@token = uri.meta['x-auth-token']
      end
    rescue OpenURI::HTTPError => e
      raise AuthenticationFailure, "Cannot authenticate against #{auth_uri}"
    end

    attr_reader :storage_url, :token

    def self.validate_options(options)
      if options['auth-token'] && (options['auth-key'] || options['auth-user'])
	raise ::OptionParser::NeedlessArgument,
	  "use either key based or token based authentication"
      end
      if options['auth-key'].nil? ^ options['auth-user'].nil?
	raise ::OptionParser::MissingArgument,
	  "'auth-user' requires 'auth-key'"
      end
      if (options['auth-token'] || options['auth-user']) && options['auth_uri'].nil?
	raise ::OptionParser::NeedlessArgument,
	  "authentication is for http uris only"
      end
    end

    def self.add_auth_token(options)
      if options['auth-user']
	orbit_credentials = self.new(options['auth_uri'],
			      options['auth-user'], options['auth-key'])
	options['auth-token'] = orbit_credentials.token
	options.delete('auth-key')
	options.delete('auth-user')
      end
    end

    def self.add_auth_to_optionparser(app)
      app.on("--auth-token AUTH_TOKEN",
	"Use AUTH_TOKEN for non-local requests")
      app.on("--auth-user AUTH_USER",
	"Use AUTH_USER for authentication")
      app.on("--auth-key AUTH_KEY",
	"Use AUTH_KEY for authentication")
    end

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
uricp-0.0.7 lib/uricp/orbit_auth.rb
uricp-0.0.6 lib/uricp/orbit_auth.rb
uricp-0.0.5 lib/uricp/orbit_auth.rb
uricp-0.0.4 lib/uricp/orbit_auth.rb
uricp-0.0.3 lib/uricp/orbit_auth.rb
uricp-0.0.2 lib/uricp/orbit_auth.rb
uricp-0.0.1 lib/uricp/orbit_auth.rb