Class: PhraseApp::Auth::Credentials

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/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/auth.rb', line 9

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
43
44
45
46
47
48
49
50
# File 'lib/auth.rb', line 26

def authenticate(req)
  if err = load_config
    return err
  end

  if err = validate
    return err
  end

  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
  else
    raise "No authentication present"
  end

  return nil

end

#hostObject



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

def host
  @host
end

#validateObject



18
19
20
21
22
23
24
# File 'lib/auth.rb', line 18

def validate
  if self.username.to_s == "" && self.token.to_s == ""
    return raise("either username or token must be given")
  else
    return nil
  end
end