-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1455 from nervosnetwork/develop
Deploy to testnet
- Loading branch information
Showing
30 changed files
with
662 additions
and
164 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module Api | ||
module V1 | ||
class UdtVerificationsController < ApplicationController | ||
before_action :check_udt_info, only: :update | ||
before_action :set_locale, only: :update | ||
|
||
def update | ||
udt_verification = UdtVerification.find_or_create_by(udt_id: @udt.id) | ||
|
||
udt_verification.refresh_token!(request.remote_ip) | ||
UdtVerificationMailer.with(email: @udt.email, token: udt_verification.token, | ||
locale: @locale).send_token.deliver_later | ||
render json: :ok | ||
rescue UdtVerification::TokenSentTooFrequentlyError | ||
raise Api::V1::Exceptions::TokenSentTooFrequentlyError | ||
end | ||
|
||
private | ||
|
||
def check_udt_info | ||
@udt = Udt.find_by(type_hash: params[:id]) | ||
raise Api::V1::Exceptions::UdtNotFoundError if @udt.nil? | ||
raise Api::V1::Exceptions::UdtNoContactEmailError if @udt.email.blank? | ||
end | ||
|
||
def set_locale | ||
@locale = params[:locale] == "zh_CN" ? "zh_CN" : "en" | ||
end | ||
end | ||
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
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class UdtVerificationMailer < ApplicationMailer | ||
default from: "[email protected]" | ||
|
||
def send_token | ||
email = params[:email] | ||
@token = params[:token] | ||
locale = params[:locale] || "en" | ||
I18n.with_locale(locale) do | ||
mail(to: email, subject: "Token Info Verification") | ||
end | ||
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
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,43 @@ | ||
class UdtVerification < ApplicationRecord | ||
SENT_FREQUENCY_MINUTES = 1 | ||
KEEP_ALIVE_MINUTES = 10 | ||
|
||
class TokenExpiredError < StandardError; end | ||
class TokenNotMatchError < StandardError; end | ||
class TokenSentTooFrequentlyError < StandardError; end | ||
|
||
belongs_to :udt | ||
|
||
def refresh_token!(ip) | ||
raise TokenSentTooFrequentlyError if sent_at.present? && self.sent_at + SENT_FREQUENCY_MINUTES.minutes > Time.now | ||
|
||
self.token = rand(999999).to_s.rjust(6, "0") | ||
self.sent_at = Time.now | ||
self.last_ip = ip | ||
self.save! | ||
end | ||
|
||
def validate_token!(token_params) | ||
raise TokenExpiredError if self.sent_at + KEEP_ALIVE_MINUTES.minutes < Time.now | ||
raise TokenNotMatchError if token != token_params.to_i | ||
end | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: udt_verifications | ||
# | ||
# id :bigint not null, primary key | ||
# token :integer | ||
# sent_at :datetime | ||
# last_ip :inet | ||
# udt_id :bigint | ||
# udt_type_hash :integer | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_udt_verifications_on_udt_id (udt_id) | ||
# index_udt_verifications_on_udt_type_hash (udt_type_hash) UNIQUE | ||
# |
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
Oops, something went wrong.