prefixed_api_key 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/prefixed_api_key.rb +50 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: abdecfa8eb7c38212b11898340f043db177f332c79455761ba41c2e832b747c7
4
+ data.tar.gz: eb823f1de406ef4a57d7adfb6014375a9a4654061ec05d7ac1461487dcf24114
5
+ SHA512:
6
+ metadata.gz: 03ac1cdc66eef24b5c0bd43483d48037309136887d9ea2e784c7643646e0bbd286fe6c8f0d46705020f7c154e339fa12472111061f7f13b7d91295db3cd774da
7
+ data.tar.gz: 7d5601288012734b54ff6d4fc75b43daad7e914985d347c483041949939a7036178d84c1da1cb29ac2998536a11d006205413120818889ca4d77a5ec7f8b3a55
@@ -0,0 +1,50 @@
1
+ require "digest"
2
+ require "securerandom"
3
+
4
+ class PrefixedApiKey
5
+ BASE58_ALPHABET = ("0".."9").to_a + ("A".."Z").to_a + ("a".."z").to_a - ["0", "O", "I", "l"]
6
+
7
+ attr_reader :key_prefix, :long_token, :short_token
8
+
9
+ def self.generate(key_prefix: "mycompany", short_token_prefix: "", short_token_length: 8, long_token_length: 24)
10
+ new(
11
+ key_prefix: key_prefix,
12
+ short_token: base58_bytes(short_token_length),
13
+ long_token: base58_bytes(long_token_length)
14
+ )
15
+ end
16
+
17
+ def self.base58_bytes(n)
18
+ # https://github.com/rails/rails/blob/3872bc0e54d32e8bf3a6299b0bfe173d94b072fc/activesupport/lib/active_support/core_ext/securerandom.rb#L19
19
+ SecureRandom.random_bytes(n).unpack("C*").map do |byte|
20
+ idx = byte % 64
21
+ idx = SecureRandom.random_number(58) if idx >= 58
22
+ BASE58_ALPHABET[idx]
23
+ end.join
24
+ end
25
+
26
+ def self.parse(token)
27
+ key_prefix, short_token, long_token = token.split("_")
28
+
29
+ new(
30
+ key_prefix: key_prefix,
31
+ short_token: short_token,
32
+ long_token: long_token
33
+ )
34
+ end
35
+
36
+ def initialize(key_prefix:, long_token:, short_token:)
37
+ @key_prefix = key_prefix
38
+ @long_token = long_token
39
+ @short_token = short_token
40
+ end
41
+
42
+ def long_token_hash
43
+ Digest::SHA256.hexdigest(long_token)
44
+ end
45
+
46
+ def token
47
+ @token ||= [key_prefix, short_token, long_token].join("_")
48
+ end
49
+ alias to_s token
50
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prefixed_api_key
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Lawson Jaglom-Kurtz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-05-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Ruby implementation of https://github.com/seamapi/prefixed-api-key
14
+ email: lawson@duck.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/prefixed_api_key.rb
20
+ homepage: https://rubygems.org/gems/prefixed_api_key
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.3.7
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Generate and parse prefixed API keys (Seam-style)
43
+ test_files: []