-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
145 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
# API to create IIIF Authentication access tokens | ||
module Iiif | ||
module Auth | ||
module V2 | ||
# Creates tokens for IIIF auth v2 | ||
# https://iiif.io/api/auth/2.0/#access-token-service | ||
class TokenController < ApplicationController | ||
skip_forgery_protection | ||
|
||
# Returns an HTML response that posts back to the parent window | ||
# See {https://iiif.io/api/auth/2.0/#workflow-from-the-browser-client-perspective} | ||
def create | ||
params.require(:origin) | ||
params.require(:messageId) | ||
|
||
token = mint_bearer_token if token_eligible_user? | ||
|
||
@message = if token | ||
{ | ||
"@context" => "http://iiif.io/api/auth/2/context.json", | ||
type: 'AuthAccessToken2', | ||
accessToken: token, | ||
expiresIn: 3600, | ||
messageId: params[:messageId] | ||
} | ||
else | ||
{ | ||
"@context" => "http://iiif.io/api/auth/2/context.json", | ||
type: 'AuthAccessTokenError2', | ||
profile: 'missingAspect', | ||
error: 'missingCredentials', | ||
heading: "Missing credentials", | ||
messageId: params[:messageId] | ||
} | ||
end | ||
|
||
# The browser-based interaction requires using iframes | ||
# We disable this header (added by default) entirely to ensure | ||
# that IIIF viewers embedded by iframes in other pages will | ||
# work as expected. | ||
response.headers['X-Frame-Options'] = "" | ||
|
||
@origin = params[:origin] | ||
|
||
render 'create', layout: false | ||
end | ||
|
||
private | ||
|
||
# An authenticated user can retrieve a token if they are logged in with webauth, | ||
# or are accessing material from a location-specific kiosk. | ||
# Other anonymous users are not eligible. | ||
def token_eligible_user? | ||
current_user.webauth_user? || current_user.location? | ||
end | ||
|
||
def mint_bearer_token | ||
"#{ActionController::HttpAuthentication::Token::TOKEN_KEY}#{current_user.token.to_s.inspect}" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<html> | ||
<body> | ||
<script> | ||
window.parent.postMessage( | ||
<%= @message.to_json.html_safe %>, | ||
<%= @origin.to_json.html_safe %> | ||
); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe 'IIIF auth v2 tokens' do | ||
let(:user) { User.new(anonymous_locatable_user: true) } | ||
|
||
before do | ||
allow_any_instance_of(Iiif::Auth::V2::TokenController).to receive(:current_user).and_return(user) | ||
end | ||
|
||
describe '#create' do | ||
let(:user) { User.new id: 'xyz', webauth_user: true } | ||
|
||
before do | ||
get '/iiif/auth/v2/token?origin=http://example.edu/&messageId=1' | ||
end | ||
|
||
it 'posts the response' do | ||
expect(response.response_code).to eq 200 | ||
expect(response.headers['X-Frame-Options']).to eq '' | ||
payload = JSON.parse(/({.*\})/.match(response.body)[1]) | ||
expect(payload).to include( | ||
{ "@context" => "http://iiif.io/api/auth/2/context.json", | ||
"type" => "AuthAccessToken2", 'accessToken' => be_present, | ||
'messageId' => '1', 'expiresIn' => 3600 } | ||
) | ||
end | ||
|
||
context 'when there is an error' do | ||
let(:user) { User.new id: 'xyz', webauth_user: false } | ||
|
||
it 'posts the response' do | ||
expect(response.response_code).to eq 200 | ||
expect(response.headers['X-Frame-Options']).to eq '' | ||
payload = JSON.parse(/({.*\})/.match(response.body)[1]) | ||
expect(payload).to include( | ||
{ "@context" => "http://iiif.io/api/auth/2/context.json", | ||
"type" => "AuthAccessTokenError2", 'profile' => 'missingAspect', | ||
'messageId' => '1', | ||
'heading' => 'Missing credentials' } | ||
) | ||
end | ||
end | ||
|
||
context 'when the origin parameter is missing' do | ||
it 'returns a 400 error' do | ||
get '/iiif/auth/v2/token?messageId=1' | ||
expect(response).to have_http_status(:bad_request) | ||
end | ||
end | ||
end | ||
end |