Class: Motivosity::Auth

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/mvclient/auth.rb

Instance Method Summary collapse

Constructor Details

#initializeAuth

Returns a new instance of Auth.



14
15
16
17
18
19
20
21
22
23
# File 'lib/mvclient/auth.rb', line 14

def initialize
  cookie_jar_path = File.expand_path("~/.motivosity-session")
  # make sure to create the cookie jar as read/write to current user only
  old_umask = File.umask(0177)
  begin
    @cookies = HTTP::CookieJar.new(store: :mozilla, filename: cookie_jar_path)
  ensure
    File.umask(old_umask)
  end
end

Instance Method Details

#auth_headersObject



47
48
49
# File 'lib/mvclient/auth.rb', line 47

def auth_headers
  { "Cookie" => HTTP::Cookie.cookie_value(@cookies.cookies) }
end

#login!(username, password) ⇒ Object

Raises:



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

def login!(username, password)
  @cookies.clear
  response = self.class.post('/login.xhtml', {
      body: {
          "loginForm" => 'loginForm',
          "email" => username,
          "j_password" => password,
          "rememberMe" => 'on',
          "signInLink" => 'Sign In',
          "javax.faces.ViewState" => "3465473682371097839:-947621468971335341"
      }
  })
  raise UnauthorizedError.new("invalid username or password") unless response.code == 302
  process_response_headers(response)
  @cookies.cookies.length > 0
end

#logout!Object



42
43
44
45
# File 'lib/mvclient/auth.rb', line 42

def logout!
  @cookies.clear
  true
end

#process_response_headers(response) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/mvclient/auth.rb', line 51

def process_response_headers(response)
  split_cookie_headers(response.headers['Set-Cookie']).each do |cookie_string|
    @cookies.parse(cookie_string, "https://www.motivosity.com/") do |cookie|
      cookie.max_age ||= 604800 if cookie.name == 'JSESSIONID' # force the gem to persist the session key (for one week)
      cookie
    end
  end
end