base64_token 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6687033029161045990b0fef4b18631af2a93621
4
+ data.tar.gz: 3c24bb165af866a248f24d43a973ea837bcc29fb
5
+ SHA512:
6
+ metadata.gz: ff78fc36d0a5039405fad8663f2fd7eef99ee2e9fc5ed69a9004f2e4dde6c88b4fb4c3d4706a63d6318863288bbaaaec663eba468ab61455b5f4cc1cb1e382c7
7
+ data.tar.gz: 96a5b13c33e8091d4978588762daf3b051c1cdb3a94547d8384ad420f0dab66ab7ff2f8fde9cb3e92f11c735207b7cab808afde4112fc7a629a012939c0dd7cf
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,59 @@
1
+ # We need to configure exemptions for blocks that we generally accept to be
2
+ # long, since they are less comparable to methods and more comparable to
3
+ # modules/classes.
4
+ Metrics/BlockLength:
5
+ ExcludedMethods:
6
+ - describe
7
+ - namespace
8
+
9
+ Style/AsciiComments:
10
+ Enabled: false
11
+
12
+ Style/Documentation:
13
+ Enabled: false
14
+
15
+ # Encoding comments are not neccessary in all 2.x versions of ruby, since
16
+ # UTF-8 has become the default encoding.
17
+ Style/Encoding:
18
+ EnforcedStyle: never
19
+ Enabled: true
20
+ Exclude:
21
+ - '*.gemspec'
22
+
23
+ # Ruby 2.3 introduced optional automatic freezing of string literals
24
+ # This might become default in future versions.
25
+ # For details on this feature see the internet, e.g. https://wyeworks.com/blog/2015/12/1/immutable-strings-in-ruby-2-dot-3
26
+ Style/FrozenStringLiteralComment:
27
+ EnforcedStyle: always
28
+
29
+ # This cop tries to make you use module_funtion instead of extend self
30
+ # This is bad because both have their own use-case and should not be used
31
+ # and sometimes cannot be used to do the same thing
32
+ Style/ModuleFunction:
33
+ Enabled: false
34
+
35
+ # While it is very often useful to separate numbers after every three digits
36
+ # for readability, this mostly doesn't make sense if the number doesn't
37
+ # represent an amount but rather an identifier. Thus the use of underscores
38
+ # every three digits is recommended but not enforced.
39
+ Style/NumericLiterals:
40
+ Enabled: false
41
+
42
+ # Do not force the same one letter variable names for all occurences of inject
43
+ Style/SingleLineBlockParams:
44
+ Enabled: false
45
+
46
+ # No significant improvement in speed or memory usage apparent. Readability is
47
+ # atrocious.
48
+ Performance/Casecmp:
49
+ Enabled: false
50
+
51
+ # Not safe in a rails context, since Relation.count is != Enumerable.count
52
+ Performance/Count:
53
+ Enabled: false
54
+
55
+ # Does not make sense when dealing with non-ActiveRecord like mongoid. That
56
+ # Mongoid for example throws error if find_by returns nothing (but we expect
57
+ # nil)
58
+ Rails/FindBy:
59
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.7
6
+
7
+ script:
8
+ - bundle exec rspec
9
+ - bundle exec rubocop
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 1.0.0 (2017-01-19)
2
+
3
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ source 'https://rubygems.org'
3
+
4
+ # Specify your gem's dependencies in base64_token.gemspec
5
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Beko Käuferportal GmbH
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ [![Gem Version](https://badge.fury.io/rb/base64_token.svg)](https://badge.fury.io/rb/base64_token)
2
+ [![Build Status](https://travis-ci.org/kaeuferportal/base64_token.svg?branch=master)](https://travis-ci.org/kaeuferportal/base64_token)
3
+
4
+ # Base64Token
5
+
6
+ This gem allows you to take a ruby hash and turn it into an encrypted token
7
+ that you can later convert back to your original hash.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'base64_token', '~> 1.0'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install base64_token
24
+
25
+ ## Usage
26
+
27
+ ````ruby
28
+ # Set the encryption key used for your token. You should store that somewhere
29
+ # if you want to recognize your own tokens at a later time.
30
+ Base64Token.encryption_key = Base64Token.generate_key
31
+ => "BgPrrt4Ltd7rYlsloSEs+cVuxcaLdjkTRFAjKWViIWo=\n"
32
+
33
+ token = Base64Token.generate(user_id: 42, valid_to: '2017-01-19T13:37:00')
34
+ => "fTsJg-2iOA5F3YC2i5tlGcWUE-npnZwSEezA-yRfhLL8aV_KE6AuGIZH5YAdgE-lLhiNUmuWCFkxlgUJy7TjdmJFscxzeS-l3CTD1or6nwR0-zHA7B-Q"
35
+
36
+ Base64Token.parse(token)
37
+ => {:user_id=>42, :valid_to=>"2017-01-19T13:37:00"}
38
+ ````
39
+
40
+ Note that:
41
+
42
+ * your hash is converted to JSON intermediately, so you can (re)store anything
43
+ that you can serialize to JSON
44
+ * to ensure consistency before and after deserialization, all hash keys have to be
45
+ symbols
46
+
47
+ ## Development
48
+
49
+ You can run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ 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).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kaeuferportal/base64_token.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'base64_token/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'base64_token'
9
+ spec.version = Base64Token::VERSION
10
+ spec.authors = ['Beko Käuferportal GmbH']
11
+ spec.email = ['oss@kaeuferportal.de']
12
+ spec.summary = 'Encodes ruby hashes as encrypted and URL-safe tokens.'
13
+ spec.homepage = 'https://github.com/kaeuferportal/base64_token'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.required_ruby_version = '>= 2.1.0'
24
+
25
+ spec.add_dependency 'rbnacl', '>= 3.0.0', '< 5.0'
26
+ spec.add_dependency 'rbnacl-libsodium', '~> 1.0'
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.13'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ spec.add_development_dependency 'rubocop', '0.47.1'
32
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'base64_token'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+ require 'base64'
3
+ require 'base64_token/version'
4
+ require 'json'
5
+
6
+ require 'rbnacl/libsodium'
7
+ require 'rbnacl'
8
+
9
+ module Base64Token
10
+ class Error < StandardError; end
11
+ class ConfigurationError < StandardError; end
12
+
13
+ class << self
14
+ def generate(**hash)
15
+ json = JSON.generate(hash)
16
+ cipher = encrypt(json)
17
+ Base64.urlsafe_encode64(cipher)
18
+ end
19
+
20
+ def parse(token)
21
+ return {} if !token || token.strip.empty?
22
+ cipher = base64_decode(token)
23
+ json = decrypt(cipher)
24
+ JSON.parse(json).map { |k, v| [k.to_sym, v] }.to_h
25
+ end
26
+
27
+ def generate_key
28
+ Base64.encode64(RbNaCl::Random.random_bytes(RbNaCl::SecretBox.key_bytes))
29
+ end
30
+
31
+ def encryption_key=(key)
32
+ @encryption_key = key
33
+ @crypto_box = nil
34
+ end
35
+
36
+ private
37
+
38
+ def encrypt(plaintext)
39
+ crypto_box.encrypt(plaintext)
40
+ end
41
+
42
+ def base64_decode(string)
43
+ Base64.urlsafe_decode64(string)
44
+ rescue ArgumentError => e
45
+ raise Error, e.message
46
+ end
47
+
48
+ def decrypt(ciphertext)
49
+ crypto_box.decrypt(ciphertext)
50
+ rescue RbNaCl::CryptoError => e
51
+ raise Error, e.message
52
+ end
53
+
54
+ def crypto_box
55
+ @crypto_box ||= begin
56
+ unless @encryption_key
57
+ raise ConfigurationError, 'Encryption key not set.'
58
+ end
59
+
60
+ key = Base64.decode64(@encryption_key)
61
+ RbNaCl::SimpleBox.from_secret_key(key)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module Base64Token
3
+ VERSION = '1.0.0'.freeze
4
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: base64_token
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Beko Käuferportal GmbH
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rbnacl
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rbnacl-libsodium
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.13'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.13'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rubocop
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '='
94
+ - !ruby/object:Gem::Version
95
+ version: 0.47.1
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '='
101
+ - !ruby/object:Gem::Version
102
+ version: 0.47.1
103
+ description:
104
+ email:
105
+ - oss@kaeuferportal.de
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - ".gitignore"
111
+ - ".rspec"
112
+ - ".rubocop.yml"
113
+ - ".travis.yml"
114
+ - CHANGELOG.md
115
+ - Gemfile
116
+ - LICENSE.md
117
+ - README.md
118
+ - Rakefile
119
+ - base64_token.gemspec
120
+ - bin/console
121
+ - bin/setup
122
+ - lib/base64_token.rb
123
+ - lib/base64_token/version.rb
124
+ homepage: https://github.com/kaeuferportal/base64_token
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: 2.1.0
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.5.1
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Encodes ruby hashes as encrypted and URL-safe tokens.
148
+ test_files: []