Module: Branca
- Defined in:
- lib/branca.rb,
lib/branca/decoder.rb,
lib/branca/version.rb,
lib/branca/exceptions.rb
Defined Under Namespace
Classes: DecodeError, Decoder, ExpiredTokenError, VersionError
Constant Summary collapse
- VERSION =
'1.0.2'
Class Attribute Summary collapse
-
.secret_key ⇒ Object
Returns the value of attribute secret_key.
-
.ttl ⇒ Object
Returns the value of attribute ttl.
Class Method Summary collapse
- .configure {|_self| ... } ⇒ Object
- .decode(token, ttl: self.ttl, secret_key: self.secret_key) ⇒ Object
- .encode(message, timestamp = Time.now.utc, secret_key: self.secret_key) ⇒ Object
Class Attribute Details
.secret_key ⇒ Object
Returns the value of attribute secret_key.
14 15 16 |
# File 'lib/branca.rb', line 14 def secret_key @secret_key end |
.ttl ⇒ Object
Returns the value of attribute ttl.
14 15 16 |
# File 'lib/branca.rb', line 14 def ttl @ttl end |
Class Method Details
.configure {|_self| ... } ⇒ Object
50 51 52 |
# File 'lib/branca.rb', line 50 def configure yield self if block_given? end |
.decode(token, ttl: self.ttl, secret_key: self.secret_key) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/branca.rb', line 27 def decode(token, ttl: self.ttl, secret_key: self.secret_key) header, bytes = token_explode(token) version, , nonce = header_explode(header) raise VersionError unless version == VERSION raise ExpiredTokenError if ( + ttl) < Time.now.utc.to_i cipher = create_cipher(secret_key) = cipher.decrypt(nonce, bytes.pack('C*'), header.pack('C*')) rescue RbNaCl::CryptoError raise DecodeError else Decoder.new(, Time.at().utc) end |
.encode(message, timestamp = Time.now.utc, secret_key: self.secret_key) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/branca.rb', line 16 def encode(, = Time.now.utc, secret_key: self.secret_key) cipher = create_cipher(secret_key) nonce = RbNaCl::Random.random_bytes(cipher.nonce_bytes) header = [VERSION, .to_i].pack('C N') + nonce ciphertext = cipher.encrypt(nonce, , header) raw_token = header + ciphertext BaseX::Base62.encode(raw_token) end |