Skip to content

Commit

Permalink
distinguish non-interactive token services by url (DLC-1177)
Browse files Browse the repository at this point in the history
  • Loading branch information
barmintor committed Jan 31, 2025
1 parent a4c66eb commit eaa32f1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
6 changes: 4 additions & 2 deletions app/models/iiif/authz/base_access_token_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ class Iiif::Authz::BaseAccessTokenService
attr_reader :id, :canvas, :route_helper
JWT_HEADER = { alg: 'HS256', typ: 'JWT' }.freeze

def initialize(canvas, route_helper:, format: nil)
def initialize(canvas, route_helper:, format: nil, profile: 'active')
@canvas = canvas
@id = route_helper.bytestream_token_url({catalog_id: canvas.solr_document.id, bytestream_id: 'content', format: format}.compact)
id_params = {catalog_id: canvas.solr_document.id, bytestream_id: 'content', format: format}
id_params[:profile] = profile unless profile == 'active'
@id = route_helper.bytestream_token_url(id_params.compact)
@route_helper = route_helper
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/iiif/authz/v2/external_access_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(canvas, route_helper:, **_args)
end

def token_service
Iiif::Authz::V2::AccessTokenService.new(canvas, route_helper: route_helper).to_h
Iiif::Authz::V2::AccessTokenService.new(canvas, route_helper: route_helper, profile: PROFILE).to_h
end

def to_h
Expand Down
2 changes: 1 addition & 1 deletion app/models/iiif/authz/v2/local_access_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(canvas, route_helper:, profile:)
end

def token_service
Iiif::Authz::V2::AccessTokenService.new(canvas, route_helper: route_helper).to_h
Iiif::Authz::V2::AccessTokenService.new(canvas, route_helper: route_helper, profile: @profile).to_h
end

def to_h
Expand Down
47 changes: 47 additions & 0 deletions spec/models/iiif/authz/v2/access_token_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'rails_helper'

describe Iiif::Authz::V2::AccessTokenService do
subject(:access_token_service) {
described_class.new(
canvas, route_helper: routes, format: format, profile: profile
)
}
let(:canvas) { instance_double(Iiif::Canvas) }
let(:expected_id) { 'expected_id' }
let(:format) { nil }
let(:routes) { instance_double(ApplicationController) }
let(:solr_document_id) { 'solr_document_id' }

before do
allow(canvas).to receive(:solr_document).and_return(SolrDocument.new({id: solr_document_id}))
allow(routes).to receive(:bytestream_token_url).with(id_params).and_return(expected_id)
end

context 'profile is external' do
let(:profile) { 'external' }
let(:id_params) { {catalog_id: solr_document_id, bytestream_id: 'content', profile: profile} }

it "creates a hashable token service with the expected id" do
expect(access_token_service.to_h['id']).to be expected_id
end
end

context 'profile is kiosk' do
let(:profile) { 'kiosk' }
let(:id_params) { {catalog_id: solr_document_id, bytestream_id: 'content', profile: profile} }

it "creates a hashable token service with the expected id" do
expect(access_token_service.to_h['id']).to be expected_id
end
end

context 'profile is active' do
let(:profile) { 'active' }
# active profile should not include additional query params to distinguish it
let(:id_params) { {catalog_id: solr_document_id, bytestream_id: 'content'} }

it "creates a hashable token service with the expected id" do
expect(access_token_service.to_h['id']).to be expected_id
end
end
end

0 comments on commit eaa32f1

Please sign in to comment.