omniauth-zendesk 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ coverage
6
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in omniauth-desk.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'rake'
8
+ end
data/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2013 Thomas Stachl <tom@desk.com>, Desk.com - A Salesforce.com Company
2
+
3
+ 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:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ 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/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # OmniAuth Zendesk
2
+
3
+ This gem contains the Zendesk strategy for OmniAuth.
4
+
5
+ Zendesk uses the HTTPBasic Authentication flow, you can read about it here: [Zendesk API - Security and Authentication](http://developer.zendesk.com/documentation/rest_api/introduction.html#security-and-authentication)
6
+
7
+ ## How To Use It
8
+
9
+ Add the strategy to your `Gemfile`:
10
+
11
+ gem 'omniauth-zendesk'
12
+
13
+ Or you can pull it directly from github eg:
14
+
15
+ gem 'omniauth-zendesk', :git => 'https://github.com/tstachl/omniauth-zendesk.git'
16
+
17
+ For a Rails application you'd now create an initializer `config/initializers/omniauth.rb`:
18
+
19
+ Rails.application.config.middleware.use OmniAuth::Builder do
20
+ provider :zendesk, :site => 'https://yoursite.zendesk.com'
21
+ end
22
+
23
+ For Sinatra you'd add this 3 lines:
24
+
25
+ use OmniAuth::Builder do
26
+ provider :zendesk, :site => 'https://yoursite.zendesk.com'
27
+ end
28
+
29
+ ## License
30
+
31
+ Copyright (c) 2013 Thomas Stachl <tom@desk.com>, Desk.com - A Salesforce.com Company
32
+
33
+ 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:
34
+
35
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
36
+
37
+ 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,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc "Run specs"
5
+ RSpec::Core::RakeTask.new
6
+
7
+ desc 'Default: run specs.'
8
+ task :default => :spec
@@ -0,0 +1,83 @@
1
+ require 'zendesk_api'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Zendesk
6
+ include OmniAuth::Strategy
7
+
8
+ option :site, nil
9
+ option :params, { site: 'site', username: 'email', password: 'password' }
10
+ option :on_failed_registration, nil
11
+
12
+ uid { identity.url }
13
+ info {
14
+ {
15
+ name: identity.name,
16
+ email: identity.email,
17
+ role: identity.role,
18
+ time_zone: identity.time_zone,
19
+ description: identity.notes,
20
+ image: identity.photo.respond_to?(:thumbnails) ? identity.photo.thumbnails.first.content_url : nil,
21
+ phone: identity.phone,
22
+ site: site
23
+ }
24
+ }
25
+ credentials { { token: username, secret: password } }
26
+ extra { { raw_info: identity } }
27
+
28
+ def request_phase
29
+ OmniAuth::Form.build(
30
+ title: (options[:title] || "Zendesk Authentication"),
31
+ url: callback_path
32
+ ) do |f|
33
+ f.text_field 'Username', options.params.username
34
+ f.password_field 'Password', options.params.password
35
+ f.text_field 'Zendesk Site', options.params.site
36
+ end.to_response
37
+ end
38
+
39
+ def callback_phase
40
+ return fail!(:invalid_credentials) if not identity or identity.email == 'invalid@example.com'
41
+ super
42
+ end
43
+
44
+ def identity
45
+ return unless site && username && password
46
+ client = ZendeskAPI::Client.new do |c|
47
+ c.url = "#{site}/api/v2"
48
+ c.username = username
49
+ c.password = password
50
+ end
51
+ @identity ||= client.current_user
52
+ end
53
+
54
+ protected
55
+ def site
56
+ options.site || validate_site(request.params[options.params.site])
57
+ end
58
+
59
+ def username
60
+ request.params[options.params.username]
61
+ end
62
+
63
+ def password
64
+ request.params[options.params.password]
65
+ end
66
+
67
+ def uri?(uri)
68
+ uri = URI.parse(uri)
69
+ uri.scheme == 'https'
70
+ rescue URI::BadURIError
71
+ false
72
+ rescue URI::InvalidURIError
73
+ false
74
+ end
75
+
76
+ def validate_site(str)
77
+ if str and str != ''
78
+ uri?(str) ? str : "https://#{str}.zendesk.com"
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module Zendesk
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require 'omniauth-zendesk/version'
2
+ require 'omniauth/strategies/zendesk'
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "omniauth-zendesk/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "omniauth-zendesk"
7
+ s.version = OmniAuth::Zendesk::VERSION
8
+ s.authors = ["Thomas Stachl"]
9
+ s.email = ["tom@desk.com"]
10
+ s.homepage = "https://github.com/tstachl/omniauth-zendesk"
11
+ s.summary = %q{OmniAuth strategy for Zendesk}
12
+ s.description = %q{OmniAuth strategy for Zendesk}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_runtime_dependency 'omniauth', '~> 1.0'
20
+ s.add_runtime_dependency 'zendesk_api', '~> 0.2.2'
21
+
22
+ s.add_development_dependency 'rspec', '~> 2.7'
23
+ s.add_development_dependency 'rack-test'
24
+ s.add_development_dependency 'simplecov'
25
+ s.add_development_dependency 'webmock'
26
+ end
@@ -0,0 +1,33 @@
1
+ {
2
+ "user": {
3
+ "url": null,
4
+ "id": null,
5
+ "external_id": null,
6
+ "name": "Anonymous user",
7
+ "alias": null,
8
+ "created_at": null,
9
+ "updated_at": null,
10
+ "active": true,
11
+ "verified": false,
12
+ "shared": false,
13
+ "locale_id": null,
14
+ "locale": "en-US",
15
+ "time_zone": "Pacific Time (US & Canada)",
16
+ "last_login_at": null,
17
+ "email": "invalid@example.com",
18
+ "phone": null,
19
+ "signature": null,
20
+ "details": null,
21
+ "notes": null,
22
+ "organization_id": null,
23
+ "role": "end-user",
24
+ "custom_role_id": null,
25
+ "moderator": false,
26
+ "ticket_restriction": null,
27
+ "only_private_comments": false,
28
+ "tags": [],
29
+ "suspended": false,
30
+ "photo": null,
31
+ "authenticity_token": "OoiEE0BhDanZaH6LsWCd9xWBTwfVg090ipqKLeCacKg="
32
+ }
33
+ }
@@ -0,0 +1,46 @@
1
+ {
2
+ "user": {
3
+ "url": "https://my.zendesk.com/api/v2/users/35436.json",
4
+ "id": 35436,
5
+ "external_id": "sai989sur98w9",
6
+ "name": "Johnny Agent",
7
+ "alias": "Mr. Johnny",
8
+ "created_at": "2009-07-20T22:55:29Z",
9
+ "updated_at": "2011-05-05T10:38:52Z",
10
+ "active": true,
11
+ "verified": true,
12
+ "shared": false,
13
+ "locale_id": 1,
14
+ "locale": "en-US",
15
+ "time_zone": "Pacific Time (US & Canada)",
16
+ "last_login_at": "2011-05-05T10:38:52Z",
17
+ "email": "john@example.com",
18
+ "phone": "555-123-4567",
19
+ "signature": "Have a nice day, Johnny",
20
+ "details": "",
21
+ "notes": "Johnny is a nice guy!",
22
+ "organization_id": 57542,
23
+ "role": "agent",
24
+ "custom_role_id": 9373643,
25
+ "moderator": true,
26
+ "ticket_restriction": "assigned",
27
+ "only_private_comments": false,
28
+ "tags": ["enterprise", "other_tag"],
29
+ "suspended": false,
30
+ "photo": {
31
+ "id": 928374,
32
+ "name": "my_funny_profile_pic.png",
33
+ "content_url": "https://my.zendesk.com/photos/my_funny_profile_pic.png",
34
+ "content_type": "image/png",
35
+ "size": 166144,
36
+ "thumbnails": [{
37
+ "id": 928375,
38
+ "name": "my_funny_profile_pic_thumb.png",
39
+ "content_url": "https://my.zendesk.com/photos/my_funny_profile_pic_thumb.png",
40
+ "content_type": "image/png",
41
+ "size": 58298
42
+ }]
43
+ },
44
+ "authenticity_token": "YPVLYV0ebkk33hYOzylaVXyQZZv1M5+kC4CN17sti4c="
45
+ }
46
+ }
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe OmniAuth::Strategies::Zendesk do
4
+ attr_accessor :app
5
+
6
+ let(:auth_hash){ last_response.headers['env']['omniauth.auth'] }
7
+
8
+ # customize rack app for testing, if block is given, reverts to default
9
+ # rack app after testing is done
10
+ def set_app!(options = {})
11
+ old_app = self.app
12
+ self.app = Rack::Builder.app do
13
+ use Rack::Session::Cookie, secret: 'ultra_strong_secret'
14
+ use OmniAuth::Strategies::Zendesk, options
15
+ run lambda{|env| [404, {'env' => env}, ["HELLO!"]]}
16
+ end
17
+ if block_given?
18
+ yield
19
+ self.app = old_app
20
+ end
21
+ self.app
22
+ end
23
+
24
+ before(:all) do
25
+ set_app!
26
+ end
27
+
28
+ describe '#request_phase' do
29
+ it 'should display a form' do
30
+ get '/auth/zendesk'
31
+ last_response.body.should be_include("<form")
32
+ end
33
+ end
34
+
35
+ describe '#callback_phase' do
36
+ context 'with valid credentials' do
37
+ before do
38
+ stub_request(:get, "https://john%40example.com:awesome@my.zendesk.com/api/v2/users/me").
39
+ to_return(status: 200, body: File.new(File.dirname(__FILE__) + '/../../fixtures/me.json'), headers: {content_type: "application/json; charset=utf-8"})
40
+ post '/auth/zendesk/callback', email: 'john@example.com', password: 'awesome', site: 'my'
41
+ end
42
+
43
+ it 'should populate the auth hash' do
44
+ auth_hash.should be_kind_of(Hash)
45
+ end
46
+
47
+ it 'should populate the uid' do
48
+ auth_hash['uid'].should == 'https://my.zendesk.com/api/v2/users/35436.json'
49
+ end
50
+
51
+ it 'should populate the info hash' do
52
+ auth_hash['info']['name'].should == 'Johnny Agent'
53
+ end
54
+ end
55
+
56
+ context 'with invalid credentials' do
57
+ before do
58
+ OmniAuth.config.on_failure = lambda{|env| [401, {}, [env['omniauth.error.type'].inspect]]}
59
+ stub_request(:get, "https://wrong%40example.com:login@my.zendesk.com/api/v2/users/me").
60
+ to_return(status: 200, body: File.new(File.dirname(__FILE__) + '/../../fixtures/anonymous.json'), headers: {content_type: "application/json; charset=utf-8"})
61
+ post '/auth/zendesk/callback', email: 'wrong@example.com', password: 'login', site: 'my'
62
+ end
63
+
64
+ it 'should fail with :invalid_credentials' do
65
+ last_response.body.should == ':invalid_credentials'
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,14 @@
1
+ $:.unshift File.expand_path('..', __FILE__)
2
+ $:.unshift File.expand_path('../../lib', __FILE__)
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+ require 'rspec'
6
+ require 'rack/test'
7
+ require 'webmock/rspec'
8
+ require 'omniauth'
9
+ require 'omniauth-zendesk'
10
+
11
+ RSpec.configure do |config|
12
+ config.include WebMock::API
13
+ config.include Rack::Test::Methods
14
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-zendesk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thomas Stachl
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: omniauth
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: zendesk_api
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.2.2
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.2.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.7'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.7'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rack-test
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: webmock
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: OmniAuth strategy for Zendesk
111
+ email:
112
+ - tom@desk.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - .rspec
119
+ - Gemfile
120
+ - LICENSE.md
121
+ - README.md
122
+ - Rakefile
123
+ - lib/omniauth-zendesk.rb
124
+ - lib/omniauth-zendesk/version.rb
125
+ - lib/omniauth/strategies/zendesk.rb
126
+ - omniauth-zendesk.gemspec
127
+ - spec/fixtures/anonymous.json
128
+ - spec/fixtures/me.json
129
+ - spec/omniauth/strategies/zendesk_spec.rb
130
+ - spec/spec_helper.rb
131
+ homepage: https://github.com/tstachl/omniauth-zendesk
132
+ licenses: []
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ segments:
144
+ - 0
145
+ hash: 1456298723233117652
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ none: false
148
+ requirements:
149
+ - - ! '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ segments:
153
+ - 0
154
+ hash: 1456298723233117652
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 1.8.24
158
+ signing_key:
159
+ specification_version: 3
160
+ summary: OmniAuth strategy for Zendesk
161
+ test_files:
162
+ - spec/fixtures/anonymous.json
163
+ - spec/fixtures/me.json
164
+ - spec/omniauth/strategies/zendesk_spec.rb
165
+ - spec/spec_helper.rb