Skip to content

Commit

Permalink
Add support for user session destruction and token validation and inv…
Browse files Browse the repository at this point in the history
…alidation
  • Loading branch information
Jeff Kunkle committed Mar 11, 2013
1 parent 266697f commit 53be429
Show file tree
Hide file tree
Showing 4 changed files with 740 additions and 244 deletions.
4 changes: 2 additions & 2 deletions crowd_rest.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_dependency 'httparty'
s.add_dependency 'httparty', '~>0.10.2'

s.add_development_dependency 'rspec'
s.add_development_dependency 'vcr'
s.add_development_dependency 'vcr', '~>1.0'
s.add_development_dependency 'fakeweb'
end
39 changes: 34 additions & 5 deletions lib/crowd_rest/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.create(username, password)
successful_response.token = response['session']['token']
end
end

def self.find(token, options = {})
request_user = options[:include] && options[:include] == :user
path = "/session/#{token}"
Expand All @@ -23,18 +23,47 @@ def self.find(token, options = {})
successful_response.user = CrowdRest::User.new(user)
end
end


def self.destroy(username, options = {})
path = "/session?username=#{username}"
path << "&except=#{options.except}" if options[:except]
response = CrowdRest.delete(path)
normalize_response(response, 204)
end

def self.validate(token, validation_factors = {})
path = "/session/#{token}"
body = "<validation-factors>"
validation_factors.each do |name, value|
body << "<validation-factor>
<name>#{name}</name>
<value>#{value}</value>
</validation-factor>"
end
body << "</validation-factors>"
response = CrowdRest.post(path, :body => body)
normalize_response(response, 200) do |successful_response|
successful_response.token = response['session']['token']
end
end

def self.invalidate(token)
path = "/session/#{token}"
response = CrowdRest.delete(path)
normalize_response(response, 204)
end

private
def self.normalize_response(response, success_code = 200)
attributes = {
:code => response.code,
:body => response.body,
:reason => response['reason'],
:message => response['message']
:reason => response['error'] ? response['error']['reason'] : response['reason'] || nil,
:message => response['error'] ? response['error']['message'] : response['message'] || nil
}

norm_response = OpenStruct.new(attributes)
yield(norm_response) if response.code == success_code
yield(norm_response) if block_given? && response.code == success_code
norm_response
end
end
Expand Down
Loading

0 comments on commit 53be429

Please sign in to comment.