79 lines
2.6 KiB
Ruby
79 lines
2.6 KiB
Ruby
# encoding: UTF-8
|
|
# frozen_string_literal: true
|
|
|
|
module API
|
|
module V2
|
|
module Account
|
|
class Otp < Grape::API
|
|
helpers API::V2::Account::Utils
|
|
resource :otp do
|
|
# for example
|
|
# for withdraw with id 3
|
|
# we create a cache with name -> withdraw_3_2fa
|
|
#
|
|
# # for example
|
|
# # for deposit with id 6
|
|
# # we create a cache with name -> deposit_6_2fa
|
|
desc 'validate user otp'
|
|
params do
|
|
requires :otp,
|
|
type: { value: Integer, message: 'integer_otp' },
|
|
allow_blank: false,
|
|
desc: 'OTP to perform action'
|
|
optional :action,
|
|
type: String,
|
|
desc: 'action for top'
|
|
# optional :id,
|
|
# type: String,
|
|
# desc: 'if for top action'
|
|
end
|
|
post do
|
|
error!({ errors: ['account.not_active_otp'] }, 422) unless current_user.otp
|
|
|
|
error!({ errors: ['account.invalid_otp'] }, 422) unless Vault::TOTP.validate?(current_user.uid, params[:otp])
|
|
|
|
action = 'validate'
|
|
# id = '0'
|
|
action = params[:action] if params[:action].present?
|
|
# id = params[:id] if params[:id].present?
|
|
current_user.write_cache("#{action}_2fa", 1, 60)
|
|
status 200
|
|
end
|
|
|
|
desc 'Send confirmations code',
|
|
success: { code: 201, message: 'Generated verification code' },
|
|
failure: [
|
|
{ code: 400, message: 'Required params are missing' },
|
|
{ code: 422, message: 'Validation errors' }
|
|
]
|
|
params do
|
|
requires :data,
|
|
type: String,
|
|
allow_blank: false,
|
|
desc: 'Account email or Telephone number'
|
|
requires :action,
|
|
type: String,
|
|
allow_blank: false,
|
|
desc: 'for what need auth code'
|
|
optional :channel,
|
|
type: String,
|
|
allow_blank: true,
|
|
default: 'email',
|
|
desc: 'channel that send in'
|
|
optional :captcha_response,
|
|
types: [String, Hash],
|
|
desc: 'Response from captcha widget'
|
|
end
|
|
post '/resend' do
|
|
current_user = Member.find_by(email: params[:data])
|
|
return status 201 if current_user.nil?
|
|
|
|
publish_confirmation_code(current_user, params[:action])
|
|
status 201
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|