omniauth-force 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +5 -0
- data/Gemfile +8 -0
- data/Rakefile +10 -0
- data/lib/ext/oauth2/client.rb +16 -0
- data/lib/omniauth/strategies/force.rb +85 -0
- data/lib/omniauth-force.rb +1 -0
- data/lib/omniauth_force/version.rb +3 -0
- data/lib/omniauth_force.rb +7 -0
- data/omniauth-force.gemspec +23 -0
- data/test/force_test.rb +82 -0
- data/test/test_helper.rb +12 -0
- metadata +100 -0
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'oauth2/client'
|
2
|
+
|
3
|
+
# Monkey patch to correct status handling
|
4
|
+
class OAuth2::Client
|
5
|
+
alias :old_request :request
|
6
|
+
def request(verb, url, params = {}, headers = {})
|
7
|
+
old_request(verb, url, params, headers)
|
8
|
+
rescue OAuth2::HTTPError => e
|
9
|
+
if e.response.status == 302
|
10
|
+
url = e.response.headers['location']
|
11
|
+
retry
|
12
|
+
else
|
13
|
+
raise e
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'omniauth/oauth'
|
2
|
+
require 'multi_json'
|
3
|
+
|
4
|
+
module OmniAuth
|
5
|
+
module Strategies
|
6
|
+
# Authenticate to force.com utilizing OAuth 2.0 and retrieve
|
7
|
+
# basic user information.
|
8
|
+
#
|
9
|
+
# @example Basic Usage
|
10
|
+
# use OmniAuth::Strategies::Force, 'consumer_key', 'consumer_secret'
|
11
|
+
class Force < OAuth2
|
12
|
+
# @param [Rack Application] app standard middleware application parameter
|
13
|
+
# @param [String] app_id the application id
|
14
|
+
# @param [String] app_secret the application secret
|
15
|
+
def initialize(app, consumer_key, consumer_secret, options = {})
|
16
|
+
options[:site] = 'https://login.salesforce.com/'
|
17
|
+
options[:authorize_path] = '/services/oauth2/authorize'
|
18
|
+
options[:access_token_path] = '/services/oauth2/token'
|
19
|
+
|
20
|
+
super(app, :force, consumer_key, consumer_secret, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
def user_data
|
24
|
+
@data ||= MultiJson.decode(@access_token.get(@access_token['id']))
|
25
|
+
end
|
26
|
+
|
27
|
+
# Monkey patch scheme for callback url
|
28
|
+
def full_host
|
29
|
+
uri = URI.parse(request.url)
|
30
|
+
uri.path = ''
|
31
|
+
uri.query = nil
|
32
|
+
uri.scheme = request.env['HTTP_X_FORWARDED_PROTO'] || request.scheme
|
33
|
+
uri.to_s
|
34
|
+
end
|
35
|
+
|
36
|
+
def request_phase
|
37
|
+
options[:response_type] ||= "code"
|
38
|
+
super
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_access_token(verifier)
|
42
|
+
access_token = client.web_server.get_access_token(
|
43
|
+
verifier,
|
44
|
+
:redirect_uri => callback_url,
|
45
|
+
:grant_type => 'authorization_code').tap do |token|
|
46
|
+
token.token_param = 'oauth_token'
|
47
|
+
end
|
48
|
+
|
49
|
+
access_token
|
50
|
+
end
|
51
|
+
|
52
|
+
def callback_phase
|
53
|
+
if request.params['error'] || request.params['error_reason']
|
54
|
+
raise CallbackError.new(request.params['error'], request.params['error_description'] || request.params['error_reason'], request.params['error_uri'])
|
55
|
+
end
|
56
|
+
|
57
|
+
@access_token = get_access_token(request.params['code'])
|
58
|
+
@env['omniauth.auth'] = auth_hash
|
59
|
+
call_app!
|
60
|
+
rescue ::OAuth2::HTTPError, ::OAuth2::AccessDenied, CallbackError => e
|
61
|
+
fail!(:invalid_credentials, e)
|
62
|
+
end
|
63
|
+
|
64
|
+
def user_info
|
65
|
+
{
|
66
|
+
'nickname' => user_data['nick_name'],
|
67
|
+
'email' => user_data['email'],
|
68
|
+
'name' => user_data['display_name'],
|
69
|
+
'urls' => {
|
70
|
+
'Force' => user_data['urls']['profile'],
|
71
|
+
}
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
def auth_hash
|
76
|
+
OmniAuth::Utils.deep_merge(super, {
|
77
|
+
'uid' => user_data['user_id'],
|
78
|
+
'user_info' => user_info,
|
79
|
+
'extra' => {'user_hash' => user_data},
|
80
|
+
'credentials' => { 'instance_url' => @access_token['instance_url'] }
|
81
|
+
})
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'omniauth_force'
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "omniauth_force/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "omniauth-force"
|
7
|
+
s.version = OmniauthForce::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Jacob Dam"]
|
10
|
+
s.email = ["ngocphuc@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Force.com strategies for OmniAuth.}
|
13
|
+
s.description = %q{Force.com strategies for OmniAuth.}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency 'oa-oauth', '~> 0.1.6'
|
21
|
+
s.add_dependency 'multi_json', '~> 0.0.2'
|
22
|
+
s.add_dependency 'oauth2', '~> 0.1.1'
|
23
|
+
end
|
data/test/force_test.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ForceTest < ActiveSupport::TestCase
|
4
|
+
include Rack::Test::Methods
|
5
|
+
|
6
|
+
def dummy_app
|
7
|
+
@dummy_app ||= lambda { |env| [200, {}, ["Dummy app"]] }
|
8
|
+
end
|
9
|
+
|
10
|
+
def app
|
11
|
+
@app ||= OmniAuth::Strategies::Force.new(dummy_app, 'force-api-key', 'force-api-secret')
|
12
|
+
end
|
13
|
+
|
14
|
+
test "request phase should redirect to force login page with correct params" do
|
15
|
+
get '/auth/force'
|
16
|
+
assert last_response.redirect?
|
17
|
+
|
18
|
+
uri = URI.parse(last_response.location)
|
19
|
+
assert_equal uri.scheme, 'https'
|
20
|
+
assert_equal uri.host, 'login.salesforce.com'
|
21
|
+
assert_equal uri.path, '/services/oauth2/authorize'
|
22
|
+
|
23
|
+
params = CGI::parse(uri.query)
|
24
|
+
assert_equal params['client_id'], ['force-api-key']
|
25
|
+
assert_equal params['redirect_uri'], ['http://example.org/auth/force/callback']
|
26
|
+
assert_equal params['response_type'], ['code']
|
27
|
+
assert_equal params['type'], ['web_server']
|
28
|
+
end
|
29
|
+
|
30
|
+
test "callback phase should " do
|
31
|
+
sf_token = {
|
32
|
+
'instance_url' => 'http://instance',
|
33
|
+
'id' => 'http://instance/id/123',
|
34
|
+
'access_token' => 'token123',
|
35
|
+
}
|
36
|
+
sf_user_data = {
|
37
|
+
'user_id' => 123,
|
38
|
+
'nick_name' => 123,
|
39
|
+
'email' => 123,
|
40
|
+
'display_name' => 123,
|
41
|
+
'urls' => {
|
42
|
+
'profile' => 'http://instance/profile/123'
|
43
|
+
},
|
44
|
+
}
|
45
|
+
auth_hash = nil
|
46
|
+
@dummy_app = lambda do |env|
|
47
|
+
auth_hash = env['omniauth.auth']
|
48
|
+
[200, {}, ["Dummy app"]]
|
49
|
+
end
|
50
|
+
|
51
|
+
connection = Faraday.new do |builder|
|
52
|
+
builder.adapter :test do |stub|
|
53
|
+
stub.post('/services/oauth2/token') {[ 200, {}, MultiJson.encode(sf_token) ]}
|
54
|
+
stub.get('/id/123?oauth_token=token123') {[ 200, {}, MultiJson.encode(sf_user_data) ]}
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
app.client.connection = connection
|
60
|
+
get '/auth/force/callback?code=code123'
|
61
|
+
|
62
|
+
assert_equal({
|
63
|
+
"provider" => "force",
|
64
|
+
"uid" => 123,
|
65
|
+
"credentials" => {
|
66
|
+
"token" => "token123",
|
67
|
+
"instance_url" => "http://instance"
|
68
|
+
},
|
69
|
+
"user_info" => {
|
70
|
+
"nickname"=>123,
|
71
|
+
"email"=>123,
|
72
|
+
"name"=>123,
|
73
|
+
"urls"=>{
|
74
|
+
"Force"=>"http://instance/profile/123"
|
75
|
+
}
|
76
|
+
},
|
77
|
+
"extra"=>{
|
78
|
+
"user_hash"=>sf_user_data
|
79
|
+
}
|
80
|
+
}, auth_hash)
|
81
|
+
end
|
82
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: omniauth-force
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jacob Dam
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-03-15 00:00:00 +07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: oa-oauth
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ~>
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.1.6
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: multi_json
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ~>
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 0.0.2
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: oauth2
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 0.1.1
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id003
|
49
|
+
description: Force.com strategies for OmniAuth.
|
50
|
+
email:
|
51
|
+
- ngocphuc@gmail.com
|
52
|
+
executables: []
|
53
|
+
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files: []
|
57
|
+
|
58
|
+
files:
|
59
|
+
- .gitignore
|
60
|
+
- Gemfile
|
61
|
+
- Rakefile
|
62
|
+
- lib/ext/oauth2/client.rb
|
63
|
+
- lib/omniauth-force.rb
|
64
|
+
- lib/omniauth/strategies/force.rb
|
65
|
+
- lib/omniauth_force.rb
|
66
|
+
- lib/omniauth_force/version.rb
|
67
|
+
- omniauth-force.gemspec
|
68
|
+
- test/force_test.rb
|
69
|
+
- test/test_helper.rb
|
70
|
+
has_rdoc: true
|
71
|
+
homepage: ""
|
72
|
+
licenses: []
|
73
|
+
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: "0"
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: "0"
|
91
|
+
requirements: []
|
92
|
+
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 1.5.2
|
95
|
+
signing_key:
|
96
|
+
specification_version: 3
|
97
|
+
summary: Force.com strategies for OmniAuth.
|
98
|
+
test_files:
|
99
|
+
- test/force_test.rb
|
100
|
+
- test/test_helper.rb
|