Module: Fridge::RailsHelpers
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/fridge/rails_helpers.rb
Instance Method Summary collapse
- #auth_domain ⇒ Object
- #bearer_token ⇒ Object
- #clear_session_cookie ⇒ Object
- #current_token ⇒ Object
- #delete_shared_cookie(name) ⇒ Object
- #fetch_shared_cookie(name) ⇒ Object
- #fridge_cookie_name ⇒ Object
- #fridge_cookie_options ⇒ Object
- #read_shared_cookie(name) ⇒ Object
- #session_actor ⇒ Object
- #session_cookie ⇒ Object
- #session_cookie=(cookie) ⇒ Object
- #session_subject ⇒ Object
- #session_token ⇒ Object
- #sessionize_token(access_token) ⇒ Object
- #token_actor ⇒ Object
- #token_scope ⇒ Object
- #token_subject ⇒ Object
-
#validate_token(access_token) ⇒ Object
Validates token, and returns the token, or nil.
-
#validate_token!(access_token) ⇒ Object
Validates token, and raises an exception if invalid.
- #write_shared_cookie(name, value, options = {}) ⇒ Object
Instance Method Details
#auth_domain ⇒ Object
128 129 130 131 132 |
# File 'lib/fridge/rails_helpers.rb', line 128 def auth_domain Aptible::Auth.configuration.root_url.sub(%r{^https?://}, '') rescue StandardError 'auth.aptible.com' end |
#bearer_token ⇒ Object
30 31 32 33 |
# File 'lib/fridge/rails_helpers.rb', line 30 def bearer_token header = request.env['HTTP_AUTHORIZATION'] header.gsub(/^Bearer /, '') unless header.nil? end |
#clear_session_cookie ⇒ Object
90 91 92 93 |
# File 'lib/fridge/rails_helpers.rb', line 90 def .delete , domain: auth_domain nil end |
#current_token ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/fridge/rails_helpers.rb', line 22 def current_token return unless bearer_token @current_token ||= AccessToken.new(bearer_token).tap do |token| validate_token!(token) end end |
#delete_shared_cookie(name) ⇒ Object
114 115 116 |
# File 'lib/fridge/rails_helpers.rb', line 114 def (name) .delete name, .slice(:domain) end |
#fetch_shared_cookie(name) ⇒ Object
108 109 110 111 112 |
# File 'lib/fridge/rails_helpers.rb', line 108 def (name) return (name) if (name) (yield) end |
#fridge_cookie_name ⇒ Object
118 119 120 |
# File 'lib/fridge/rails_helpers.rb', line 118 def Fridge.configuration. end |
#fridge_cookie_options ⇒ Object
122 123 124 125 126 |
# File 'lib/fridge/rails_helpers.rb', line 122 def secure = !Rails.env.development? = { domain: auth_domain, secure: secure, httponly: true } .merge(Fridge.configuration.) end |
#read_shared_cookie(name) ⇒ Object
104 105 106 |
# File 'lib/fridge/rails_helpers.rb', line 104 def (name) [name] end |
#session_actor ⇒ Object
39 40 41 |
# File 'lib/fridge/rails_helpers.rb', line 39 def session_actor session_token.actor if session_token end |
#session_cookie ⇒ Object
82 83 84 |
# File 'lib/fridge/rails_helpers.rb', line 82 def [] end |
#session_cookie=(cookie) ⇒ Object
86 87 88 |
# File 'lib/fridge/rails_helpers.rb', line 86 def () [] = end |
#session_subject ⇒ Object
35 36 37 |
# File 'lib/fridge/rails_helpers.rb', line 35 def session_subject session_token.subject if session_token end |
#session_token ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/fridge/rails_helpers.rb', line 43 def session_token return unless @session_token ||= AccessToken.new().tap do |token| validate_token!(token).downgrade end rescue StandardError end |
#sessionize_token(access_token) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/fridge/rails_helpers.rb', line 71 def sessionize_token(access_token) # Ensure that any cookie-persisted tokens are read-only access_token.scope = 'read' jwt = access_token.serialize self. = { value: jwt, expires: access_token.expires_at }.merge() end |
#token_actor ⇒ Object
18 19 20 |
# File 'lib/fridge/rails_helpers.rb', line 18 def token_actor current_token.actor if current_token end |
#token_scope ⇒ Object
10 11 12 |
# File 'lib/fridge/rails_helpers.rb', line 10 def token_scope current_token.scope if current_token end |
#token_subject ⇒ Object
14 15 16 |
# File 'lib/fridge/rails_helpers.rb', line 14 def token_subject current_token.subject if current_token end |
#validate_token(access_token) ⇒ Object
Validates token, and returns the token, or nil
54 55 56 57 58 59 |
# File 'lib/fridge/rails_helpers.rb', line 54 def validate_token(access_token) validator = Fridge.configuration.validator validator.call(access_token) && access_token rescue StandardError false end |
#validate_token!(access_token) ⇒ Object
Validates token, and raises an exception if invalid
62 63 64 65 66 67 68 69 |
# File 'lib/fridge/rails_helpers.rb', line 62 def validate_token!(access_token) validator = Fridge.configuration.validator if validator.call(access_token) access_token else raise InvalidToken, 'Rejected by validator' end end |
#write_shared_cookie(name, value, options = {}) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/fridge/rails_helpers.rb', line 95 def (name, value, = {}) raise 'Can only write string cookie values' unless value.is_a?(String) [name] = { value: value, expires: [:expires] || 1.year.from_now }.merge() end |