This repository has been archived by the owner on Apr 27, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Burke Libbey
committed
Feb 10, 2012
0 parents
commit f32e0fd
Showing
11 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.gem | ||
.bundle | ||
Gemfile.lock | ||
pkg/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source "http://rubygems.org" | ||
|
||
# Specify your gem's dependencies in google_auth.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require "bundler/gem_tasks" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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%;">☃</div>' | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module GoogleAuth | ||
VERSION = "0.0.1" | ||
end |