app_identity 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,233 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "base64"
4
+
5
+ # AppIdentity is a Ruby implementation of the Kinetic Commerce application
6
+ # [identity proof algorithm](spec.md).
7
+ #
8
+ # It implements identity proof generation and validation functions. These
9
+ # functions expect to work with an application object (AppIdentity::App).
10
+ #
11
+ # ## Configuration
12
+ #
13
+ # Most of the configuration is specified in the application, but at an
14
+ # application level, AppIdentity can be configured to disallow explicit
15
+ # versions:
16
+ #
17
+ # ```ruby
18
+ # AppIdentity.disallow_version 1, 3, 4
19
+ # ```
20
+ #
21
+ # This would only allow AppIdentity version 2.
22
+ class AppIdentity
23
+ # The version of the AppIdentity library.
24
+ VERSION = "1.0.0"
25
+ # The name of this implementation for App Identity.
26
+ NAME = "AppIdentity for Ruby"
27
+ # The spec version supported in this library.
28
+ SPEC_VERSION = 4
29
+
30
+ # The name, version, and supported specification version of this App Identity
31
+ # package for Ruby.
32
+ INFO = {name: NAME, version: VERSION, spec_version: SPEC_VERSION}.freeze
33
+ end
34
+
35
+ require "app_identity/app"
36
+ require "app_identity/error"
37
+ require "app_identity/internal"
38
+ require "app_identity/validation"
39
+ require "app_identity/versions"
40
+
41
+ class AppIdentity
42
+ class << self
43
+ # Generate an identity proof string for the given application. Returns the
44
+ # generated proof or `nil`.
45
+ #
46
+ # If `nonce` is provided, it must conform to the shape expected by the proof
47
+ # version. If not provided, it will be generated.
48
+ #
49
+ # If `version` is provided, it will be used to generate the nonce and the
50
+ # proof. This will allow a lower level application to raise its version level.
51
+ #
52
+ # If there is *any* error during the generation of the identity proof
53
+ # string, `nil` will be returned.
54
+ #
55
+ # ### Examples
56
+ #
57
+ # A version 1 app can have a fixed nonce, which will always produce the same
58
+ # value.
59
+ #
60
+ # ```ruby
61
+ # app = AppIdentity::App.new({version: 1, id: "decaf", secret: "bad"})
62
+ # AppIdentity.generate_proof(app, nonce: "hello")
63
+ # # => "ZGVjYWY6aGVsbG86RDNGNjJCQTYyOEIyMzhEOTgwM0MyNEU4NkNCOTY3M0ZEOTVCNTdBNkJGOTRFMkQ2NTMxQTRBODg1OTlCMzgzNQ=="
64
+ # ```
65
+ #
66
+ # A version 2 app fails when given a non-timestamp nonce.
67
+ #
68
+ # ```ruby
69
+ # AppIdentity.generate_proof(v1(), version: 2, nonce: "hello")
70
+ # # => nil
71
+ # ```
72
+ #
73
+ # A version 2 app _cannot_ generate a version 1 nonce.
74
+ #
75
+ # ```ruby
76
+ # AppIdentity.generate_proof(v2(), version: 1)
77
+ # # => nil
78
+ # ```
79
+ #
80
+ # A version 2 app will be rejected if the version has been disallowed.
81
+ #
82
+ # ```ruby
83
+ # AppIdentity.generate_proof(v2(), disallowed: [1, 2])
84
+ # # => nil
85
+ # ```
86
+ #
87
+ # Note that the optional `disallowed` parameter is *in addition* to the
88
+ # global `disallowed` configuration.
89
+ def generate_proof(app, nonce: nil, version: nil, disallowed: nil)
90
+ internal.generate_proof!(app, nonce: nonce, version: version, disallowed: disallowed)
91
+ rescue
92
+ nil
93
+ end
94
+
95
+ # Generate an identity proof string for the given application. Returns the
96
+ # generated proof or raises an exception
97
+ #
98
+ # If `nonce` is provided, it must conform to the shape expected by the proof
99
+ # version. If not provided, it will be generated.
100
+ #
101
+ # If `version` is provided, it will be used to generate the nonce and the
102
+ # proof. This will allow a lower level application to raise its version level.
103
+ #
104
+ # If there is *any* error during the generation of the identity proof
105
+ # string, an exception will be raised.
106
+ #
107
+ # ### Examples
108
+ #
109
+ # A version 1 app can have a fixed nonce, which will always produce the same
110
+ # value.
111
+ #
112
+ # ```ruby
113
+ # app = AppIdentity::App.new({version: 1, id: "decaf", secret: "bad"})
114
+ # AppIdentity.generate_proof!(app, nonce: "hello")
115
+ # # => "ZGVjYWY6aGVsbG86RDNGNjJCQTYyOEIyMzhEOTgwM0MyNEU4NkNCOTY3M0ZEOTVCNTdBNkJGOTRFMkQ2NTMxQTRBODg1OTlCMzgzNQ=="
116
+ # ```
117
+ #
118
+ # A version 2 app fails when given a non-timestamp nonce.
119
+ #
120
+ # ```ruby
121
+ # AppIdentity.generate_proof!(v1(), version: 2, nonce: "hello")
122
+ # # => nil
123
+ # ```
124
+ #
125
+ # A version 2 app _cannot_ generate a version 1 nonce.
126
+ #
127
+ # ```ruby
128
+ # AppIdentity.generate_proof!(v2(), version: 1)
129
+ # # => nil
130
+ # ```
131
+ #
132
+ # A version 2 app will be rejected if the version has been disallowed.
133
+ #
134
+ # ```ruby
135
+ # AppIdentity.generate_proof!(v2(), disallowed: [1, 2])
136
+ # # => nil
137
+ # ```
138
+ #
139
+ # Note that the optional `disallowed` parameter is *in addition* to the
140
+ # global `disallowed` configuration.
141
+ def generate_proof!(app, nonce: nil, version: nil, disallowed: nil)
142
+ internal.generate_proof!(app, nonce: nonce, version: version, disallowed: disallowed)
143
+ rescue
144
+ raise AppIdentity::Error, :generate_proof
145
+ end
146
+
147
+ # Parses a proof string and returns a Hash containing the proof parts or `nil`
148
+ # if the proof is cannot be determined as valid.
149
+ def parse_proof(proof)
150
+ internal.parse_proof!(proof)
151
+ rescue
152
+ nil
153
+ end
154
+
155
+ # Parses a proof string and returns a Hash containing the proof parts or
156
+ # raises an exception if the proof is cannot be determined as valid.
157
+ def parse_proof!(proof)
158
+ internal.parse_proof!(proof)
159
+ rescue
160
+ raise AppIdentity::Error, :parse_proof
161
+ end
162
+
163
+ # Verify a `AppIdentity` proof value using a a provided `app`. Returns the
164
+ # validated app or `nil`.
165
+ #
166
+ # The `proof` may be provided either as a string or a parsed proof (from
167
+ # `#parse_proof`). String proof values are usually obtained from HTTP
168
+ # headers. At Kinetic Commerce, this has generally jeen `KCS-Application` or
169
+ # `KCS-Service`.
170
+ #
171
+ # The `app` can be provided as an AppIdentity::App, a valid input to
172
+ # AppIdentity::App.new, or an finder callable that accepts a single
173
+ # parameter—the parsed proof value—and returns an AppIdentity::App input.
174
+ #
175
+ # ```ruby
176
+ # AppIdentity.verify_proof(proof, ->(proof) {
177
+ # IdentityApplications.get(proof[:id]
178
+ # }))
179
+ # ```
180
+ #
181
+ # Note that the optional `disallowed` parameter is *in addition* to the
182
+ # global `disallowed` configuration.
183
+ def verify_proof(proof, app, disallowed: nil)
184
+ internal.verify_proof!(proof, app, disallowed: disallowed)
185
+ rescue
186
+ nil
187
+ end
188
+
189
+ # Verify a `AppIdentity` proof value using a a provided `app`. Returns the
190
+ # validated app, `nil`, or raises an exception on error.
191
+ #
192
+ # The `proof` may be provided either as a string or a parsed proof (from
193
+ # `#parse_proof`). String proof values are usually obtained from HTTP
194
+ # headers. At Kinetic Commerce, this has generally jeen `KCS-Application` or
195
+ # `KCS-Service`.
196
+ #
197
+ # The `app` can be provided as an AppIdentity::App, a valid input to
198
+ # AppIdentity::App.new, or an finder callable that accepts a single
199
+ # parameter—the parsed proof value—and returns an AppIdentity::App input.
200
+ #
201
+ # ```ruby
202
+ # AppIdentity.verify_proof!(proof, ->(proof) {
203
+ # IdentityApplications.get(proof[:id]
204
+ # }))
205
+ # ```
206
+ #
207
+ # Note that the optional `disallowed` parameter is *in addition* to the
208
+ # global `disallowed` configuration.
209
+ def verify_proof!(proof, app, disallowed: nil)
210
+ internal.verify_proof!(proof, app, disallowed: disallowed)
211
+ rescue
212
+ raise AppIdentity::Error, :verify_proof
213
+ end
214
+
215
+ # Add the `versions` to the global disallowed versions list.
216
+ def disallow_version(*versions)
217
+ AppIdentity::Versions.disallow(*versions)
218
+ end
219
+
220
+ # Remove the `versions` from the global disallowed versions list.
221
+ def allow_version(*versions)
222
+ AppIdentity::Versions.allow(*versions)
223
+ end
224
+
225
+ private :new
226
+
227
+ private
228
+
229
+ def internal
230
+ Internal.instance
231
+ end
232
+ end
233
+ end
@@ -0,0 +1,168 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction, and
10
+ distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by the copyright
13
+ owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all other entities
16
+ that control, are controlled by, or are under common control with that entity.
17
+ For the purposes of this definition, "control" means (i) the power, direct or
18
+ indirect, to cause the direction or management of such entity, whether by
19
+ contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
20
+ outstanding shares, or (iii) beneficial ownership of such entity.
21
+
22
+ "You" (or "Your") shall mean an individual or Legal Entity exercising
23
+ permissions granted by this License.
24
+
25
+ "Source" form shall mean the preferred form for making modifications, including
26
+ but not limited to software source code, documentation source, and configuration
27
+ files.
28
+
29
+ "Object" form shall mean any form resulting from mechanical transformation or
30
+ translation of a Source form, including but not limited to compiled object code,
31
+ generated documentation, and conversions to other media types.
32
+
33
+ "Work" shall mean the work of authorship, whether in Source or Object form, made
34
+ available under the License, as indicated by a copyright notice that is included
35
+ in or attached to the work (an example is provided in the Appendix below).
36
+
37
+ "Derivative Works" shall mean any work, whether in Source or Object form, that
38
+ is based on (or derived from) the Work and for which the editorial revisions,
39
+ annotations, elaborations, or other modifications represent, as a whole, an
40
+ original work of authorship. For the purposes of this License, Derivative Works
41
+ shall not include works that remain separable from, or merely link (or bind by
42
+ name) to the interfaces of, the Work and Derivative Works thereof.
43
+
44
+ "Contribution" shall mean any work of authorship, including the original version
45
+ of the Work and any modifications or additions to that Work or Derivative Works
46
+ thereof, that is intentionally submitted to Licensor for inclusion in the Work
47
+ by the copyright owner or by an individual or Legal Entity authorized to submit
48
+ on behalf of the copyright owner. For the purposes of this definition,
49
+ "submitted" means any form of electronic, verbal, or written communication sent
50
+ to the Licensor or its representatives, including but not limited to
51
+ communication on electronic mailing lists, source code control systems, and
52
+ issue tracking systems that are managed by, or on behalf of, the Licensor for
53
+ the purpose of discussing and improving the Work, but excluding communication
54
+ that is conspicuously marked or otherwise designated in writing by the copyright
55
+ owner as "Not a Contribution."
56
+
57
+ "Contributor" shall mean Licensor and any individual or Legal Entity on behalf
58
+ of whom a Contribution has been received by Licensor and subsequently
59
+ incorporated within the Work.
60
+
61
+ 2. Grant of Copyright License.
62
+
63
+ Subject to the terms and conditions of this License, each Contributor hereby
64
+ grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
65
+ irrevocable copyright license to reproduce, prepare Derivative Works of,
66
+ publicly display, publicly perform, sublicense, and distribute the Work and such
67
+ Derivative Works in Source or Object form.
68
+
69
+ 3. Grant of Patent License.
70
+
71
+ Subject to the terms and conditions of this License, each Contributor hereby
72
+ grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
73
+ irrevocable (except as stated in this section) patent license to make, have
74
+ made, use, offer to sell, sell, import, and otherwise transfer the Work, where
75
+ such license applies only to those patent claims licensable by such Contributor
76
+ that are necessarily infringed by their Contribution(s) alone or by combination
77
+ of their Contribution(s) with the Work to which such Contribution(s) was
78
+ submitted. If You institute patent litigation against any entity (including
79
+ a cross-claim or counterclaim in a lawsuit) alleging that the Work or
80
+ a Contribution incorporated within the Work constitutes direct or contributory
81
+ patent infringement, then any patent licenses granted to You under this License
82
+ for that Work shall terminate as of the date such litigation is filed.
83
+
84
+ 4. Redistribution.
85
+
86
+ You may reproduce and distribute copies of the Work or Derivative Works thereof
87
+ in any medium, with or without modifications, and in Source or Object form,
88
+ provided that You meet the following conditions:
89
+
90
+ - You must give any other recipients of the Work or Derivative Works a copy of
91
+ this License; and
92
+ - You must cause any modified files to carry prominent notices stating that You
93
+ changed the files; and
94
+ - You must retain, in the Source form of any Derivative Works that You
95
+ distribute, all copyright, patent, trademark, and attribution notices from the
96
+ Source form of the Work, excluding those notices that do not pertain to any
97
+ part of the Derivative Works; and
98
+ - If the Work includes a "NOTICE" text file as part of its distribution, then
99
+ any Derivative Works that You distribute must include a readable copy of the
100
+ attribution notices contained within such NOTICE file, excluding those notices
101
+ that do not pertain to any part of the Derivative Works, in at least one of
102
+ the following places: within a NOTICE text file distributed as part of the
103
+ Derivative Works; within the Source form or documentation, if provided along
104
+ with the Derivative Works; or, within a display generated by the Derivative
105
+ Works, if and wherever such third-party notices normally appear. The contents
106
+ of the NOTICE file are for informational purposes only and do not modify the
107
+ License. You may add Your own attribution notices within Derivative Works that
108
+ You distribute, alongside or as an addendum to the NOTICE text from the Work,
109
+ provided that such additional attribution notices cannot be construed as
110
+ modifying the License.
111
+
112
+ You may add Your own copyright statement to Your modifications and may provide
113
+ additional or different license terms and conditions for use, reproduction, or
114
+ distribution of Your modifications, or for any such Derivative Works as a whole,
115
+ provided Your use, reproduction, and distribution of the Work otherwise complies
116
+ with the conditions stated in this License.
117
+
118
+ 5. Submission of Contributions.
119
+
120
+ Unless You explicitly state otherwise, any Contribution intentionally submitted
121
+ for inclusion in the Work by You to the Licensor shall be under the terms and
122
+ conditions of this License, without any additional terms or conditions.
123
+ Notwithstanding the above, nothing herein shall supersede or modify the terms of
124
+ any separate license agreement you may have executed with Licensor regarding
125
+ such Contributions.
126
+
127
+ 6. Trademarks.
128
+
129
+ This License does not grant permission to use the trade names, trademarks,
130
+ service marks, or product names of the Licensor, except as required for
131
+ reasonable and customary use in describing the origin of the Work and
132
+ reproducing the content of the NOTICE file.
133
+
134
+ 7. Disclaimer of Warranty.
135
+
136
+ Unless required by applicable law or agreed to in writing, Licensor provides the
137
+ Work (and each Contributor provides its Contributions) on an "AS IS" BASIS,
138
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
139
+ including, without limitation, any warranties or conditions of TITLE,
140
+ NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
141
+ solely responsible for determining the appropriateness of using or
142
+ redistributing the Work and assume any risks associated with Your exercise of
143
+ permissions under this License.
144
+
145
+ 8. Limitation of Liability.
146
+
147
+ In no event and under no legal theory, whether in tort (including negligence),
148
+ contract, or otherwise, unless required by applicable law (such as deliberate
149
+ and grossly negligent acts) or agreed to in writing, shall any Contributor be
150
+ liable to You for damages, including any direct, indirect, special, incidental,
151
+ or consequential damages of any character arising as a result of this License or
152
+ out of the use or inability to use the Work (including but not limited to
153
+ damages for loss of goodwill, work stoppage, computer failure or malfunction, or
154
+ any and all other commercial damages or losses), even if such Contributor has
155
+ been advised of the possibility of such damages.
156
+
157
+ 9. Accepting Warranty or Additional Liability.
158
+
159
+ While redistributing the Work or Derivative Works thereof, You may choose to
160
+ offer, and charge a fee for, acceptance of support, warranty, indemnity, or
161
+ other liability obligations and/or rights consistent with this License. However,
162
+ in accepting such obligations, You may act only on Your own behalf and on Your
163
+ sole responsibility, not on behalf of any other Contributor, and only if You
164
+ agree to indemnify, defend, and hold each Contributor harmless for any liability
165
+ incurred by, or claims asserted against, such Contributor by reason of your
166
+ accepting any such warranty or additional liability.
167
+
168
+ END OF TERMS AND CONDITIONS
data/licences/DCO.txt ADDED
@@ -0,0 +1,34 @@
1
+ Developer Certificate of Origin
2
+ Version 1.1
3
+
4
+ Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
5
+
6
+ Everyone is permitted to copy and distribute verbatim copies of this
7
+ license document, but changing it is not allowed.
8
+
9
+
10
+ Developer's Certificate of Origin 1.1
11
+
12
+ By making a contribution to this project, I certify that:
13
+
14
+ (a) The contribution was created in whole or in part by me and I
15
+ have the right to submit it under the open source license
16
+ indicated in the file; or
17
+
18
+ (b) The contribution is based upon previous work that, to the best
19
+ of my knowledge, is covered under an appropriate open source
20
+ license and I have the right under that license to submit that
21
+ work with modifications, whether created in whole or in part
22
+ by me, under the same open source license (unless I am
23
+ permitted to submit under a different license), as indicated
24
+ in the file; or
25
+
26
+ (c) The contribution was provided directly to me by some other
27
+ person who certified (a), (b) or (c) and I have not modified
28
+ it.
29
+
30
+ (d) I understand and agree that this project and the contribution
31
+ are public and that a record of the contribution (including all
32
+ personal information I submit with it, including my sign-off) is
33
+ maintained indefinitely and may be redistributed consistent with
34
+ this project or the open source license(s) involved.