rack-cas-rails 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.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/README.md +142 -0
- data/lib/rack-cas/rails/controllers.rb +37 -0
- data/lib/rack-cas/rails/helpers.rb +36 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 94069c44e3881fdf468716d0cde889f52bb29dc0
|
4
|
+
data.tar.gz: a6561f72a97430cde57571c08a509241ba811150
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f4c4d0cf508b1eccec73e857220e52345b654fd7521d70693503651abe422f40b37f8181aa2f9c354f7495f7a8884b66d7086f6e3c06ca9cb72adca196755473
|
7
|
+
data.tar.gz: 8aee86f4813563c42758a484fb173403b4d4fa09b42e4438cbaf42d7aed88a7ba631d9ec3b113db89e3b4cc14f635768ae2c49df06e67ff5023f97609090417e
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Nathan Brazil
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
# rack-cas-rails
|
2
|
+
|
3
|
+
While [rbCAS/CASinoApp](http://rbcas.com) and [biola/rack-cas](https://github.com/biola/rack-cas) are both great
|
4
|
+
and wonderful, there is gap between them. Namely, the bits needed to enable a Rails application to use rack-cas to integrate with
|
5
|
+
CASinoApp for authentication are still missing.
|
6
|
+
|
7
|
+
This gem aims to fill in this void.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add the following line to a Rails application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem "rack-cas-rails"
|
15
|
+
```
|
16
|
+
|
17
|
+
Then open up your config/application.rb file, and add the following:
|
18
|
+
|
19
|
+
## Requirements
|
20
|
+
|
21
|
+
The rack-cas-rails gem relies on the following:
|
22
|
+
|
23
|
+
* A CAS-compliant server, such as [CASinoApp](http://rbcas.com)
|
24
|
+
* [rack-cas](https://github.com/biola/rack-cas)
|
25
|
+
* [rails](http://rubyonrails.org/)
|
26
|
+
|
27
|
+
## Basic Usage
|
28
|
+
|
29
|
+
The first thing you need to do is to make your Application class (file ```config/application.rb```) aware of the CAS-compliant
|
30
|
+
server you are integrating with by pointing out its base URL, like so:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
module MyGreatApplication
|
34
|
+
class Application < Rails::Application
|
35
|
+
# ...
|
36
|
+
# URL of CAS server
|
37
|
+
config.rack_cas.server_url = "https://sso.example.org/"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
In the simplest scenario, you'll want your entire application protected by authentication. That is, unless a user has authenticated,
|
43
|
+
he can do nothing. To do so, add the following ```before_action``` callback to your ApplicationController (file
|
44
|
+
```app/controllers/application_ronctoller.rb```):
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class ApplicationController < ActionController::Base
|
48
|
+
# authenticate all actions for all controllers
|
49
|
+
before_action :authenticate!
|
50
|
+
# ...
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
The ```authenticate!``` method will check to see if a browser session is authenticated. If it is, controller execution will continue.
|
55
|
+
Otherwise, it will render the ```public/401.html``` file as well as return a HTTP status of 401.
|
56
|
+
|
57
|
+
So, now, create a ```pubilc/401.html``` file in your application. You can simply copy an existing file, rename and change its
|
58
|
+
contents.
|
59
|
+
|
60
|
+
## Helper Methods
|
61
|
+
|
62
|
+
The rack-cas-rails gem also augments the ApplicationHelper module with these methods:
|
63
|
+
|
64
|
+
* login_url
|
65
|
+
* logout_url
|
66
|
+
|
67
|
+
When invoked, these helpers will renturn the CAS-integrated login in and log out URLs, respectively.
|
68
|
+
|
69
|
+
## What Is Still Missing
|
70
|
+
|
71
|
+
Even with the rack-cas and rack-cas-rails gems, the aforementioned basic authentication scheme is still incomplete. Namely, for an
|
72
|
+
authenticated session, which user does it belong to?
|
73
|
+
|
74
|
+
Various Rails authentication gems makes the currently authenticated user available as an object via the ```current_user``` helper
|
75
|
+
method. The rack-cas-rails gem does not provide this functionality. But you can look to gems such as
|
76
|
+
[OmniAuth](https://github.com/intridea/omniauth), [Devise](https://github.com/plataformatec/devise), and so on
|
77
|
+
to provide it.
|
78
|
+
|
79
|
+
But, assuming your application has **users** table in its database containing user records which are uniquely identifiable by a username
|
80
|
+
column, you can add the following code to your ApplictionController:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
class ApplicationController
|
84
|
+
|
85
|
+
# ...
|
86
|
+
|
87
|
+
def current_user
|
88
|
+
authenciated? ? User.find_by_login(request.session["cas"]["user"]) : nil
|
89
|
+
end
|
90
|
+
|
91
|
+
helper_method :current_user
|
92
|
+
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
Lastly, change your ```views/layouts/application.html.erb``` to be as follows:
|
97
|
+
|
98
|
+
```erb
|
99
|
+
<!DOCTYPE html>
|
100
|
+
<html>
|
101
|
+
<head>
|
102
|
+
<title>Testapp420</title>
|
103
|
+
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
|
104
|
+
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
|
105
|
+
<%= csrf_meta_tags %>
|
106
|
+
</head>
|
107
|
+
<body>
|
108
|
+
<divi id="nav-header">
|
109
|
+
<% if authenticated? %>
|
110
|
+
<span>Logged in as <%= current_user.username %>.</span>
|
111
|
+
<span style="float:right"><%= link_to "Logout", logout_url %></span>
|
112
|
+
<% else %>
|
113
|
+
<span><%= link_to "Login", login_url %></span>
|
114
|
+
<% end %>
|
115
|
+
</div>
|
116
|
+
<hr />
|
117
|
+
<div>
|
118
|
+
<%= yield %>
|
119
|
+
</div>
|
120
|
+
</body>
|
121
|
+
</html>
|
122
|
+
```
|
123
|
+
|
124
|
+
## Summary
|
125
|
+
|
126
|
+
To recap, you'll have integrated your Rails application with a CAS-compliant server by making these changes to your application:
|
127
|
+
|
128
|
+
1. Add config.rack_cas.server_url to config/application.rb
|
129
|
+
2. Add ```before_action :authenticate!``` to ApplicationController
|
130
|
+
3. Add ```current_user``` method to ApplictionController
|
131
|
+
4. Add simple navigational header to make use of ```current_user``` and the ```login_url```/```logout_url``` helpers
|
132
|
+
|
133
|
+
As such, you can expect the following behavior:
|
134
|
+
|
135
|
+
* When you browse to any view within your application using a fresh session, you'll be re-directed to the sign-in page
|
136
|
+
* After you authenticate, you'll be re-directed back to the page you browsed to
|
137
|
+
* When you click the Logout link, your session will end, and the browser will be re-directed back to the login page
|
138
|
+
|
139
|
+
## Credits
|
140
|
+
|
141
|
+
A big *thank-you* goes out the teams and contributors behind [CASinoApp](http://rbcas.com) and
|
142
|
+
[rack-cas](https://github.com/biola/rack-cas), without whom this gem will not be possible.
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module RackCAS
|
2
|
+
module Rails
|
3
|
+
|
4
|
+
##
|
5
|
+
# All actions in controllers derived from this controller require authentication.
|
6
|
+
|
7
|
+
class AuthenticatedController < ApplicationController
|
8
|
+
before_action :authenticate!
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
##
|
15
|
+
# Augment Rails' ApplicationController class with authentication related methods.
|
16
|
+
|
17
|
+
class ApplicationController
|
18
|
+
|
19
|
+
##
|
20
|
+
# When invoked, will force authenticate. Most likely to be invoked as a before_action.
|
21
|
+
|
22
|
+
def authenticate!
|
23
|
+
authenticated? or render(:file => "public/401.html", :status => :unauthorized) # HTTP 401
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# Determines whether the current request belongs to a session that is authenticated or not.
|
28
|
+
# @return [Bool] True if current request belongs to an authenticated session, false otherwise.
|
29
|
+
|
30
|
+
def authenticated?
|
31
|
+
request.session["cas"] && request.session["cas"]["user"]
|
32
|
+
end
|
33
|
+
|
34
|
+
helper_method :authenticate!
|
35
|
+
helper_method :authenticated?
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module RackCAS
|
2
|
+
module Rails
|
3
|
+
end
|
4
|
+
end
|
5
|
+
|
6
|
+
##
|
7
|
+
# Augment the ApplicationHelper module with these methods.
|
8
|
+
|
9
|
+
module ApplicationHelper
|
10
|
+
|
11
|
+
##
|
12
|
+
# Renders the CAS login URL with re-direct back to some URL.
|
13
|
+
# @param service_url [String] Optional url to redirect to after authentication.
|
14
|
+
# @return [String] The CAS login URL.
|
15
|
+
|
16
|
+
def login_url(service_url=request.url)
|
17
|
+
url = URI(Rails.application.config.rack_cas.server_url)
|
18
|
+
url.path = "/login"
|
19
|
+
url.query = "service=#{service_url || request.url}"
|
20
|
+
url.to_s
|
21
|
+
end
|
22
|
+
|
23
|
+
##
|
24
|
+
# Renders the CAS logout URL with re-direct back to some URL (e.g. the root URL). The logout path is "/logout",
|
25
|
+
# which is actually undocumented. I had to find out by looking into the source code of the rack-cas gem.
|
26
|
+
# @param service_url [String] Optional url to redirect to after authentication.
|
27
|
+
# @return [String] The CAS logout URL.
|
28
|
+
|
29
|
+
def logout_url(service_url=request.url)
|
30
|
+
url = URI(request.url)
|
31
|
+
url.path = "/logout"
|
32
|
+
url.query = "service=#{service_url || request.url}"
|
33
|
+
url.to_s
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-cas-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nathan Brazil
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack-cas
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.9.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.9.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.2'
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 4.2.0
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '4.2'
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 4.2.0
|
47
|
+
description: Provides the integration glue between a Rails application and biola/rack-cas
|
48
|
+
so that a CAS-compliant server (only tested with CASinoApp) can be used for authentication
|
49
|
+
by the application.
|
50
|
+
email: nb@bitaxis.com
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- LICENSE
|
56
|
+
- README.md
|
57
|
+
- lib/rack-cas/rails/controllers.rb
|
58
|
+
- lib/rack-cas/rails/helpers.rb
|
59
|
+
homepage: https://github.com/bitaxis/json_hash.git
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.4.5
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Enables a Rails application to use CAS-compliant server for authentication.
|
83
|
+
test_files: []
|
84
|
+
has_rdoc:
|