Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Qiita login #8

Merged
merged 12 commits into from
May 22, 2017
Merged

Qiita login #8

merged 12 commits into from
May 22, 2017

Conversation

tomoasleep
Copy link
Member

@tomoasleep tomoasleep commented May 7, 2017

QiitaのOAuthを利用してアカウント作成とログインが行えるようにする。

2017-05-18 19 52 06

仕様

  • ホーム画面 ( /about )とログイン画面 ( /session/new ) に「Qiitaアカウントで参加ボタン」が追加される
  • ボタンを押すと、紐付いている場合はログインが行える。
    • 紐付いていない場合は紐付け作業を行う
      • ログインしていない場合は専用の登録フォーム ( `` ) でアカウントの登録作業を行う
        • 専用フォームではメールアドレスとアカウント名を入力してMastodonアカウントの新規作成を行う
        • パスワードはランダムな文字列がダミーのパスワードとして入る
          - ユーザーには知らされないので再設定するまでパスワードログインできない
          - 設定画面からパスワードを設定することが出来る。最初の1回だけ現在のパスワードを入れずにパスワードを変更することが出来る
      • ログインしている場合では、MastodonアカウントとQiitaアカウントを紐付ける
  • 設定画面から紐付けが行える
    • 現在は紐付け解除はできない

動作を見たい場合の注意

QIitaで自分のアカウントでアプリケーションを作成し、.env に適宜以下の内容を追加しておいてください。

# XXXXを自分のアプリケーションのtokenの値に変える。
QIITA_CLIENT_ID=XXXX
QIITA_CLIENT_SECRET=XXXX

@tomoasleep tomoasleep force-pushed the qiita-login branch 2 times, most recently from e3b5ad2 to 4cb7f0e Compare May 10, 2017 07:52
@tomoasleep tomoasleep changed the title [WIP] Qiita login Qiita login May 11, 2017
@tomoasleep
Copy link
Member Author

@takashi @yujinakayama レビューお願いします 🙏

@tomoasleep
Copy link
Member Author

@takashi の助言でCookieStoreにAuthHash保存するようにした。

@tomoasleep
Copy link
Member Author

そういえば、メール認証をしないとログインできないので新規登録までに、

  1. Qiitaで参加ボタンを押す
  2. 新規登録フォームでユーザーネームとメールアドレスを送信する
  3. メール認証をする
  4. Qiitaで参加ボタンを押してログイン

…と使えるようになるまでに2回Qiitaで参加ボタンを押す必要があるのが面倒だ…

@@ -0,0 +1,3 @@
class OauthAuthorization < ApplicationRecord
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OauthAuthorization という名前、 Authorization と2回行っているようで不自然なのと、 Oauth(という仕様)とのAuthorization のようにとれ、違和感があります。

この場合 Qiita との Authorization なわけなので、QiitaAuthorization とするのはどうでしょうか

User.new(
email: email,
locale: locale,
password: password,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここで password をユーザの代わりに追加しているけど、このパスワードはユーザーに知らされる?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

知らされないので、パスワードを再発行するまでパスワード認証は実質利用できない状態です。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dummy_password_flag を足して初回だけ現在のパスワードを入力しなくてもパスワードを変更できるようにした

初回のみ現在のパスワードを入力するinputを非表示にしている。

2017-05-18 11 01 44

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2017-05-18 19 49 01

最初にパスワードを変更する場合だけ 新しい じゃなくした

private

def store_omniauth_auth
session[:devise_omniauth_auth] = request.env['omniauth.auth'].to_json,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cookie store を使用しているならば、serializer を marshal にしていても json にしていても hash はそのまま格納できるはずです。 to_json 必要なさそう

@tomoasleep tomoasleep force-pushed the qiita-login branch 4 times, most recently from 0057a13 to b7aee85 Compare May 18, 2017 10:46
@tomoasleep
Copy link
Member Author

新規登録の認証の仕様の変更

現在の仕様だと、Qiitaアカウントで新規登録してログインした状態になるまで都度2回QiitaのOAuthの認可画面を表示する必要があるので、新規登録してから1日の間はメール認証しなくてもログイン出来るように仕様を変更する。その場合、画面上部にアラートが表示される。

2017-05-18 17 08 57

@tomoasleep
Copy link
Member Author

負荷対策用の、新規登録の制限

(負荷対策のために、)管理画面からQiita連携以外での新規登録を停止することも出来るようにする。

2017-05-18 19 06 44

2017-05-18 19 52 22

@tomoasleep
Copy link
Member Author

@takashi 再度レビューお願いします

@@ -0,0 +1,34 @@
# frozen_string_literal: true

class Auth::OmniauthCallbacksController < Devise::OmniauthCallbacksController
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

今更なのですが、 ここで行っているのは結局 QiitaAuthorization モデルの作成ですし、Setting の方も QiitaAuthorizationsController なのでAuth::QiitaAuthorizationsController が適当なのではないかと思います 🙇

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QiitaAuthorizationsを確かに作成してはいるのですが、どちらかと言うとここはDeviseの提供する仕組みの上でOmniauthのcallbackを処理する性格のほうが強いので、Deviseの命名に寄せていた方が良いと考えているのですが、どう思いますか?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Omniauthのcallbackを処理するが、実際には Oauth Server 側 (今回だとQiita) で明示的にResource Owner のリソースを OauthClient 受け渡すための callback url を記述しているので、omniauth という gem が提供している仕組みではあるが QiitaAuthorization を作成する、という考え方がしっくりくるのかな、と思った。
けど特に強い意見があるわけではないのでこのままでも大丈夫です 🙆‍♂️

Copy link

@takashi takashi May 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

補足すると、 qiita というアクションに違和感があって、 アクションは原則 crud のみで構成されるべき、という rails の controller の実装的にこうすればいいんじゃないか、とおもった、というのもあります (Qiita:Authorization#create でもいいのではという意味)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qiita というアクションに違和感があって、 アクションは原則 crud のみで構成されるべき、という rails の controller の実装的にこうすればいいんじゃないか

これはたしかにそうだと思いますが、今回は素直に提供する仕組みに乗っかって様子見ようと思います。forkなので。

Copy link

@takashi takashi May 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、本家がそういう実装なのか。 okです 👌

}

AlertBar.propTypes = {
isEmailConfirmed: PropTypes.bool,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これは require 属性をつけなくても大丈夫ですか?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

付けといたほうが良さそうですね、thx 🙇

{
(!isEmailConfirmed &&
<div className='alert'>
<i className='fa fa-fw fa-exclamation-triangle' /><FormattedMessage id='alert_bar.email_confirm_alert' defaultMessage='Your email address is not confirmed. Please confirm the sent email.' />
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defaultMessage が使われるのはどのようなケースですか?

Copy link
Member Author

@tomoasleep tomoasleep May 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mastodonはja, en以外のlocaleを用意していて、それらの他のlocaleを使用している場合に使われます。

2017-05-18 23 30 35

else
flash[:alert] = I18n.t('omniauth_callbacks.failure')
end
redirect_to settings_qiita_authorizations_path
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

インデントが1つ深いかも

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😳

def qiita
auth_hash = request.env['omniauth.auth']

if current_user
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この 「current_user が存在した場合」というのは 既存の password 認証のアカウントに Qiita アカウントに紐付ける というプロセスなので、今すぐには必要ない部分ですか?(消す必要はないと思っているが確認です)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

はい、現状使われることはない部分です 🙆

@tomoasleep
Copy link
Member Author

@takashi 修正 or コメントしたので再度レビューお願いします 🙏

@@ -14,6 +14,7 @@ const ja = {
"account.unblock": "ブロック解除",
"account.unfollow": "フォロー解除",
"account.unmute": "ミュート解除",
"alert_bar.email_confirm_alert": "メールアドレスの確認が完了していません。登録したアドレス宛に送信されたメールを確認してください。",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1日の期限をもってログインできなくなるよ、という旨をここで書いたほうが親切かなと思ったけどどうでしょう?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かにそうですね👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2017-05-22 13 54 42

直した ✌️

Copy link

@takashi takashi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tomoasleep
Copy link
Member Author

@takashi レビューthx 🙏

@tomoasleep tomoasleep merged commit 75748eb into increments:increments May 22, 2017
@tomoasleep tomoasleep deleted the qiita-login branch May 22, 2017 05:19
yumetodo added a commit to yumetodo/mastodon that referenced this pull request Mar 1, 2020
Conflicts:
	Gemfile
	Gemfile.lock
	app/javascript/mastodon/features/compose/index.js
	app/serializers/initial_state_serializer.rb
	app/views/about/_registration.html.haml
	app/views/admin/settings/edit.html.haml
	app/views/auth/registrations/edit.html.haml
	app/views/shared/_og.html.haml
	config/initializers/omniauth.rb
	package.json

Look at f6df9d0 .
The diff is too difficult to read however,
in file app/views/auth/registrations/edit.html.haml,
`simple_form.labels.defaults.password` was suddenly replaced to
`simple_form.labels.defaults.new_password`.
This can be proved by this file on ef27a0b
which is a parent commit of that.

However, this replacement has been continued for about 3 years.
And there is no significant difference.
So, I decided not to recover this replacement.

ref:
- 1cd0497
- increments#8
- f4d549d
- mastodon#8703
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants