Skip to content

Commit

Permalink
Merge pull request #10 from youzik/slack_auth
Browse files Browse the repository at this point in the history
Slack auth
  • Loading branch information
Ch4s3 authored Oct 17, 2016
2 parents 1dd77ce + bb256f0 commit 0cda67d
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 12 deletions.
7 changes: 6 additions & 1 deletion lib/generators/sorcery/templates/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@


# -- external --
# What providers are supported by this app, i.e. [:twitter, :facebook, :github, :linkedin, :xing, :google, :liveid, :salesforce] .
# What providers are supported by this app, i.e. [:twitter, :facebook, :github, :linkedin, :xing, :google, :liveid, :salesforce, :slack] .
# Default: `[]`
#
# config.external_providers =
Expand Down Expand Up @@ -144,6 +144,11 @@
# config.vk.callback_url = "http://0.0.0.0:3000/oauth/callback?provider=vk"
# config.vk.user_info_mapping = {:login => "domain", :name => "full_name"}
#
#config.slack.callback_url = "http://0.0.0.0:3000/oauth/callback?provider=slack"
#config.slack.key = ''
#config.slack.secret = ''
#config.slack.user_info_mapping = {email: 'email'}
#
# To use liveid in development mode you have to replace mydomain.com with
# a valid domain even in development. To use a valid domain in development
# simply add your domain in your /etc/hosts file in front of 127.0.0.1
Expand Down
1 change: 1 addition & 0 deletions lib/sorcery/controller/submodules/external.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def self.included(base)
require 'sorcery/providers/jira'
require 'sorcery/providers/salesforce'
require 'sorcery/providers/paypal'
require 'sorcery/providers/slack'

Config.module_eval do
class << self
Expand Down
46 changes: 46 additions & 0 deletions lib/sorcery/providers/slack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Sorcery
module Providers
# This class adds support for OAuth with slack.com.

class Slack < Base

include Protocols::Oauth2

attr_accessor :auth_path, :scope, :token_url, :user_info_path

def initialize
super

@scope = 'identity.basic, identity.email'
@site = 'https://slack.com/'
@user_info_path = 'https://slack.com/api/users.identity'
@auth_path = '/oauth/authorize'
@token_url = '/api/oauth.access'
end

def get_user_hash(access_token)
response = access_token.get(user_info_path, params: { token: access_token.token })
auth_hash(access_token).tap do |h|
h[:user_info] = JSON.parse(response.body)
h[:user_info]['email'] = h[:user_info]['user']['email']
h[:uid] = h[:user_info]['user']['id']
end
end

# calculates and returns the url to which the user should be redirected,
# to get authenticated at the external provider's site.
def login_url(params, session)
authorize_url({ authorize_url: auth_path })
end

# tries to login the user from access token
def process_callback(params, session)
args = {}.tap do |a|
a[:code] = params[:code] if params[:code]
end

get_access_token(args, token_url: token_url, token_method: :post)
end
end
end
end
1 change: 0 additions & 1 deletion sorcery.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,3 @@ Gem::Specification.new do |s|
s.add_development_dependency "rspec-rails", "~> 3.1.0"
s.add_development_dependency "test-unit", "~> 3.1.0"
end

29 changes: 22 additions & 7 deletions spec/controllers/controller_oauth2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
expect(flash[:notice]).to eq "Success!"
end

[:github, :google, :liveid, :vk, :salesforce, :paypal].each do |provider|
[:github, :google, :liveid, :vk, :salesforce, :paypal, :slack].each do |provider|

describe "with #{provider}" do

Expand Down Expand Up @@ -205,7 +205,7 @@
end

sorcery_reload!([:user_activation,:external], :user_activation_mailer => ::SorceryMailer)
sorcery_controller_property_set(:external_providers, [:facebook, :github, :google, :liveid, :vk, :salesforce, :paypal])
sorcery_controller_property_set(:external_providers, [:facebook, :github, :google, :liveid, :vk, :salesforce, :paypal, :slack])

sorcery_controller_external_property_set(:facebook, :key, "eYVNBjBDi33aa9GkA3w")
sorcery_controller_external_property_set(:facebook, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
Expand All @@ -228,8 +228,13 @@
sorcery_controller_external_property_set(:paypal, :key, "eYVNBjBDi33aa9GkA3w")
sorcery_controller_external_property_set(:paypal, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
sorcery_controller_external_property_set(:paypal, :callback_url, "http://blabla.com")
sorcery_controller_external_property_set(:slack, :key, "eYVNBjBDi33aa9GkA3w")
sorcery_controller_external_property_set(:slack, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
sorcery_controller_external_property_set(:slack, :callback_url, "http://blabla.com")
end



after(:all) do
if SORCERY_ORM == :active_record
ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/activation")
Expand Down Expand Up @@ -287,7 +292,7 @@
end
end

%w(facebook github google liveid vk salesforce).each do |provider|
%w(facebook github google liveid vk salesforce slack).each do |provider|
context "when #{provider}" do
before(:each) do
sorcery_controller_property_set(:register_login_time, true)
Expand Down Expand Up @@ -327,7 +332,7 @@

let(:user) { double('user', id: 42) }

%w(facebook github google liveid vk salesforce).each do |provider|
%w(facebook github google liveid vk salesforce slack).each do |provider|
context "when #{provider}" do
before(:each) do
sorcery_model_property_set(:authentications_class, Authentication)
Expand Down Expand Up @@ -389,7 +394,13 @@ def stub_all_oauth2_requests!
"first_name"=>"Noam",
"last_name"=>"Ben Ari"
}
]}.to_json }
],
"user"=> {
"name"=>"Sonny Whether",
"id"=>"123",
"email"=>"[email protected]"
}
}.to_json }
allow(access_token).to receive(:get) { response }
allow(access_token).to receive(:token) { "187041a618229fdaf16613e96e1caabc1e86e46bbfad228de41520e63fe45873684c365a14417289599f3" }
# access_token params for VK auth
Expand All @@ -398,7 +409,7 @@ def stub_all_oauth2_requests!
end

def set_external_property
sorcery_controller_property_set(:external_providers, [:facebook, :github, :google, :liveid, :vk, :salesforce, :paypal])
sorcery_controller_property_set(:external_providers, [:facebook, :github, :google, :liveid, :vk, :salesforce, :paypal, :slack])
sorcery_controller_external_property_set(:facebook, :key, "eYVNBjBDi33aa9GkA3w")
sorcery_controller_external_property_set(:facebook, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
sorcery_controller_external_property_set(:facebook, :callback_url, "http://blabla.com")
Expand All @@ -420,6 +431,9 @@ def set_external_property
sorcery_controller_external_property_set(:paypal, :key, "eYVNBjBDi33aa9GkA3w")
sorcery_controller_external_property_set(:paypal, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
sorcery_controller_external_property_set(:paypal, :callback_url, "http://blabla.com")
sorcery_controller_external_property_set(:slack, :key, "eYVNBjBDi33aa9GkA3w")
sorcery_controller_external_property_set(:slack, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
sorcery_controller_external_property_set(:slack, :callback_url, "http://blabla.com")
end

def provider_url(provider)
Expand All @@ -429,7 +443,8 @@ def provider_url(provider)
google: "https://accounts.google.com/o/oauth2/auth?client_id=#{::Sorcery::Controller::Config.google.key}&display&redirect_uri=http%3A%2F%2Fblabla.com&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&state",
liveid: "https://oauth.live.com/authorize?client_id=#{::Sorcery::Controller::Config.liveid.key}&display&redirect_uri=http%3A%2F%2Fblabla.com&response_type=code&scope=wl.basic+wl.emails+wl.offline_access&state",
vk: "https://oauth.vk.com/authorize?client_id=#{::Sorcery::Controller::Config.vk.key}&display&redirect_uri=http%3A%2F%2Fblabla.com&response_type=code&scope=#{::Sorcery::Controller::Config.vk.scope}&state",
salesforce: "https://login.salesforce.com/services/oauth2/authorize?client_id=#{::Sorcery::Controller::Config.salesforce.key}&display&redirect_uri=http%3A%2F%2Fblabla.com&response_type=code&scope#{'=' + ::Sorcery::Controller::Config.salesforce.scope unless ::Sorcery::Controller::Config.salesforce.scope.nil?}&state"
salesforce: "https://login.salesforce.com/services/oauth2/authorize?client_id=#{::Sorcery::Controller::Config.salesforce.key}&display&redirect_uri=http%3A%2F%2Fblabla.com&response_type=code&scope#{'=' + ::Sorcery::Controller::Config.salesforce.scope unless ::Sorcery::Controller::Config.salesforce.scope.nil?}&state",
slack: "https://slack.com/oauth/authorize?client_id=#{::Sorcery::Controller::Config.slack.key}&display&redirect_uri=http%3A%2F%2Fblabla.com&response_type=code&scope=identity.basic%2C+identity.email&state"
}[provider]
end
end
20 changes: 20 additions & 0 deletions spec/rails_app/app/controllers/sorcery_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ def login_at_test_salesforce
login_at(:salesforce)
end

def login_at_test_slack
login_at(:slack)
end

def login_at_test_with_state
login_at(:facebook, {state: 'bla'})
end
Expand Down Expand Up @@ -199,6 +203,14 @@ def test_login_from_salesforce
end
end

def test_login_from_slack
if @user = login_from(:slack)
redirect_to 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
end

def test_return_to_with_external_twitter
if @user = login_from(:twitter)
redirect_back_or_to 'bla', notice: 'Success!'
Expand Down Expand Up @@ -273,6 +285,14 @@ def test_return_to_with_external_salesforce
end
end

def test_return_to_with_external_slack
if @user = login_from(:slack)
redirect_back_or_to 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
end

def test_create_from_provider
provider = params[:provider]
login_from(provider)
Expand Down
3 changes: 3 additions & 0 deletions spec/rails_app/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
get :test_login_from_vk
get :test_login_from_jira
get :test_login_from_salesforce
get :test_login_from_slack
get :login_at_test
get :login_at_test_twitter
get :login_at_test_facebook
Expand All @@ -36,6 +37,7 @@
get :login_at_test_vk
get :login_at_test_jira
get :login_at_test_salesforce
get :login_at_test_slack
get :test_return_to_with_external
get :test_return_to_with_external_twitter
get :test_return_to_with_external_facebook
Expand All @@ -46,6 +48,7 @@
get :test_return_to_with_external_vk
get :test_return_to_with_external_jira
get :test_return_to_with_external_salesforce
get :test_return_to_with_external_slack
get :test_http_basic_auth
get :some_action_making_a_non_persisted_change_to_the_user
post :test_login_with_remember
Expand Down
6 changes: 3 additions & 3 deletions spec/shared_examples/user_shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,11 @@ class Admin2 < User; end
let(:user_with_pass) { create_new_user({:username => 'foo_bar', :email => "[email protected]", :password => 'foobar'})}

specify { expect(user_with_pass).to respond_to :valid_password? }

it "returns true if password is correct" do
expect(user_with_pass.valid_password?("foobar")).to be true
end

it "returns false if password is incorrect" do
expect(user_with_pass.valid_password?("foobug")).to be false
end
Expand Down Expand Up @@ -541,7 +541,7 @@ def self.matches?(crypted,*tokens)
User.sorcery_adapter.delete_all
end

[:facebook, :github, :google, :liveid].each do |provider|
[:facebook, :github, :google, :liveid, :slack].each do |provider|

it "does not send activation email to external users" do
old_size = ActionMailer::Base.deliveries.size
Expand Down

0 comments on commit 0cda67d

Please sign in to comment.