Class: PrefixedApiKey
- Inherits:
-
Object
- Object
- PrefixedApiKey
- Defined in:
- lib/prefixed_api_key.rb
Constant Summary collapse
- BASE58_ALPHABET =
("0".."9").to_a + ("A".."Z").to_a + ("a".."z").to_a - ["0", "O", "I", "l"]
Instance Attribute Summary collapse
-
#key_prefix ⇒ Object
readonly
Returns the value of attribute key_prefix.
-
#long_token ⇒ Object
readonly
Returns the value of attribute long_token.
-
#short_token ⇒ Object
readonly
Returns the value of attribute short_token.
Class Method Summary collapse
- .base58_bytes(n) ⇒ Object
- .generate(key_prefix: "mycompany", short_token_prefix: "", short_token_length: 8, long_token_length: 24) ⇒ Object
- .parse(token) ⇒ Object
Instance Method Summary collapse
-
#initialize(key_prefix:, long_token:, short_token:) ⇒ PrefixedApiKey
constructor
A new instance of PrefixedApiKey.
- #long_token_hash ⇒ Object
- #token ⇒ Object (also: #to_s)
Constructor Details
#initialize(key_prefix:, long_token:, short_token:) ⇒ PrefixedApiKey
Returns a new instance of PrefixedApiKey.
36 37 38 39 40 |
# File 'lib/prefixed_api_key.rb', line 36 def initialize(key_prefix:, long_token:, short_token:) @key_prefix = key_prefix @long_token = long_token @short_token = short_token end |
Instance Attribute Details
#key_prefix ⇒ Object (readonly)
Returns the value of attribute key_prefix.
7 8 9 |
# File 'lib/prefixed_api_key.rb', line 7 def key_prefix @key_prefix end |
#long_token ⇒ Object (readonly)
Returns the value of attribute long_token.
7 8 9 |
# File 'lib/prefixed_api_key.rb', line 7 def long_token @long_token end |
#short_token ⇒ Object (readonly)
Returns the value of attribute short_token.
7 8 9 |
# File 'lib/prefixed_api_key.rb', line 7 def short_token @short_token end |
Class Method Details
.base58_bytes(n) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/prefixed_api_key.rb', line 17 def self.base58_bytes(n) # https://github.com/rails/rails/blob/3872bc0e54d32e8bf3a6299b0bfe173d94b072fc/activesupport/lib/active_support/core_ext/securerandom.rb#L19 SecureRandom.random_bytes(n).unpack("C*").map do |byte| idx = byte % 64 idx = SecureRandom.random_number(58) if idx >= 58 BASE58_ALPHABET[idx] end.join end |
.generate(key_prefix: "mycompany", short_token_prefix: "", short_token_length: 8, long_token_length: 24) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/prefixed_api_key.rb', line 9 def self.generate(key_prefix: "mycompany", short_token_prefix: "", short_token_length: 8, long_token_length: 24) new( key_prefix: key_prefix, short_token: base58_bytes(short_token_length), long_token: base58_bytes(long_token_length) ) end |
.parse(token) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/prefixed_api_key.rb', line 26 def self.parse(token) key_prefix, short_token, long_token = token.split("_") new( key_prefix: key_prefix, short_token: short_token, long_token: long_token ) end |
Instance Method Details
#long_token_hash ⇒ Object
42 43 44 |
# File 'lib/prefixed_api_key.rb', line 42 def long_token_hash Digest::SHA256.hexdigest(long_token) end |
#token ⇒ Object Also known as: to_s
46 47 48 |
# File 'lib/prefixed_api_key.rb', line 46 def token @token ||= [key_prefix, short_token, long_token].join("_") end |