Skip to content

Commit

Permalink
Pass CAS response to fetch_raw_info (#47)
Browse files Browse the repository at this point in the history
Forwards the XML response received from CASAuth to the fetch_raw_info
callback, which now accepts five arguments (cas, options, ticket, user
info, and the raw response). Developers who override fetch_raw_info with
a Proc are unaffected -- the new argument is silently discarded -- but
developers who use a Lambda will need to update their code.
  • Loading branch information
danschmidt5189 committed Jul 18, 2018
1 parent 8b9bc95 commit 5fcb389
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ Other configuration options:

```ruby
provider :cas,
fetch_raw_info: lambda { |strategy, options, ticket, user_info|
ExternalService.get(user_info[:user]).attributes
}
fetch_raw_info: Proc.new { |strategy, opts, ticket, user_info, rawxml|
return {} if user_info.empty? || rawxml.nil? # Auth failed

extra_info = ExternalService.get(user_info[:user]).attributes
extra_info.merge!({'roles' => rawxml.xpath('//cas:roles').map(&:text)})
extra_info
}
```

Configurable options for values returned by CAS:
Expand Down
7 changes: 5 additions & 2 deletions lib/omniauth/strategies/cas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,11 @@ def validate_service_ticket(ticket)
private

def fetch_raw_info(ticket)
ticket_user_info = validate_service_ticket(ticket).user_info
custom_user_info = options.fetch_raw_info.call(self, options, ticket, ticket_user_info)
validator = validate_service_ticket(ticket)
ticket_user_info = validator.user_info
ticket_success_body = validator.success_body
custom_user_info = options.fetch_raw_info.call(self,
options, ticket, ticket_user_info, ticket_success_body)
self.raw_info = ticket_user_info.merge(custom_user_info)
end

Expand Down
2 changes: 2 additions & 0 deletions lib/omniauth/strategies/cas/service_ticket_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class CAS
class ServiceTicketValidator
VALIDATION_REQUEST_HEADERS = { 'Accept' => '*/*' }

attr_reader :success_body

# Build a validator from a +configuration+, a
# +return_to+ URL, and a +ticket+.
#
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/cas_success.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
<cas:image>/images/user.jpg</cas:image>
<cas:phone>555-555-5555</cas:phone>
<cas:hire_date>2004-07-13</cas:hire_date>
<cas:roles>senator</cas:roles>
<cas:roles>lobbyist</cas:roles>
<cas:roles>financier</cas:roles>
</cas:authenticationSuccess>
</cas:serviceResponse>
3 changes: 3 additions & 0 deletions spec/fixtures/cas_success_jasig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<cas:image>/images/user.jpg</cas:image>
<cas:phone>555-555-5555</cas:phone>
<cas:hire_date>2004-07-13</cas:hire_date>
<cas:roles>senator</cas:roles>
<cas:roles>lobbyist</cas:roles>
<cas:roles>financier</cas:roles>
</cas:attributes>
</cas:authenticationSuccess>
</cas:serviceResponse>
13 changes: 12 additions & 1 deletion spec/omniauth/strategies/cas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@
let(:app) do
Rack::Builder.new {
use OmniAuth::Test::PhonySession
use MyCasProvider, name: :cas, host: 'cas.example.org', ssl: false, port: 8080, uid_field: :employeeid
use MyCasProvider,
name: :cas,
host: 'cas.example.org',
ssl: false,
port: 8080,
uid_field: :employeeid,
fetch_raw_info: Proc.new { |v, opts, ticket, info, node|
info.empty? ? {} : {
"roles" => node.xpath('//cas:roles').map(&:text),
}
}
run lambda { |env| [404, {'Content-Type' => 'text/plain'}, [env.key?('omniauth.auth').to_s]] }
}.to_app
end
Expand Down Expand Up @@ -184,6 +194,7 @@
expect(subject.user).to eq 'psegel'
expect(subject.employeeid).to eq '54'
expect(subject.hire_date).to eq '2004-07-13'
expect(subject.roles).to eq %w(senator lobbyist financier)
end
end

Expand Down

0 comments on commit 5fcb389

Please sign in to comment.