riiif 0.2.4 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +19 -0
- data/app/controllers/riiif/images_controller.rb +24 -9
- data/app/models/riiif/image.rb +2 -1
- data/lib/riiif/nil_authorization_service.rb +10 -0
- data/lib/riiif/version.rb +1 -1
- data/lib/riiif.rb +1 -0
- data/spec/controllers/images_controller_spec.rb +46 -17
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba70ff2bc96642dfaef0a13aa368721e64d819b9
|
4
|
+
data.tar.gz: f2211f18a7da732e70f6151b907c6cbf66d2a88a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a593880f25ace996a90457cf99aaba644fc4c4410622e4203f3e968609d598ffd1080b33698e45d9de3b9f63be76b836b89b12a236c49e5bfb9c231e2b8dcaeb
|
7
|
+
data.tar.gz: b560c0e4f6bb82d44c671f1c32a0ddf24eb6aeac633357aa9e0885886e247a6e987055c629b2ef618499c486f9c1c80d5c285bda00ee972a81a6070e66d1cdc5
|
data/README.md
CHANGED
@@ -101,6 +101,25 @@ You can do this to create a default Riiif::Image to use (useful for passing "mis
|
|
101
101
|
Riiif::Image.new('no_image', Riiif::File.new(Riiif.not_found_image))
|
102
102
|
```
|
103
103
|
|
104
|
+
## Authorization
|
105
|
+
|
106
|
+
The controller will call an authorization service with the controller context. This service must have a method `can?(action, image)` which returns a boolean. The default service is the `RIIIF::NilAuthrorizationService` which permits all requests.
|
107
|
+
|
108
|
+
In this example we've dissallowed all requests:
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
class NoService
|
112
|
+
def initalize(controller)
|
113
|
+
end
|
114
|
+
|
115
|
+
def can?(action, image)
|
116
|
+
false
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
Riiif::Image.authorization_service = NoService
|
121
|
+
```
|
122
|
+
|
104
123
|
## Integration with Hydra/Fedora
|
105
124
|
|
106
125
|
Create an initializer like this in `config/initializers/riiif_initializer.rb`
|
@@ -9,15 +9,17 @@ module Riiif
|
|
9
9
|
def show
|
10
10
|
begin
|
11
11
|
image = model.new(image_id)
|
12
|
-
status = :
|
12
|
+
status = if authorization_service.can?(:show, image)
|
13
|
+
:ok
|
14
|
+
else
|
15
|
+
:unauthorized
|
16
|
+
end
|
13
17
|
rescue ImageNotFoundError
|
14
|
-
|
15
|
-
image = model.new(image_id, Riiif::File.new(Riiif.not_found_image))
|
16
|
-
status = :not_found
|
17
|
-
else
|
18
|
-
raise
|
19
|
-
end
|
18
|
+
status = :not_found
|
20
19
|
end
|
20
|
+
|
21
|
+
image = not_found_image unless status == :ok
|
22
|
+
|
21
23
|
data = image.render(params.permit(:region, :size, :rotation, :quality, :format))
|
22
24
|
headers['Access-Control-Allow-Origin'] = '*'
|
23
25
|
send_data data,
|
@@ -28,8 +30,12 @@ module Riiif
|
|
28
30
|
|
29
31
|
def info
|
30
32
|
image = model.new(image_id)
|
31
|
-
|
32
|
-
|
33
|
+
if authorization_service.can?(:info, image)
|
34
|
+
headers['Access-Control-Allow-Origin'] = '*'
|
35
|
+
render json: image.info.merge(server_info), content_type: 'application/ld+json'
|
36
|
+
else
|
37
|
+
render json: { error: 'unauthorized' }, status: :unauthorized
|
38
|
+
end
|
33
39
|
end
|
34
40
|
|
35
41
|
# this is a workaround for https://github.com/rails/rails/issues/25087
|
@@ -50,10 +56,19 @@ module Riiif
|
|
50
56
|
params[:id]
|
51
57
|
end
|
52
58
|
|
59
|
+
def authorization_service
|
60
|
+
model.authorization_service.new(self)
|
61
|
+
end
|
62
|
+
|
53
63
|
def link_header
|
54
64
|
response.headers["Link"] = "<#{LEVEL1}>;rel=\"profile\""
|
55
65
|
end
|
56
66
|
|
67
|
+
def not_found_image
|
68
|
+
raise "Not found image doesn't exist" unless Riiif.not_found_image
|
69
|
+
model.new(image_id, Riiif::File.new(Riiif.not_found_image))
|
70
|
+
end
|
71
|
+
|
57
72
|
CONTEXT = '@context'
|
58
73
|
CONTEXT_URI = 'http://iiif.io/api/image/2/context.json'
|
59
74
|
ID = '@id'
|
data/app/models/riiif/image.rb
CHANGED
@@ -2,8 +2,9 @@ require 'digest/md5'
|
|
2
2
|
module Riiif
|
3
3
|
class Image
|
4
4
|
|
5
|
-
class_attribute :file_resolver, :info_service
|
5
|
+
class_attribute :file_resolver, :info_service, :authorization_service
|
6
6
|
self.file_resolver = FileSystemFileResolver.new
|
7
|
+
self.authorization_service = NilAuthorizationService
|
7
8
|
|
8
9
|
# this is the default info service
|
9
10
|
# returns a hash with the original image dimensions.
|
data/lib/riiif/version.rb
CHANGED
data/lib/riiif.rb
CHANGED
@@ -6,7 +6,7 @@ describe Riiif::ImagesController do
|
|
6
6
|
routes { Riiif::Engine.routes }
|
7
7
|
|
8
8
|
describe "#show" do
|
9
|
-
it "
|
9
|
+
it "sends images to the service" do
|
10
10
|
image = double
|
11
11
|
expect(Riiif::Image).to receive(:new).with('abcd1234').and_return(image)
|
12
12
|
expect(image).to receive(:render).with("region" => 'full', "size" => 'full',
|
@@ -20,6 +20,21 @@ describe Riiif::ImagesController do
|
|
20
20
|
expect(response.headers['Access-Control-Allow-Origin']).to eq '*'
|
21
21
|
end
|
22
22
|
|
23
|
+
context "with an unauthorized image" do
|
24
|
+
let(:auth) { double("no auth service", can?: false) }
|
25
|
+
let(:not_found_image) { double("not_found_image", render: 'test data') }
|
26
|
+
before do
|
27
|
+
allow(controller).to receive(:authorization_service).and_return(auth)
|
28
|
+
allow(controller).to receive(:not_found_image).and_return(not_found_image)
|
29
|
+
end
|
30
|
+
it "renders 401" do
|
31
|
+
get :show, id: 'abcd1234', action: "show", region: 'full', size: 'full',
|
32
|
+
rotation: '0', quality: 'default', format: 'jpg'
|
33
|
+
expect(response.body).to eq "test data"
|
34
|
+
expect(response.code).to eq "401"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
23
38
|
context "with a invalid region" do
|
24
39
|
it "renders 400" do
|
25
40
|
image = double("an image")
|
@@ -89,21 +104,35 @@ describe Riiif::ImagesController do
|
|
89
104
|
end
|
90
105
|
end
|
91
106
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
"@
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
107
|
+
describe "info" do
|
108
|
+
it "returns info" do
|
109
|
+
image = double
|
110
|
+
expect(Riiif::Image).to receive(:new).with('abcd1234').and_return(image)
|
111
|
+
expect(image).to receive(:info).and_return({width: 6000, height: 4000 })
|
112
|
+
get :info, id: 'abcd1234', format: 'json'
|
113
|
+
expect(response).to be_successful
|
114
|
+
json = JSON.parse(response.body)
|
115
|
+
expect(json).to eq "@context" => "http://iiif.io/api/image/2/context.json",
|
116
|
+
"@id" =>"http://test.host/images/abcd1234",
|
117
|
+
"width" =>6000,
|
118
|
+
"height" =>4000,
|
119
|
+
"profile" => ["http://iiif.io/api/image/2/level1.json", "formats" => ["jpg", "png"]],
|
120
|
+
'protocol' => 'http://iiif.io/api/image'
|
121
|
+
expect(response.headers['Link']).to eq '<http://iiif.io/api/image/2/level1.json>;rel="profile"'
|
122
|
+
expect(response.headers['Content-Type']).to eq 'application/ld+json; charset=utf-8'
|
123
|
+
expect(response.headers['Access-Control-Allow-Origin']).to eq '*'
|
124
|
+
end
|
125
|
+
|
126
|
+
context "with an unauthorized image" do
|
127
|
+
let(:auth) { double("no auth service", can?: false) }
|
128
|
+
before do
|
129
|
+
allow(controller).to receive(:authorization_service).and_return(auth)
|
130
|
+
end
|
131
|
+
it "renders 401" do
|
132
|
+
get :info, id: 'abcd1234', format: 'json'
|
133
|
+
expect(response.body).to eq "{\"error\":\"unauthorized\"}"
|
134
|
+
expect(response.code).to eq "401"
|
135
|
+
end
|
136
|
+
end
|
108
137
|
end
|
109
138
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: riiif
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Coyne
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -118,6 +118,7 @@ files:
|
|
118
118
|
- lib/riiif/engine.rb
|
119
119
|
- lib/riiif/file_system_file_resolver.rb
|
120
120
|
- lib/riiif/http_file_resolver.rb
|
121
|
+
- lib/riiif/nil_authorization_service.rb
|
121
122
|
- lib/riiif/rails/routes.rb
|
122
123
|
- lib/riiif/routes.rb
|
123
124
|
- lib/riiif/version.rb
|