Skip to content
This repository has been archived by the owner on Apr 27, 2021. It is now read-only.

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Burke Libbey committed Feb 10, 2012
0 parents commit f32e0fd
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
*.gem
.bundle
Gemfile.lock
pkg/*
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source "http://rubygems.org"

# Specify your gem's dependencies in google_auth.gemspec
gemspec
19 changes: 19 additions & 0 deletions README.md
@@ -0,0 +1,19 @@
# GoogleAuth

## What

GoogleAuth is a convenience wrapper around `omniauth-google-apps` that
makes a lot of assumptions, and requires less setup.

## How

# Gemfile
gem 'google_auth'

# config/initializers/google_auth.rb
Rails.application.config.google_auth.domain = 'yourgoogleappsdomain.com'
# other options: 'user_class', 'path'. See source for details.

# Whichever controller you want behind the login-wall
include GoogleAuth::Controller

1 change: 1 addition & 0 deletions Rakefile
@@ -0,0 +1 @@
require "bundler/gem_tasks"
26 changes: 26 additions & 0 deletions app/controllers/sessions_controller.rb
@@ -0,0 +1,26 @@
class SessionsController < ApplicationController
skip_before_filter :ensure_authenticated

def new
redirect_to '/auth/g'
end

def create
if auth = request.env['omniauth.auth']
user = User.find_or_initialize_by_email(auth['info']['email'])
user.uid = auth['uid']
user.name = auth['info']['name']
user.save!

session[:user_id] = user.id
redirect_to session[:redirect] || root_url
else
render '/auth/failure'
end
end

def failure
render :inline => 'Snowman says no. <div id="snowman" style="text-align:center; font-size:4000%;">&#9731;</div>'
end
end

5 changes: 5 additions & 0 deletions config/routes.rb
@@ -0,0 +1,5 @@
GoogleAuth::Engine.routes.draw do
match '/login', :to => 'sessions#new', :as => :login
match '/auth/g/callback', :to => 'sessions#create'
match '/auth/failure', :to => 'sessions#failure'
end
20 changes: 20 additions & 0 deletions google_auth.gemspec
@@ -0,0 +1,20 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "google_auth/version"

Gem::Specification.new do |s|
s.name = "google_auth"
s.version = GoogleAuth::VERSION
s.authors = ["Burke Libbey"]
s.email = ["burke.libbey@shopify.com"]
s.homepage = ""
s.summary = %q{Convenience wrapper of omniauth-google-apps}
s.description = %q{Convenience wrapper for omniauth-google-apps: Makes a lot of assumptions, requires less configuration.}

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_runtime_dependency 'omniauth-google-apps'
end
28 changes: 28 additions & 0 deletions lib/google_auth.rb
@@ -0,0 +1,28 @@
require 'google_auth/version'
require 'google_auth/engine'
require 'google_auth/controller'

module GoogleAuth

def self.domain
Rails.application.config.google_auth.domain or
raise "domain missing! Add config.google_auth.domain to an initializer."
end

def self.path
Rails.application.config.google_auth.path or
'g'
end

def self.user_class
klass = Rails.application.config.google_auth.path
return klass if klass

begin
User
rescue
raise "User class not found! Add config.google_auth.user_class to an initializer."
end
end

end
23 changes: 23 additions & 0 deletions lib/google_auth/controller.rb
@@ -0,0 +1,23 @@
module GoogleAuth
module Controller

def self.included(base)
base.send(:before_filter, :ensure_authenticated)
base.send(:helper_method, :current_user)
end

def current_user
@current_user ||= begin
GoogleAuth.user_class.find_by_id(session[:user_id]) if session[:user_id]
end
end

def ensure_authenticated
unless current_user
session[:redirect] = request.fullpath
redirect_to(login_path)
end
end

end
end
19 changes: 19 additions & 0 deletions lib/google_auth/engine.rb
@@ -0,0 +1,19 @@
require 'omniauth-google-apps'
require 'openid/store/filesystem'

module GoogleAuth
class Engine < ::Rails::Engine

config.google_auth = ActiveSupport::OrderedOptions.new

initializer 'google_auth' do |app|
# OpenID.fetcher.ca_file = 'vendor/ca-bundle.crt'
app.middleware.use OmniAuth::Builder do
provider :google_apps,
store: OpenID::Store::Filesystem.new('./tmp'),
name: GoogleAuth.path,
domain: GoogleAuth.domain
end
end
end
end
3 changes: 3 additions & 0 deletions lib/google_auth/version.rb
@@ -0,0 +1,3 @@
module GoogleAuth
VERSION = "0.0.1"
end

0 comments on commit f32e0fd

Please sign in to comment.