Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR-1] [WIP] Bernie OAuth (Gatekeeper) Integration #132

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ SENTRY_DSN=
CORS_ORIGINS=
MIN_COMPATIBLE_APP_VERSION=
FORCE_SSL=
AUTH_API_URL=
AUTH_API_APPLICATION_ID=
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ gem 'newrelic_rpm'

gem 'rack-cors', require: 'rack/cors'

gem 'rest-client'

group :development, :production do
gem 'rails_12factor'
end
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ DEPENDENCIES
rails (= 4.2.4)
rails-api
rails_12factor
rest-client
rspec-rails
rspec-sidekiq
sentry-raven
Expand Down
4 changes: 4 additions & 0 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: "[email protected]"
layout 'mailer'
end
8 changes: 8 additions & 0 deletions app/mailers/gatekeeper_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class GatekeeperMailer < ApplicationMailer
def pre_transfer(user)
mail(
to: user.email,
subject: "Coming soon: New user login system for Field the Bern"
)
end
end
29 changes: 29 additions & 0 deletions app/models/api_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class ApiUser < ActiveRecord::Base
belongs_to :user

def api_create!(user)
user_info = {
"user" => {
"email" => user.email,
"encrypted_password" => user.encrypted_password,
"first_name" => user.first_name,
"last_name" => user.last_name
}
}

response = RestClient.post("#{ENV["AUTH_API_URL"]}/users", user_info.merge(application_id: ENV['AUTH_API_APPLICATION_ID']))
response = JSON.load(response.body)

ApiUser.create!(api_user_id: response["id"], api_access_token: response["access_token"], user_id: user.id)
end

def api_save!(user_params)
user_params = ActionController::Parameters.new({ user: user_params })

user_info = {
"user" => user_params.require(:user).permit(:email, :encrypted_password, :first_name, :last_name).to_h
}

RestClient.put("#{ENV["AUTH_API_URL"]}/users/me", user_info, access_token: self.api_access_token)
end
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class User < ActiveRecord::Base

include Clearance::User

has_one :api_user

has_many :devices

has_many :visits
Expand Down
13 changes: 13 additions & 0 deletions app/views/gatekeeper_mailer/pre_transfer.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1>Coming soon: New login system for Bernie 2016</h1>

<p>
Hello! Thank you so much for helping to canvass across the United States with the Field the Bern
app! Your work is key to winning this campaign. In order to make your work easier, a new service is
being created, and it's called Gatekeeper. Gatekeeper is a single sign on (a.k.a. SSO for the techies
out there). Starting with the Field the Bern, Gatekeeper will allow volunteers to use a single email
and password across all Bernie 2016 applications. Stay tuned for more applications that will be using
this new and exciting service!

Thanks so much for your work,
The “Field the Bern” team
</p>
11 changes: 11 additions & 0 deletions app/views/gatekeeper_mailer/pre_transfer.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Coming soon: New login system for Bernie 2016

Hello! Thank you so much for helping to canvass across the United States with the Field the Bern
app! Your work is key to winning this campaign. In order to make your work easier, a new service is
being created, and it's called Gatekeeper. Gatekeeper is a single sign on (a.k.a. SSO for the techies
out there). Starting with the Field the Bern, Gatekeeper will allow volunteers to use a single email
and password across all Bernie 2016 applications. Stay tuned for more applications that will be using
this new and exciting service!

Thanks so much for your work,
The “Field the Bern” team
5 changes: 5 additions & 0 deletions app/views/layouts/mailer.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<%= yield %>
</body>
</html>
1 change: 1 addition & 0 deletions app/views/layouts/mailer.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= yield %>
10 changes: 10 additions & 0 deletions db/migrate/20160220020210_create_api_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateApiUsers < ActiveRecord::Migration
def change
create_table :api_users do |t|
t.string :api_access_token
t.integer :api_user_id
t.integer :user_id
t.timestamps null: false
end
end
end
10 changes: 9 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20151220213249) do
ActiveRecord::Schema.define(version: 20160220020210) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -64,6 +64,14 @@
t.integer "last_visited_by_id"
end

create_table "api_users", force: :cascade do |t|
t.string "api_access_token"
t.integer "api_user_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "devices", force: :cascade do |t|
t.integer "user_id"
t.string "token", null: false
Expand Down
29 changes: 29 additions & 0 deletions lib/tasks/gatekeeper.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace :gatekeeper do
task migrate_users: :environment do
users = User.all
counter = 0
puts "Creating API integration for #{users.count} users"

users.each do |user|
user.api_user = ApiUser.new.api_create!(user)
counter += 1
puts "Finished API integration for #{counter}/#{users.count}"
end

puts "API integration completed!"
end

task notify_users: :environment do
users = User.all
counter = 0
puts "Sending notification mailers for #{users.count} users"

users.each do |user|
GatekeeperMailer.pre_transfer(user)
counter += 1
puts "Sent notification mailer for #{counter}/#{users.count}"
end

puts "Notification mailer email blast completed!"
end
end
20 changes: 20 additions & 0 deletions spec/mailers/gatekeeper_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "rails_helper"

RSpec.describe GatekeeperMailer, type: :mailer do
describe "pre_transfer" do
before do
@user = FactoryGirl.create(:user)
@mail = GatekeeperMailer.pre_transfer(@user)
end

it "renders the headers" do
expect(@mail.subject).to eq("Coming soon: New user login system for Field the Bern")
expect(@mail.to).to eq([@user.email])
expect(@mail.from).to eq(["[email protected]"])
end

it "renders the body" do
expect(@mail.body.encoded).to match("Coming soon")
end
end
end
13 changes: 13 additions & 0 deletions spec/models/api_user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'rails_helper'

RSpec.describe ApiUser, type: :model do
context 'schema' do
it { should have_db_column(:api_access_token).of_type(:string) }
it { should have_db_column(:api_user_id).of_type(:integer) }
it { should have_db_column(:user_id).of_type(:integer) }
end

context 'associations' do
it { should belong_to(:user) }
end
end
1 change: 1 addition & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
it { should have_many(:passive_relationships) }
it { should have_many(:followers) }
it { should have_many(:following) }
it { should have_one(:api_user) }
end

context 'validations' do
Expand Down