Module: EncodedToken

Extended by:
ErrorMessages
Defined in:
lib/encoded_token.rb,
lib/encoded_token/utils.rb,
lib/encoded_token/cipher.rb,
lib/encoded_token/encoder.rb,
lib/encoded_token/version.rb,
lib/encoded_token/encryptor.rb,
lib/encoded_token/configuration.rb,
lib/encoded_token/error_messages.rb,
lib/encoded_token/encoder/utf8_decoder.rb,
lib/encoded_token/encoder/utf8_encoder.rb,
lib/encoded_token/encoder/legacy_decoder.rb,
lib/encoded_token/encoder/legacy_encoder.rb

Overview

EncodedToken

Encodes a UTF-8 String to produce a Secure Token, then decodes the Secure Token to return the original input.

  • The given input is encoded using a substitution cipher, then padded with alphanumeric characters to a random length.

  • Multiple substitution ciphers are used to improve security.

examples:

EncodedToken.encode(12345)
# => "b4ex6AEB62jlBGpVAGNou8iRmD7pnHGHafQlAHB7w0J"

EncodedToken.decode("b4ex6AEB62jlBGpVAGNou8iRmD7pnHGHafQlAHB7w0J")
# => "12345"

Defined Under Namespace

Modules: Encoder, ErrorMessages, Utils, VERSION Classes: Cipher, Configuration, Encryptor

Constant Summary

Constants included from ErrorMessages

ErrorMessages::CIPHER_ERRORS, ErrorMessages::CONFIG_ERRORS, ErrorMessages::ENCODED_TOKEN_ERRORS, ErrorMessages::ENCODER_ERRORS, ErrorMessages::ENCRYPTOR_ERRORS, ErrorMessages::ERROR_MESSAGES, ErrorMessages::V1_ENCODER_ERRORS

Class Method Summary collapse

Class Method Details

.configurationConfiguration

Returns the configuration instance.

Returns:

  • (Configuration)

    a memoized instance of the Configuration class.



56
57
58
# File 'lib/encoded_token.rb', line 56

def configuration
  @configuration ||= Configuration.instance
end

.configure {|block| ... } ⇒ void

This method returns an undefined value.

Applies the provided block to the configuration and builds the ciphers.

Examples:

EncodedToken.configure do |config|
  config.seed = 12345
end

Yields:

  • (block)

    a configuration block



73
74
75
76
77
# File 'lib/encoded_token.rb', line 73

def configure
  yield(configuration)

  Encoder.build_ciphers!
end

.decode(token) ⇒ String?

Decodes a given token to the original string

Parameters:

  • token (String)

    an encoded-token string

Returns:

  • (String)

    the decoded value if successful

  • (nil)

    if unsuccessful



124
125
126
# File 'lib/encoded_token.rb', line 124

def decode(token)
  Encoder.decode(token)
end

.decode!(token) ⇒ String

Decodes a given token to the original string, raising an exception if encountered

Parameters:

  • token (String)

    an encoded-token string

Returns:

  • (String)

    the decoded value

Raises:

  • (ArgumentError, RuntimeError)

    if the decoding fails



142
143
144
# File 'lib/encoded_token.rb', line 142

def decode!(token)
  Encoder.decode!(token)
end

.encode(input, encoder = :utf8) ⇒ String?

Encodes a given input to an encoded token.

Parameters:

  • input (Integer, String, *.to_s)

    any object that responds to '.to_s'

  • encoder (:utf8, :legacy) (defaults to: :utf8)

    the encoder to use.

Returns:

  • (String)

    if successful

  • (nil)

    if unsuccessful



90
91
92
# File 'lib/encoded_token.rb', line 90

def encode(input, encoder = :utf8)
  Encoder.encode(input, encoder)
end

.encode!(input, encoder = :utf8) ⇒ String

Encodes a given input to an encoded token, raising an exception if encountered

Parameters:

  • input (Integer, String, *.to_s)

    any object that responds to '.to_s'

  • encoder (:utf8, :legacy) (defaults to: :utf8)

    the encoding to use.

Returns:

  • (String)

    the encoded token

Raises:

  • (ArgumentError, RuntimeError)

    if the encoding fails



108
109
110
# File 'lib/encoded_token.rb', line 108

def encode!(input, encoder = :utf8)
  Encoder.encode!(input, encoder)
end

.gem_versionGem::Version

Returns the gem version.

Returns:

  • (Gem::Version)


10
11
12
# File 'lib/encoded_token/version.rb', line 10

def self.gem_version
  Gem::Version.new VERSION::STRING
end