Skip to content

Commit 52adcae

Browse files
committed
fix scoping edge cases
1 parent d0721d3 commit 52adcae

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

app/controllers/api/v1/application_controller.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module V1
33
class ApplicationController < ActionController::API
44
prepend_view_path "app/views/api/v1"
55

6-
helper_method :current_identity, :current_program, :current_scopes, :acting_as_program
6+
helper_method :current_identity, :current_program, :current_scopes, :acting_as_program, :identity_authorized_for_scope?
77

88
attr_reader :current_identity
99
attr_reader :current_program
@@ -22,6 +22,14 @@ class ApplicationController < ActionController::API
2222
render json: { error: e.message }, status: :bad_request
2323
end
2424

25+
def identity_authorized_for_scope?(identity, scope)
26+
if current_identity
27+
@current_token.scopes.include?(scope)
28+
else
29+
identity.access_tokens.to_a.any? { |t| t.application_id == current_program.id && t.scopes.include?(scope) }
30+
end
31+
end
32+
2533
private
2634

2735
def authenticate!
@@ -34,14 +42,15 @@ def authenticate!
3442
if @current_token.is_a?(OAuthToken)
3543
@current_identity = @current_token.resource_owner
3644
@current_program = @current_token.application
45+
@current_scopes = @current_token.scopes
3746
unless @current_program&.active?
3847
return render json: { error: "invalid_auth" }, status: :unauthorized
3948
end
4049
else
4150
@acting_as_program = true
4251
@current_program = @current_token
52+
@current_scopes = @current_program.scopes
4353
end
44-
@current_scopes = @current_program.scopes
4554
end
4655
end
4756
end

app/controllers/api/v1/identities_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def me
99

1010
def show
1111
raise Pundit::NotAuthorizedError unless acting_as_program
12-
@identity = ident_scope.find_by_public_id!(params[:id])
12+
@identity = ident_scope.includes(:access_tokens, :addresses, :verifications).find_by_public_id!(params[:id])
1313
render :show
1414
end
1515

@@ -28,7 +28,7 @@ def set_slack_id
2828

2929
def index
3030
raise Pundit::NotAuthorizedError unless acting_as_program
31-
@identities = ident_scope.all
31+
@identities = ident_scope.all.includes(:access_tokens, :addresses, :verifications)
3232
render :index
3333
end
3434

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module API::V1::ApplicationHelper
2-
def scope(scope, &)
2+
def scope(scope, identity: nil, &)
33
return unless current_scopes.include?(scope)
4+
return unless identity.nil? || identity_authorized_for_scope?(identity, scope)
45
yield
56
end
67
end

app/views/api/v1/identities/_identity.jb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ ident = {
22
id: identity.public_id
33
}
44

5-
scope "verification_status" do
5+
scope "verification_status", identity: do
66
ident[:ysws_eligible] = identity.ysws_eligible
77
ident[:verification_status] = identity.verification_status
88
end
99

10-
scope "basic_info" do
10+
scope "basic_info", identity: do
1111
ident[:first_name] = identity.first_name
1212
ident[:last_name] = identity.last_name
1313
ident[:primary_email] = identity.primary_email
@@ -22,25 +22,25 @@ scope "basic_info" do
2222
ident[:birthday] = identity.birthday
2323
end
2424

25-
scope "email" do
25+
scope "email", identity: do
2626
ident[:primary_email] = identity.primary_email
2727
end
2828

29-
scope "name" do
29+
scope "name", identity: do
3030
ident[:first_name] = identity.first_name
3131
ident[:last_name] = identity.last_name
3232
end
3333

34-
scope "slack_id" do
34+
scope "slack_id", identity: do
3535
ident[:slack_id] = identity.slack_id
3636
end
3737

38-
scope "legal_name" do
38+
scope "legal_name", identity: do
3939
ident[:legal_first_name] = identity.legal_first_name
4040
ident[:legal_last_name] = identity.legal_last_name
4141
end
4242

43-
scope "address" do
43+
scope "address", identity: do
4444
ident[:addresses] = identity.addresses.map do |address|
4545
render address
4646
end

0 commit comments

Comments
 (0)