ruby-paseto 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +8 -0
  3. data/CODE_OF_CONDUCT.md +84 -0
  4. data/LICENSE.txt +21 -0
  5. data/README.md +549 -0
  6. data/lib/paseto/asn1/algorithm_identifier.rb +17 -0
  7. data/lib/paseto/asn1/curve_private_key.rb +22 -0
  8. data/lib/paseto/asn1/ec_private_key.rb +27 -0
  9. data/lib/paseto/asn1/ecdsa_full_r.rb +26 -0
  10. data/lib/paseto/asn1/ecdsa_sig_value.rb +23 -0
  11. data/lib/paseto/asn1/ecdsa_signature.rb +49 -0
  12. data/lib/paseto/asn1/ed25519_identifier.rb +15 -0
  13. data/lib/paseto/asn1/named_curve.rb +17 -0
  14. data/lib/paseto/asn1/one_asymmetric_key.rb +32 -0
  15. data/lib/paseto/asn1/private_key.rb +17 -0
  16. data/lib/paseto/asn1/private_key_algorithm_identifier.rb +17 -0
  17. data/lib/paseto/asn1/public_key.rb +17 -0
  18. data/lib/paseto/asn1/subject_public_key_info.rb +28 -0
  19. data/lib/paseto/asn1.rb +101 -0
  20. data/lib/paseto/asymmetric_key.rb +100 -0
  21. data/lib/paseto/configuration/box.rb +23 -0
  22. data/lib/paseto/configuration/decode_configuration.rb +68 -0
  23. data/lib/paseto/configuration.rb +18 -0
  24. data/lib/paseto/interface/i_d.rb +23 -0
  25. data/lib/paseto/interface/key.rb +113 -0
  26. data/lib/paseto/interface/pbkd.rb +83 -0
  27. data/lib/paseto/interface/pie.rb +59 -0
  28. data/lib/paseto/interface/pke.rb +86 -0
  29. data/lib/paseto/interface/serializer.rb +19 -0
  30. data/lib/paseto/interface/version.rb +161 -0
  31. data/lib/paseto/interface/wrapper.rb +20 -0
  32. data/lib/paseto/operations/i_d.rb +48 -0
  33. data/lib/paseto/operations/id/i_dv3.rb +20 -0
  34. data/lib/paseto/operations/id/i_dv4.rb +20 -0
  35. data/lib/paseto/operations/pbkd/p_b_k_dv3.rb +85 -0
  36. data/lib/paseto/operations/pbkd/p_b_k_dv4.rb +94 -0
  37. data/lib/paseto/operations/pbkw.rb +73 -0
  38. data/lib/paseto/operations/pke/p_k_ev3.rb +97 -0
  39. data/lib/paseto/operations/pke/p_k_ev4.rb +95 -0
  40. data/lib/paseto/operations/pke.rb +57 -0
  41. data/lib/paseto/operations/wrap.rb +29 -0
  42. data/lib/paseto/paserk.rb +55 -0
  43. data/lib/paseto/paserk_types.rb +46 -0
  44. data/lib/paseto/protocol/version3.rb +100 -0
  45. data/lib/paseto/protocol/version4.rb +99 -0
  46. data/lib/paseto/result.rb +9 -0
  47. data/lib/paseto/serializer/optional_json.rb +30 -0
  48. data/lib/paseto/serializer/raw.rb +23 -0
  49. data/lib/paseto/sodium/curve_25519.rb +46 -0
  50. data/lib/paseto/sodium/safe_ed25519_loader.rb +19 -0
  51. data/lib/paseto/sodium/stream/base.rb +82 -0
  52. data/lib/paseto/sodium/stream/x_cha_cha20_xor.rb +31 -0
  53. data/lib/paseto/sodium.rb +5 -0
  54. data/lib/paseto/symmetric_key.rb +119 -0
  55. data/lib/paseto/token.rb +127 -0
  56. data/lib/paseto/token_types.rb +29 -0
  57. data/lib/paseto/util.rb +105 -0
  58. data/lib/paseto/v3/local.rb +63 -0
  59. data/lib/paseto/v3/public.rb +204 -0
  60. data/lib/paseto/v4/local.rb +56 -0
  61. data/lib/paseto/v4/public.rb +169 -0
  62. data/lib/paseto/validator.rb +154 -0
  63. data/lib/paseto/verifiers/footer.rb +30 -0
  64. data/lib/paseto/verifiers/payload.rb +42 -0
  65. data/lib/paseto/verify.rb +48 -0
  66. data/lib/paseto/version.rb +6 -0
  67. data/lib/paseto/versions.rb +25 -0
  68. data/lib/paseto/wrappers/pie/pie_v3.rb +72 -0
  69. data/lib/paseto/wrappers/pie/pie_v4.rb +72 -0
  70. data/lib/paseto/wrappers/pie.rb +71 -0
  71. data/lib/paseto.rb +99 -0
  72. data/paseto.gemspec +58 -0
  73. data/sorbet/config +3 -0
  74. data/sorbet/rbi/annotations/rainbow.rbi +269 -0
  75. data/sorbet/rbi/gems/ast@2.4.2.rbi +584 -0
  76. data/sorbet/rbi/gems/diff-lcs@1.5.0.rbi +1083 -0
  77. data/sorbet/rbi/gems/docile@1.4.0.rbi +376 -0
  78. data/sorbet/rbi/gems/ffi@1.15.5.rbi +1994 -0
  79. data/sorbet/rbi/gems/io-console@0.5.11.rbi +8 -0
  80. data/sorbet/rbi/gems/irb@1.5.1.rbi +342 -0
  81. data/sorbet/rbi/gems/json@2.6.3.rbi +1541 -0
  82. data/sorbet/rbi/gems/multi_json@1.15.0.rbi +267 -0
  83. data/sorbet/rbi/gems/netrc@0.11.0.rbi +158 -0
  84. data/sorbet/rbi/gems/oj@3.13.23.rbi +603 -0
  85. data/sorbet/rbi/gems/openssl@3.0.1.rbi +1735 -0
  86. data/sorbet/rbi/gems/parallel@1.22.1.rbi +277 -0
  87. data/sorbet/rbi/gems/rainbow@3.1.1.rbi +407 -0
  88. data/sorbet/rbi/gems/rake@13.0.6.rbi +3021 -0
  89. data/sorbet/rbi/gems/rbnacl@7.1.1.rbi +3218 -0
  90. data/sorbet/rbi/gems/regexp_parser@2.6.1.rbi +3481 -0
  91. data/sorbet/rbi/gems/reline@0.3.1.rbi +8 -0
  92. data/sorbet/rbi/gems/rexml@3.2.5.rbi +4717 -0
  93. data/sorbet/rbi/gems/rspec-core@3.12.0.rbi +10887 -0
  94. data/sorbet/rbi/gems/rspec-expectations@3.12.0.rbi +8090 -0
  95. data/sorbet/rbi/gems/rspec-mocks@3.12.0.rbi +5300 -0
  96. data/sorbet/rbi/gems/rspec-support@3.12.0.rbi +1617 -0
  97. data/sorbet/rbi/gems/rspec@3.12.0.rbi +88 -0
  98. data/sorbet/rbi/gems/ruby-progressbar@1.11.0.rbi +1239 -0
  99. data/sorbet/rbi/gems/simplecov-html@0.12.3.rbi +219 -0
  100. data/sorbet/rbi/gems/simplecov@0.21.2.rbi +2135 -0
  101. data/sorbet/rbi/gems/simplecov_json_formatter@0.1.4.rbi +8 -0
  102. data/sorbet/rbi/gems/thor@1.2.1.rbi +3956 -0
  103. data/sorbet/rbi/gems/timecop@0.9.6.rbi +350 -0
  104. data/sorbet/rbi/gems/unicode-display_width@2.3.0.rbi +48 -0
  105. data/sorbet/rbi/gems/webrick@1.7.0.rbi +2555 -0
  106. data/sorbet/rbi/gems/yard-sorbet@0.7.0.rbi +391 -0
  107. data/sorbet/rbi/gems/yard@0.9.28.rbi +17816 -0
  108. data/sorbet/rbi/gems/zeitwerk@2.6.6.rbi +950 -0
  109. data/sorbet/rbi/shims/multi_json.rbi +19 -0
  110. data/sorbet/rbi/shims/openssl.rbi +111 -0
  111. data/sorbet/rbi/shims/rbnacl.rbi +65 -0
  112. data/sorbet/rbi/shims/zeitwerk.rbi +6 -0
  113. data/sorbet/rbi/todo.rbi +7 -0
  114. data/sorbet/tapioca/config.yml +30 -0
  115. data/sorbet/tapioca/require.rb +12 -0
  116. metadata +376 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 17a0cc0d3441ffa880aacd8cda52257eac34ae0030b6420f86505e7177ed641c
4
+ data.tar.gz: 1c97be78e2b9580e60643d492a0d590c9e89ec19e59a5c0fcface329c0a10d94
5
+ SHA512:
6
+ metadata.gz: ad05a5d5e3580589ff8bdb3da032858e5af522458552ce86121c823efa267e3ae790d703ea0ce286dfeaf0a17fc3785df64de4114c37d05ac325f65788b77887
7
+ data.tar.gz: dc99f930cac852930a117c5df2c0a7bea7139e23a816742e04d642c415cd0ff9e8ca9d93760c53e3aad35ff707341f213dca33601db36ca0a615ddf1daf263c9
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-12-13
4
+
5
+ - Initial release of ruby-paseto
6
+ - Full support for PASETO tokens
7
+ - Full support for PASERK extensions
8
+ - Entire library, except for custom FFI bindings, uses typed: strict
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at TODO: Write your email address. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Joe Truba
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,549 @@
1
+ # Paseto
2
+ [![Ruby Style Guide](https://img.shields.io/badge/code_style-community-brightgreen.svg)](https://rubystyle.guide) [![CircleCI](https://dl.circleci.com/status-badge/img/gh/bannable/paseto/tree/main.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/gh/bannable/paseto/tree/main) [![Maintainability](https://api.codeclimate.com/v1/badges/0bc8fcc6751880b68a9c/maintainability)](https://codeclimate.com/github/bannable/paseto/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/0bc8fcc6751880b68a9c/test_coverage)](https://codeclimate.com/github/bannable/paseto/test_coverage)
3
+
4
+ This is an implementation of the [PASETO token protocol](https://github.com/paseto-standard/paseto-spec), written in Ruby, which supports versions [v3](https://github.com/paseto-standard/paseto-spec/tree/master/docs/01-Protocol-Versions#version-3-nist-modern) and [v4](https://github.com/paseto-standard/paseto-spec/tree/master/docs/01-Protocol-Versions#version-4-sodium-modern). This library passes all [official test vectors](https://github.com/paseto-standard/test-vectors) for supported versions and purposes.
5
+
6
+ Additionally, the library uses [Sorbet](https://sorbet.org) to enforce types at runtime.
7
+
8
+ ## Installing
9
+
10
+ ### RbNaCl and libsodium
11
+
12
+ **Optional for v4.local tokens support**: Handling v4.local tokens requires [`RbNaCl`](https://github.com/RubyCrypto/rbnacl) with `libsodium 1.0.0` or newer.
13
+
14
+ You can find instructions for obtaining libsodium at https://libsodium.org.
15
+
16
+ If you do not intend to create or parse `v4.local` tokens, feel free to skip this dependency.
17
+
18
+ ### Using Bundler:
19
+
20
+ Add the following to your Gemfile:
21
+ ```
22
+ gem 'ruby-paseto'
23
+ # and optionally:
24
+ gem 'rbnacl', '~> 7.1.1'
25
+ ```
26
+
27
+ Then, run `bundle install` and `require 'paseto'`.
28
+
29
+ ## Supported PASETO versions
30
+
31
+ `paseto` supports these PASETO versions and purposes:
32
+ | purpose | v4 | v3 |
33
+ | ---------| ---- | ---- |
34
+ | `local` | ✅ | ✅ |
35
+ | `public` | ✅ | ✅ |
36
+
37
+ ## Support for PASERK types
38
+
39
+ | | v4 | v3 |
40
+ | ------------- | ---- | ---- |
41
+ | [`lid`](https://github.com/paseto-standard/paserk/blob/8cc4934687a3c9235387d005fb79eec33f43166d/types/lid.md) | ✅ | ✅ |
42
+ | [`sid`](https://github.com/paseto-standard/paserk/blob/8cc4934687a3c9235387d005fb79eec33f43166d/types/sid.md) | ✅ | ✅ |
43
+ | [`pid`](https://github.com/paseto-standard/paserk/blob/8cc4934687a3c9235387d005fb79eec33f43166d/types/pid.md) | ✅ | ✅ |
44
+ | [`local`](https://github.com/paseto-standard/paserk/blob/8cc4934687a3c9235387d005fb79eec33f43166d/types/local.md) | ✅ | ✅ |
45
+ | [`secret`](https://github.com/paseto-standard/paserk/blob/8cc4934687a3c9235387d005fb79eec33f43166d/types/secret.md) | ✅ | ✅ |
46
+ | [`public`](https://github.com/paseto-standard/paserk/blob/8cc4934687a3c9235387d005fb79eec33f43166d/types/public.md) | ✅ | ✅ |
47
+ | [`seal`](https://github.com/paseto-standard/paserk/blob/8cc4934687a3c9235387d005fb79eec33f43166d/types/seal.md) | ✅ | ✅ |
48
+ | [`local-wrap`](https://github.com/paseto-standard/paserk/blob/8cc4934687a3c9235387d005fb79eec33f43166d/types/local-wrap.md) | ✅ | ✅ |
49
+ | [`secret-wrap`](https://github.com/paseto-standard/paserk/blob/8cc4934687a3c9235387d005fb79eec33f43166d/types/secret-wrap.md) | ✅ | ✅ |
50
+ | [`local-pw`](https://github.com/paseto-standard/paserk/blob/8cc4934687a3c9235387d005fb79eec33f43166d/types/local-pw.md) | ✅ | ✅ |
51
+ | [`secret-pw`](https://github.com/paseto-standard/paserk/blob/8cc4934687a3c9235387d005fb79eec33f43166d/types/secret-pw.md) | ✅ | ✅ |
52
+
53
+ ## Implementation Guideline compliance
54
+
55
+ - [x] require payload to be UTF-8 encoded
56
+ - [x] enforce JSON encoding of payload
57
+ - [x] require topmost object to be an object, map, or associative array
58
+ - [x] protect against loading off-curve public keys
59
+ - [x] support for [Validators](https://github.com/paseto-standard/paseto-spec/blob/master/docs/02-Implementation-Guide/02-Validators.md)
60
+ - [x] protect against arbitrary/invalid data in [Registered Claims](https://github.com/paseto-standard/paseto-spec/blob/master/docs/02-Implementation-Guide/04-Claims.md)
61
+ - [x] [Key-ID support](https://github.com/paseto-standard/paseto-spec/blob/master/docs/02-Implementation-Guide/01-Payload-Processing.md#key-id-support)
62
+
63
+ ## Basic Usage
64
+
65
+ ```ruby
66
+ claims = { "foo" => "bar", "baz" => 1, "time" => Time.now }
67
+ symmetric = Paseto::V4::Local.generate
68
+ asymmetric = Paseto::V4::Public.generate
69
+
70
+ footer = {'kid' => asymmetric.pid}
71
+ signed_token = asymmetric.encode(claims, footer: footer)
72
+ # => "v4.public.eyJ..."
73
+
74
+ result = asymmetric.decode(signed_token)
75
+ # => <Paseto::Result claims={"exp"=>"2022-12-...", "iat"=>"2022-12-...",
76
+ # "nbf"=>"2022-12-...", "foo"=>"bar", "baz"=>1, "time"=>"2022-12-..."},
77
+ # footer={"kid"=>"k4.pid.Mu7prut6-zPCIkJ..."}>
78
+
79
+ result.claims == claims
80
+ # => false
81
+
82
+ result.claims['foo'] == 'bar'
83
+ # => true
84
+
85
+ # To opt out of default claims use `encode!`
86
+ # When using encode!, footer must be a string if provided
87
+ encrypted_token = symmetric.encode!(claims, footer: JSON.dump(footer))
88
+ # => "v4.local.aM0k..."
89
+
90
+ result = symmetric.decode(encrypted_token)
91
+ # => <Paseto::Result claims={"foo"=>"bar", "baz"=>1, "time"=>"2022-12-..."},
92
+ # footer={"kid"=>"k4.pid.h8fe-zLYEss_..."}>
93
+
94
+ result.claims == claims
95
+ # => true
96
+ ```
97
+
98
+ The `encode` and `decode` methods always comply with the [PASETO Payload Processing guidelines](https://github.com/paseto-standard/paseto-spec/blob/master/docs/02-Implementation-Guide/01-Payload-Processing.md).
99
+
100
+ `paseto` uses `multi_json` to handle JSON serialization and deserialization, so you may configure your adapter as you please.
101
+
102
+ **Symbol keys are always converted to strings.** Serializer configurations or options which cause keys to decode as non-string values, such as `symbolize_keys`, or cause registered claim values to decode as non-strings, are unsupported.
103
+
104
+ ```ruby
105
+ encrypter.decode(encrypted_token)
106
+ # => {"foo" => "bar", "baz" => {"^t"=>"2022-12-08T20:26:55.213+00:00"}}
107
+
108
+ # You may pass JSON adapter options to encode and decode
109
+ encrypter.decode(encrypted_token, mode: :object)
110
+ # => {"foo" => "bar", "baz" => 2022-12-08 20:26:55 15023727/70368744177664 +0000}
111
+
112
+ # Or setting default options with Oj
113
+ Oj.default_options = {mode: :object}
114
+ encrypter.decode(encrypted_token)
115
+ # => {"foo" => "bar", "baz" => 2022-12-08 20:26:55 15023727/70368744177664 +0000}
116
+ ```
117
+
118
+ See [Registered Claims](#registered-claims) for more information on registered claim validation.
119
+
120
+ ### Encryption and Decryption
121
+
122
+ These operations are performed with `Local` key instances, which are always initialized with a 256-bit raw key.
123
+
124
+ ```ruby
125
+ crypt = Paseto::V4::Local.generate
126
+ token = crypt.encode({'foo' => 'bar'}) # => "v4.local.DhLZ..." (local PASETO)
127
+ token = crypt.encode({'foo' => 'bar'},
128
+ footer: '',
129
+ implicit_assertion: ''
130
+ ) # same as above
131
+ result = crypt.decode(token) # => <Paseto::Result ...>
132
+ result.claims # => {'foo' => 'bar'}
133
+
134
+ # exporting key material
135
+ crypt.key # => The IKM used for this key as raw bytes
136
+ crypt.lid # => "k4.lid.uGj..." (PASERK type lid)
137
+ crypt.id # => same as above
138
+ crypt.paserk # => "k4.local.tnVpN4t..." (PASERK Type local)
139
+ ```
140
+
141
+ ### Message Signing and Verification
142
+
143
+ These options are performed with `Public` key instances, which may be initialized with a DER- or PEM-encoded public or private key.
144
+
145
+ ```ruby
146
+ signer = Paseto::V4::Public.generate
147
+ token = signer.encode({'foo' => 'bar'}) # => "v4.public.sKd3..." (public PASETO)
148
+ token = signer.encode(
149
+ {'foo' => 'bar'},
150
+ footer: '',
151
+ implicit_assertion: ''
152
+ ) # same as above
153
+ result = signer.decode(token) # => <Paseto::Result ...>
154
+ result.claims # => {'foo' => 'bar'}
155
+
156
+ # or initialize only for verification
157
+ verifier = Paseto::V4::Public.new(signer.public_to_pem)
158
+ verifier.decode(token) # => <Paseto::Result ...>
159
+ verifier.encode({'foo' => 'bar'}) # => ArgumentError
160
+
161
+ # exporting key material
162
+ signer.public_to_pem # => PEM
163
+ signer.private_to_pem # => PEM if private material available, otherwise ArgumentError
164
+ signer.sid # => "k4.sid.y5x..." (PASERK Type sid)
165
+ signer.id # => same as above
166
+ signer.pid # => "k4.pid.5g4..." (PASERK Type pid)
167
+ verifier.pid # => same as above!
168
+ verifier.id # => same as above
169
+ ```
170
+ ## PASETO/PASERK v4, Sodium Modern
171
+
172
+ [Description](https://github.com/paseto-standard/paseto-spec/tree/master/docs/01-Protocol-Versions#version-4-nist-modern) and [Specification](https://github.com/paseto-standard/paseto-spec/blob/master/docs/01-Protocol-Versions/Version4.md).
173
+
174
+ The `rbnacl` gem is required for access to any v4 algorithm.
175
+
176
+ ### Encryption and Decryption
177
+
178
+ ```ruby
179
+ crypt = Paseto::V4::Local.generate
180
+ # or initialize with a known 256-bit string
181
+ ikm = SecureRandom.bytes(32)
182
+ crypt = Paseto::V4::Local.new(ikm: ikm)
183
+ ```
184
+
185
+ ### Message Signing and Verification
186
+
187
+ ```ruby
188
+ signer = Paseto::V4::Public.generate # => Paseto::V4::Public
189
+ # or initialize from a DER- or PEM-encoded Ed25519 key
190
+ pem = File.read('my.pem')
191
+ signer = Paseto::V4::Public.new(pem)
192
+ ```
193
+
194
+ ## PASETO v3, NIST Modern
195
+
196
+ [Description](https://github.com/paseto-standard/paseto-spec/tree/master/docs/01-Protocol-Versions#version-3-nist-modern) and [Specification](https://github.com/paseto-standard/paseto-spec/blob/master/docs/01-Protocol-Versions/Version3.md).
197
+
198
+ The `openssl` gem is required for access to any v3 algorithm.
199
+
200
+ ### Encryption and Decryption
201
+
202
+ ```ruby
203
+ crypt = Paseto::V3::Local.generate
204
+ # or initialize with a known key
205
+ ikm = SecureRandom.bytes(32)
206
+ crypt = Paseto::V3::Local.new(ikm: ikm)
207
+ ```
208
+
209
+ ### Message Signing and Verification
210
+
211
+ ```ruby
212
+ signer = Paseto::V3::Public.generate # => Paseto::V3::Public
213
+ # or initialize from a PEM- or DER-encoded secp384r1 key
214
+ signer = Paseto::V3::Public.new(signer.private_to_pem)
215
+ ```
216
+
217
+ # PASERK
218
+
219
+ ## Accessing the PASETO Footer Before Decoding
220
+
221
+ `Paseto::Token` is used for decoding and accessing the different components of a PASETO, and can be used for retrieving a `kid` or `wpk` footer claim before decoding the full token.
222
+
223
+ `Token` objects respond to `#inspect` and `to_s` with the PASETO token that they represent.
224
+
225
+ ```ruby
226
+ signer = Paseto::V4::Public.generate
227
+
228
+ claims = { "foo" => "bar", "baz" => 1, "time" => Time.now }
229
+ footer = '"foo"'
230
+ signed_token = signer.encode(claims, footer: footer)
231
+
232
+ t = Paseto::Token.parse(signed_token) # => the signed_token value
233
+ t.class # => Paseto::Token
234
+ t.footer # => "\"foo\""
235
+
236
+ # You must decode! a Token with the correct Key before accessing the payload
237
+ t.payload # => Paseto::ParseError
238
+ t.decode!(signer) # => {"exp"=> ...}
239
+ t.payload # => {"exp"=> ...}
240
+
241
+ # The footer is automatically deserialized to a Hash when appropriate
242
+ signed_token = signer.encode(claims, footer: {'kid' => signer.pid})
243
+ t = Paseto::Token.parse(signed_token) # => "v4.public.fOQ2s..."
244
+
245
+ t.footer # => {"kid"=>"k4.pid.h5u7u..."}
246
+ t.footer['kid'] == signer.pid # => true
247
+ key = MyKeyStore.fetch(footer['kid'])
248
+ t.decode!(key) # => {"exp"=> ...}
249
+ ```
250
+
251
+ ## [ID](https://github.com/paseto-standard/paserk/blob/8cc4934687a3c9235387d005fb79eec33f43166d/operations/ID.md), PASERK IDs
252
+
253
+ See [Basic Usage](#basic-usage).
254
+
255
+ ## [Wrap](https://github.com/paseto-standard/paserk/blob/8cc4934687a3c9235387d005fb79eec33f43166d/operations/Wrap.md), Symmetric-Key Wrapping
256
+
257
+ Encrypt a symmetric key with another symmetric key using the PIE wrapping algorithm. Other wrapping algorithms are not supported.
258
+
259
+ ```ruby
260
+ wrapping_key = Paseto::V4::Local.generate
261
+ local_secret = Paseto::V4::Local.generate
262
+
263
+ # Encrypt local_secret with wrapping_key using the PIE algorithm
264
+ paserk = wrapping_key.wrap(local_secret) # => "k4.local-wrap.pie.8qQp..."
265
+
266
+ # Decrypt a secret-wrap or local-wrap PASERK
267
+ unwrapped = wrapping_key.unwrap(paserk) # => #<Paseto::V4::Local ...>
268
+ unwrapped == local_secret # => true
269
+ ```
270
+
271
+ ## [PKE](https://github.com/paseto-standard/paserk/blob/8cc4934687a3c9235387d005fb79eec33f43166d/operations/PKE.md), Public-Key Encryption
272
+
273
+ Encrypt a symmetric key with a public key, and decrypt with a private key.
274
+
275
+ ```ruby
276
+ wrapping_key = Paseto::V4::Public.generate
277
+ local_secret = Paseto::V4::Local.generate
278
+
279
+ # Seal a Local key with a Public key
280
+ paserk = wrapping_key.seal(local_secret) # => "k4.seal.I1Hn..."
281
+
282
+ # Unseal a sealed PASERK
283
+ unwrapped = wrapping_key.unseal(paserk) # => #<Paseto::V4::Local ...>
284
+ unwrapped == local_secret # => true
285
+ ```
286
+
287
+ ## [PBKD](https://github.com/paseto-standard/paserk/blob/8cc4934687a3c9235387d005fb79eec33f43166d/operations/PBKW.md), Password-Based Key Wrapping
288
+
289
+ **Note:** This operation is slow.
290
+
291
+ This operation encrypts any key with a password-derived encryption key.
292
+
293
+ Different parameters for the derivation may be provided depending on the version of the key being encrypted.
294
+
295
+ ```ruby
296
+ secret_key = Paseto::V4::Local.generate # May be either Local or Public
297
+
298
+ # Equivelant to :sensitive for both values
299
+ options = { opslimit: 4, memlimit: 1_073_741_824 }
300
+
301
+ paserk = secret_key.pbkd(password: 'hunter2', options: options)
302
+ # => k4.local-pw.hSmzsCmWhiT...
303
+
304
+ key = Paseto::Paserk.from_paserk(paserk: paserk, password: 'hunter2')
305
+ # => #<Paseto::V4::Local ...>
306
+
307
+ key == secret_key
308
+ # => true
309
+ ```
310
+
311
+ ### Version 4, Argon2id Parameters
312
+
313
+ | param | allowed values | default |
314
+ | :--------: | :---------: | :-----: |
315
+ | `opslimit` | Symbol \| Integer | `:interactive` |
316
+ | `memlimit` | Symbol \| Integer | `:interactive` |
317
+
318
+ Allowed symbol values are `:interactive`, `:moderate` and `:sensitive` as described in [RbNaCl documentation](https://rubydoc.info/github/rubycrypto/rbnacl/RbNaCl%2FPasswordHash%2FArgon2:initialize).
319
+
320
+ You most likely do not want to use the `:interactive` default in production. See [libsodium's documentation](https://libsodium.gitbook.io/doc/~/revisions/-LL5wcLSbESuQhMZUBZo/password_hashing/the_argon2i_function#guidelines-for-choosing-the-parameters) for parameter guidance.
321
+
322
+
323
+ ### Version 3, PBKDF2 Parameters
324
+
325
+ | param | allowed values | default |
326
+ | :--------: | :---------: | :-----: |
327
+ | `iterations` | Integer | `100_000` |
328
+
329
+ As with the Version 4 parameters above, you most likely do not want to use the default value and should pick an `iterations` value with time objectives similar to the argon2 guidance.
330
+
331
+ # Registered Claims
332
+
333
+ PASETO [reserves some claim names](https://github.com/paseto-standard/paseto-spec/blob/master/docs/02-Implementation-Guide/04-Claims.md) for particular use in the protocol, and this gem supports verification of all reserved claims.
334
+
335
+ In the default configuration, the follow claims are verified when present: `exp`, `nbf` and `iat`
336
+
337
+ Verification behavior can be controlled by passing a kwarg for the setting to `decode` calls, or through library-level configuration.
338
+
339
+ ## Default Configuration
340
+
341
+ See the appropriate section below for more information on configuring each `verify_foo`.
342
+ ```ruby
343
+ Paseto.configure do |config|
344
+ # Controls the behavior of footer deserialization in Result objects.
345
+ # Paseto::Serializer::Raw is the other built-in option,
346
+ # but is incompatible with registered claim validation.
347
+ config.footer_serializer = Paseto::Serializer::OptionalJson
348
+
349
+ config.verify_exp = true
350
+ config.verify_nbf = true
351
+ config.verify_iat = true
352
+
353
+ config.verify_iss = false
354
+ config.verify_aud = false
355
+ config.verify_sub = false
356
+ config.verify_jti = false
357
+ end
358
+ ```
359
+
360
+ ## Audience Claim
361
+
362
+ | claim type | config type | default |
363
+ | :--------: | :---------: | :-----: |
364
+ | String | False \| String \| Array[String] | `false` |
365
+
366
+ ```ruby
367
+ crypt = Paseto::V4::Local.generate
368
+ hash = { aud: 'example.com', data: 'data' }
369
+ payload = crypt.encode(hash)
370
+
371
+ audience = 'some.example.com'
372
+ crypt.decode(payload, verify_aud: audience) # => Paseto::InvalidAudience
373
+
374
+ audience = ['some.example.com', 'another.example.com', 'example.com']
375
+ crypt.decode(payload, verify_aud: audience) # => { 'aud' => ... }
376
+ ```
377
+
378
+ ## Expiration Claim
379
+
380
+ | claim type | config type | default |
381
+ | :--------: | :---------: | :-----: |
382
+ | DateTime | Boolean | `true` |
383
+
384
+ ```ruby
385
+ crypt = Paseto::V4::Local.generate
386
+ # Override the default value by specifying a new value in your hash
387
+ hash = { exp: (Time.now - 1).iso8601, data: 'data' }
388
+ payload = crypt.encode(hash)
389
+
390
+ crypt.decode(payload) # => Paseto::ExpiredToken
391
+
392
+ crypt.decode(payload, verify_exp: false) # { 'exp' => ... }
393
+ ```
394
+
395
+ ## Issued At Claim
396
+
397
+ | claim type | config type | default |
398
+ | :--------: | :---------: | :-----: |
399
+ | DateTime | Boolean | `true` |
400
+
401
+ ```ruby
402
+ crypt = Paseto::V4::Local.generate
403
+ # Override the default value by specifying a new value in your hash
404
+ hash = { iat: (Time.now + 5).iso8601, data: 'data' }
405
+ payload = crypt.encode(payload: hash)
406
+
407
+ crypt.decode(payload) # => Paseto::ImmatureToken
408
+
409
+ crypt.decode(payload, verify_iat: false) # { 'iat' => ... }
410
+ ```
411
+
412
+ ### Issuer Claim
413
+
414
+ | claim type | config type | default |
415
+ | :--------: | :---------: | :-----: |
416
+ | String | Boolean \| String \| Regexp \| Proc | `false` |
417
+
418
+ ```ruby
419
+ crypt = Paseto::V4::Local.generate
420
+ hash = { iss: 'example.com', data: 'data' }
421
+ payload = crypt.encode(payload: hash)
422
+
423
+ crypt.decode(payload, verify_iss: 'not.example.com') # => Paseto::InvalidIssuer
424
+ ```
425
+
426
+ You may also pass a Regexp or Proc with arity 1, and verification will succeed if the regexp matches or the proc returns truthy.
427
+
428
+ ```ruby
429
+ issuer = /\Aexample\.com\z/
430
+ crypt.decode(payload, verify_issuer: issuer) # { 'iss' => ... }
431
+
432
+ issuer_proc = ->(iss) { iss.end_with?('example.com') }
433
+ crypt.decode(payload, verify_issuer: issuer_proc) # { 'iss' => ... }
434
+
435
+ # or verify only presence
436
+ crypt.decode(payload, verify_issuer: true) # { 'iss' => ... }
437
+ ```
438
+
439
+ ### Not Before Claim
440
+
441
+ | claim type | config type | default |
442
+ | :--------: | :---------: | :-----: |
443
+ | DateTime | Boolean | `true` |
444
+
445
+ ```ruby
446
+ crypt = Paseto::V4::Local.generate
447
+ # Override the default value by specifying a new value in your hash
448
+ hash = { nbf: (Time.now + 5).iso8601, data: 'data' }
449
+ payload = crypt.encode(payload: hash)
450
+
451
+ crypt.decode(payload) # => Paseto::InactiveToken
452
+ crypt.decode(payload, verify_nbf: false) # => { 'nbf' => ... }
453
+ ```
454
+
455
+ ### Subject Claim
456
+
457
+ | claim type | config type | default |
458
+ | :--------: | :---------: | :-----: |
459
+ | String | False \| String | `false` |
460
+
461
+ ```ruby
462
+ crypt = Paseto::V4::Local.generate
463
+ hash = { sub: 'example.com', data: 'data' }
464
+ payload = crypt.encode(payload: hash)
465
+
466
+ crypt.decode(payload, verify_sub: 'example.com') # { 'sub' => ... }
467
+ crypt.decode(payload, verify_sub: 'example.org') # => Paseto::InvalidSubject
468
+ ```
469
+
470
+ ### Token Identifier Claim
471
+
472
+ | claim type | config type | default |
473
+ | :--------: | :---------: | :-----: |
474
+ | String | Boolean \| String \| Proc | `false` |
475
+
476
+ ```ruby
477
+ crypt = Paseto::V4::Local.generate
478
+ hash = { data: 'data' }
479
+ payload = crypt.encode(hash)
480
+
481
+ # Require presence
482
+ crypt.decode(payload, verify_jti: true)) # => Paseto::InvalidTokenIdentifier
483
+
484
+ # Require exact value
485
+ hash[:jti] = 'foo'
486
+ payload = crypt.encode(payload: hash)
487
+ crypt.decode(payload, verify_jti: 'foo')) # { 'data' => ... }
488
+ crypt.decode(payload, verify_jti: 'bar')) # Paseto::InvalidTokenIdentifier
489
+
490
+ # Or something more complex
491
+ jti_proc = ->(jti) { jti == 'bar'}
492
+ crypt.decode(payload, verify_jti: jti_proc)) # Paseto::InvalidTokenIdentifier
493
+ ```
494
+
495
+ ## Development
496
+
497
+ This repository includes a [VSCode DevContainer](.devcontainer) configuration which automatically includes extensions for both Sorbet and Solargraph, and configures a docker image with libsodium.
498
+
499
+ After checking out the repo, run `bin/setup` to install dependencies. If you are using the provided DevContainer, this happens automatically after the container image is first created.
500
+
501
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
502
+
503
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
504
+
505
+ ### Type Checking
506
+
507
+ `paseto` uses `sorbet` to provide both static and runtime type checking.
508
+
509
+ You can learn more over at the `sorbet` [documentation](https://sorbet.org/docs/overview).
510
+
511
+ ### Running Tests
512
+
513
+ The tests are written with rspec. [Appraisal](https://github.com/thoughtbot/appraisal) is used to ensure compatibility with 3rd party dependencies providing cryptographic features.
514
+
515
+ ```
516
+ bundle install
517
+ appraisal install
518
+ # in parallel
519
+ appraisal rake
520
+ # or not
521
+ appraisal rake specs
522
+ ```
523
+
524
+ ### Updating RBI Files
525
+
526
+ To check that RBI files for gems are up-to-date:
527
+ ```
528
+ appraisal rbnacl bin/tapioca gems --verify
529
+ ```
530
+
531
+ To update RBI files for gems:
532
+ ```
533
+ appraisal rbnacl bin/tapioca gems
534
+ appraisal rbnacl bin/tapioca annotations
535
+ ```
536
+
537
+ ## Contributing
538
+
539
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bannable/paseto.
540
+
541
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT.md).
542
+
543
+ ## License
544
+
545
+ The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
546
+
547
+ ## Code of Conduct
548
+
549
+ Everyone interacting in the Paseto project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).