Skip to content
This repository has been archived by the owner on Feb 16, 2018. It is now read-only.

Commit

Permalink
Apps can read their own data
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Stuart committed Sep 4, 2012
1 parent 64b90e1 commit 301af3a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
4 changes: 2 additions & 2 deletions lib/tentd/api/apps.rb
Expand Up @@ -18,8 +18,8 @@ def action(env)

class AuthorizeReadOne < Middleware
def action(env)
if env.params.app_id && env.current_auth && env.current_auth.kind_of?(Model::AppAuthorization) &&
env.current_auth.app_id == env.params.app_id
if env.params.app_id && env.current_auth && ((env.current_auth.kind_of?(Model::AppAuthorization) &&
env.current_auth.app_id == env.params.app_id) || (env.current_auth.kind_of?(Model::App) && env.current_auth.id == env.params.app_id))
(env.authorized_scopes ||= []) << :read_secrets if env.params.read_secrets.to_s == 'true'
else
authorize_env!(env, :read_apps)
Expand Down
54 changes: 35 additions & 19 deletions spec/integration/api/apps_spec.rb
Expand Up @@ -10,6 +10,7 @@ def authorize!(*scopes)
env['current_auth'] = stub(
:kind_of? => true,
:app_id => nil,
:id => nil,
:scopes => scopes
)
end
Expand Down Expand Up @@ -141,32 +142,47 @@ def authorize!(*scopes)

context 'when authorized via identity' do
let(:_app) { Fabricate(:app) }
before do
env['current_auth'] = Fabricate(:app_authorization, :app => _app)
end
examples = proc do
context 'app with :id exists' do
context 'with read_secrets params' do
before { params['read_secrets'] = true }
it 'should return app with mac_key' do
app = _app
json_get "/apps/#{app.public_id}", params, env
expect(last_response.status).to eq(200)
body = JSON.parse(last_response.body)
[:name, :description, :url, :icon, :redirect_uris, :scopes, :mac_key_id, :mac_key, :mac_timestamp_delta, :mac_algorithm].each { |key|
expect(body[key.to_s].to_json).to eq(app.send(key).to_json)
}
expect(body['id']).to eq(app.public_id)
end
end

context 'app with :id exists' do
context 'with read_secrets params' do
before { params['read_secrets'] = true }
it 'should return app with mac_key' do
app = _app
json_get "/apps/#{app.public_id}", params, env
body = JSON.parse(last_response.body)
[:name, :description, :url, :icon, :redirect_uris, :scopes, :mac_key_id, :mac_key, :mac_timestamp_delta, :mac_algorithm].each { |key|
expect(body[key.to_s].to_json).to eq(app.send(key).to_json)
}
expect(body['id']).to eq(app.public_id)
context 'without read_secrets params', &without_mac_key
end

context 'app with :id does not exist' do
it 'should return 403' do
json_get '/apps/app-id', params, env
expect(last_response.status).to eq(403)
end
end
end

context 'without read_secrets params', &without_mac_key
context 'when AppAuthorization' do
before do
env['current_auth'] = Fabricate(:app_authorization, :app => _app)
end

context &examples
end

context 'app with :id does not exist' do
it 'should return 403' do
json_get '/apps/app-id', params, env
expect(last_response.status).to eq(403)
context 'when App' do
before do
env['current_auth'] = _app
end

context &examples
end
end

Expand Down

0 comments on commit 301af3a

Please sign in to comment.