-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Facebook deletion callback #7093
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tomhughes
wants to merge
2
commits into
openstreetmap:master
Choose a base branch
from
tomhughes:facebook-deletion-callback
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Accounts | ||
| class AuthDeletionsController < ApplicationController | ||
| layout :site_layout | ||
|
|
||
| before_action :set_locale | ||
|
|
||
| authorize_resource :class => :auth_deletion | ||
|
|
||
| def show | ||
| @auth_provider = params.expect(:provider) | ||
| @auth_uid, @time = Rails | ||
| .application | ||
| .message_verifier(:social_login_deletion) | ||
| .verify(params.expect(:confirmation_code)) | ||
| rescue ActiveSupport::MessageVerifier::InvalidSignature | ||
| head :bad_request | ||
| end | ||
|
|
||
| def create | ||
| if params.expect(:provider) == "facebook" | ||
| create_facebook | ||
| else | ||
| head :not_found | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def create_facebook | ||
| encoded_signature, payload = params.expect(:signed_request).split(".", 2) | ||
| signature = Base64.urlsafe_decode64(encoded_signature) | ||
|
|
||
| raise ActionController::BadRequest unless signature == OpenSSL::HMAC.digest("SHA256", Settings.facebook_auth_secret, payload) | ||
|
|
||
| data = JSON.parse(Base64.urlsafe_decode64(payload)) | ||
| user = User.find_by!(:auth_provider => "facebook", :auth_uid => data["user_id"]) | ||
|
|
||
| user.auth_provider = nil | ||
| user.auth_uid = nil | ||
| user.save! | ||
|
|
||
| @confirmation_code = Rails | ||
| .application | ||
| .message_verifier(:social_login_deletion) | ||
| .generate([data["user_id"], Time.now.to_i]) | ||
|
|
||
| render :formats => [:json] | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| json.url auth_delete_url(:provider => params[:provider], | ||
| :confirmation_code => @confirmation_code) | ||
| json.confirmation_code @confirmation_code |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <% content_for :heading do %> | ||
| <h1><%= t ".title" %></h1> | ||
| <% end %> | ||
|
|
||
| <p> | ||
| <%= t ".body", :provider => t("auth.providers.#{@auth_provider}"), :id => @auth_uid, :time => l(Time.at(@time).utc) -%> | ||
| </p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
test/controllers/accounts/auth_deletions_controller_test.rb
|
tomhughes marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "test_helper" | ||
|
|
||
| module Accounts | ||
| class AuthDeletionsControllerTest < ActionDispatch::IntegrationTest | ||
| ## | ||
| # test all routes which lead to this controller | ||
| def test_routes | ||
| assert_routing( | ||
| { :path => "/auth/provider/delete", :method => :get }, | ||
| { :controller => "accounts/auth_deletions", :action => "show", :provider => "provider" } | ||
| ) | ||
| assert_routing( | ||
| { :path => "/auth/provider/delete", :method => :post }, | ||
| { :controller => "accounts/auth_deletions", :action => "create", :provider => "provider" } | ||
| ) | ||
| end | ||
|
|
||
| ## | ||
| # test showing status of a facebook deletion request | ||
| def test_show_facebook | ||
| user = create(:user, :auth_provider => "facebook", :auth_uid => "12345") | ||
| confirmation_code = Rails.application.message_verifier(:social_login_deletion).generate([user.auth_uid, Time.now.to_i]) | ||
|
|
||
| get auth_delete_path(:provider => "facebook", :confirmation_code => confirmation_code) | ||
| assert_response :success | ||
| assert_select "p", /^Data for Facebook ID 12345 was removed at .* and it was disconnected from the associated OpenStreetMap account\.$/ | ||
| end | ||
|
|
||
| ## | ||
| # test showing status of a facebook deletion request with no code | ||
| def test_show_facebook_no_code | ||
| get auth_delete_path(:provider => "facebook") | ||
| assert_response :bad_request | ||
| end | ||
|
|
||
| ## | ||
| # test showing status of a facebook deletion request with an invalid code | ||
| def test_show_facebook_invalid_code | ||
| get auth_delete_path(:provider => "facebook", :confirmation_code => "invalid") | ||
| assert_response :bad_request | ||
| end | ||
|
|
||
| ## | ||
| # test creation of a facebook deletion request | ||
| def test_create_facebook | ||
| user = create(:user, :auth_provider => "facebook", :auth_uid => "12345") | ||
|
|
||
| payload = Base64.urlsafe_encode64( | ||
| JSON.generate( | ||
| :algorithm => "HMAC-SHA256", | ||
| :expires => Time.now.to_i + 3600, | ||
| :issued_at => Time.now.to_i, | ||
| :user_id => "12345" | ||
| ) | ||
| ) | ||
| signature = OpenSSL::HMAC.digest("SHA256", Settings.facebook_auth_secret, payload) | ||
| encoded_signature = Base64.urlsafe_encode64(signature) | ||
| signed_request = [encoded_signature, payload].join(".") | ||
|
|
||
| post auth_delete_path(:provider => "facebook"), :params => { :signed_request => signed_request } | ||
| assert_response :success | ||
| js = ActiveSupport::JSON.decode(@response.body) | ||
| assert_not_nil js | ||
| assert_not_nil js["confirmation_code"] | ||
| assert_equal auth_delete_url(:provider => "facebook", :confirmation_code => js["confirmation_code"]), js["url"] | ||
|
|
||
| assert_nil user.reload.auth_provider | ||
| assert_nil user.reload.auth_uid | ||
| end | ||
|
|
||
| ## | ||
| # test creation of a facebook deletion request with an invalid signature | ||
| def test_create_facebook_bad_signature | ||
| create(:user, :auth_provider => "facebook", :auth_uid => "12345") | ||
|
|
||
| payload = Base64.urlsafe_encode64( | ||
| JSON.generate( | ||
| :algorithm => "HMAC-SHA256", | ||
| :expires => Time.now.to_i + 3600, | ||
| :issued_at => Time.now.to_i, | ||
| :user_id => "12345" | ||
| ) | ||
| ) | ||
| signature = OpenSSL::HMAC.digest("SHA256", "invalid secret", payload) | ||
| encoded_signature = Base64.urlsafe_encode64(signature) | ||
| signed_request = [encoded_signature, payload].join(".") | ||
|
|
||
| post auth_delete_path(:provider => "facebook"), :params => { :signed_request => signed_request } | ||
| assert_response :bad_request | ||
| end | ||
|
|
||
| ## | ||
| # test creation of a facebook deletion request for an unassociated ID | ||
| def test_create_facebook_not_associated | ||
| payload = Base64.urlsafe_encode64( | ||
| JSON.generate( | ||
| :algorithm => "HMAC-SHA256", | ||
| :expires => Time.now.to_i + 3600, | ||
| :issued_at => Time.now.to_i, | ||
| :user_id => "12345" | ||
| ) | ||
| ) | ||
| signature = OpenSSL::HMAC.digest("SHA256", Settings.facebook_auth_secret, payload) | ||
| encoded_signature = Base64.urlsafe_encode64(signature) | ||
| signed_request = [encoded_signature, payload].join(".") | ||
|
|
||
| post auth_delete_path(:provider => "facebook"), :params => { :signed_request => signed_request } | ||
| assert_response :not_found | ||
| end | ||
|
|
||
| ## | ||
| # test creation of a deletion request for an unsupported provider | ||
| def test_create_unsupported | ||
| post auth_delete_path(:provider => "unsupported") | ||
| assert_response :not_found | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For readability, how about avoiding the multiple nesting?
The multiple
returnsare not great, but I think there's more gained than lost here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll have to think about that one... I'm not really a fan of early/multiple returns...
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively could be done with exceptions:
Which brings another reason to test the "no facebook" case: turns out that
ActionController::RoutingErrorrequires a message, and I only noticed because the second instance was being tested (but not the first one).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Full list of supported exceptions in actionpack:/lib/action_dispatch/middleware/exception_wrapper.rb. Custom ones probably can be added (I don't remember from the top of my head).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've reworked it to be more exception based though
ActionController::BadRequestis the only one I'm raising explicitly, and I'm a bit nervous about that to be honest as I'm not sure those exceptions are intended for external use as they're not documented at all.Possibly we should define our own exception instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this 👍 I should have thought of using
find_by!.Yeah, those exceptions sit in a weird place where they are not well documented, but I think the community tends to know about them. (My perception; might be wrong). Having said that... looks like this is a bit better documented now, I just found this in the Rails guides: https://guides.rubyonrails.org/configuring.html#config-action-dispatch-rescue-responses
So I think this is ok, but if you think it'd be clearer to be more explicit with a custom exception (either adding it to the list or having a custom
rescue_from), then that works for me too.