shibboleth-rails 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,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in shibboleth-rails.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ class UserSessionsController < ApplicationController
2
+
3
+ skip_before_filter :require_shibboleth
4
+ before_filter :not_in_production
5
+
6
+ def new
7
+ @users = User.all
8
+ end
9
+
10
+ def create
11
+ session[:simulate_id] = params[:user_id]
12
+ redirect_to root_url
13
+ end
14
+
15
+ def destroy
16
+ session[:simulate_id] = nil
17
+ redirect_to new_user_session_url, :notice => "Logout successful!"
18
+ end
19
+
20
+ private
21
+ def not_in_production
22
+ redirect_to root_url if Rails.env.production?
23
+ end
24
+
25
+ end
@@ -0,0 +1,4 @@
1
+ = form_tag user_session_path do
2
+ Login as:
3
+ = select_tag :user_id, options_from_collection_for_select(@users, :id, :name_n)
4
+ = submit_tag "Login"
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ Rails.application.routes.draw do
2
+ unless Rails.env.production?
3
+ resource :user_session, :only => [:new, :create, :destroy]
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ require "shibboleth-rails/version"
2
+ require 'shibboleth-rails/engine'
3
+ require 'shibboleth-rails/controller_additions'
4
+ require 'shibboleth-rails/user_model_additions'
@@ -0,0 +1,43 @@
1
+ module Shibboleth::Rails
2
+
3
+ module ControllerAdditions
4
+ private
5
+
6
+ def authenticated?
7
+ request.env['employeeNumber'].present?
8
+ end
9
+
10
+ def shibboleth
11
+ {:emplid => request.env['employeeNumber'],
12
+ :name_n => request.env['REMOTE_USER'].chomp("@osu.edu")}
13
+ end
14
+
15
+ def current_user
16
+ return @current_user if defined?(@current_user)
17
+ @current_user = if session[:simulate_id].present?
18
+ User.find(session[:simulate_id])
19
+ elsif authenticated?
20
+ User.find_or_create_from_shibboleth(shibboleth)
21
+ end
22
+ end
23
+
24
+ def require_shibboleth
25
+ unless current_user
26
+ if Rails.env.production?
27
+ base = request.protocol + request.host
28
+ requested_url = base + request.request_uri
29
+ redirect_to [base, '/Shibboleth.sso/Login?target=',
30
+ CGI.escape(requested_url)].join
31
+ else
32
+ redirect_to new_user_session_url, :notice => 'Login first, please.'
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ ActionController::Base.class_eval do
41
+ include Shibboleth::Rails::ControllerAdditions
42
+ helper_method :current_user
43
+ end
@@ -0,0 +1,6 @@
1
+ module Shibboleth::Rails
2
+
3
+ class Engine < ::Rails::Engine
4
+ end
5
+
6
+ end
@@ -0,0 +1,15 @@
1
+ module Shibboleth::Rails
2
+
3
+ module ModelAdditions
4
+ def find_or_create_from_shibboleth(identity)
5
+ user = find_or_create_by_emplid(identity)
6
+
7
+ # names change due to marriage, etc.
8
+ # update_attribute is a NOOP if not different
9
+ user.update_attribute(:name_n, identity[:name_n])
10
+
11
+ user
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,5 @@
1
+ module Shibboleth
2
+ module Rails
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "shibboleth-rails/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "shibboleth-rails"
7
+ s.version = Shibboleth::Rails::VERSION
8
+ s.authors = ["mikegee"]
9
+ s.email = ["gee.24@osu.edu"]
10
+ s.homepage = "https://github.com/ASCTech/shibboleth-rails"
11
+ s.summary = %q{This Rails plugin integrates Shibboletth single signon.}
12
+ s.description = %q{Environment variables that Shibboleth sets are used to determine 'current_user'. An interface to login as any user is also provided for running in development.}
13
+
14
+ s.rubyforge_project = "shibboleth-rails"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency "rails", '>3.0'
22
+ s.add_runtime_dependency "haml"
23
+ end
@@ -0,0 +1,34 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe UserSessionsController do
4
+ before { @user = Factory(:user) }
5
+
6
+ describe 'loading the login page' do
7
+ before { get :new }
8
+ it { should respond_with(:success) }
9
+ it { should assign_to(:user_session) }
10
+ it { should assign_to(:users), :with => [@user] }
11
+ end
12
+
13
+ describe 'loggin in' do
14
+ before { post :create, :user_id => @user.id }
15
+ it 'should login the user' do
16
+ UserSession.find.user.should == @user
17
+ end
18
+ it { should respond_with(:redirect), :to => root_url }
19
+ it { should set_the_flash.to("You are now logged in as #{@user.name_n}.") }
20
+ end
21
+
22
+ describe 'logging out' do
23
+ before do
24
+ UserSession.create(@user)
25
+ delete :destroy
26
+ end
27
+ it 'should log out the user' do
28
+ UserSession.find.should be_nil
29
+ end
30
+ it { should respond_with(:redirect), :to => new_user_session_url }
31
+ it { should set_the_flash.to('Logout successful!') }
32
+ end
33
+
34
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shibboleth-rails
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - mikegee
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-08-27 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rails
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">"
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 3
31
+ - 0
32
+ version: "3.0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: haml
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ description: Environment variables that Shibboleth sets are used to determine 'current_user'. An interface to login as any user is also provided for running in development.
50
+ email:
51
+ - gee.24@osu.edu
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - .gitignore
60
+ - Gemfile
61
+ - Rakefile
62
+ - app/controllers/user_sessions_controller.rb
63
+ - app/views/user_sessions/new.html.haml
64
+ - config/routes.rb
65
+ - lib/shibboleth-rails.rb
66
+ - lib/shibboleth-rails/controller_additions.rb
67
+ - lib/shibboleth-rails/engine.rb
68
+ - lib/shibboleth-rails/user_model_additions.rb
69
+ - lib/shibboleth-rails/version.rb
70
+ - shibboleth-rails.gemspec
71
+ - spec/controllers/user_sessions_controller_spec.rb
72
+ homepage: https://github.com/ASCTech/shibboleth-rails
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options: []
77
+
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ hash: 3
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ requirements: []
99
+
100
+ rubyforge_project: shibboleth-rails
101
+ rubygems_version: 1.8.10
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: This Rails plugin integrates Shibboletth single signon.
105
+ test_files:
106
+ - spec/controllers/user_sessions_controller_spec.rb