paseto 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +53 -0
- data/LICENSE.txt +21 -0
- data/README.md +84 -0
- data/Rakefile +6 -0
- data/bin/console +10 -0
- data/bin/setup +8 -0
- data/lib/paseto.rb +45 -0
- data/lib/paseto/error.rb +6 -0
- data/lib/paseto/local.rb +84 -0
- data/lib/paseto/public.rb +96 -0
- data/lib/paseto/token.rb +37 -0
- data/lib/paseto/version.rb +3 -0
- data/paseto.gemspec +29 -0
- metadata +136 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 511f7637a0d6963e65acfd2cbaaf7dd00dabf788
|
4
|
+
data.tar.gz: 74a86c0ef1aae46ec7f2dc850c9c584e9a637b3b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '05862baa88aaeb0d3867c2862dc5a10ee1ab5fe4a9347abf12a6ff106f5b8306618a572fa9613d778f13aa566eb3aeee0f32c9a689151cc2b90a82105720e0dc'
|
7
|
+
data.tar.gz: 0ecb74d4b10ec7ce169825a606321b27e6e3ccd75da35146b9b0ab54c2dc09f293449966507b33924d8c79a86bb3a11fc07723867c12a17f790386fd25b5e208
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at mguymon@instructure.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
GIT
|
2
|
+
remote: https://github.com/anirishduck/rbnacl
|
3
|
+
revision: ff58f9e5dcae0617a3ead398aadd51553c6baed1
|
4
|
+
specs:
|
5
|
+
rbnacl (5.0.0)
|
6
|
+
ffi
|
7
|
+
|
8
|
+
PATH
|
9
|
+
remote: .
|
10
|
+
specs:
|
11
|
+
paseto (0.2.0)
|
12
|
+
rbnacl-libsodium (~> 1.0)
|
13
|
+
|
14
|
+
GEM
|
15
|
+
remote: https://rubygems.org/
|
16
|
+
specs:
|
17
|
+
coderay (1.1.2)
|
18
|
+
diff-lcs (1.3)
|
19
|
+
ffi (1.9.23)
|
20
|
+
method_source (0.9.0)
|
21
|
+
pry (0.11.3)
|
22
|
+
coderay (~> 1.1.0)
|
23
|
+
method_source (~> 0.9.0)
|
24
|
+
rake (10.5.0)
|
25
|
+
rbnacl-libsodium (1.0.16)
|
26
|
+
rbnacl (>= 3.0.1)
|
27
|
+
rspec (3.7.0)
|
28
|
+
rspec-core (~> 3.7.0)
|
29
|
+
rspec-expectations (~> 3.7.0)
|
30
|
+
rspec-mocks (~> 3.7.0)
|
31
|
+
rspec-core (3.7.1)
|
32
|
+
rspec-support (~> 3.7.0)
|
33
|
+
rspec-expectations (3.7.0)
|
34
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
+
rspec-support (~> 3.7.0)
|
36
|
+
rspec-mocks (3.7.0)
|
37
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
38
|
+
rspec-support (~> 3.7.0)
|
39
|
+
rspec-support (3.7.1)
|
40
|
+
|
41
|
+
PLATFORMS
|
42
|
+
ruby
|
43
|
+
|
44
|
+
DEPENDENCIES
|
45
|
+
bundler (~> 1.16)
|
46
|
+
paseto!
|
47
|
+
pry (~> 0.11)
|
48
|
+
rake (~> 10.0)
|
49
|
+
rbnacl!
|
50
|
+
rspec (~> 3.0)
|
51
|
+
|
52
|
+
BUNDLED WITH
|
53
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Michael Guymon
|
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,84 @@
|
|
1
|
+
# Paseto
|
2
|
+
|
3
|
+
![](https://travis-ci.org/mguymon/paseto.rb.svg?branch=master)
|
4
|
+
|
5
|
+
Ruby implementation of [Paseto](https://github.com/paragonie/paseto) using [libsodium](https://github.com/crypto-rb/rbnacl) that currently only supports [Version 2](https://github.com/paragonie/paseto/blob/master/docs/01-Protocol-Versions/Version2.md).
|
6
|
+
|
7
|
+
> Paseto (Platform-Agnostic SEcurity TOkens) is a specification for secure stateless tokens.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'paseto'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install paseto
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
For all examples:
|
28
|
+
|
29
|
+
require 'paseto'
|
30
|
+
|
31
|
+
## Signing Plaintext Public Messages
|
32
|
+
|
33
|
+
Public = Paseto::V2::Public
|
34
|
+
key = Public::SecretKey.generate
|
35
|
+
token = key.sign('too many secrets')
|
36
|
+
|
37
|
+
saved_key = key.public_key.encode64 # you can save this string to a db
|
38
|
+
decoded_key = Public::PublicKey.decode64(saved_key)
|
39
|
+
decoded_key.verify(token) # => 'too many secrets'
|
40
|
+
|
41
|
+
## Encrypting Messages with a Shared Secret
|
42
|
+
|
43
|
+
Key = Paseto::V2::Local::Key
|
44
|
+
key = Key.generate
|
45
|
+
token = key.encrypt('a fancy message')
|
46
|
+
|
47
|
+
saved_key = key.encode64 # a base64 string representation of the key
|
48
|
+
decoded_key = Key.decode64(saved_key)
|
49
|
+
decoded_key.decrypt(token) # => 'a fancy message'
|
50
|
+
|
51
|
+
## Using the Message Footer
|
52
|
+
|
53
|
+
The message footer is transmitted plaintext, and can be used for key lookup.
|
54
|
+
Note that you still must always verify the token via `#decrypt`, or `#verify`,
|
55
|
+
as this also verifies the integrity of the footer:
|
56
|
+
|
57
|
+
Local = Paseto::V2::Local
|
58
|
+
token = Paseto.parse(raw_data)
|
59
|
+
# NOTE: this has not yet been verified! You will always want to call
|
60
|
+
# .decrypt or .verify *immediately* after using this value. Otherwise, you
|
61
|
+
# will be using data that has not been authenticated.
|
62
|
+
kid = token.footer
|
63
|
+
|
64
|
+
saved_key = database.find_key(kid) # find the previously stored key
|
65
|
+
decoded_key = Local::Key.decode64(saved_key)
|
66
|
+
decoded_key.decrypt(token) # => 'too many secrets'
|
67
|
+
|
68
|
+
## Development
|
69
|
+
|
70
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
71
|
+
|
72
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/paseto. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
77
|
+
|
78
|
+
## License
|
79
|
+
|
80
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
81
|
+
|
82
|
+
## Code of Conduct
|
83
|
+
|
84
|
+
Everyone interacting in the Paseto project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/paseto/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/lib/paseto.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'rbnacl/libsodium'
|
3
|
+
|
4
|
+
require 'paseto/version'
|
5
|
+
require 'paseto/error'
|
6
|
+
require 'paseto/token'
|
7
|
+
require 'paseto/public'
|
8
|
+
require 'paseto/local'
|
9
|
+
|
10
|
+
module Paseto
|
11
|
+
|
12
|
+
# https://github.com/paragonie/paseto/blob/master/docs/01-Protocol-Versions/Common.md#pae-definition
|
13
|
+
def self.encode_length(n)
|
14
|
+
str = []
|
15
|
+
(0..7).each do |i|
|
16
|
+
# Clear the MSB for interoperability
|
17
|
+
n &= 127 if (i === 7)
|
18
|
+
|
19
|
+
str << (n & 255)
|
20
|
+
n = n >> 8
|
21
|
+
end
|
22
|
+
|
23
|
+
str.pack('Q')
|
24
|
+
end
|
25
|
+
|
26
|
+
# https://github.com/paragonie/paseto/blob/master/docs/01-Protocol-Versions/Common.md#pae-definition
|
27
|
+
def self.pre_auth_encode(*pieces)
|
28
|
+
compacted_pieces = pieces.compact
|
29
|
+
|
30
|
+
initial_output = encode_length(compacted_pieces.length)
|
31
|
+
compacted_pieces.reduce(initial_output) do |output, piece|
|
32
|
+
output += encode_length(piece.length)
|
33
|
+
output += piece
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.decode64(str)
|
38
|
+
Base64.urlsafe_decode64(str)
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.encode64(bin)
|
42
|
+
# Remove the padding on the encode64
|
43
|
+
Base64.urlsafe_encode64(bin).gsub(/=+$/, '')
|
44
|
+
end
|
45
|
+
end
|
data/lib/paseto/error.rb
ADDED
data/lib/paseto/local.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
module Paseto
|
2
|
+
module V2
|
3
|
+
module Local
|
4
|
+
HEADER = 'v2.local'
|
5
|
+
NONCE_BYTES = RbNaCl::AEAD::XChaCha20Poly1305IETF.nonce_bytes
|
6
|
+
|
7
|
+
NonceError = Class.new(Paseto::Error)
|
8
|
+
|
9
|
+
class Key
|
10
|
+
def self.generate
|
11
|
+
new(RbNaCl::Random.random_bytes(RbNaCl::AEAD::XChaCha20Poly1305IETF.key_bytes))
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.decode64(encoded_key)
|
15
|
+
new(Paseto.decode64(encoded_key))
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(key)
|
19
|
+
@key = key
|
20
|
+
@aead = RbNaCl::AEAD::XChaCha20Poly1305IETF.new(key)
|
21
|
+
end
|
22
|
+
|
23
|
+
def encode64
|
24
|
+
Paseto.encode64(@key)
|
25
|
+
end
|
26
|
+
|
27
|
+
def encrypt(message, footer = nil)
|
28
|
+
# Make a nonce: A single-use value never repeated under the same key
|
29
|
+
nonce = generate_nonce(message)
|
30
|
+
|
31
|
+
# Encrypt a message with the AEAD
|
32
|
+
ciphertext = @aead.encrypt(nonce, message, additional_data(nonce, footer))
|
33
|
+
|
34
|
+
Paseto::Token.new(HEADER, nonce + ciphertext, footer).to_message
|
35
|
+
end
|
36
|
+
|
37
|
+
def decrypt(token, footer = nil)
|
38
|
+
footer ||= token.footer if token.is_a? Paseto::Token
|
39
|
+
parsed = Paseto.verify_token(token, HEADER, footer)
|
40
|
+
|
41
|
+
nonce = parsed.payload[0, NONCE_BYTES]
|
42
|
+
ciphertext = parsed.payload[NONCE_BYTES..-1]
|
43
|
+
|
44
|
+
raise BadMessageError.new('Unable to process message') if nonce.nil? || ciphertext.nil?
|
45
|
+
|
46
|
+
begin
|
47
|
+
data = additional_data(nonce, footer)
|
48
|
+
@aead.decrypt(nonce, ciphertext, data)
|
49
|
+
rescue RbNaCl::LengthError
|
50
|
+
raise NonceError, 'Invalid nonce'
|
51
|
+
rescue RbNaCl::CryptoError
|
52
|
+
raise AuthenticationError, 'Token signature invalid'
|
53
|
+
rescue
|
54
|
+
raise TokenError, 'Unable to process message'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def generate_nonce_key
|
61
|
+
RbNaCl::Random.random_bytes(NONCE_BYTES)
|
62
|
+
end
|
63
|
+
|
64
|
+
def generate_nonce(message)
|
65
|
+
RbNaCl::Hash::Blake2b.digest(message,
|
66
|
+
key: generate_nonce_key,
|
67
|
+
digest_size: NONCE_BYTES)
|
68
|
+
end
|
69
|
+
|
70
|
+
def additional_data(nonce, footer)
|
71
|
+
Paseto.pre_auth_encode(HEADER + '.', nonce, footer)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.encrypt(message, key, footer = nil)
|
76
|
+
key.encrypt(message, footer)
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.decrypt(token, key, footer = nil)
|
80
|
+
key.decrypt(token, footer)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module Paseto
|
2
|
+
module V2
|
3
|
+
module Public
|
4
|
+
HEADER = 'v2.public'
|
5
|
+
SIGNATURE_BYTES = RbNaCl::SigningKey.signature_bytes
|
6
|
+
BadMessageError = Class.new(Paseto::Error)
|
7
|
+
|
8
|
+
module Encoder
|
9
|
+
private
|
10
|
+
def encode_message(message, footer)
|
11
|
+
Paseto.pre_auth_encode(HEADER + '.', message, footer)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class SecretKey
|
16
|
+
include Encoder
|
17
|
+
|
18
|
+
def self.generate
|
19
|
+
new(RbNaCl::SigningKey.generate)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.decode64(encoded_key)
|
23
|
+
new(Paseto.decode64(encoded_key))
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(key)
|
27
|
+
@key = key
|
28
|
+
@nacl = RbNaCl::SigningKey.new(key)
|
29
|
+
end
|
30
|
+
|
31
|
+
def sign(message, footer = nil)
|
32
|
+
data = encode_message(message, footer)
|
33
|
+
# Sign a message with the signing key
|
34
|
+
signature = @nacl.sign(data)
|
35
|
+
|
36
|
+
Paseto::Token.new(HEADER, message + signature, footer).to_message
|
37
|
+
end
|
38
|
+
|
39
|
+
def verify(message, footer = nil)
|
40
|
+
public_key.verify(message, footer)
|
41
|
+
end
|
42
|
+
|
43
|
+
def public_key
|
44
|
+
PublicKey.new(@nacl.verify_key.to_bytes)
|
45
|
+
end
|
46
|
+
|
47
|
+
def encode64
|
48
|
+
Paseto.encode64(@key)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class PublicKey
|
53
|
+
include Encoder
|
54
|
+
|
55
|
+
def self.decode64(encoded_key)
|
56
|
+
new(Paseto.decode64(encoded_key))
|
57
|
+
end
|
58
|
+
|
59
|
+
def initialize(key)
|
60
|
+
@key = key
|
61
|
+
@nacl = RbNaCl::VerifyKey.new(key)
|
62
|
+
end
|
63
|
+
|
64
|
+
def encode64
|
65
|
+
Paseto.encode64(@key)
|
66
|
+
end
|
67
|
+
|
68
|
+
def verify(token, footer = nil)
|
69
|
+
footer ||= token.footer if token.is_a? Paseto::Token
|
70
|
+
parsed = Paseto.verify_token(token, HEADER, footer)
|
71
|
+
|
72
|
+
decoded_message = parsed.payload[0..-(SIGNATURE_BYTES + 1)]
|
73
|
+
signature = parsed.payload[-SIGNATURE_BYTES..-1]
|
74
|
+
|
75
|
+
raise BadMessageError.new('Unable to process message') if decoded_message.nil? || signature.nil?
|
76
|
+
|
77
|
+
begin
|
78
|
+
data = encode_message(decoded_message, footer)
|
79
|
+
@nacl.verify(signature, data)
|
80
|
+
decoded_message
|
81
|
+
rescue RbNaCl::BadSignatureError
|
82
|
+
raise AuthenticationError.new('Token signature invalid')
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.sign(message, key, footer = nil)
|
88
|
+
key.sign(message, footer)
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.verify(token, key, footer = nil)
|
92
|
+
key.verify(token, footer)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/lib/paseto/token.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module Paseto
|
2
|
+
Token = Struct.new(:header, :payload, :footer) do
|
3
|
+
def to_message
|
4
|
+
message = [
|
5
|
+
header,
|
6
|
+
Paseto.encode64(payload)
|
7
|
+
]
|
8
|
+
|
9
|
+
message << Paseto.encode64(footer) if footer
|
10
|
+
|
11
|
+
message.join('.')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.verify_token(token, expected_header, expected_footer)
|
16
|
+
token = parse(token) unless token.is_a? Token
|
17
|
+
if token.header != expected_header
|
18
|
+
raise HeaderError.new("Invalid message header: #{token.header}")
|
19
|
+
end
|
20
|
+
|
21
|
+
if token.footer != expected_footer
|
22
|
+
raise TokenError.new("Invalid message footer: #{token.footer.inspect}")
|
23
|
+
end
|
24
|
+
|
25
|
+
token
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.parse(raw)
|
29
|
+
version, purpose, payload, footer = raw.split('.')
|
30
|
+
|
31
|
+
header = "#{version}.#{purpose}"
|
32
|
+
footer = Paseto.decode64(footer) unless footer.nil?
|
33
|
+
payload = Paseto.decode64(payload) unless payload.nil?
|
34
|
+
|
35
|
+
Token.new(header, payload, footer)
|
36
|
+
end
|
37
|
+
end
|
data/paseto.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'paseto/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'paseto'
|
8
|
+
spec.version = Paseto::VERSION
|
9
|
+
spec.authors = ['Michael Guymon', 'Frank Murphy']
|
10
|
+
spec.email = ['mguymon@instructure.com', 'fmurphy@instructure.com']
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby impl of Paseto}
|
13
|
+
spec.description = %q{Ruby impl of Paseto}
|
14
|
+
spec.homepage = 'https://github.com/mguymon/paseto.rb'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = 'bin'
|
21
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_dependency 'rbnacl-libsodium', '~> 1.0'
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
28
|
+
spec.add_development_dependency 'pry', '~> 0.11'
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: paseto
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Guymon
|
8
|
+
- Frank Murphy
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2018-04-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rbnacl-libsodium
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: bundler
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '1.16'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '1.16'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '10.0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '10.0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rspec
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "~>"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '3.0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '3.0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: pry
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - "~>"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0.11'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0.11'
|
84
|
+
description: Ruby impl of Paseto
|
85
|
+
email:
|
86
|
+
- mguymon@instructure.com
|
87
|
+
- fmurphy@instructure.com
|
88
|
+
executables:
|
89
|
+
- console
|
90
|
+
- setup
|
91
|
+
extensions: []
|
92
|
+
extra_rdoc_files: []
|
93
|
+
files:
|
94
|
+
- ".gitignore"
|
95
|
+
- ".rspec"
|
96
|
+
- ".travis.yml"
|
97
|
+
- CODE_OF_CONDUCT.md
|
98
|
+
- Gemfile
|
99
|
+
- Gemfile.lock
|
100
|
+
- LICENSE.txt
|
101
|
+
- README.md
|
102
|
+
- Rakefile
|
103
|
+
- bin/console
|
104
|
+
- bin/setup
|
105
|
+
- lib/paseto.rb
|
106
|
+
- lib/paseto/error.rb
|
107
|
+
- lib/paseto/local.rb
|
108
|
+
- lib/paseto/public.rb
|
109
|
+
- lib/paseto/token.rb
|
110
|
+
- lib/paseto/version.rb
|
111
|
+
- paseto.gemspec
|
112
|
+
homepage: https://github.com/mguymon/paseto.rb
|
113
|
+
licenses:
|
114
|
+
- MIT
|
115
|
+
metadata: {}
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements: []
|
131
|
+
rubyforge_project:
|
132
|
+
rubygems_version: 2.6.8
|
133
|
+
signing_key:
|
134
|
+
specification_version: 4
|
135
|
+
summary: Ruby impl of Paseto
|
136
|
+
test_files: []
|