Module: Clowk::Authenticable

Extended by:
ActiveSupport::Concern
Included in:
BaseController
Defined in:
lib/clowk/authenticable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.install_dynamic_methods(base) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/clowk/authenticable.rb', line 10

def self.install_dynamic_methods(base)
  scope = Clowk.config.prefix_by.to_s
  current_method = :"current_#{scope}"
  authenticate_method = :"authenticate_#{scope}!"
  signed_in_method = :"#{scope}_signed_in?"

  enforce_session_method = :"#{scope}_enforce_session!"
  sign_out_method = :"#{scope}_sign_out!"

  base.class_eval do
    unless current_method == :clowk_current_resource
      define_method(current_method) do
        clowk_current_resource
      end
    end

    unless authenticate_method == :clowk_authenticate!
      define_method(authenticate_method) do
        clowk_authenticate!
      end
    end

    unless signed_in_method == :clowk_signed_in?
      define_method(signed_in_method) do
        clowk_signed_in?
      end
    end

    unless enforce_session_method == :clowk_enforce_session!
      define_method(enforce_session_method) do
        clowk_enforce_session!
      end
    end

    unless sign_out_method == :clowk_sign_out!
      define_method(sign_out_method) do
        clowk_sign_out!
      end
    end

    helper_method current_method, authenticate_method, signed_in_method, :current_token if respond_to?(:helper_method)
  end
end

Instance Method Details

#clowk_authenticate!Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/clowk/authenticable.rb', line 96

def clowk_authenticate!
  return clowk_handle_unauthenticated unless clowk_signed_in?

  # A valid token proves who signed in, not that the session still stands —
  # revocation lives server-side. Opt in with config.enforce_active_session
  # to pay a lookup (cached for session_status_ttl) on every authentication.
  if Clowk.config.enforce_active_session
    clowk_enforce_session!
    return if respond_to?(:performed?) && performed?
  end

  clowk_current_resource
end

#clowk_current_resourceObject



58
59
60
61
62
63
# File 'lib/clowk/authenticable.rb', line 58

def clowk_current_resource
  @clowk_current_resource ||= begin
    payload = stored_user_payload || verified_request_payload
    payload ? Current.new(payload) : nil
  end
end

#clowk_enforce_session!Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/clowk/authenticable.rb', line 81

def clowk_enforce_session!
  return if clowk_session_active?

  session_info = clowk_session_status
  callback = Clowk.config.on_session_expired

  if callback.respond_to?(:call)
    callback.call(self, session_info)

    return
  end

  clowk_handle_expired_session(session_info)
end

#clowk_session_active?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/clowk/authenticable.rb', line 77

def clowk_session_active?
  clowk_session_status&.dig(:status) == "active"
end

#clowk_session_statusObject



73
74
75
# File 'lib/clowk/authenticable.rb', line 73

def clowk_session_status
  @clowk_session_status ||= resolve_session_status
end

#clowk_sign_out!Object



110
111
112
113
114
115
# File 'lib/clowk/authenticable.rb', line 110

def clowk_sign_out!
  clowk_session_store&.delete(Clowk.config.session_key)
  clowk_cookie_jar&.delete(Clowk.config.cookie_key)

  @clowk_current_resource = nil
end

#clowk_signed_in?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/clowk/authenticable.rb', line 69

def clowk_signed_in?
  clowk_current_resource.present?
end

#current_tokenObject



65
66
67
# File 'lib/clowk/authenticable.rb', line 65

def current_token
  stored_session&.dig("token") || extracted_token
end