Permalink
Browse files

loading tokenifier.yml in a Rails application

  • Loading branch information...
1 parent 71aa5b4 commit 30d0127ccf68395445c13ebe064bd7cadf234a82 @dml dml committed Dec 23, 2011
View
@@ -0,0 +1 @@
+tokenifier-([\d\.]+).gem
View
@@ -1,30 +1,56 @@
# Tokenifier
Tokenifier is a Gibberish gem wrapper.
-It provides an approach to encrypt and decrypt structures like Strings, Hashes.
+It provides an approach to encrypt and decrypt structures like Numeric, String and Hash.
+
+[![Build Status](https://secure.travis-ci.org/ludo/tokenifier.png)](http://travis-ci.org/ludo/tokenifier)
+
## Installation
+Install the gem
+
+ gem install tokenifier
+
+Tokenifier will install gibberish the gem as dependency
+
+ require "tokenifier"
+
+Thats it.
+
+
+
+## Rails
+
Put the string into Gemfile
- gem "tokenifier"
+ gem "tokenifier", "~> 0.1"
+
+Run tokenifier:install generator
-Add config/tokenifier.yml in rails application
+ rails g tokenifier:install
+
+Rails generator will create config/tokenifier.yml with unique secret strings.
development:
secret: 7e991d82a0dd42b0afa293a339308c6f
+You have to use a permanent secret string to decrypt tokens.
+If no secret defined for environment Tokenifier uses random secret string each execution time.
+
+
+
## Usage
-To encrypt data into hash
+To encrypt data
- Tokenifier.encrypt("string") # =>
- Tokenifier.encrypt(:key => 'value') # =>
+ Tokenifier.encrypt("string") # => "U2FsdGVkX1+YHpkTh..."
+ Tokenifier.encrypt(:key => 'value') # => "U2FsdGVkX18ts+aRd..."
-To decrypt data into hash
+To decrypt data
- Tokenifier.decrypt(" ...") # =>
- Tokenifier.decrypt(" ...") # =>
+ Tokenifier.decrypt("U2FsdGVkX1+...") # => "string"
+ Tokenifier.decrypt("U2FsdGVkX18...") # => {"key" => "value"}
Errors handling
@@ -33,12 +59,14 @@ Errors handling
Tokenifier.decrypt("malformed hash") # => raises Tokenifier::Error
-Using custom secret
+Custom secret usage
data = Tokenifier.encrypt("string", :secret => 'secret')
Tokenifier.decrypt(data, :secret => 'secret') # => "string"
Tokenifier.decrypt(data) # => raises Tokenifier::Error, "Got a malformed string"
+
+
## CLI usage
Usage:
@@ -51,8 +79,8 @@ Commands:
e|encrypt - Does data encryption of any string data
d|decrypt - Does data decryption from hashed data.
- NOTE: You have to use permanent secret to decryption data.
- Tokinifier generates dafult secret each execution time.
+ NOTE: You have to use a permanent secret to decrypt a data.
+ Tokinifier generates random secret string each execution time instead.
Examples:
View
@@ -1,7 +1,6 @@
require 'gibberish'
require 'tokenifier/error'
require 'tokenifier/random'
-require 'tokenifier/rails'
require 'tokenifier/cipher'
require 'tokenifier/encrypt'
require 'tokenifier/decrypt'
View
@@ -1,14 +1,45 @@
+require 'yaml'
+
module Tokenifier
module Cipher
def secret
- @secret ||= Tokenifier::Rails.secret || Tokenifier::Random.secret
+ @secret ||= load_rails_secret
+ @secret ||= Tokenifier::Random.secret
+ @secret
end
def cipher(*args, &block)
aes = Gibberish::AES.new(args.first || secret)
block_given? ? yield(aes) : aes
end
+ def load_rails_secret
+ return unless defined?(Rails)
+
+ filename = Rails.root.join('config', 'tokenifier.yml')
+
+ puts filename
+
+ if File.exists?(filename)
+ config = YAML.load(
+ ERB.new(
+ IO.read(filename)
+ ).result
+ )[Rails.env]
+
+ @secret = config['secret']
+ else
+ @secret = Tokenifier::Random.secret
+
+ Rails.logger.warn "*** Tokenifier warning:"
+ Rails.logger.warn "*** Config tokenifier.yml file not found."
+ Rails.logger.warn "*** Run rails g tokenifier:install to generate one"
+ Rails.logger.warn "*** Using secret: #{@secret}"
+ end
+
+ @secret
+ end
+
end
end
View
@@ -14,24 +14,24 @@ def optparse
Usage:
- tokenifier [options] COMMAND 'custom string'
+ tokenifier [options] COMMAND 'custom string'
Commands:
- s|secret - Generates secret string
- e|encrypt - Does data encryption of any string data
- d|decrypt - Does data decryption from hashed data.
+ s|secret - Generates secret string
+ e|encrypt - Does data encryption of any string data
+ d|decrypt - Does data decryption from hashed data.
- NOTE: You have to use permanent secret to decryption data.
- Tokinifier generates dafult secret each execution time.
+ NOTE: You have to use a permanent secret to decrypt a data.
+ Tokinifier generates random secret string each execution time instead.
Examples:
- tokenifier encrypt "CUSTOM DATA"
- tokenifier decrypt "CUSTOM DATA"
+ tokenifier encrypt "CUSTOM DATA"
+ tokenifier decrypt "CUSTOM DATA"
- tokenifier --secret MYSECRET e "CUSTOM DATA"
- tokenifier --secret MYSECRET d "ENCRYPTED DATA"
+ tokenifier --secret MYSECRET e "CUSTOM DATA"
+ tokenifier --secret MYSECRET d "ENCRYPTED DATA"
USAGE
View
@@ -1,7 +0,0 @@
-module Tokenifier
- class Rails
- def self.secret
- nil
- end
- end
-end
@@ -1,3 +1,3 @@
module Tokenifier
- VERSION = '0.0.1'
+ VERSION = '0.1.0'
end
View
@@ -48,4 +48,28 @@
end
+ describe ".load_rails_secret" do
+
+ let(:rails) { Class.new }
+
+ before {
+ rails.stub_chain(:root, :join).and_return(File.expand_path('../support/config/tokenifier.yml', __FILE__))
+ rails.stub_chain(:logger).and_return(mock(:logger, :warn => nil))
+ rails.stub(:env).and_return('test')
+ }
+
+ specify {
+ with_stub_const(:Rails, rails) do
+ subject.load_rails_secret.should == 'aaabbbccc'
+ end
+ }
+
+ specify {
+ without_const(:Rails) do
+ subject.load_rails_secret.should_not == 'aaabbbccc'
+ end
+ }
+
+ end
+
end
View
@@ -22,14 +22,12 @@
context "a few key-value pairs" do
let(:hsh) { {:a => 2, :b => 'string 123', :c => true} }
- let(:expected) { "c:true#a:2#b:string 123" }
+ let(:expected) { "a:2#b:string 123#c:true" }
specify { packed.should == expected }
end
context "we are not supporting nested hashes yet due simplicity of solution" do
- let(:hsh) { {:a => 2, :b => 'string 123', :sub => { :a => 33 } } }
- let(:expected) { "sub:a33#a:2#b:string 123" }
- specify { packed.should == expected }
+ pending
end
end
View
@@ -1 +1,34 @@
-require 'tokenifier'
+require 'tokenifier'
+
+def without_const(const)
+ if Object.const_defined?(const)
+ begin
+ @const = const
+ Object.send(:remove_const, const)
+ yield
+ ensure
+ Object.const_set(const, @const)
+ end
+ else
+ yield
+ end
+end
+
+def with_stub_const(const, value)
+ if Object.const_defined?(const)
+ begin
+ @const = const
+ Object.const_set(const, value)
+ yield
+ ensure
+ Object.const_set(const, @const)
+ end
+ else
+ begin
+ Object.const_set(const, value)
+ yield
+ ensure
+ Object.send(:remove_const, const)
+ end
+ end
+end
@@ -0,0 +1,2 @@
+test:
+ secret: aaabbbccc
View
@@ -25,8 +25,4 @@
specify { data.should == decrypted }
end
- context "rails support" do
-
- end
-
end

0 comments on commit 30d0127

Please sign in to comment.