Class: PhraseApp::Auth::Credentials

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/phraseapp-ruby/auth.rb

Overview

See PhraseApp::Client for usage example

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Credentials

Returns a new instance of Credentials.



9
10
11
12
# File 'lib/phraseapp-ruby/auth.rb', line 9

def initialize(hash={})
  super(hash)
  @host ||= hash.fetch(:host, PhraseApp::URL)
end

Instance Method Details

#authenticate(req) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/phraseapp-ruby/auth.rb', line 24

def authenticate(req)
  load_config
  validate!

  if self.token && self.token != ""
    req["Authorization"] = "token #{self.token}"
  elsif self.username && self.username != ""
    req.basic_auth(self.username, self.password)

    if self.tfa or self.tfa_token # TFA only required for username+password based login.
      raise "Multi-Factor Token required in config but not provided." unless self.tfa_token
      req["X-PhraseApp-OTP"] = auth.tfa_token
    end
  end

  return nil
end

#hostObject



14
15
16
# File 'lib/phraseapp-ruby/auth.rb', line 14

def host
  @host
end

#validate!Object



18
19
20
21
22
# File 'lib/phraseapp-ruby/auth.rb', line 18

def validate!
  if self.username.to_s == "" && self.token.to_s == ""
    raise ValidationError.new("either username or token must be given")
  end
end