Class: Motion::Authentication::DeviseCookieAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/project/strategies/devise_cookie_auth.rb

Class Method Summary collapse

Class Method Details

.get_csrf_token(sign_in_url, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/project/strategies/devise_cookie_auth.rb', line 20

def get_csrf_token(, &block)
  HTTP.get() 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_sessionObject



43
44
45
46
47
48
# 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) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/project/strategies/devise_cookie_auth.rb', line 5

def (, params, &block)
  get_csrf_token() do |param_name, token|
    namespace = params[:namespace] || :user
    HTTP.post(, 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()).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) ⇒ Object



50
51
52
53
# File 'lib/project/strategies/devise_cookie_auth.rb', line 50

def sign_out(&block)
  MotionKeychain.remove :session_cookie
  block.call
end

.signed_in?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/project/strategies/devise_cookie_auth.rb', line 39

def signed_in?
  MotionKeychain.get(:session_cookie) && restore_session
end


35
36
37
# File 'lib/project/strategies/devise_cookie_auth.rb', line 35

def store_session_cookie(cookie)
  MotionKeychain.set :session_cookie, JSON.generate(properties: cookie.properties)
end