rails-auth 0.0.0 → 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 +4 -4
- data/.rspec +2 -0
- data/.rubocop.yml +18 -0
- data/.travis.yml +9 -1
- data/CONDUCT.md +5 -0
- data/CONTRIBUTING.md +14 -0
- data/Gemfile +11 -2
- data/Guardfile +5 -0
- data/LICENSE +202 -0
- data/README.md +267 -7
- data/Rakefile +3 -1
- data/lib/rails/auth.rb +3 -6
- data/lib/rails/auth/acl.rb +88 -0
- data/lib/rails/auth/acl/matchers/allow_all.rb +23 -0
- data/lib/rails/auth/acl/middleware.rb +31 -0
- data/lib/rails/auth/acl/resource.rb +74 -0
- data/lib/rails/auth/exceptions.rb +9 -0
- data/lib/rails/auth/principals.rb +36 -0
- data/lib/rails/auth/rack.rb +19 -0
- data/lib/rails/auth/rspec.rb +6 -0
- data/lib/rails/auth/rspec/helper_methods.rb +51 -0
- data/lib/rails/auth/rspec/matchers/acl_matchers.rb +13 -0
- data/lib/rails/auth/version.rb +4 -1
- data/lib/rails/auth/x509/filter/java.rb +25 -0
- data/lib/rails/auth/x509/filter/pem.rb +14 -0
- data/lib/rails/auth/x509/matcher.rb +22 -0
- data/lib/rails/auth/x509/middleware.rb +78 -0
- data/lib/rails/auth/x509/principal.rb +41 -0
- data/rails-auth.gemspec +20 -9
- data/spec/fixtures/example_acl.yml +27 -0
- data/spec/rails/auth/acl/matchers/allow_all_spec.rb +32 -0
- data/spec/rails/auth/acl/middleware_spec.rb +24 -0
- data/spec/rails/auth/acl/resource_spec.rb +105 -0
- data/spec/rails/auth/acl_spec.rb +28 -0
- data/spec/rails/auth/principals_spec.rb +36 -0
- data/spec/rails/auth/rspec/helper_methods_spec.rb +42 -0
- data/spec/rails/auth/rspec/matchers/acl_matchers_spec.rb +20 -0
- data/spec/rails/auth/x509/matcher_spec.rb +21 -0
- data/spec/rails/auth/x509/middleware_spec.rb +74 -0
- data/spec/rails/auth/x509/principal_spec.rb +27 -0
- data/spec/rails/auth_spec.rb +5 -0
- data/spec/spec_helper.rb +23 -0
- data/spec/support/claims_predicate.rb +11 -0
- data/spec/support/create_certs.rb +59 -0
- metadata +60 -24
- data/bin/console +0 -14
- data/bin/setup +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a492a2d3e2769b62746079b9c9d0d56fa13d9c56
|
4
|
+
data.tar.gz: 6eda7789839d1bb66b642b3c6af8f3ad213f98d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad6c4c9707ff5067b0e16f2e78f5afd66602b954a828e1a346128c318c5eeff4fdc2da755e3144a5b92c29f4063aff9c1e2645dad1bd420bb34edb08442ffe88
|
7
|
+
data.tar.gz: fe5328588172103e3ef15343dfef43b83261db496830375394a5d0151af5f939e39ce5514db5b0e474ff8cdc861a6ab8ef9137d628655f2034ede34d5bf987bf
|
data/.rspec
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
AllCops:
|
2
|
+
DisplayCopNames: true
|
3
|
+
|
4
|
+
Style/StringLiterals:
|
5
|
+
EnforcedStyle: double_quotes
|
6
|
+
|
7
|
+
Metrics/ParameterLists:
|
8
|
+
Max: 5
|
9
|
+
CountKeywordArgs: false
|
10
|
+
|
11
|
+
Metrics/LineLength:
|
12
|
+
Max: 128
|
13
|
+
|
14
|
+
Metrics/MethodLength:
|
15
|
+
Max: 25
|
16
|
+
|
17
|
+
Metrics/AbcSize:
|
18
|
+
Max: 20
|
data/.travis.yml
CHANGED
data/CONDUCT.md
ADDED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
### Sign the CLA
|
2
|
+
|
3
|
+
Any contributors to the master *rails-auth* repository must sign the
|
4
|
+
[Individual Contributor License Agreement (CLA)]. It's a short form that covers
|
5
|
+
our bases and makes sure you're eligible to contribute.
|
6
|
+
|
7
|
+
### Submitting a Pull Request
|
8
|
+
|
9
|
+
When you have a change you'd like to see in the master repository, send a
|
10
|
+
[pull request]. Before we merge your request, we'll make sure you're in the list
|
11
|
+
of people who have signed a CLA.
|
12
|
+
|
13
|
+
[Individual Contributor License Agreement (CLA)]: https://spreadsheets.google.com/spreadsheet/viewform?formkey=dDViT2xzUHAwRkI3X3k5Z0lQM091OGc6MQ&ndplr=1
|
14
|
+
[pull request]: https://github.com/square/rails-auth/pulls
|
data/Gemfile
CHANGED
@@ -1,4 +1,13 @@
|
|
1
|
-
source
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
group :development do
|
4
|
+
gem "guard-rspec"
|
5
|
+
end
|
6
|
+
|
7
|
+
group :test do
|
8
|
+
gem "rspec"
|
9
|
+
gem "rubocop", "0.36.0"
|
10
|
+
gem "certificate_authority", require: false
|
11
|
+
end
|
2
12
|
|
3
|
-
# Specify your gem's dependencies in rails-auth.gemspec
|
4
13
|
gemspec
|
data/Guardfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/README.md
CHANGED
@@ -1,8 +1,19 @@
|
|
1
1
|
# Rails::Auth
|
2
2
|
|
3
|
-
|
3
|
+
Modular resource-based authentication and authorization for Rails/Rack
|
4
4
|
|
5
|
-
|
5
|
+
## Description
|
6
|
+
|
7
|
+
Rails::Auth is a flexible library designed for both authentication (AuthN) and
|
8
|
+
authorization (AuthZ) using Rack Middleware. It splits the AuthN and AuthZ
|
9
|
+
steps into separate middleware classes, using AuthN middleware to first verify
|
10
|
+
request client identities, or "principals", then authorizing the request
|
11
|
+
via separate AuthZ middleware that consumes these principals, e.g. access
|
12
|
+
control lists (ACLs).
|
13
|
+
|
14
|
+
Rails::Auth can be used to authenticate and authorize end users using browser
|
15
|
+
cookies, service-to-service requests using X.509 client certificates, or any
|
16
|
+
other clients with credentials that have proper authenticating middleware.
|
6
17
|
|
7
18
|
## Installation
|
8
19
|
|
@@ -22,15 +33,264 @@ Or install it yourself as:
|
|
22
33
|
|
23
34
|
## Usage
|
24
35
|
|
25
|
-
|
36
|
+
To use Rails::Auth you will need to configure the relevant AuthN and AuthZ
|
37
|
+
middleware for your app.
|
38
|
+
|
39
|
+
Rails::Auth ships with the following middleware:
|
40
|
+
|
41
|
+
* **AuthN**: `Rails::Auth::X509::Middleware`: support for authenticating
|
42
|
+
principals by their SSL/TLS client certificates.
|
43
|
+
* **AuthZ**: `Rails::Auth::ACL::Middleware`: support for authorizing requests
|
44
|
+
using Access Control Lists (ACLs).
|
45
|
+
|
46
|
+
Documentation of these middleware and how to use them is provided below.
|
47
|
+
|
48
|
+
### Access Control Lists (ACLs)
|
49
|
+
|
50
|
+
ACLs are the main tool Rails::Auth provides for AuthZ. ACLs use a set of
|
51
|
+
route-by-route matchers to control access to particular resources.
|
52
|
+
Unlike some Rails AuthZ frameworks, this gem grants/denies access to
|
53
|
+
controller actions, rather than helping you provide different content to
|
54
|
+
different roles or varying the parameters allowed in, say, an update action.
|
55
|
+
|
56
|
+
Rails::Auth encourages the use of YAML files for storing ACL definitions,
|
57
|
+
although the use of YAML is not mandatory and the corresponding object
|
58
|
+
structure output from `YAML.load` can be passed in instead. The following is
|
59
|
+
an example of an ACL definition in YAML:
|
60
|
+
|
61
|
+
```yaml
|
62
|
+
---
|
63
|
+
- resources:
|
64
|
+
- method: ALL
|
65
|
+
path: /foo/bar/.*
|
66
|
+
allow_x509_subject:
|
67
|
+
ou: ponycopter
|
68
|
+
allow_claims:
|
69
|
+
groups: ["example"]
|
70
|
+
- resources:
|
71
|
+
- method: ALL
|
72
|
+
path: /_admin/?.*
|
73
|
+
allow_claims:
|
74
|
+
groups: ["admins"]
|
75
|
+
- resources:
|
76
|
+
- method: GET
|
77
|
+
path: /internal/frobnobs/.*
|
78
|
+
allow_x509_subject:
|
79
|
+
ou: frobnobber
|
80
|
+
- resources:
|
81
|
+
- method: GET
|
82
|
+
path: /
|
83
|
+
allow_all: true
|
84
|
+
```
|
85
|
+
|
86
|
+
An ACL consists of a list of guard expressions, each of which contains a list
|
87
|
+
of resources and a set of predicates which can authorize access to those
|
88
|
+
resources. *Any* matching predicate will authorize access to any of the
|
89
|
+
resources listed for a given expression.
|
90
|
+
|
91
|
+
Resources are defined by the following constraints:
|
92
|
+
|
93
|
+
* **method**: The requested HTTP method, or `"ALL"` to allow any method
|
94
|
+
* **path**: A regular expression to match the path. `\A` and `\z` are added by
|
95
|
+
default to the beginning and end of the regex to ensure the entire path and
|
96
|
+
not a substring is matched.
|
97
|
+
|
98
|
+
Once you've defined an ACL, you'll need to create a corresponding ACL object
|
99
|
+
in Ruby and a middleware to authorize requests using that ACL. Add the
|
100
|
+
following code anywhere you can modify the middleware chain (e.g. config.ru):
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
app = MyRackApp.new
|
104
|
+
|
105
|
+
acl = Rails::Auth::ACL.from_yaml(
|
106
|
+
File.read("/path/to/my/acl.yaml"),
|
107
|
+
matchers: { allow_claims: MyClaimsPredicate }
|
108
|
+
)
|
109
|
+
|
110
|
+
acl_auth = Rails::Auth::ACL::Middleware.new(app, acl: acl)
|
111
|
+
|
112
|
+
run acl_auth
|
113
|
+
```
|
114
|
+
|
115
|
+
You'll need to pass in a hash of predicate matchers that correspond to the
|
116
|
+
keys in the ACL. See the "X.509 Client Certificates" section below for how
|
117
|
+
to configure the middleware for `allow_x509_subject`.
|
118
|
+
|
119
|
+
The following predicate matchers are built-in and always available:
|
120
|
+
|
121
|
+
* **allow_all**: (options: `true` or `false`) always allow requests to the
|
122
|
+
given resources (so long as `true` is passed as the option)
|
123
|
+
|
124
|
+
Custom predicate matchers can be any Ruby class that responds to the `#match`
|
125
|
+
method. The full Rack environment is passed to `#match`. The corresponding
|
126
|
+
object from the ACL definition is passed to the class's `#initialize` method.
|
127
|
+
Here is an example of a simple custom predicate matcher:
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
class MyClaimsPredicate
|
131
|
+
def initialize(options)
|
132
|
+
@options = options
|
133
|
+
end
|
134
|
+
|
135
|
+
def match(env)
|
136
|
+
claims = Rails::Auth.principals(env)["claims"]
|
137
|
+
return false unless principal
|
138
|
+
|
139
|
+
@options["groups"].any? { |group| claims["groups"].include?(group) }
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
```
|
144
|
+
|
145
|
+
### X.509 Client Certificates
|
146
|
+
|
147
|
+
Add an `Rails::Auth::X509::Middleware` object to your Rack middleware chain to
|
148
|
+
verify X.509 client certificates (in e.g. config.ru):
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
app = MyRackApp.new
|
152
|
+
|
153
|
+
acl = Rails::Auth::ACL.from_yaml(
|
154
|
+
File.read("/path/to/my/acl.yaml")
|
155
|
+
matchers: { allow_x509_subject: Rails::Auth::X509::Matcher }
|
156
|
+
)
|
157
|
+
|
158
|
+
acl_auth = Rails::Auth::ACL::Middleware.new(app, acl: acl)
|
159
|
+
|
160
|
+
x509_auth = Rails::Auth::X509::Middleware.new(
|
161
|
+
acl_auth,
|
162
|
+
ca_file: "/path/to/my/cabundle.pem"
|
163
|
+
cert_filters: { 'X-SSL-Client-Cert' => :pem },
|
164
|
+
require_cert: true
|
165
|
+
)
|
26
166
|
|
27
|
-
|
167
|
+
run x509_auth
|
168
|
+
```
|
169
|
+
|
170
|
+
The constructor takes the following parameters:
|
171
|
+
|
172
|
+
* **app**: the next Rack middleware in the chain. You'll likely want to use
|
173
|
+
an `Rails::Auth::ACL::Middleware` instance as the next middleware in the chain.
|
174
|
+
* **ca_file**: Path to the certificate authority (CA) bundle with which to
|
175
|
+
authenticate clients. This will typically be the certificates for the
|
176
|
+
internal CA(s) you use to issue X.509 certificates to internal services, as
|
177
|
+
opposed to commercial CAs typically used by browsers. Client certificates
|
178
|
+
will be ignored unless they can be verified by one of the CAs in this bundle.
|
179
|
+
* **cert_filters**: A `Hash` which configures how client certificates are
|
180
|
+
extracted from the Rack environment. You will need to configure your web
|
181
|
+
server to include the certificate in the Rack environment. See notes below
|
182
|
+
for more details.
|
183
|
+
* **require_cert**: (default `false`) require a valid client cert in order for
|
184
|
+
the request to complete. This disallows access to your app from any clients
|
185
|
+
who do not have a valid client certificate. When enabled, the middleware
|
186
|
+
will raise the `Rails::Auth::X509::CertificateVerifyFailed` exception.
|
187
|
+
|
188
|
+
When creating `Rails::Auth::ACL::Middleware`, make sure to pass in
|
189
|
+
`matchers: { allow_x509_subject: Rails::Auth::X509::Matcher }` in order to use
|
190
|
+
this predicate in your ACLs. This predicate matcher is not enabled by default.
|
191
|
+
|
192
|
+
For client certs to work, you will need to configure your web server to include
|
193
|
+
them in your Rack environment, and also configure `cert_filters` correctly to
|
194
|
+
filter and process them from the Rack environment.
|
195
|
+
|
196
|
+
For example, if you're using nginx + Passenger, you'll need to add something
|
197
|
+
like the following to your nginx configuration:
|
198
|
+
|
199
|
+
```
|
200
|
+
passenger_set_cgi_param X-SSL-Client-Cert $ssl_client_raw_cert;
|
201
|
+
```
|
202
|
+
|
203
|
+
Once the client certificate is in the Rack environment in some form, you'll
|
204
|
+
need to configure a filter object which can convert it from its Rack
|
205
|
+
environment form into an `OpenSSL::X509::Certificate` instance. There are
|
206
|
+
two built in filters you can reference as symbols to do this:
|
207
|
+
|
208
|
+
* `:pem`: parses certificates from the Privacy Enhanced Mail format
|
209
|
+
* `:java`: converts `sun.security.x509.X509CertImpl` object instances
|
210
|
+
|
211
|
+
The `cert_filters` parameter is a mapping of Rack environment names to
|
212
|
+
corresponding filters:
|
213
|
+
|
214
|
+
```ruby
|
215
|
+
cert_filters: { 'X-SSL-Client-Cert' => :pem }
|
216
|
+
```
|
217
|
+
|
218
|
+
In addition to these symbols, a filter can be any object that responds to the
|
219
|
+
`#call` method, such as a `Proc`. The following filter will parse PEM
|
220
|
+
certificates:
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
cert_filters: { 'X-SSL-Client-Cert' => proc { |pem| OpenSSL::X509::Certificate.new(pem) } }
|
224
|
+
```
|
28
225
|
|
29
|
-
|
226
|
+
When certificates are recognized and verified, an `Rails::Auth::X509::Principal`
|
227
|
+
object will be added to the Rack environment under `env["rails-auth.principals"]["x509"]`.
|
228
|
+
This middleware will never add any certificate to the environment's principals
|
229
|
+
that hasn't been verified against the configured CA bundle.
|
30
230
|
|
31
|
-
|
231
|
+
## RSpec integration
|
232
|
+
|
233
|
+
Rails::Auth includes built-in matchers that allow you to write tests for your
|
234
|
+
ACLs to ensure they have the behavior you expect.
|
235
|
+
|
236
|
+
To enable RSpec support, require the following:
|
237
|
+
|
238
|
+
```ruby
|
239
|
+
require "rails/auth/rspec"
|
240
|
+
```
|
241
|
+
|
242
|
+
Below is an example of how to write an ACL spec:
|
243
|
+
|
244
|
+
```ruby
|
245
|
+
RSpec.describe "example_acl.yml", acl_spec: true do
|
246
|
+
let(:example_principals) { x509_principal_hash(ou: "ponycopter") }
|
247
|
+
|
248
|
+
subject do
|
249
|
+
Rails::Auth::ACL.from_yaml(
|
250
|
+
File.read("/path/to/example_acl.yml"),
|
251
|
+
matchers: { allow_x509_subject: Rails::Auth::X509::Matcher }
|
252
|
+
)
|
253
|
+
end
|
254
|
+
|
255
|
+
describe "/path/to/resource" do
|
256
|
+
it { is_expected.to permit get_request(principals: example_principals) }
|
257
|
+
it { is_expected.not_to permit get_request) }
|
258
|
+
end
|
259
|
+
end
|
260
|
+
```
|
261
|
+
|
262
|
+
The following helper methods are available:
|
263
|
+
|
264
|
+
* `x509_principal`, `x509_principal_hash`: create instance doubles of Rails::Auth::X509::Principals
|
265
|
+
* Request builders: The following methods build requests from the described path:
|
266
|
+
* `get_request`
|
267
|
+
* `head_request`
|
268
|
+
* `put_request`
|
269
|
+
* `post_request`
|
270
|
+
* `delete_request`
|
271
|
+
* `options_request`
|
272
|
+
* `path_request`
|
273
|
+
* `link_request`
|
274
|
+
* `unlink_request`
|
275
|
+
|
276
|
+
The following matchers are available:
|
277
|
+
|
278
|
+
* `allow_request`: allows a request with the given Rack environment, and optional principals
|
32
279
|
|
33
280
|
## Contributing
|
34
281
|
|
35
|
-
|
282
|
+
Any contributors to the master *rails-auth* repository must sign the
|
283
|
+
[Individual Contributor License Agreement (CLA)]. It's a short form that covers
|
284
|
+
our bases and makes sure you're eligible to contribute.
|
285
|
+
|
286
|
+
When you have a change you'd like to see in the master repository, send a
|
287
|
+
[pull request]. Before we merge your request, we'll make sure you're in the list
|
288
|
+
of people who have signed a CLA.
|
289
|
+
|
290
|
+
[Individual Contributor License Agreement (CLA)]: https://spreadsheets.google.com/spreadsheet/viewform?formkey=dDViT2xzUHAwRkI3X3k5Z0lQM091OGc6MQ&ndplr=1
|
291
|
+
[pull request]: https://github.com/square/rails-auth/pulls
|
292
|
+
|
293
|
+
## License
|
36
294
|
|
295
|
+
Copyright (c) 2016 Square Inc. Distributed under the Apache 2.0 License.
|
296
|
+
See LICENSE file for further details.
|