Skip to content

Commit

Permalink
added password parser
Browse files Browse the repository at this point in the history
  • Loading branch information
peppyheppy committed Jan 25, 2012
1 parent d05aab6 commit b1a724f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 14 deletions.
36 changes: 26 additions & 10 deletions lib/janrain/capture/client/entity.rb
Expand Up @@ -9,18 +9,20 @@ class Entity


# https://janraincapture.com/docs/api_entity.html # https://janraincapture.com/docs/api_entity.html
def self.by_token(token, options={}) def self.by_token(token, options={})
get("/entity", headers: { 'Authorization' => "OAuth #{token}" }) logres(get("/entity", headers: { 'Authorization' => "OAuth #{token}" }))
end end


# https://janraincapture.com/docs/api_entity.html # https://janraincapture.com/docs/api_entity.html
def self.by_id(id, overrides={}) def self.by_id(id, overrides={})
attrs = defaults.merge(overrides) attrs = defaults.merge(overrides)
get("/entity", query: attrs.merge(id: id)) logreq(attrs.merge(id: id))
logres(get("/entity", query: attrs.merge(id: id)))
end end


# https://janraincapture.com/docs/api_entity.count.html # https://janraincapture.com/docs/api_entity.count.html
def self.count(overrides={}) def self.count(overrides={})
get("/entity.count", query: defaults.merge(overrides)) logreq(defaults.merge(overrides))
logres(get("/entity.count", query: defaults.merge(overrides)))
end end


# https://janraincapture.com/docs/api_entity.create.html # https://janraincapture.com/docs/api_entity.create.html
Expand All @@ -30,23 +32,25 @@ def self.create(attrs={})
if type_name = attrs.delete(:type_name) if type_name = attrs.delete(:type_name)
params = defaults.merge(type_name: type_name) params = defaults.merge(type_name: type_name)
end end
post("/entity.create", query: params.merge(attributes: attrs.to_json)) logreq(params.merge(attributes: attrs.to_json))
logres(post("/entity.create", query: params.merge(attributes: attrs.to_json)))
end end


# https://janraincapture.com/docs/api_entity.bulkCreate.html # https://janraincapture.com/docs/api_entity.bulkCreate.html
# returns an array of ids and/or errors # returns an array of ids and/or errors
def self.bulk_create(new_entities=[], overrides={}) def self.bulk_create(new_entities=[], overrides={})
post("/entity.bulkCreate", query: defaults. query = defaults.merge(overrides).merge(all_attributes: new_entities.to_json)
merge(overrides). logreq(query)
merge(all_attributes: new_entities.to_json)) logres(post("/entity.bulkCreate", query: query))
end end


# https://janraincapture.com/docs/api_entity.delete.html # https://janraincapture.com/docs/api_entity.delete.html
# XXX: does not support plural deletion, nor created/lastUpdated date # XXX: does not support plural deletion, nor created/lastUpdated date
# checks. Its a sharp delete -- don't hurt yourself! # checks. Its a sharp delete -- don't hurt yourself!
def self.delete(id, overrides={}) def self.delete(id, overrides={})
attrs = defaults.merge(overrides) attrs = defaults.merge(overrides)
post("/entity.delete", query: attrs.merge(id: id)) logreq(attrs.merge(id: id))
logres(post("/entity.delete", query: attrs.merge(id: id)))
end end


# https://janraincapture.com/docs/api_entity.update.html # https://janraincapture.com/docs/api_entity.update.html
Expand All @@ -56,7 +60,9 @@ def self.update(id, attrs={})
if type_name = attrs.delete(:type_name) if type_name = attrs.delete(:type_name)
params = defaults.merge(type_name: type_name) params = defaults.merge(type_name: type_name)
end end
post("/entity.update", query: params.merge(id: id, attributes: attrs.to_json)) query = params.merge(id: id, attributes: attrs.to_json)
logreq(query)
logres(post("/entity.update", query: query))
end end


# XXX: not supporting replace.... don't have a real case for it yet # XXX: not supporting replace.... don't have a real case for it yet
Expand All @@ -65,11 +71,21 @@ def self.update(id, attrs={})
# https://janraincapture.com/docs/api_entity.find.html # https://janraincapture.com/docs/api_entity.find.html
def self.find(filter, options={}) def self.find(filter, options={})
params = defaults.merge(filter: filter).merge(options) params = defaults.merge(filter: filter).merge(options)
get('/entity.find', query: params) logreq(params)
logres(get('/entity.find', query: params))
end end


private private


def self.logreq(attrs)
Rails.logger.info("Janrain::Entity Request: (#{caller.first.split(':in ').last}) #{attrs.inspect}")
end

def self.logres(response)
Rails.logger.info("Janrain::Entity Response: (#{caller.first.split(':in ').last}) #{response.body}")
response
end

def self.defaults def self.defaults
{ {
type_name: Config.capture.entity['schema_type_name'], type_name: Config.capture.entity['schema_type_name'],
Expand Down
17 changes: 14 additions & 3 deletions lib/janrain/capture/user.rb
Expand Up @@ -20,6 +20,19 @@ def authenticate(code, options={})
end end
false false
end end

def password_parser(stuff)
parsed_password = {}
begin
password = JSON.load(stuff) rescue YAML.load(stuff) rescue {}
if password.is_a? Hash
parsed_password = password
end
rescue => e
parsed_password = {}
end
parsed_password
end
end end


def to_capture(only_changes=false) def to_capture(only_changes=false)
Expand All @@ -37,9 +50,7 @@ def to_capture(only_changes=false)
all all
end end


if capture_id.blank? attrs.delete_if { |a,v| a == 'id' || a == 'capture_id'}
attrs.delete_if { |a,v| a == 'id' || a == 'capture_id'}
end
attrs attrs
end end


Expand Down
46 changes: 45 additions & 1 deletion spec/lib/janrain/capture/user_spec.rb
Expand Up @@ -25,6 +25,50 @@
} }
end end


context "password parser" do
it "should return an empty hash if password is null" do
pass = TestUser.password_parser(nil)
pass.should be_a Hash
pass.should be_blank
end

it "should return an empty hash if password is empty string" do
pass = TestUser.password_parser('')
pass.should be_a Hash
pass.should be_blank
end

it "should return an empty hash if password is non json/yaml string" do
pass = TestUser.password_parser('hello hello')
pass.should be_a Hash
pass.should be_blank
end

it "should return an empty hash if password is not valid json/yaml" do
pass = TestUser.password_parser('{ :dddd => :qqqq }')

puts "YYYYY: #{pass.inspect}"

pass.should be_a Hash
pass.should be_blank
end

it "should parse valid json" do
pass = TestUser.password_parser('{ "a": "b"}')
pass.should be_a Hash
pass.should_not be_blank
pass['a'].should == 'b'
end

it "should parse valid yaml" do
pass = TestUser.password_parser("--- \nvalue: p8ssw0rd\ntype: password-rr\nsalt: lee\n")
pass.should be_a Hash
pass.should_not be_blank
pass['value'].should == 'p8ssw0rd'
pass['type'].should == 'password-rr'
end
end

context "capture persistence" do context "capture persistence" do
context "new capture user" do context "new capture user" do


Expand Down Expand Up @@ -108,7 +152,7 @@
@attrs.should be_a Hash @attrs.should be_a Hash
@attrs.should have_key 'email' @attrs.should have_key 'email'
@attrs['email'].should == @user_params[:email] @attrs['email'].should == @user_params[:email]
@attrs.should have_key 'id' # @attrs.should have_key 'id'
end end


it "should exclude locally cached values" do it "should exclude locally cached values" do
Expand Down

0 comments on commit b1a724f

Please sign in to comment.