Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/pull/3300'
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Oct 7, 2021
2 parents 0ac0f4f + 350ac30 commit 5966acc
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 80 deletions.
6 changes: 6 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,10 @@ def safe_referer(referer)

referer.to_s
end

def scope_enabled?(scope)
doorkeeper_token&.includes_scope?(scope) || current_token&.includes_scope?(scope)
end

helper_method :scope_enabled?
end
4 changes: 2 additions & 2 deletions app/controllers/oauth2_applications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def set_application
end

def application_params
params[:doorkeeper_application][:scopes]&.delete("")
params.require(:doorkeeper_application)
params[:oauth2_application][:scopes]&.delete("")
params.require(:oauth2_application)
.permit(:name, :redirect_uri, :confidential, :scopes => [])
.merge(:owner => current_resource_owner)
end
Expand Down
13 changes: 13 additions & 0 deletions app/models/oauth2_application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Oauth2Application < Doorkeeper::Application
belongs_to :owner, :polymorphic => true

validate :allowed_scopes

private

def allowed_scopes
return if owner.administrator?

errors.add(:scopes) if scopes.any? { |scope| Oauth::PRIVILEGED_SCOPES.include?(scope) }
end
end
2 changes: 2 additions & 0 deletions app/views/api/users/_user.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,7 @@ json.user do
json.count user.sent_messages.size
end
end

json.email user.email if scope_enabled?(:read_email)
end
end
1 change: 1 addition & 0 deletions app/views/api/users/_user.xml.builder
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ xml.tag! "user", :id => user.id,
:unread => user.new_messages.size
xml.tag! "sent", :count => user.sent_messages.size
end
xml.tag! "email", user.email if scope_enabled?(:read_email)
end
end
2 changes: 1 addition & 1 deletion app/views/oauth2_applications/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
<%= f.form_group :confidential do %>
<%= f.check_box :confidential %>
<% end %>
<%= f.collection_check_boxes :scopes, Oauth.scopes, :name, :description %>
<%= f.collection_check_boxes :scopes, Oauth.scopes(:privileged => current_user.administrator?), :name, :description %>
<%= f.primary %>
12 changes: 7 additions & 5 deletions config/initializers/doorkeeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
# end
# end

application_class "Oauth2Application"

# Enables polymorphic Resource Owner association for Access Tokens and Access Grants.
# By default this option is disabled.
#
Expand Down Expand Up @@ -221,7 +223,7 @@
# https://doorkeeper.gitbook.io/guides/ruby-on-rails/scopes

# default_scopes :public
optional_scopes(*Oauth::SCOPES)
optional_scopes(*Oauth::SCOPES, *Oauth::PRIVILEGED_SCOPES)

# Allows to restrict only certain scopes for grant_type.
# By default, all the scopes will be available for all the grant types.
Expand Down Expand Up @@ -417,10 +419,10 @@
# Under some circumstances you might want to have applications auto-approved,
# so that the user skips the authorization step.
# For example if dealing with a trusted application.
#
# skip_authorization do |resource_owner, client|
# client.superapp? or resource_owner.admin?
# end

skip_authorization do |_, client|
client.scopes.include?("skip_authorization")
end

# Configure custom constraints for the Token Introspection request.
# By default this configuration option allows to introspect a token by another
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2367,6 +2367,8 @@ en:
read_gpx: Read private GPS traces
write_gpx: Upload GPS traces
write_notes: Modify notes
read_email: Read user email address
skip_authorization: Auto approve application
oauth_clients:
new:
title: "Register a new application"
Expand Down
7 changes: 5 additions & 2 deletions lib/oauth.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Oauth
SCOPES = %w[read_prefs write_prefs write_diary write_api read_gpx write_gpx write_notes].freeze
PRIVILEGED_SCOPES = %w[read_email skip_authorization].freeze

class Scope
attr_reader :name
Expand All @@ -13,7 +14,9 @@ def description
end
end

def self.scopes
SCOPES.collect { |s| Scope.new(s) }
def self.scopes(privileged: false)
scopes = SCOPES
scopes += PRIVILEGED_SCOPES if privileged
scopes.collect { |s| Scope.new(s) }
end
end
Loading

0 comments on commit 5966acc

Please sign in to comment.