This repository has been archived by the owner on Jul 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add authentication using Google OAuth
- Add users table - Whitelist user email domains - Add environment variable configuration
- Loading branch information
Showing
10 changed files
with
168 additions
and
2 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,16 @@ | ||
# Orientation configuration | ||
# | ||
# set this to 'codeschool.com:pluralsight.com' to only authorize | ||
# emails from these two domains to sign in to Orientation | ||
APP_DOMAIN=compliments.dev | ||
EMAIL_WHITELIST= | ||
|
||
# Google OAuth 2 | ||
# | ||
# Create a new project on Google's API console here: https://console.developers.google.com/ | ||
# Then enable the Google+ and Contacts APIs for this project. | ||
# | ||
# e.g. 831a244758x7.apps.googleusercontent.com | ||
GOOGLE_KEY= | ||
# e.g. 5Ac5Fcigyty0tRO6b4c4Zh4E | ||
GOOGLE_SECRET= |
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 |
---|---|---|
|
@@ -11,3 +11,4 @@ | |
/log/* | ||
!/log/.keep | ||
/tmp | ||
.env |
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
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
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
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,33 @@ | ||
class SessionsController < ApplicationController | ||
def new | ||
origin = { origin: session.delete(:return_to) }.to_query | ||
redirect_to("/auth/google_oauth2?#{origin}") | ||
end | ||
|
||
def create | ||
user = User.find_or_create_from_omniauth(auth_hash) | ||
if user.valid? | ||
session[:user_id] = user.id | ||
flash[:notice] = "Signed in!" | ||
# OmniAuth automatically saves the HTTP_REFERER when you begin the auth process | ||
redirect_to request.env['omniauth.origin'] || root_url | ||
else | ||
flash[:error] = "You need a #{ENV.fetch('APP_DOMAIN')} account to sign in." | ||
redirect_to root_url | ||
end | ||
end | ||
|
||
def destroy | ||
session[:user_id] = nil | ||
redirect_to root_url, notice: "Signed out!" | ||
end | ||
|
||
protected | ||
|
||
def auth_hash | ||
# calling to_h because Strong Parameters don't allow direct access | ||
# to request parameters, even when passed to a class outside the | ||
# controller scope. | ||
request.env['omniauth.auth'].to_h | ||
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 |
---|---|---|
@@ -1,9 +1,46 @@ | ||
class User < ActiveRecord::Base | ||
|
||
validates :email, presence: true | ||
validate :whitelisted_email, if: -> { self.class.email_whitelist? } | ||
|
||
def self.find_or_create_from_omniauth(auth) | ||
find_and_update_from_omniauth(auth) or create_from_omniauth(auth) | ||
end | ||
|
||
def self.create_from_omniauth(auth) | ||
create do |user| | ||
user.provider = auth["provider"] | ||
user.uid = auth["uid"] | ||
user.name = auth["info"]["name"] | ||
user.email = auth["info"]["email"] | ||
user.image = auth["info"]["image"] | ||
end | ||
end | ||
|
||
def self.find_and_update_from_omniauth(auth) | ||
find_by(auth.slice("provider","uid")).tap do |user| | ||
user && user.update_attribute(:image, auth["info"]["image"]) | ||
end | ||
end | ||
|
||
def to_s | ||
self.name || self.email | ||
end | ||
|
||
private | ||
|
||
def self.email_whitelist? | ||
!!ENV['EMAIL_WHITELIST'] | ||
end | ||
|
||
def email_whitelist | ||
ENV["EMAIL_WHITELIST"].split(":") | ||
end | ||
|
||
def whitelisted_email | ||
if email_whitelist.none? { |email| self.email.include?(email) } | ||
errors.add(:email, "doesn't match the email domain whitelist: #{email_whitelist}") | ||
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 @@ | ||
Rails.application.config.middleware.use OmniAuth::Builder do | ||
provider :google_oauth2, ENV["GOOGLE_KEY"], ENV["GOOGLE_SECRET"] | ||
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
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