-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from youzik/slack_auth
Slack auth
- Loading branch information
Showing
8 changed files
with
101 additions
and
12 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
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,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 |
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 |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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") | ||
|
@@ -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") | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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 | ||
|
@@ -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") | ||
|
@@ -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) | ||
|
@@ -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 |
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 |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|