module LinkedRails::Auth::AuthHelper

Constants

SAFE_METHODS
UNSAFE_METHODS

Public Instance Methods

current_user() click to toggle source
# File lib/linked_rails/auth/auth_helper.rb, line 12
def current_user
  return request.env['Current-User'] if request.env['Current-User']
  return @current_user if instance_variable_defined?(:@current_user)

  @current_user ||= current_resource_owner || create_guest_user

  handle_invalid_token unless valid_token?

  @current_user
end
doorkeeper_token() click to toggle source
Calls superclass method
# File lib/linked_rails/auth/auth_helper.rb, line 23
def doorkeeper_token
  request.env['Doorkeeper-Token'] || super
end

Private Instance Methods

create_guest_user() click to toggle source
# File lib/linked_rails/auth/auth_helper.rb, line 29
def create_guest_user
  LinkedRails.guest_user_class.new
end
doorkeeper_scopes() click to toggle source
# File lib/linked_rails/auth/auth_helper.rb, line 33
def doorkeeper_scopes
  doorkeeper_token&.scopes || []
end
doorkeeper_token_payload() click to toggle source
# File lib/linked_rails/auth/auth_helper.rb, line 37
def doorkeeper_token_payload
  @doorkeeper_token_payload ||= JWT.decode(
    doorkeeper_token.token,
    Doorkeeper::JWT.configuration.secret_key,
    true,
    algorithms: [Doorkeeper::JWT.configuration.encryption_method.to_s.upcase]
  )[0]
end
doorkeeper_unauthorized_render_options(error: nil) click to toggle source
# File lib/linked_rails/auth/auth_helper.rb, line 46
def doorkeeper_unauthorized_render_options(error: nil)
  {
    json: {
      error: :invalid_token,
      error_description: error&.description
    }
  }
end
generate_access_token(resource_owner) click to toggle source
# File lib/linked_rails/auth/auth_helper.rb, line 55
def generate_access_token(resource_owner)
  Doorkeeper::AccessToken.find_or_create_for(
    application: doorkeeper_token&.application,
    resource_owner: resource_owner,
    scopes: resource_owner.guest? ? :guest : :user,
    expires_in: Doorkeeper.configuration.access_token_expires_in,
    use_refresh_token: true
  )
end
handle_invalid_token() click to toggle source
# File lib/linked_rails/auth/auth_helper.rb, line 65
def handle_invalid_token
  @current_user = create_guest_user
end
require_doorkeeper_token?() click to toggle source
# File lib/linked_rails/auth/auth_helper.rb, line 90
def require_doorkeeper_token?
  UNSAFE_METHODS.include?(request.method)
end
sign_in(resource, *_args) click to toggle source
# File lib/linked_rails/auth/auth_helper.rb, line 69
def sign_in(resource, *_args)
  @current_user = resource
  update_oauth_token(generate_access_token(resource))

  return if request.env['warden'].blank? || warden.user(:user) == resource

  warden.set_user(resource, scope: :user, store: false)
end
sign_out(*args) click to toggle source
Calls superclass method
# File lib/linked_rails/auth/auth_helper.rb, line 78
def sign_out(*args)
  super

  doorkeeper_token.revoke if doorkeeper_token&.resource_owner_id
  update_oauth_token(generate_access_token(create_guest_user))
end
update_oauth_token(token) click to toggle source
# File lib/linked_rails/auth/auth_helper.rb, line 85
def update_oauth_token(token)
  response.headers['New-Refresh-Token'] = token.refresh_token
  response.headers['New-Authorization'] = token.token
end
valid_token?() click to toggle source
# File lib/linked_rails/auth/auth_helper.rb, line 94
def valid_token?
  return !require_doorkeeper_token? if doorkeeper_token.blank?

  doorkeeper_token&.accessible?
end