forked from yuina1056/sorcery_twofactor_sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
171 additions
and
122 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,6 +1,11 @@ | ||
# README | ||
|
||
sorceryで二段階認証を実装したサンプルコード | ||
# Sorceryで二段階認証 2FA を実装したサンプルコード | ||
|
||
ちょっとした解説を書いたブログ | ||
https://yu1056y.hatenablog.com/entry/2019/04/21/164540 | ||
|
||
その元ネタ. Rails v4.2, google-authenticator-rails v1.2. | ||
https://moneyforward.com/engineers_blog/2015/08/06/google-authenticator-rails/ | ||
|
||
マネーフォワードエンジニアブログのほうはよいが、それを元にしたゆいな (id:yu1056y) 氏のソースコードは、控えめに言って問題が多く、作り直した。 | ||
|
||
|
This file contains 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 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 |
---|---|---|
@@ -1,32 +1,30 @@ | ||
# -*- coding:utf-8 -*- | ||
# frozen_string_literal: true | ||
|
||
# 単数形 | ||
class SessionController < ApplicationController | ||
skip_before_action :require_login, expect: [:destroy] | ||
prepend_before_action :authenticate_with_two_factor, only: :create | ||
skip_before_action :check_mfa | ||
skip_before_action :require_login, only: [:new, :create] | ||
# ログイン後, MFAせずにログアウトの経路がある | ||
skip_before_action :check_mfa, only: [:destroy] | ||
|
||
# GET /login | ||
def new | ||
@user = User.new | ||
end | ||
|
||
def create; end | ||
# POST /session | ||
def create | ||
if login(params[:email], params[:password]) | ||
redirect_back_or_to('/', notice: 'Login successful') | ||
else | ||
flash[:alert] = 'Login failed' | ||
render 'new' | ||
end | ||
end | ||
|
||
# POST /logout | ||
def destroy | ||
logout | ||
redirect_to(:users, notice: 'Logged out!') | ||
redirect_to '/', notice: 'Logged out!' | ||
end | ||
|
||
private | ||
|
||
def authenticate_with_two_factor | ||
user = User.find_by(email: params[:email]) | ||
if user.blank? | ||
render action: 'new' | ||
return | ||
end | ||
session[:email] = params[:email] | ||
session[:password] = params[:password] | ||
|
||
redirect_to new_user_mfa_session_path | ||
end | ||
end |
This file contains 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,33 @@ | ||
# -*- coding:utf-8 -*- | ||
# frozen_string_literal: true | ||
|
||
# 単数形 | ||
class UserMfaSessionController < ApplicationController | ||
# 2FAなので、ユーザログインは必須. | ||
#skip_before_action :require_login | ||
skip_before_action :check_mfa | ||
|
||
# GET /user_mfa_session/new | ||
def new | ||
@user = current_user | ||
raise "internal error" if !@user.google_secret | ||
end | ||
|
||
# POST /user_mfa_session | ||
def create | ||
@user = current_user | ||
if @user.google_authentic?(params[:auth][:mfa_code]) | ||
# persistence_token はクッキーのキー. | ||
unless @user.persistence_token | ||
@user.persistence_token = SecureRandom.hex | ||
@user.save! | ||
end | ||
UserMfaSession.create(@user) | ||
redirect_to user_path(@user), notice: 'MFA successful' | ||
else | ||
flash[:alert] = "Wrong code" | ||
render :new | ||
end | ||
end | ||
|
||
end |
This file was deleted.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
class WelcomeController < ApplicationController | ||
skip_before_action :require_login | ||
|
||
# GET / | ||
def index | ||
end | ||
end |
This file contains 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 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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
# -*- coding:utf-8 -*- | ||
# frozen_string_literal: true | ||
|
||
# データベースに保存されない. -> クッキーに保存される | ||
class UserMfaSession < GoogleAuthenticatorRails::Session::Base | ||
end |
This file contains 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 |
---|---|---|
@@ -1,27 +1,45 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Tutorial</title> | ||
<%= stylesheet_link_tag "application" %> | ||
<%= javascript_include_tag "application" %> | ||
<%= csrf_meta_tags %> | ||
</head> | ||
<body> | ||
<head> | ||
<title>Tutorial</title> | ||
<%= stylesheet_link_tag "application" %> | ||
<%= javascript_include_tag "application" %> | ||
<%= csrf_meta_tags %> | ||
</head> | ||
<body> | ||
|
||
<div id="nav"> | ||
<% if current_user %> | ||
<%= link_to "Edit Profile", edit_user_path(current_user.id) %> | ||
<%= link_to "Logout", :logout, method: :post %> | ||
<% else %> | ||
<nav class="navbar navbar-expand-lg bg-light"> | ||
<div class="container-fluid"> | ||
<a class="navbar-brand" >2FA Sample</a> | ||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" | ||
data-bs-target="#navbarTogglerDemo02" | ||
aria-controls="navbarTogglerDemo02" aria-expanded="false" | ||
aria-label="Toggle navigation"> | ||
<span class="navbar-toggler-icon"></span> | ||
</button> | ||
<div class="collapse navbar-collapse" id="navbarTogglerDemo02"> | ||
<% if current_user %> | ||
Logged-in: <%= current_user.email %> | ||
<%= link_to "Edit Profile...", edit_user_path(current_user.id) %> | ||
<%= link_to "Logout", logout_path, method: :post %> | ||
<% else %> | ||
<%= link_to "Register", new_user_path %> | | ||
<%= link_to "Login", :login %> | ||
<% end %> | ||
</div> | ||
<div> | ||
<p id="notice"><%= flash[:notice] %></p> | ||
<p id="alert"><%= flash[:alert] %></p> | ||
</div> | ||
<%= yield %> | ||
<%= link_to "Log In", login_path %> | ||
<% end %> | ||
</div> | ||
</div> | ||
</nav> | ||
|
||
</body> | ||
<div> | ||
<% if (notice = flash[:notice]) %> | ||
<p id="notice"><%= notice %></p> | ||
<% end %> | ||
<% if (alert = flash[:alert]) %> | ||
<p id="alert"><%= alert %></p> | ||
<% end %> | ||
</div> | ||
|
||
<%= yield %> | ||
|
||
</body> | ||
</html> |
This file contains 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 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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
<h1>Login</h1> | ||
|
||
<h1>Log In</h1> | ||
|
||
<% if current_user %> | ||
別のユーザでログインする | ||
<% end %> | ||
|
||
<%= render 'form' %> | ||
|
||
<%= link_to 'Back', sessions_path %> | ||
|
This file contains 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,14 @@ | ||
|
||
<% if !@user.persistence_token %> | ||
<p>Google認証システムを開き、[+追加] から, QRコードをスキャンしてください。<br /> | ||
<%= image_tag @user.google_qr_uri %> | ||
<% end %> | ||
|
||
<p>Google認証システムに表示される 6桁のコードを入力してください。 | ||
|
||
<%= form_tag user_mfa_session_path, method: :post do %> | ||
<div class="actions"> | ||
<%= text_field :auth, :mfa_code %> | ||
<%= submit_tag 'authenticate' %> | ||
</div> | ||
<% end %> |
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
<p id="notice"><%= notice %></p> | ||
|
||
<h1>Users</h1> | ||
|
||
<table> | ||
|
This file contains 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 |
---|---|---|
|
@@ -2,4 +2,3 @@ | |
|
||
<%= render 'form', user: @user %> | ||
|
||
<%= link_to 'Back', users_path %> |
This file contains 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<p id="notice"><%= notice %></p> | ||
|
||
<h1>User <%= @user.email %></h1> | ||
|
||
<p> | ||
<strong>Email:</strong> | ||
|
File renamed without changes.
Oops, something went wrong.