Created
February 5, 2014 13:55
-
-
Save kidach1/8824053 to your computer and use it in GitHub Desktop.
TwilioとRailsで、1通1円のSMS認証を実装してみる ref: http://qiita.com/kidachi_/items/cd63063fa98aaa5030bd
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
{ | |
"AccountSid"=>"hogehoge", | |
"MessageSid"=>"SMbfea2d641027f6f3434bf656c0b55d97", | |
"Body"=>"Hello, Qiita!", | |
"ToZip"=>"60081", | |
"ToCity"=>"FOX LAKE", | |
"FromState"=>"", | |
"ToState"=>"IL", | |
"SmsSid"=>"SMbfea2d641027f6f3434bf656c0b55d97", | |
"To"=>"+18xxxxxxxx", | |
"ToCountry"=>"US", | |
"FromCountry"=>"JP", | |
"SmsMessageSid"=>"SMbfea2d641027f6f3434bf656c0b55d97", | |
"ApiVersion"=>"2010-04-01", | |
"FromCity"=>"", | |
"SmsStatus"=>"received", | |
"NumMedia"=>"0", | |
"From"=>"+8180xxxxxxxx", | |
"FromZip"=>"" | |
} |
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
params[:From] # ユーザの電話番号 | |
params[:Body] # ユーザ入力値。ユーザidと認証トークンを入力してもらう。 |
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
user_id + '_' + token |
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
class SmsAuthController < ApplicationController | |
TWILIO_NUMBER = '+81xxxxxxxx' # 電話番号 | |
# 認証ページ(GET) | |
# viewでは@sender_num, @user.sms_token, @user.idを表示 | |
def index | |
@user = User.find(current_user.id) | |
@sender_num = TWILIO_NUMBER | |
@user.sms_token = generate_token | |
@user.save! | |
end | |
# 認証処理(POST) | |
def verify | |
begin | |
params_check;format_params;user_check;token_check; | |
logger.error("valid user!") | |
render :json => { code: 0 } | |
rescue => e | |
logger.error("error: #{e.message}") | |
render :json => { code: 400, message: e.message } | |
return | |
end | |
end | |
private | |
# 認証token生成 | |
def generate_token | |
((0..9).to_a + ("a".."z").to_a + ("A".."Z").to_a).sample(4).join | |
end | |
# パラメータチェック | |
def params_check | |
raise 'Body is invalid' if params[:Body].nil? | |
raise "Body isn't contain underscore" if !params[:Body].include?("_") | |
raise 'From is invalid' if params[:From].nil? | |
end | |
# パラメータ整形 | |
# params[:Body]は「user_id + '_' + token」の形式を想定 | |
def format_params | |
body = params[:Body].split("_") | |
@user_id = body[0] | |
@token = body[1] | |
@user.mobile_number = params[:From].sub(/^\+81/, '0') | |
raise 'user_id is nil' if @user_id.nil? | |
raise 'token is nil' if @token.nil? | |
raise 'mobile_number is nil' if @user.mobile_number.nil? | |
end | |
# ユーザチェック | |
def user_check | |
@user = User.find_by_id(@user_id) | |
raise 'user is invalid' if @user.nil? | |
end | |
# 認証トークンチェック | |
def token_check | |
raise 'token is invalid' if @user.sms_token == @token | |
@user.verify = true | |
@user.save! | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment