module Fridge::RailsHelpers

Public Instance Methods

auth_domain() click to toggle source
# File lib/fridge/rails_helpers.rb, line 128
def auth_domain
  domain = URI.parse(Aptible::Auth.configuration.root_url).host

  # On localhost we fall back to the default setting b/c browsers won't set
  # cookies if localhost is named
  domain == 'localhost' ? :all : domain
rescue StandardError
  'auth.aptible.com'
end
bearer_token() click to toggle source
# File lib/fridge/rails_helpers.rb, line 30
def bearer_token
  header = request.env['HTTP_AUTHORIZATION']
  header.gsub(/^Bearer /, '') unless header.nil?
end
current_token() click to toggle source
# 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
session_actor() click to toggle source
# File lib/fridge/rails_helpers.rb, line 39
def session_actor
  session_token.actor if session_token
end
session_subject() click to toggle source
# File lib/fridge/rails_helpers.rb, line 35
def session_subject
  session_token.subject if session_token
end
session_token() click to toggle source
# File lib/fridge/rails_helpers.rb, line 43
def session_token
  return unless session_cookie

  @session_token ||= AccessToken.new(session_cookie).tap do |token|
    validate_token!(token).downgrade
  end
rescue StandardError
  clear_session_cookie
end
sessionize_token(access_token) click to toggle source
# 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.session_cookie = {
    value: jwt,
    expires: access_token.expires_at
  }.merge(fridge_cookie_options)
end
token_actor() click to toggle source
# File lib/fridge/rails_helpers.rb, line 18
def token_actor
  current_token.actor if current_token
end
token_scope() click to toggle source
# File lib/fridge/rails_helpers.rb, line 10
def token_scope
  current_token.scope if current_token
end
token_subject() click to toggle source
# File lib/fridge/rails_helpers.rb, line 14
def token_subject
  current_token.subject if current_token
end
validate_token(access_token) click to toggle source

Validates token, and returns the token, or nil

# 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) click to toggle source

Validates token, and raises an exception if invalid

# 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