Class: Uricp::OrbitAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/uricp/orbit_auth.rb

Constant Summary collapse

AuthenticationFailure =
Class.new(ArgumentError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_uri, auth_id, auth_key) ⇒ OrbitAuth

Returns a new instance of OrbitAuth.



8
9
10
11
12
# File 'lib/uricp/orbit_auth.rb', line 8

def initialize(auth_uri, auth_id, auth_key)
  @auth_uri = auth_uri
  @auth_id = auth_id
  @auth_key = auth_key
end

Class Method Details

.add_auth_to_optionparser(app) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/uricp/orbit_auth.rb', line 56

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

.add_auth_token(options) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/uricp/orbit_auth.rb', line 42

def self.add_auth_token(options)
  if options['auth-user']
    orbit_credentials = new(options['auth_uri'],
                            options['auth-user'], options['auth-key'])
    options['authenticator'] = orbit_credentials.method(:token)
  elsif options['auth-token']
    orbit_token = options['auth-token']
    options['authenticator'] = -> { orbit_token }
  end
  options.delete('auth-key')
  options.delete('auth-user')
  options.delete('auth-token')
end

.validate_options(options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/uricp/orbit_auth.rb', line 27

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

Instance Method Details

#storage_urlObject



14
15
16
17
18
19
20
# File 'lib/uricp/orbit_auth.rb', line 14

def storage_url
  @storage_url ||
    begin
      authenticate
      @storage_url
    end
end

#tokenObject



22
23
24
25
# File 'lib/uricp/orbit_auth.rb', line 22

def token
  authenticate
  @token
end