Class: Motion::Authentication::DeviseCookieAuth
- Inherits:
-
Object
- Object
- Motion::Authentication::DeviseCookieAuth
- Defined in:
- lib/project/strategies/devise_cookie_auth.rb
Class Method Summary collapse
- .get_csrf_token(sign_in_url, &block) ⇒ Object
- .restore_session ⇒ Object
- .sign_in(sign_in_url, params, &block) ⇒ Object
- .sign_out(&block) ⇒ Object
- .signed_in? ⇒ Boolean
- .store_session_cookie(cookie) ⇒ Object
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(sign_in_url, &block) HTTP.get(sign_in_url) do |response| doc = Motion::HTML.parse(response.body) = doc.query('head meta[name="csrf-param"]').first = doc.query('head meta[name="csrf-token"]').first if && param_name = ['content'] token = ['content'] block.call(param_name, token) else mp 'Couldnt parse CSRF token from HTML' end end end |
.restore_session ⇒ Object
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) = NSHTTPCookie.(data['properties']) NSHTTPCookieStorage.sharedHTTPCookieStorage.setCookie() 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 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 = NSHTTPCookieStorage.sharedHTTPCookieStorage.(NSURL.URLWithString(sign_in_url)).first () 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
39 40 41 |
# File 'lib/project/strategies/devise_cookie_auth.rb', line 39 def signed_in? MotionKeychain.get(:session_cookie) && restore_session end |
.store_session_cookie(cookie) ⇒ Object
35 36 37 |
# File 'lib/project/strategies/devise_cookie_auth.rb', line 35 def () MotionKeychain.set :session_cookie, JSON.generate(properties: .properties) end |