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.



11
12
13
14
# File 'lib/phraseapp-ruby/auth.rb', line 11

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

Instance Method Details

#authenticate(req) ⇒ Object



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

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



16
17
18
# File 'lib/phraseapp-ruby/auth.rb', line 16

def host
  @host
end

#validate!Object



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

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