diff --git a/Gemfile b/Gemfile index f9e7eff..c25e39e 100644 --- a/Gemfile +++ b/Gemfile @@ -50,11 +50,9 @@ if RUBY_VERSION != '3.3.1' end # Facebook. -# 2020年5月現在, 比較的最近までメンテナンスされているものは、次の2択: -# - https://github.com/nov/fb_graph2 -# - https://github.com/arsduo/koala -# facebook_oauth は 2011年で終了している。 -gem 'fb_graph2' +# ▲ 'fb_graph2' は rack-oauth2 v2 で動かなくなっている。もう使えない. +# koala はメンテナンスが継続している。 +# 別サンプルをつくった; https://gitlab.com/netsphere/rails-examples/-/tree/main/rails7/ gem 'rack-oauth2' diff --git a/app/controllers/connect/facebook_controller.rb b/app/controllers/connect/facebook_controller.rb deleted file mode 100644 index 189cecb..0000000 --- a/app/controllers/connect/facebook_controller.rb +++ /dev/null @@ -1,17 +0,0 @@ -# -*- coding:utf-8 -*- - -class Connect::FacebookController < ApplicationController - - # Facebook から戻ってくる. - # GET /connect/facebook - def show - if login(Connect::Facebook, cookies, nil) - redirect_back_or_to('/dashboard', notice: 'Login successful') - else - flash[:alert] = 'Login failed' - redirect_to '/' - end - end - -end - diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 8723640..52bf622 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -5,6 +5,5 @@ class DashboardController < ApplicationController def show @account = current_user @clients = @account.clients - print Connect::Facebook.all.inspect # DEBUG end end diff --git a/app/models/connect/facebook.rb b/app/models/connect/facebook.rb deleted file mode 100644 index 3b17d68..0000000 --- a/app/models/connect/facebook.rb +++ /dev/null @@ -1,115 +0,0 @@ -# -*- coding:utf-8 -*- - -# uninitialized constant #::HTTPClient 対策 -require 'httpclient' - -# undefined method `filter_request' for an instance of FbGraph2::RequestFilter::Authenticator 対策 -# rack-oauth2 v2 で `filter_request()` が削除されている。復元する -module Rack - module OAuth2 - class AccessToken - class Authenticator -# Callback called in HTTPClient (before sending a request) - # request:: HTTP::Message - def filter_request(request) - @token.authenticate(request) - end - - # Callback called in HTTPClient (after received a response) - # response:: HTTP::Message - # request:: HTTP::Message - def filter_response(response, request) - # nothing to do - end - end - end - end -end - - -class Connect::Facebook < Connect::Base - validates :identifier, presence: true, uniqueness: true - validates :access_token, presence: true, uniqueness: true - - # @return [FbGraph2::User] ユーザによって許可されたデータ. - # email も得られないことがあることに注意. Mandatory にできない。 - # { - # id: 略, - # raw_attributes: {"name": 略, ...}, - # access_token: 略, - # name: "堀川 久", - # email: "hisashi.horikawa@gmail.com", - # first_name: "久", - # last_name: "堀川", - # gender: "male", - # birthday: Dateオブジェクト, - # location: FbGraph2::Pageオブジェクト, - # age_range: FbGraph2::Struct::AgeRangeオブジェクト - # } - # 言語が取れていない。 - def me - # どのようなフィールドがあるかは, グラフAPIを見よ. - # https://developers.facebook.com/docs/graph-api/ - @me ||= FbGraph2::User.me(self.access_token).fetch( - fields:[:name, :email, :first_name, :last_name, - :location, :languages, :gender, :birthday, :age_range]) - end - - - class << self - def config - return @config if @config - - # Ruby 3.1 で YAML (psych) 4.0.0 がバンドル。非互換. - @config = YAML.load_file("#{Rails.root}/config/connect/facebook.yml", - aliases: true)[Rails.env].symbolize_keys - if Rails.env.production? - @config.merge!( - client_id: ENV['fb_client_id'], - client_secret: ENV['fb_client_secret'] - ) - end - return @config - end - - def auth - FbGraph2::Auth.new config[:client_id], config[:client_secret] - end - - # Facebook client-side は Implicit Flow なので, 必ずトークンの検証が必要. - # これを怠ると, token hijacking される。 - # Facebook サイトの文書は, 相互に異なったことが書いてあったり、 - # そもそも検証が必要ということを強調しておらず、ひどい。 - def authenticate(cookies, dummy) - # fb_graph2 では, from_cookie() 内で, SignedRequest の検証を自動的に行う. - # client_secret のハッシュ値との比較。 - # => なので, client_secret が必要。 - begin - _auth_ = auth.from_cookie(cookies) - rescue FbGraph2::Auth::SignedRequest::VerificationFailed - # ユーザが [キャンセル] を押した場合 - return [nil, :user_canceled] - end - # DEBUG # - -<% end %> - - - - diff --git a/app/views/top/index.html.erb b/app/views/top/index.html.erb index 3d53f87..ffa00fc 100644 --- a/app/views/top/index.html.erb +++ b/app/views/top/index.html.erb @@ -6,7 +6,6 @@

Admin login:

diff --git a/config/connect/facebook.yml.sample b/config/connect/facebook.yml.sample deleted file mode 100644 index 12604f4..0000000 --- a/config/connect/facebook.yml.sample +++ /dev/null @@ -1,26 +0,0 @@ - -# Apps: -# https://developers.facebook.com/apps/ - -# Permissions Reference - Facebook Login -# https://developers.facebook.com/docs/facebook-login/permissions - -development: &defaults - client_id: HERE IS 'APP ID' - client_secret: HERE IS 'APP SECRET' - scope: user_birthday,user_location,email,user_friends,user_gender,user_hometown - -# Requires the App Review: -# - user_age_range -# - user_birthday -# - user_friends -# - user_gender -# - user_hometown -# - user_location -# - user_photos - -test: - <<: *defaults - -production: - <<: *defaults diff --git a/db/migrate/20110829024031_create_connect_facebooks.rb b/db/migrate/20110829024031_create_connect_facebooks.rb deleted file mode 100644 index 4e66ef4..0000000 --- a/db/migrate/20110829024031_create_connect_facebooks.rb +++ /dev/null @@ -1,17 +0,0 @@ -# -*- coding:utf-8 -*- - -class CreateConnectFacebooks < ActiveRecord::Migration[4.2] - def self.up - # 単数形 -> config/initializers/inflections.rb で単複同形の宣言要. - create_table :connect_facebook do |t| - t.references :account, null: false, foreign_key: true - t.string :identifier, null:false, index: {unique: true} - t.string :access_token, null:false, index: {unique: true} - t.timestamps - end - end - - def self.down - drop_table :connect_facebook - end -end diff --git a/db/schema.rb b/db/schema.rb index 08f1554..d42b29c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -87,16 +87,6 @@ t.index ["identifier"], name: "index_clients_on_identifier", unique: true end - create_table "connect_facebook", id: :serial, force: :cascade do |t| - t.integer "account_id", null: false - t.string "identifier", null: false - t.string "access_token", null: false - t.datetime "created_at" - t.datetime "updated_at" - t.index ["access_token"], name: "index_connect_facebook_on_access_token", unique: true - t.index ["identifier"], name: "index_connect_facebook_on_identifier", unique: true - end - create_table "connect_google", id: :serial, force: :cascade do |t| t.integer "account_id", null: false t.string "identifier", null: false @@ -169,7 +159,6 @@ add_foreign_key "authorizations", "fake_users" add_foreign_key "authorizations", "request_objects" add_foreign_key "clients", "accounts" - add_foreign_key "connect_facebook", "accounts" add_foreign_key "connect_google", "accounts" add_foreign_key "id_tokens", "clients" add_foreign_key "id_tokens", "fake_users" diff --git a/test/controllers/connect/facebook_controller_test.rb b/test/controllers/connect/facebook_controller_test.rb deleted file mode 100644 index 8f30615..0000000 --- a/test/controllers/connect/facebook_controller_test.rb +++ /dev/null @@ -1,48 +0,0 @@ -require "test_helper" - -class Connect::FacebooksControllerTest < ActionDispatch::IntegrationTest - setup do - @connect_facebook = connect_facebooks(:one) - end - - test "should get index" do - get connect_facebooks_url - assert_response :success - end - - test "should get new" do - get new_connect_facebook_url - assert_response :success - end - - test "should create connect_facebook" do - assert_difference('Connect::Facebook.count') do - post connect_facebooks_url, params: { connect_facebook: { } } - end - - assert_redirected_to connect_facebook_url(Connect::Facebook.last) - end - - test "should show connect_facebook" do - get connect_facebook_url(@connect_facebook) - assert_response :success - end - - test "should get edit" do - get edit_connect_facebook_url(@connect_facebook) - assert_response :success - end - - test "should update connect_facebook" do - patch connect_facebook_url(@connect_facebook), params: { connect_facebook: { } } - assert_redirected_to connect_facebook_url(@connect_facebook) - end - - test "should destroy connect_facebook" do - assert_difference('Connect::Facebook.count', -1) do - delete connect_facebook_url(@connect_facebook) - end - - assert_redirected_to connect_facebooks_url - end -end diff --git a/test/fixtures/connect/facebooks.yml b/test/fixtures/connect/facebooks.yml deleted file mode 100644 index 5181636..0000000 --- a/test/fixtures/connect/facebooks.yml +++ /dev/null @@ -1,11 +0,0 @@ -# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html - -# This model initially had no columns defined. If you add columns to the -# model remove the '{}' from the fixture names and add the columns immediately -# below each fixture, per the syntax in the comments below -# -one: {} -# column: value -# -two: {} -# column: value diff --git a/test/models/connect/facebook_test.rb b/test/models/connect/facebook_test.rb deleted file mode 100644 index 6ecdf82..0000000 --- a/test/models/connect/facebook_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require "test_helper" - -class Connect::FacebookTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end diff --git a/test/system/connect/facebooks_test.rb b/test/system/connect/facebooks_test.rb deleted file mode 100644 index 65b82e2..0000000 --- a/test/system/connect/facebooks_test.rb +++ /dev/null @@ -1,41 +0,0 @@ -require "application_system_test_case" - -class Connect::FacebooksTest < ApplicationSystemTestCase - setup do - @connect_facebook = connect_facebooks(:one) - end - - test "visiting the index" do - visit connect_facebooks_url - assert_selector "h1", text: "Connect/Facebooks" - end - - test "creating a Facebook" do - visit connect_facebooks_url - click_on "New Connect/Facebook" - - click_on "Create Facebook" - - assert_text "Facebook was successfully created" - click_on "Back" - end - - test "updating a Facebook" do - visit connect_facebooks_url - click_on "Edit", match: :first - - click_on "Update Facebook" - - assert_text "Facebook was successfully updated" - click_on "Back" - end - - test "destroying a Facebook" do - visit connect_facebooks_url - page.accept_confirm do - click_on "Destroy", match: :first - end - - assert_text "Facebook was successfully destroyed" - end -end