class Motion::Authentication::DeviseCookieAuth

Public Class Methods

get_csrf_token(sign_in_url, &block) click to toggle source
# File lib/project/strategies/devise_cookie_auth.rb, line 20
def get_csrf_token(sign_in_url, &block)
  HTTP.get(sign_in_url) do |response|
    doc = Motion::HTML.parse(response.body)
    param_meta_tag = doc.query('head meta[name="csrf-param"]').first
    token_meta_tag = doc.query('head meta[name="csrf-token"]').first
    if param_meta_tag && token_meta_tag
      param_name = param_meta_tag['content']
      token = token_meta_tag['content']
      block.call(param_name, token)
    else
      mp 'Couldnt parse CSRF token from HTML'
    end
  end
end
restore_session() click to toggle source
# File lib/project/strategies/devise_cookie_auth.rb, line 43
def restore_session
  json = MotionKeychain.get(:session_cookie)
  data = JSON.parse(json)
  cookie = NSHTTPCookie.cookieWithProperties(data['properties'])
  NSHTTPCookieStorage.sharedHTTPCookieStorage.setCookie(cookie)
end
sign_in(sign_in_url, params, &block) click to toggle source
# File lib/project/strategies/devise_cookie_auth.rb, line 5
def sign_in(sign_in_url, params, &block)
  get_csrf_token(sign_in_url) do |param_name, token|
    namespace = params[:namespace] || :user
    HTTP.post(sign_in_url, form: { namespace => params, param_name => token }, follow_redirects: false) do |response|
      if response.status_code == 302 # assume success due to redirect
        cookie = NSHTTPCookieStorage.sharedHTTPCookieStorage.cookiesForURL(NSURL.URLWithString(sign_in_url)).first
        store_session_cookie(cookie)
        block.call(true)
      else # didn't redirect, must be invalid credentials
        block.call(false)
      end
    end
  end
end
sign_out(&block) click to toggle source
# File lib/project/strategies/devise_cookie_auth.rb, line 50
def sign_out(&block)
  MotionKeychain.remove :session_cookie
  block.call
end
signed_in?() click to toggle source
# File lib/project/strategies/devise_cookie_auth.rb, line 39
def signed_in?
  MotionKeychain.get(:session_cookie) && restore_session
end