token_checksum 0.1.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
+ SHA256:
3
+ metadata.gz: 14d770b16d0d2063bd7fccb11c4d6a2c6fd748545ffe08cb7a6f66a5fcbcd0da
4
+ data.tar.gz: 350aaaff96ae975c1e727bdabb987bcb34c610b6249bb2398c173ee12d5d51f9
5
+ SHA512:
6
+ metadata.gz: cd520256bd3f79799296f298fe3d074ac698df8cb8a834d75aa9c70148fb2217a286246a5bad46488fe593f2455a8a54a6aa5ae103774704ac9cfff5688de121
7
+ data.tar.gz: f2130b655f599decab22827b6519f792211bae0d057a51905377cc0a6097502b224b8cf3ee5caf2d80d5ef0f9aeddb10c6ab9885e2526c2a655e66fdf3b1cd9b
data/.rubocop.yml ADDED
@@ -0,0 +1,7 @@
1
+ inherit_gem:
2
+ rubocop-standard:
3
+ - config/default.yml
4
+ - config/minitest.yml
5
+
6
+ Naming/FileName:
7
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-08-30
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in token_checksum.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "rubocop-standard"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Garen J. Torikian
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,43 @@
1
+ # TokenChecksum
2
+
3
+ Generates a 37 character long random token, with an (optional) prefix and a 32-bit checksum in the last 6 digits. Inspired by:
4
+
5
+ * https://github.blog/2021-04-05-behind-githubs-new-authentication-token-formats/
6
+ * https://github.com/stefansundin/token-checksum
7
+ * https://github.com/alexanderschau/access_token
8
+
9
+
10
+ ## Installation
11
+
12
+ Install the gem and add to the application's Gemfile by executing:
13
+
14
+ $ bundle add token_checksum
15
+
16
+ If bundler is not being used to manage dependencies, install the gem by executing:
17
+
18
+ $ gem install token_checksum
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ token_one = TokenChecksum.generate("xoxo")
24
+ # "xoxo_3Q8oOwJyFzbuUaYIv2CPyu12K6gjmy2O8PIK"
25
+
26
+ # highly recommended that you introduce a secret as well
27
+ token_two = TokenChecksum.generate("xoxo", secret: "foo")
28
+ # "xoxo_4ftnAniunUKy6x0V75sMVg1VerpU2y1FoRT2"
29
+
30
+ # can also validate checksums
31
+ TokenChecksum.valid?(token_one)
32
+ # true
33
+
34
+ TokenChecksum.valid?(token_two, secret: "foo")
35
+ # true
36
+
37
+ TokenChecksum.valid?(token_two, secret: "bleh")
38
+ # FALSE
39
+ ```
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: [:test, :rubocop]
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TokenChecksum
4
+ module Base62
5
+ PRIMITIVES = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] + \
6
+ ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] + \
7
+ ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
8
+ PRIMITIVES_SIZE = 62
9
+
10
+ class << self
11
+ def encode(int, min_length: 0)
12
+ return "".rjust(min_length, PRIMITIVES[0]) if int <= 0
13
+
14
+ result = ""
15
+ while int > 0
16
+ result = PRIMITIVES[int % PRIMITIVES_SIZE] + result
17
+ int /= PRIMITIVES_SIZE
18
+ end
19
+
20
+ result.rjust(min_length, PRIMITIVES[0])
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TokenChecksum
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "token_checksum/base_62"
4
+ require_relative "token_checksum/version"
5
+
6
+ require "zlib"
7
+ require "securerandom"
8
+ require "securecompare"
9
+
10
+ if ENV.fetch("DEBUG", "false")
11
+ require "debug"
12
+ end
13
+
14
+ module TokenChecksum
15
+ class << self
16
+ def generate(prefix, secret: "")
17
+ suffix = (random_base62 + random_base62)[0...30]
18
+ first_part = "#{prefix}_#{suffix}"
19
+ checksum = generate_checksum("#{first_part}#{secret}")
20
+ "#{first_part}#{checksum}"
21
+ end
22
+
23
+ def valid?(token, secret: "")
24
+ return false if token.empty?
25
+
26
+ provided_checksum = token[-6..-1]
27
+ return false if provided_checksum.empty?
28
+
29
+ # This is the token without the final checksum
30
+ checksumless_string = token[0..-7]
31
+ return false if checksumless_string.empty?
32
+
33
+ calculated_checksum = generate_checksum("#{checksumless_string}#{secret}")
34
+
35
+ SecureCompare.compare(calculated_checksum, provided_checksum)
36
+ end
37
+
38
+ private def generate_checksum(string)
39
+ checksum = Zlib.crc32(string)
40
+ Base62.encode(checksum, min_length: 6)
41
+ end
42
+
43
+ private def random_base62
44
+ Base62.encode(SecureRandom.uuid.delete("-").hex)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,4 @@
1
+ module TokenChecksum
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/token_checksum/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "token_checksum"
7
+ spec.version = TokenChecksum::VERSION
8
+ spec.authors = ["Garen J. Torikian"]
9
+ spec.email = ["gjtorikian@users.noreply.github.com"]
10
+
11
+ spec.summary = "A token generator with an identifiable prefix and a 32-bit checksum suffix."
12
+ spec.homepage = "https://github.com/gjtorikian/token_checksum"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 3.0", "< 4.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(__dir__) do
21
+ %x(git ls-files -z).split("\x0").reject do |f|
22
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
23
+ end
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency("securecompare", "~> 1.0")
30
+
31
+ spec.add_development_dependency("debug") if "#{RbConfig::CONFIG["MAJOR"]}.#{RbConfig::CONFIG["MINOR"]}".to_f >= 3.1
32
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: token_checksum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Garen J. Torikian
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-08-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: securecompare
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: debug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - gjtorikian@users.noreply.github.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rubocop.yml"
49
+ - CHANGELOG.md
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - lib/token_checksum.rb
55
+ - lib/token_checksum/base_62.rb
56
+ - lib/token_checksum/version.rb
57
+ - sig/token_checksum.rbs
58
+ - token_checksum.gemspec
59
+ homepage: https://github.com/gjtorikian/token_checksum
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ homepage_uri: https://github.com/gjtorikian/token_checksum
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '3.0'
73
+ - - "<"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubygems_version: 3.3.7
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: A token generator with an identifiable prefix and a 32-bit checksum suffix.
86
+ test_files: []