Module: Base64Token

Defined in:
lib/base64_token.rb,
lib/base64_token/version.rb

Defined Under Namespace

Classes: ConfigurationError, Error

Constant Summary collapse

VERSION =
'1.0.2'.freeze

Class Method Summary collapse

Class Method Details

.encryption_key=(key) ⇒ Object



33
34
35
36
# File 'lib/base64_token.rb', line 33

def encryption_key=(key)
  @encryption_key = key
  @crypto_box = nil
end

.generate(**hash) ⇒ Object



14
15
16
17
18
# File 'lib/base64_token.rb', line 14

def generate(**hash)
  json = JSON.generate(hash)
  cipher = encrypt(json)
  Base64.urlsafe_encode64(cipher)
end

.generate_keyObject



27
28
29
30
31
# File 'lib/base64_token.rb', line 27

def generate_key
  Base64.strict_encode64(
    RbNaCl::Random.random_bytes(RbNaCl::SecretBox.key_bytes)
  )
end

.parse(token) ⇒ Object



20
21
22
23
24
25
# File 'lib/base64_token.rb', line 20

def parse(token)
  return {} if !token || token.strip.empty?
  cipher = base64_decode(token)
  json = decrypt(cipher)
  JSON.parse(json).map { |k, v| [k.to_sym, v] }.to_h
end