Skip to content

Commit e561b14

Browse files
osw-gitlabrspeicher
authored andcommitted
Backport gitlab-ee!2456
1 parent 9981814 commit e561b14

12 files changed

+57
-8
lines changed

app/controllers/admin/applications_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ def set_application
5050

5151
# Only allow a trusted parameter "white list" through.
5252
def application_params
53-
params[:doorkeeper_application].permit(:name, :redirect_uri, :scopes)
53+
params[:doorkeeper_application].permit(:name, :redirect_uri, :trusted, :scopes)
5454
end
5555
end

app/views/admin/applications/_form.html.haml

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
.col-sm-10
77
= f.text_field :name, class: 'form-control'
88
= doorkeeper_errors_for application, :name
9+
910
= content_tag :div, class: 'form-group' do
1011
= f.label :redirect_uri, class: 'col-sm-2 control-label'
1112
.col-sm-10
@@ -19,6 +20,13 @@
1920
%code= Doorkeeper.configuration.native_redirect_uri
2021
for local tests
2122

23+
= content_tag :div, class: 'form-group' do
24+
= f.label :trusted, class: 'col-sm-2 control-label'
25+
.col-sm-10
26+
= f.check_box :trusted
27+
%span.help-block
28+
Trusted applications are automatically authorized on GitLab OAuth flow.
29+
2230
.form-group
2331
= f.label :scopes, class: 'col-sm-2 control-label'
2432
.col-sm-10

app/views/admin/applications/index.html.haml

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
%th Name
1212
%th Callback URL
1313
%th Clients
14+
%th Trusted
1415
%th
1516
%th
1617
%tbody.oauth-applications
@@ -19,5 +20,6 @@
1920
%td= link_to application.name, admin_application_path(application)
2021
%td= application.redirect_uri
2122
%td= application.access_tokens.map(&:resource_owner_id).uniq.count
23+
%td= application.trusted? ? 'Y': 'N'
2224
%td= link_to 'Edit', edit_admin_application_path(application), class: 'btn btn-link'
2325
%td= render 'delete_form', application: application

app/views/admin/applications/show.html.haml

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
%div
2424
%span.monospace= uri
2525

26+
%tr
27+
%td
28+
Trusted
29+
%td
30+
= @application.trusted? ? 'Y' : 'N'
31+
2632
= render "shared/tokens/scopes_list", token: @application
2733

2834
.form-actions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: Skip oAuth authorization for trusted applications
3+
merge_request:
4+
author:

config/initializers/doorkeeper.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@
9292
# Under some circumstances you might want to have applications auto-approved,
9393
# so that the user skips the authorization step.
9494
# For example if dealing with trusted a application.
95-
# skip_authorization do |resource_owner, client|
96-
# client.superapp? or resource_owner.admin?
97-
# end
95+
skip_authorization do |resource_owner, client|
96+
client.application.trusted?
97+
end
9898

9999
# WWW-Authenticate Realm (default "Doorkeeper").
100100
# realm "Doorkeeper"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class AddTrustedColumnToOauthApplications < ActiveRecord::Migration
2+
include Gitlab::Database::MigrationHelpers
3+
4+
DOWNTIME = false
5+
6+
disable_ddl_transaction!
7+
8+
def up
9+
add_column_with_default(:oauth_applications, :trusted, :boolean, default: false)
10+
end
11+
12+
def down
13+
remove_column(:oauth_applications, :trusted)
14+
end
15+
end

db/schema.rb

+1
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,7 @@
997997
t.datetime "updated_at"
998998
t.integer "owner_id"
999999
t.string "owner_type"
1000+
t.boolean "trusted", default: false, null: false
10001001
end
10011002

10021003
add_index "oauth_applications", ["owner_id", "owner_type"], name: "index_oauth_applications_on_owner_id_and_owner_type", using: :btree

doc/integration/oauth_provider.md

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ it from the admin area.
6363

6464
![OAuth admin_applications](img/oauth_provider_admin_application.png)
6565

66+
You're also able to mark an application as _trusted_ when creating it through the admin area. By doing that,
67+
the user authorization step is automatically skipped for this application.
68+
6669
---
6770

6871
## Authorized applications

spec/controllers/admin/applications_controller_spec.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@
2828

2929
describe 'POST #create' do
3030
it 'creates the application' do
31+
create_params = attributes_for(:application, trusted: true)
32+
3133
expect do
32-
post :create, doorkeeper_application: attributes_for(:application)
34+
post :create, doorkeeper_application: create_params
3335
end.to change { Doorkeeper::Application.count }.by(1)
3436

3537
application = Doorkeeper::Application.last
3638

3739
expect(response).to redirect_to(admin_application_path(application))
40+
expect(application).to have_attributes(create_params.except(:uid, :owner_type))
3841
end
3942

4043
it 'renders the application form on errors' do
@@ -49,10 +52,12 @@
4952

5053
describe 'PATCH #update' do
5154
it 'updates the application' do
52-
patch :update, id: application.id, doorkeeper_application: { redirect_uri: 'http://example.com/' }
55+
patch :update, id: application.id, doorkeeper_application: { redirect_uri: 'http://example.com/', trusted: true }
56+
57+
application.reload
5358

5459
expect(response).to redirect_to(admin_application_path(application))
55-
expect(application.reload.redirect_uri).to eq 'http://example.com/'
60+
expect(application).to have_attributes(redirect_uri: 'http://example.com/', trusted: true)
5661
end
5762

5863
it 'renders the application form on errors' do

spec/controllers/oauth/authorizations_controller_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
end
4343

4444
it 'deletes session.user_return_to and redirects when skip authorization' do
45+
doorkeeper.update(trusted: true)
4546
request.session['user_return_to'] = 'http://example.com'
46-
allow(controller).to receive(:skip_authorization?).and_return(true)
4747

4848
get :new, params
4949

spec/features/admin/admin_manage_applications_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,24 @@
1313

1414
fill_in :doorkeeper_application_name, with: 'test'
1515
fill_in :doorkeeper_application_redirect_uri, with: 'https://test.com'
16+
check :doorkeeper_application_trusted
1617
click_on 'Submit'
1718
expect(page).to have_content('Application: test')
1819
expect(page).to have_content('Application Id')
1920
expect(page).to have_content('Secret')
21+
expect(page).to have_content('Trusted Y')
2022

2123
click_on 'Edit'
2224
expect(page).to have_content('Edit application')
2325

2426
fill_in :doorkeeper_application_name, with: 'test_changed'
27+
uncheck :doorkeeper_application_trusted
28+
2529
click_on 'Submit'
2630
expect(page).to have_content('test_changed')
2731
expect(page).to have_content('Application Id')
2832
expect(page).to have_content('Secret')
33+
expect(page).to have_content('Trusted N')
2934

3035
visit admin_applications_path
3136
page.within '.oauth-applications' do

0 commit comments

Comments
 (0)