omniauth-samaritan 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 39ec2609e204a96eecec459fb575fa152b8e5484
4
+ data.tar.gz: 5e87c91a899a30a09cae65920e32817009ddfd7e
5
+ SHA512:
6
+ metadata.gz: 39ec5703b47370b3bee69db37d67761fe711cfbca001bd03ec36bfbfa8b2469e6ab389ceeee852f82e9c016557854253520f46ca6a57236caf6a79d8e250db67
7
+ data.tar.gz: 21e6ab94e157144082d22a1029f879a4ec5c78cc23b7d6bc1d9bb50c96a21f0bdd53f17b99e874ae4a214e8212a2f100eb939131e3928c004bdcfd7abf0e884a
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .ruby-gemset
7
+ .ruby-version
8
+ .rvmrc
9
+ Gemfile.lock
10
+ InstalledFiles
11
+ _yardoc
12
+ coverage
13
+ doc/
14
+ lib/bundler/man
15
+ pkg
16
+ rdoc
17
+ spec/reports
18
+ test/tmp
19
+ test/version_tmp
20
+ tmp
21
+ .powenv
22
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :example do
6
+ gem 'sinatra'
7
+ end
data/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # OmniAuth Samaritan OAuth2 Strategy
2
+
3
+ Strategy to authenticate with Samaritan via OAuth2 in OmniAuth.
4
+
5
+ #For more information on usage and configuration, visit our [documentation site](http://docs.samaritanministries.org/ruby-oauth/).
6
+
7
+ ## License
8
+
9
+ Copyright (c) 2014 by Samaritan Ministries International
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require File.join('bundler', 'gem_tasks')
3
+ require File.join('rspec', 'core', 'rake_task')
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module Samaritan
3
+ VERSION = "0.0.5"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require File.join('omniauth', 'strategies', 'samaritan')
@@ -0,0 +1,93 @@
1
+ require 'omniauth/strategies/oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Samaritan < OmniAuth::Strategies::OAuth2
6
+
7
+ option :name, 'samaritan'
8
+ option :environment
9
+
10
+ option :client_options, {}
11
+
12
+ option :sandbox_client_options, {
13
+ :site => 'https://sandbox.smchcn.net/',
14
+ :authorize_url => '/asrv/smi/oauth/authorize',
15
+ :token_url => '/asrv/smi/oauth/token',
16
+ :identity_url => '/SmiIdentity/api/identity/mine'}
17
+
18
+ option :production_client_options, {
19
+ :site => 'https://api.smchcn.net/',
20
+ :authorize_url => '/asrv/smi/oauth/authorize',
21
+ :token_url => '/asrv/smi/oauth/token',
22
+ :identity_url => '/SmiIdentity/api/identity/mine'}
23
+
24
+ def client_options
25
+ client_options = options.client_options
26
+ client_options = options.sandbox_client_options if options.environment == :sandbox
27
+ client_options = options.production_client_options if options.environment == :production
28
+ client_options
29
+ end
30
+
31
+ def request_phase
32
+ if request.params['access_token']
33
+ self.access_token = build_access_token_from_params(request.params)
34
+ env['omniauth.auth'] = auth_hash
35
+ call_app!
36
+ else
37
+ super
38
+ end
39
+ end
40
+
41
+ def client
42
+ ::OAuth2::Client.new(options.client_id, options.client_secret, deep_symbolize(client_options))
43
+ end
44
+
45
+ def token_params
46
+ super.merge({:headers => {'Authorization' => authorization(options.client_id, options.client_secret)}})
47
+ end
48
+
49
+ def authorization(client_id, client_secret)
50
+ 'Basic ' + Base64.encode64(client_id + ':' + client_secret).gsub("\n", '')
51
+ end
52
+
53
+ uid { raw_info['id'] }
54
+
55
+ info do
56
+ prune!({
57
+ :name => raw_info['nickname'],
58
+ :email => raw_info['email_address'],
59
+ :member_id => raw_info['member_id'],
60
+ :membership_id => raw_info['context'],
61
+ :is_approved => raw_info['is_approved'],
62
+ :has_claimed_membership => raw_info['has_claimed_membership'],
63
+ :is_locked_out => raw_info['is_locked_out']
64
+ })
65
+ end
66
+
67
+ extra do
68
+ hash = {}
69
+ hash[:raw_info] = raw_info unless skip_info?
70
+ prune! hash
71
+ end
72
+
73
+ def raw_info
74
+ identity_endpoint = client_options[:site].to_s.gsub(/\/\z/, '') + client_options[:identity_url].to_s
75
+ @raw_info ||= access_token.get(identity_endpoint).parsed
76
+ end
77
+
78
+ private
79
+
80
+ def build_access_token_from_params(params)
81
+ ::OAuth2::AccessToken.new(client, params['access_token'])
82
+ end
83
+
84
+ def prune!(hash)
85
+ hash.delete_if do |_, v|
86
+ prune!(v) if v.is_a?(Hash)
87
+ v.nil? || (v.respond_to?(:empty?) && v.empty?)
88
+ end
89
+ end
90
+
91
+ end
92
+ end
93
+ end
@@ -0,0 +1 @@
1
+ require File.join('omniauth', 'samaritan')
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path(File.join('..', 'lib', 'omniauth', 'samaritan', 'version'), __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.add_dependency 'omniauth', '~> 1.0'
6
+
7
+ gem.authors = ["Doug Bradbury", "Ben Voss"]
8
+ gem.email = ["smi@8thlight.com"]
9
+ gem.description = %q{A Samaritan OAuth2 strategy for OmniAuth 1.x.}
10
+ gem.summary = %q{A Samaritan OAuth2 strategy for OmniAuth 1.x}
11
+ gem.homepage = "http://docs.samaritanministries.org/ruby-oauth/"
12
+ gem.licenses = ['MIT']
13
+
14
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
15
+ gem.files = `git ls-files`.split("\n")
16
+ gem.test_files = `git ls-files -- {spec}/*`.split("\n")
17
+ gem.name = "omniauth-samaritan"
18
+ gem.require_paths = ["lib"]
19
+ gem.version = OmniAuth::Samaritan::VERSION
20
+
21
+ gem.add_runtime_dependency 'omniauth-oauth2', '~> 1.1', '>= 1.1.2'
22
+
23
+ gem.add_development_dependency 'rspec', '~> 2.6.0', '>= 2.6.0'
24
+ gem.add_development_dependency 'rake', '~> 0'
25
+ end
@@ -0,0 +1,145 @@
1
+ require 'spec_helper'
2
+ require 'omniauth-samaritan'
3
+
4
+ describe OmniAuth::Strategies::Samaritan do
5
+ let(:request) { double('Request', :params => {}, :cookies => {}, :env => {}) }
6
+ let(:app) {
7
+ lambda do
8
+ [200, {}, ["Hello."]]
9
+ end
10
+ }
11
+
12
+ subject do
13
+ OmniAuth::Strategies::Samaritan.new(app, 'appid', 'secret', @options || {}).tap do |strategy|
14
+ strategy.stub(:request) {
15
+ request
16
+ }
17
+ end
18
+ end
19
+
20
+ before do
21
+ OmniAuth.config.test_mode = true
22
+ end
23
+
24
+ after do
25
+ OmniAuth.config.test_mode = false
26
+ end
27
+
28
+ describe '#client_options' do
29
+ it 'has correct site for sandbox' do
30
+ @options = {:environment => :sandbox}
31
+ subject.client.site.should eq('https://sandbox.smchcn.net/')
32
+ end
33
+
34
+ it 'has correct site for production' do
35
+ @options = {:environment => :production}
36
+ subject.client.site.should eq('https://api.smchcn.net/')
37
+ end
38
+
39
+ it 'has correct authorize_url' do
40
+ @options = {:environment => :production}
41
+ subject.client.options[:authorize_url].should eq('/asrv/smi/oauth/authorize')
42
+ end
43
+
44
+ it 'has correct token_url' do
45
+ @options = {:environment => :production}
46
+ subject.client.options[:token_url].should eq('/asrv/smi/oauth/token')
47
+ end
48
+
49
+ describe "overrides" do
50
+ it 'should allow overriding the site' do
51
+ @options = {:client_options => {'site' => 'https://example.com'}}
52
+ subject.client.site.should == 'https://example.com'
53
+ end
54
+
55
+ it 'should allow overriding the authorize_url' do
56
+ @options = {:client_options => {'authorize_url' => 'https://example.com'}}
57
+ subject.client.options[:authorize_url].should == 'https://example.com'
58
+ end
59
+
60
+ it 'should allow overriding the token_url' do
61
+ @options = {:client_options => {'token_url' => 'https://example.com'}}
62
+ subject.client.options[:token_url].should == 'https://example.com'
63
+ end
64
+ end
65
+ end
66
+
67
+ describe '#token_params' do
68
+ it 'should include any token params passed in the :token_params option' do
69
+ @options = {:token_params => {:foo => 'bar', :baz => 'zip'}}
70
+ subject.token_params['foo'].should eq('bar')
71
+ subject.token_params['baz'].should eq('zip')
72
+ end
73
+ end
74
+
75
+ describe "#token_options" do
76
+ it 'should include top-level options that are marked as :token_options' do
77
+ @options = {:token_options => [:scope, :foo], :scope => 'bar', :foo => 'baz', :bad => 'not_included'}
78
+ subject.token_params['scope'].should eq('bar')
79
+ subject.token_params['foo'].should eq('baz')
80
+ subject.token_params['bad'].should eq(nil)
81
+ end
82
+ end
83
+
84
+ describe '#callback_path' do
85
+ it 'has the correct callback path' do
86
+ subject.callback_path.should eq('/auth/samaritan/callback')
87
+ end
88
+ end
89
+
90
+ describe '#extra' do
91
+ let(:client) do
92
+ OAuth2::Client.new('abc', 'def') do |builder|
93
+ builder.request :url_encoded
94
+ builder.adapter :test do |stub|
95
+ stub.get('/SmiIdentity/api/identity/mine') {|env| [200, {'content-type' => 'application/json'}, '{"id": "12345"}']}
96
+ end
97
+ end
98
+ end
99
+ let(:access_token) { OAuth2::AccessToken.from_hash(client, {}) }
100
+
101
+ before do
102
+ @options = { :environment => :sandbox }
103
+ subject.stub(:access_token => access_token)
104
+ end
105
+
106
+
107
+ describe 'raw_info' do
108
+ context 'when skip_info is false' do
109
+
110
+ it 'should include raw_info' do
111
+ subject.extra[:raw_info].should eq('id' => '12345')
112
+ end
113
+ end
114
+ end
115
+
116
+ end
117
+
118
+ describe 'populate auth hash urls' do
119
+ it 'should populate url map in auth hash if link present in raw_info' do
120
+ subject.stub(:raw_info){{"id" => "765b1357-8cb5-4b3e-a4bb-239e3af38399","email_address"=>"gotteo@gmail.com","is_approved"=>true,"is_locked_out"=>false,"sub"=>"765b1357-8cb5-4b3e-a4bb-239e3af38399","member_id"=>"44561","context"=>"14470","has_claimed_membership"=>true,"nickname"=>"Greg Otte"}}
121
+ subject.info.should_not have_key(:urls)
122
+ subject.info[:name].should == "Greg Otte"
123
+ subject.info[:email].should == "gotteo@gmail.com"
124
+ subject.info[:is_approved].should == true
125
+ subject.info[:has_claimed_membership].should == true
126
+ subject.info[:is_locked_out].should == false
127
+ subject.info[:member_id].should == "44561"
128
+ subject.info[:membership_id].should == "14470"
129
+ end
130
+
131
+ end
132
+
133
+ describe "pre-authorized" do
134
+ it "should create an access token" do
135
+ subject.stub(:env){ {}}
136
+ subject.should_receive(:call_app!)
137
+ subject.stub(:raw_info){{"id" => "765b1357-8cb5-4b3e-a4bb-239e3af38399","email_address"=>"gotteo@gmail.com","is_approved"=>true,"is_locked_out"=>false,"sub"=>"765b1357-8cb5-4b3e-a4bb-239e3af38399","member_id"=>"44561","context"=>"14470","has_claimed_membership"=>true,"nickname"=>"Greg Otte"}}
138
+ request.params["access_token"] = "1234567890"
139
+ subject.request_phase
140
+ subject.access_token.should_not be_nil
141
+ end
142
+
143
+ end
144
+
145
+ end
@@ -0,0 +1,2 @@
1
+ require File.join('bundler', 'setup')
2
+ require 'rspec'
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-samaritan
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Doug Bradbury
8
+ - Ben Voss
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: omniauth
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: '1.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: '1.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: omniauth-oauth2
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: '1.1'
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.1.2
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: '1.1'
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.1.2
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 2.6.0
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: 2.6.0
58
+ type: :development
59
+ prerelease: false
60
+ version_requirements: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ~>
63
+ - !ruby/object:Gem::Version
64
+ version: 2.6.0
65
+ - - '>='
66
+ - !ruby/object:Gem::Version
67
+ version: 2.6.0
68
+ - !ruby/object:Gem::Dependency
69
+ name: rake
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ~>
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ description: A Samaritan OAuth2 strategy for OmniAuth 1.x.
83
+ email:
84
+ - smi@8thlight.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - .gitignore
90
+ - Gemfile
91
+ - README.md
92
+ - Rakefile
93
+ - lib/omniauth-samaritan.rb
94
+ - lib/omniauth/samaritan.rb
95
+ - lib/omniauth/samaritan/version.rb
96
+ - lib/omniauth/strategies/samaritan.rb
97
+ - omniauth-samaritan.gemspec
98
+ - spec/omniauth/strategies/samaritan_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: http://docs.samaritanministries.org/ruby-oauth/
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.1.11
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: A Samaritan OAuth2 strategy for OmniAuth 1.x
124
+ test_files: []