LoginSignup
4
5

More than 5 years have passed since last update.

mixi OpenID が終了するので omniauth-mixi に移行する

Last updated at Posted at 2015-10-09

mixi OpenID が 2016/1/25 に終了するため、これをログイン認証で使用しているサービスは mixi Graph API に移行する必要があります。

以下、Rails + OmniAuth (omniauth-openid) + Devise という前提で、移行する手順を書いていきます。

Gemfile

まず、OmniAuth 用に omniauth-mixi という便利な gem があるので、これを使います。
ただ、本家の gem だと、今回の移行に必要な server_state パラメータの処理が行われていません (2015/10/9 現在)。ですので、修正したものを使います。

Gemfile
gem 'omniauth-mixi', github: 'milk1000cc/omniauth-mixi', branch: 'support-server-state'

omniauth-openid gem は使わなくなるので、他に OpenID の認証を行っていなければ削除しても大丈夫です。

config/initializers/devise.rb

config/initializers/devise.rb
config.omniauth(:mixi,
  ENV['MIXI_CONSUMER_KEY'], ENV['MIXI_CONSUMER_SECRET'],
  scope: 'openid openid2 r_profile')

移行の際、mixi OpenID のユーザ ID が必要なので、scope に openid opendid2 を指定します。(参考)
ConsumerKey の取得は、以下のページなどが参考になります。
http://developer.mixi.co.jp/connect/mixi_graph_api/services/

移行処理

OmniauthCallbacksController で、認証前に以下のようなメソッドを呼べば、mixi OpenID で登録されていたユーザの uid を更新することができます。

def migrate_old_mixi_uid
  auth = request.env['omniauth.auth']
  old_uid = JWT.decode(auth.credentials.id_token, nil, false)[0]['openid2_id']
  if user = User.find_by(provider: auth.provider, uid: old_uid)
    user.update! uid: auth.uid
  end
end

認証処理自体は、変更する必要がありません。

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5