class Rack::NinjaAuth::Middleware

Constants

SALT_BYTES
SESSION_KEY

Public Class Methods

new(app, email_matcher: //, secured_routes: //, not_allowed_file: nil, authorized_file: nil) click to toggle source
Calls superclass method
# File lib/rack/ninja_auth.rb, line 18
def initialize(app, email_matcher: //, secured_routes: //, not_allowed_file: nil, authorized_file: nil)
  $stderr.puts "Please set NINJA_GOOGLE_CLIENT_ID and NINJA_GOOGLE_CLIENT_SECRET to use NinjaAuth" unless ENV["NINJA_GOOGLE_CLIENT_ID"] && ENV["NINJA_GOOGLE_CLIENT_SECRET"]
  @main_app = app
  @email_matcher = email_matcher
  @secured_route_matcher = secured_routes
  @not_allowed_file = ::File.join(__dir__, '../../views/401.html')
  @not_allowed_file = not_allowed_file if not_allowed_file && ::File.exists?(not_allowed_file)
  @authorized_file = ::File.join(__dir__, '../../views/200.html')
  @authorized_file = authorized_file if authorized_file && ::File.exists?(authorized_file)
  super()
end

Private Instance Methods

allowable_email?(email) click to toggle source
# File lib/rack/ninja_auth.rb, line 64
def allowable_email?(email)
  email.respond_to?(:match) && email.match(@email_matcher)
end
authenticate!(email:) click to toggle source
# File lib/rack/ninja_auth.rb, line 60
def authenticate!(email:)
  session[SESSION_KEY] = { 'email' => email }
end
is_authenticated?() click to toggle source
# File lib/rack/ninja_auth.rb, line 68
def is_authenticated?
  fields = session[SESSION_KEY] || {}
  allowable_email?(fields['email'])
end
is_internal_request?() click to toggle source
# File lib/rack/ninja_auth.rb, line 77
def is_internal_request?
  !!env['REQUEST_URI'].match(%r{^/auth/google_oauth2})
end
is_unprotected_request?() click to toggle source
# File lib/rack/ninja_auth.rb, line 73
def is_unprotected_request?
  !env['PATH_INFO'].match(@secured_route_matcher)
end