class Motivosity::Auth

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

auth_headers() click to toggle source
# File lib/mvclient/auth.rb, line 47
def auth_headers
  { "Cookie" => HTTP::Cookie.cookie_value(@cookies.cookies) }
end
login!(username, password) click to toggle source
# 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!() click to toggle source
# File lib/mvclient/auth.rb, line 42
def logout!
  @cookies.clear
  true
end
process_response_headers(response) click to toggle source
# 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

Private Instance Methods