class Bc::RequireGoogleAuth

Constants

DEFAULT_AFTER_AUTH_PATH
DEFAULT_ALLOWED_PATHS
DEFAULT_SESSION_KEY
OMNIAUTH_SESSION_KEY
VERSION

Public Class Methods

new(app, opts={}) click to toggle source
# File lib/bc/require_google_auth.rb, line 17
def initialize(app, opts={})
  @allowed_paths = opts[:allowed_paths] || DEFAULT_ALLOWED_PATHS
  @session_key = opts[:session_key] || DEFAULT_SESSION_KEY
  @authorized_emails = opts[:authorized_emails]
  @after_auth_path = opts[:after_auth_path] || DEFAULT_AFTER_AUTH_PATH
  @app = app
end

Public Instance Methods

allowed_path?(req) click to toggle source
# File lib/bc/require_google_auth.rb, line 32
def allowed_path?(req)
  @allowed_paths.include?(req.path)
end
auth_callback?(req) click to toggle source
# File lib/bc/require_google_auth.rb, line 25
def auth_callback?(req)
  return false unless req.path == '/auth/google_oauth2/callback'
  return false unless req.env[OMNIAUTH_SESSION_KEY]
  return false unless req.env[OMNIAUTH_SESSION_KEY][:info]
  return true
end
authorized_email?(req) click to toggle source
# File lib/bc/require_google_auth.rb, line 40
def authorized_email?(req)
  @authorized_emails.include?(req.env[OMNIAUTH_SESSION_KEY][:info][:email])
end
authorized_session?(req) click to toggle source
# File lib/bc/require_google_auth.rb, line 36
def authorized_session?(req)
  !!req.session[@session_key]
end
call(env) click to toggle source
# File lib/bc/require_google_auth.rb, line 62
def call(env)
  req = Rack::Request.new(env)

  if auth_callback?(req)
    handle_auth_callback(req)
  elsif authorized_session?(req) || allowed_path?(req)
    @app.call(env)
  else
    handle_unauthorized
  end
end
handle_auth_callback(req) click to toggle source
# File lib/bc/require_google_auth.rb, line 50
def handle_auth_callback(req)
  if authorized_email?(req)
    req.session[@session_key] = req.env[OMNIAUTH_SESSION_KEY][:info]
  else
    req.session.delete(@session_key)
  end

  res = Rack::Response.new
  res.redirect @after_auth_path, status=302
  res.finish
end
handle_unauthorized() click to toggle source
# File lib/bc/require_google_auth.rb, line 44
def handle_unauthorized
  res = Rack::Response.new
  res.redirect '/auth/google_oauth2', status=302
  res.finish
end