Skip to content

Commit

Permalink
Provide a way to fetch all ticket attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
rhruiz committed Jul 12, 2023
1 parent 4e825e2 commit cc80fdd
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## UNRELEASED

### Added
- Provide `Cassette::Authentication::User#attribute` to fetch generic attributes
from the user in the ticket validation response

## [1.6.0] - 2020-11-26
### Changed
- Make `cas_extra_attributes` accessible as strings or symbols when restoring
Expand Down
18 changes: 11 additions & 7 deletions lib/cassette/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ def ticket_user(ticket, service = config.service)

logger.info("Validation resut: #{response.inspect}")

Cassette::Authentication::User.new(
login: ticket_response.login,
name: ticket_response.name,
authorities: ticket_response.authorities,
ticket: ticket,
config: config
) if ticket_response.login
if ticket_response.login
attributes = ticket_response.attributes.dup.merge(
login: ticket_response.login,
name: ticket_response.name,
authorities: ticket_response.authorities,
ticket: ticket,
config: config
)

Cassette::Authentication::User.new(attributes)
end
rescue => exception
logger.error "Error while authenticating ticket #{ticket}: #{exception.message}"
raise Cassette::Errors::Forbidden, exception.message
Expand Down
5 changes: 5 additions & 0 deletions lib/cassette/authentication/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ def initialize(attrs = {})
@type = attrs[:type]
@email = attrs[:email]
@ticket = attrs[:ticket]
@attributes = attrs
@authorities = Cassette::Authentication::Authorities
.parse(attrs.fetch(:authorities, '[]'), config && config.base_authority)
end

def attribute(key)
attributes[key]
end

%w(customer employee).each do |type|
define_method :"#{type}?" do
!@type.nil? && @type.to_s.downcase == type.to_s
Expand Down
7 changes: 4 additions & 3 deletions lib/cassette/http/ticket_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module Cassette
module Http
class TicketResponse
attr_reader :login, :name, :authorities
attr_reader :login, :name, :authorities, :attributes

def initialize(response)
namespaces = { "cas" => "http://www.yale.edu/tp/cas" }
Expand All @@ -22,8 +22,9 @@ def initialize(response)
elements.
map { |e| [e.name, e.text] }]

@name = attributes['cn']
@authorities = attributes['authorities']
@name = attributes.delete('cn')
@authorities = attributes.delete('authorities')
@attributes = attributes
end
end
end
Expand Down
46 changes: 46 additions & 0 deletions spec/cassette/authentication/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,52 @@
authorities: '[CUSTOMERAPI, SAPI]', config: config)
end
end

context 'attributes' do
it 'takes login from the attributes' do
user = described_class.new(login: 'john.doe')

expect(user.login).to eql('john.doe')
end

it 'takes name from the attributes' do
user = described_class.new(name: 'John Doe')

expect(user.name).to eql('John Doe')
end
end
end

describe '#attribute' do
it 'returns attributes given to the user' do
user = described_class.new(
login: 'john.doe',
name: 'John Doe',
attributes: { 'attribute' => 'something' }
)

expect(user.attribute('attribute')).to eql('something')
end

it 'retuns nil for attributes not given to the user' do
user = described_class.new(
login: 'john.doe',
name: 'John Doe',
attributes: { 'attribute' => 'something' }
)

expect(user.attribute('other_attribute')).to be_nil
end

it 'does not return attributes that are already extracted' do
user = described_class.new(
login: 'john.doe',
name: 'John Doe',
attributes: { 'attribute' => 'something' }
)

expect(user.attribute('login')).to be_nil
end
end

describe '#has_role?' do
Expand Down
6 changes: 6 additions & 0 deletions spec/cassette/authentication_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
it 'returns an User' do
expect(ticket_user).to be_instance_of(Cassette::Authentication::User)
end

it 'sets attributes from the ticket response' do
expect(ticket_user.login).to eql('test-user')
expect(ticket_user.name).to eql('Test System')
expect(ticket_user.attribute('type')).to eql('system')
end
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/cassette/http/ticket_response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,22 @@
it { is_expected.to be_nil }
end
end

describe '#attributes' do
subject(:attributes) { ticket_response.attributes }

context 'when response is successful' do
let(:xml_response) { fixture('cas/success.xml') }

it 'returns the attributes not already extracted' do
expect(subject).to eq('type' => 'System', 'attribute' => 'something')
end
end

context 'when response is not successful' do
let(:xml_response) { fixture('cas/fail.xml') }

it { is_expected.to be_empty }
end
end
end
2 changes: 2 additions & 0 deletions spec/fixtures/cas/success.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<cas:attributes>
<cas:authorities>[CUPOM, AUDITING,]</cas:authorities>
<cas:cn>Test System</cas:cn>
<cas:type>System</cas:type>
<cas:attribute>something</cas:attribute>
</cas:attributes>
<!-- End Ldap Attributes -->
</cas:authenticationSuccess>
Expand Down

0 comments on commit cc80fdd

Please sign in to comment.