zendesk_remote_auth 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Tobias Crawley
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,89 @@
1
+ = zendesk_remote_auth
2
+
3
+ zendesk_remote_auth is a simple gem providing help with Zendesk's SSO
4
+ functionality (see http://www.zendesk.com/api/remote_authentication).
5
+
6
+ == Installation and Setup
7
+
8
+ * Install:
9
+
10
+ gem install tobias-zendesk_remote_auth
11
+
12
+ * Setup:
13
+ You will need to give the gem your token and authentication url,
14
+ perhaps in an initializer:
15
+ Zendesk::RemoteAuth.token = 'YOUR-TOKEN'
16
+ Zendesk::RemoteAuth.auth_url = 'https://yourcompany.zendesk.com/access/remote/'
17
+
18
+ and config the gem in environment.rb (if using rails):
19
+ config.gem 'tobias-zendesk_remote_auth', :lib => 'zendesk_remote_auth', :source => http://gems.github.com'
20
+
21
+ == Usage
22
+
23
+ Mixin the Zendesk::RemoteAuthHelper module wherever needed, then call:
24
+ zendesk_remote_auth_url(:name => 'user name',
25
+ :email => 'user email',
26
+ <optional params>)
27
+
28
+ This will return a url you can redirect the user to to log them in to
29
+ your zendesk account.
30
+
31
+ As a convenience, you can pass a user object to zendesk_remote_auth_url:
32
+ zendesk_remote_auth_url(user)
33
+
34
+ This user must respond_to? :name and :email, and its :id will be used
35
+ as the :external_id (making it useless with user objects that return
36
+ an ephemeral object_id, but works well with ActiveRecord and the
37
+ like). If the user object responds to :zendesk_organization, that will
38
+ be used as the :organization for the call.
39
+
40
+ This method will generate and include the hash of the parameters for
41
+ you if necessary.
42
+
43
+ == Example Auth Controller
44
+
45
+ Here is an example controller that handles login and logout for
46
+ zendesk:
47
+
48
+ # Uses restful-authentication style auth.
49
+ #
50
+ # Define the following in routes.rb:
51
+ # map.with_options :controller => 'zendesk_auth' do |zd|
52
+ # zd.connect '/zendesk/authorize', :action => 'authorize'
53
+ # zd.connect '/zendesk/logout', :action => 'logout'
54
+ # end
55
+ class ZendeskAuthController < ApplicationController
56
+ include Zendesk::RemoteAuthHelper
57
+
58
+ skip_before_filter :login_required, :only => :logout
59
+
60
+ def authorize
61
+ redirect_to zendesk_remote_auth_url(current_user)
62
+ end
63
+
64
+ def logout
65
+ redirect_to logout_url
66
+ end
67
+
68
+ protected
69
+ def login_required
70
+ if !logged_in?
71
+ flash[:notice] = 'You must log in to access the support site.'
72
+ store_location
73
+ redirect_to login_path
74
+ end
75
+ end
76
+ end
77
+
78
+
79
+ == Note on Patches/Pull Requests
80
+
81
+ * Fork the project.
82
+ * Make your feature addition or bug fix.
83
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
84
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
85
+ * Send me a pull request. Bonus points for topic branches.
86
+
87
+ == Copyright
88
+
89
+ Copyright (c) 2009 Tobias Crawley. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "zendesk_remote_auth"
8
+ gem.summary = %Q{Helper for Zendesk SSO/remote authentication}
9
+ gem.description = %Q{See the README.}
10
+ gem.email = "tcrawley@gmail.com"
11
+ gem.homepage = "http://github.com/tobias/zendesk_remote_auth"
12
+ gem.authors = ["Tobias Crawley"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ gem.add_development_dependency "mocha"
15
+ gem.add_development_dependency "matchy"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/*_test.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << 'test'
34
+ test.pattern = 'test/**/*_test.rb'
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :rcov do
39
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
40
+ end
41
+ end
42
+
43
+ task :test => :check_dependencies
44
+
45
+ task :default => :test
46
+
47
+ begin
48
+ require 'yard'
49
+ YARD::Rake::YardocTask.new
50
+ rescue LoadError
51
+ task :yardoc do
52
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
53
+ end
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.9.0
@@ -0,0 +1,82 @@
1
+ require 'digest/md5'
2
+ require 'active_support' #gives us Hash.to_query
3
+
4
+ ##
5
+ # Provides a helper to use Zendesk's SSO/remote auth service.
6
+ # see: http://www.zendesk.com/api/remote_authentication
7
+ module Zendesk
8
+
9
+ ##
10
+ # Handles storing the token and auth_url (endpoint) for the Zendesk side.
11
+ class RemoteAuth
12
+ class << self
13
+ attr_writer :auth_url, :token
14
+
15
+ def token
16
+ raise ArgumentError.new('Zendesk token must be set. Set with Zendesk::RemoteAuth.token = <token>') unless @token
17
+ @token
18
+ end
19
+
20
+ def auth_url
21
+ raise ArgumentError.new('Zendesk auth_url must be set. Set with Zendesk::RemoteAuth.auth_url = <url>') unless @auth_url
22
+ @auth_url
23
+ end
24
+ end
25
+ end
26
+
27
+ ##
28
+ # Provides the method that generates the auth url. Mixin where required.
29
+ module RemoteAuthHelper
30
+ ##
31
+ # Takes a hash of parameters and generates the hashed auth
32
+ # url. The hash must include the :name and :email for the user.
33
+ # See: http://www.zendesk.com/api/remote_authentication for a list
34
+ # of all of the parameters.
35
+ #
36
+ # If the :timestamp is not provided, Time.now will be used. If an
37
+ # :external_id is provided, then the :hash will be generated.
38
+ #
39
+ # As a convenience, a user object can be passed instead, but must
40
+ # respond_to? at least :email and :name. The :id of the user
41
+ # object will be used as the :external_id, and if the user
42
+ # responds to :zendesk_organization, that will be used as the
43
+ # :organization.
44
+ def zendesk_remote_auth_url(user_or_params)
45
+ params = user_or_params.is_a?(Hash) ? user_or_params : user_to_params(user_or_params)
46
+ validate_params(params)
47
+ params[:timestamp] = Time.now.utc.to_i unless params[:timestamp]
48
+ params[:hash] = params[:external_id] ? generate_hash(Zendesk::RemoteAuth.token, params) : ''
49
+
50
+ "#{Zendesk::RemoteAuth.auth_url}?#{params.to_query}"
51
+ end
52
+
53
+ private
54
+ def user_to_params(user)
55
+ params = { }
56
+ [[:email, :email],
57
+ [:name, :name],
58
+ [:external_id, :id],
59
+ [:organization, :zendesk_organization]].each do |param, field|
60
+ params[param] = user.send(field) if user.respond_to?(field)
61
+ end
62
+ params
63
+ end
64
+
65
+ def validate_params(params)
66
+ [:email, :name].each do |param|
67
+ raise ArgumentError.new("Required parameter :#{param} not given") unless params[param]
68
+ end
69
+ end
70
+
71
+ def generate_hash(token, params)
72
+ str_to_hash = params[:name].clone
73
+ str_to_hash << params[:email]
74
+ str_to_hash << params[:external_id].to_s if params[:external_id]
75
+ str_to_hash << params[:organization].to_s if params[:organization]
76
+ str_to_hash << token
77
+ str_to_hash << params[:timestamp].to_s
78
+ Digest::MD5.hexdigest(str_to_hash)
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+ require 'matchy'
6
+
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
9
+ require 'zendesk_remote_auth'
10
+
11
+ class Test::Unit::TestCase
12
+ end
@@ -0,0 +1,103 @@
1
+ require 'test_helper'
2
+
3
+ class ZendeskRemoteAuthTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @auth = Object.new
7
+ @auth.extend(Zendesk::RemoteAuthHelper)
8
+ end
9
+
10
+ context 'RemoteAuth' do
11
+ setup do
12
+ Zendesk::RemoteAuth.token = Zendesk::RemoteAuth.auth_url = nil
13
+ end
14
+
15
+ should 'raise exception if token is not set' do
16
+ assert_raise ArgumentError do
17
+ Zendesk::RemoteAuth.token
18
+ end
19
+ end
20
+
21
+ should 'return the token without exception if it is set' do
22
+ Zendesk::RemoteAuth.token = 'blah'
23
+ Zendesk::RemoteAuth.token.should == 'blah'
24
+ end
25
+
26
+
27
+ should 'raise exception if auth_url is not set' do
28
+ assert_raise ArgumentError do
29
+ Zendesk::RemoteAuth.auth_url
30
+ end
31
+ end
32
+
33
+ should 'return the auth_url without exception if it is set' do
34
+ Zendesk::RemoteAuth.auth_url = 'blah'
35
+ Zendesk::RemoteAuth.auth_url.should == 'blah'
36
+ end
37
+ end
38
+
39
+
40
+ context 'url generation' do
41
+ setup do
42
+ Zendesk::RemoteAuth.token = 'the_token'
43
+ Zendesk::RemoteAuth.auth_url = 'the_url'
44
+ @valid_params = { :email => 'test@example.com', :name => 'blah'}
45
+ end
46
+
47
+ context 'required fields' do
48
+ should 'raise an argument error the name is not provided' do
49
+ assert_raise ArgumentError do
50
+ @valid_params.delete(:name)
51
+ @auth.zendesk_remote_auth_url(@valid_params)
52
+ end
53
+ end
54
+
55
+ should 'raise an argument error the email is not provided' do
56
+ assert_raise ArgumentError do
57
+ @valid_params.delete(:email)
58
+ @auth.zendesk_remote_auth_url(@valid_params)
59
+ end
60
+ end
61
+
62
+ end
63
+
64
+ should 'return a url that starts with the auth_url' do
65
+ @auth.zendesk_remote_auth_url(@valid_params)
66
+ end
67
+
68
+ should 'have an empty hash param if external_id not provided' do
69
+ @auth.zendesk_remote_auth_url(@valid_params).should =~ /hash=(&|$)/
70
+ end
71
+
72
+ should 'have a hash param if external_id provided' do
73
+ @auth.zendesk_remote_auth_url(@valid_params.merge(:external_id => 'id')).should_not =~ /hash=(&|$)/
74
+ end
75
+
76
+ context 'given a user object' do
77
+ setup do
78
+ @user = mock
79
+ @user.expects(:name).returns('a_name')
80
+ @user.expects(:email).returns('an_email')
81
+ end
82
+
83
+ should 'pull the name from the user' do
84
+ @auth.zendesk_remote_auth_url(@user).should =~ /name=a_name/
85
+ end
86
+
87
+ should 'pull the email from the user' do
88
+ @auth.zendesk_remote_auth_url(@user).should =~ /email=an_email/
89
+ end
90
+
91
+ should 'pull the id from the user' do
92
+ @user.expects(:id).returns('an_id')
93
+ @auth.zendesk_remote_auth_url(@user).should =~ /external_id=an_id/
94
+ end
95
+
96
+ should 'pull the organization from the user if available' do
97
+ @user.expects(:zendesk_organization).returns('org')
98
+ @auth.zendesk_remote_auth_url(@user).should =~ /organization=org/
99
+ end
100
+
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,59 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{zendesk_remote_auth}
8
+ s.version = "0.9.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tobias Crawley"]
12
+ s.date = %q{2009-10-01}
13
+ s.description = %q{See the README.}
14
+ s.email = %q{tcrawley@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/zendesk_remote_auth.rb",
27
+ "test/test_helper.rb",
28
+ "test/zendesk_remote_auth_test.rb",
29
+ "zendesk_remote_auth.gemspec"
30
+ ]
31
+ s.homepage = %q{http://github.com/tobias/zendesk_remote_auth}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{Helper for Zendesk SSO/remote authentication}
36
+ s.test_files = [
37
+ "test/test_helper.rb",
38
+ "test/zendesk_remote_auth_test.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
47
+ s.add_development_dependency(%q<mocha>, [">= 0"])
48
+ s.add_development_dependency(%q<matchy>, [">= 0"])
49
+ else
50
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
51
+ s.add_dependency(%q<mocha>, [">= 0"])
52
+ s.add_dependency(%q<matchy>, [">= 0"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
56
+ s.add_dependency(%q<mocha>, [">= 0"])
57
+ s.add_dependency(%q<matchy>, [">= 0"])
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zendesk_remote_auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Tobias Crawley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-01 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: mocha
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: matchy
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ description: See the README.
46
+ email: tcrawley@gmail.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - LICENSE
53
+ - README.rdoc
54
+ files:
55
+ - .document
56
+ - .gitignore
57
+ - LICENSE
58
+ - README.rdoc
59
+ - Rakefile
60
+ - VERSION
61
+ - lib/zendesk_remote_auth.rb
62
+ - test/test_helper.rb
63
+ - test/zendesk_remote_auth_test.rb
64
+ - zendesk_remote_auth.gemspec
65
+ has_rdoc: true
66
+ homepage: http://github.com/tobias/zendesk_remote_auth
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options:
71
+ - --charset=UTF-8
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ version:
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: "0"
85
+ version:
86
+ requirements: []
87
+
88
+ rubyforge_project:
89
+ rubygems_version: 1.3.5
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Helper for Zendesk SSO/remote authentication
93
+ test_files:
94
+ - test/test_helper.rb
95
+ - test/zendesk_remote_auth_test.rb