class Bandwidth::MultiFactorAuth::MFAController

MFAController

Public Class Methods

new(config, http_call_back: nil) click to toggle source
Calls superclass method Bandwidth::BaseController::new
# File lib/bandwidth/multi_factor_auth_lib/multi_factor_auth/controllers/mfa_controller.rb, line 10
def initialize(config, http_call_back: nil)
  super(config, http_call_back: http_call_back)
end

Public Instance Methods

create_messaging_two_factor(account_id, body) click to toggle source

Allows a user to send a MFA code through a text message (SMS) @param [String] account_id Required parameter: Bandwidth Account ID with Messaging service enabled @param [TwoFactorCodeRequestSchema] body Required parameter: Example: @return [TwoFactorMessagingResponse] response from the API call

# File lib/bandwidth/multi_factor_auth_lib/multi_factor_auth/controllers/mfa_controller.rb, line 81
def create_messaging_two_factor(account_id,
                                body)
  # Prepare query url.
  _query_builder = config.get_base_uri(Server::MULTIFACTORAUTHDEFAULT)
  _query_builder << '/accounts/{accountId}/code/messaging'
  _query_builder = APIHelper.append_url_with_template_parameters(
    _query_builder,
    'accountId' => { 'value' => account_id, 'encode' => false }
  )
  _query_url = APIHelper.clean_url _query_builder

  # Prepare headers.
  _headers = {
    'accept' => 'application/json',
    'content-type' => 'application/json; charset=utf-8'
  }

  # Prepare and execute HttpRequest.
  _request = config.http_client.post(
    _query_url,
    headers: _headers,
    parameters: body.to_json
  )
  MultiFactorAuthBasicAuth.apply(config, _request)
  _response = execute_request(_request)

  # Validate response against endpoint and global error codes.
  if _response.status_code == 400
    raise ErrorWithRequestException.new(
      'If there is any issue with values passed in by the user',
      _response
    )
  elsif _response.status_code == 401
    raise UnauthorizedRequestException.new(
      'Authentication is either incorrect or not present',
      _response
    )
  elsif _response.status_code == 403
    raise ForbiddenRequestException.new(
      'The user is not authorized to access this resource',
      _response
    )
  elsif _response.status_code == 500
    raise ErrorWithRequestException.new(
      'An internal server error occurred',
      _response
    )
  end
  validate_response(_response)

  # Return appropriate response type.
  decoded = APIHelper.json_deserialize(_response.raw_body)
  ApiResponse.new(
    _response,
    data: TwoFactorMessagingResponse.from_hash(decoded)
  )
end
create_verify_two_factor(account_id, body) click to toggle source

Allows a user to verify an MFA code @param [String] account_id Required parameter: Bandwidth Account ID with Two-Factor enabled @param [TwoFactorVerifyRequestSchema] body Required parameter: Example: @return [TwoFactorVerifyCodeResponse] response from the API call

# File lib/bandwidth/multi_factor_auth_lib/multi_factor_auth/controllers/mfa_controller.rb, line 144
def create_verify_two_factor(account_id,
                             body)
  # Prepare query url.
  _query_builder = config.get_base_uri(Server::MULTIFACTORAUTHDEFAULT)
  _query_builder << '/accounts/{accountId}/code/verify'
  _query_builder = APIHelper.append_url_with_template_parameters(
    _query_builder,
    'accountId' => { 'value' => account_id, 'encode' => false }
  )
  _query_url = APIHelper.clean_url _query_builder

  # Prepare headers.
  _headers = {
    'accept' => 'application/json',
    'content-type' => 'application/json; charset=utf-8'
  }

  # Prepare and execute HttpRequest.
  _request = config.http_client.post(
    _query_url,
    headers: _headers,
    parameters: body.to_json
  )
  MultiFactorAuthBasicAuth.apply(config, _request)
  _response = execute_request(_request)

  # Validate response against endpoint and global error codes.
  if _response.status_code == 400
    raise ErrorWithRequestException.new(
      'If there is any issue with values passed in by the user',
      _response
    )
  elsif _response.status_code == 401
    raise UnauthorizedRequestException.new(
      'Authentication is either incorrect or not present',
      _response
    )
  elsif _response.status_code == 403
    raise ForbiddenRequestException.new(
      'The user is not authorized to access this resource',
      _response
    )
  elsif _response.status_code == 429
    raise ErrorWithRequestException.new(
      'The user has made too many bad requests and is temporarily locked' \
      ' out',
      _response
    )
  elsif _response.status_code == 500
    raise ErrorWithRequestException.new(
      'An internal server error occurred',
      _response
    )
  end
  validate_response(_response)

  # Return appropriate response type.
  decoded = APIHelper.json_deserialize(_response.raw_body)
  ApiResponse.new(
    _response,
    data: TwoFactorVerifyCodeResponse.from_hash(decoded)
  )
end
create_voice_two_factor(account_id, body) click to toggle source

Allows a user to send a MFA code through a phone call @param [String] account_id Required parameter: Bandwidth Account ID with Voice service enabled @param [TwoFactorCodeRequestSchema] body Required parameter: Example: @return [TwoFactorVoiceResponse] response from the API call

# File lib/bandwidth/multi_factor_auth_lib/multi_factor_auth/controllers/mfa_controller.rb, line 19
def create_voice_two_factor(account_id,
                            body)
  # Prepare query url.
  _query_builder = config.get_base_uri(Server::MULTIFACTORAUTHDEFAULT)
  _query_builder << '/accounts/{accountId}/code/voice'
  _query_builder = APIHelper.append_url_with_template_parameters(
    _query_builder,
    'accountId' => { 'value' => account_id, 'encode' => false }
  )
  _query_url = APIHelper.clean_url _query_builder

  # Prepare headers.
  _headers = {
    'accept' => 'application/json',
    'content-type' => 'application/json; charset=utf-8'
  }

  # Prepare and execute HttpRequest.
  _request = config.http_client.post(
    _query_url,
    headers: _headers,
    parameters: body.to_json
  )
  MultiFactorAuthBasicAuth.apply(config, _request)
  _response = execute_request(_request)

  # Validate response against endpoint and global error codes.
  if _response.status_code == 400
    raise ErrorWithRequestException.new(
      'If there is any issue with values passed in by the user',
      _response
    )
  elsif _response.status_code == 401
    raise UnauthorizedRequestException.new(
      'Authentication is either incorrect or not present',
      _response
    )
  elsif _response.status_code == 403
    raise ForbiddenRequestException.new(
      'The user is not authorized to access this resource',
      _response
    )
  elsif _response.status_code == 500
    raise ErrorWithRequestException.new(
      'An internal server error occurred',
      _response
    )
  end
  validate_response(_response)

  # Return appropriate response type.
  decoded = APIHelper.json_deserialize(_response.raw_body)
  ApiResponse.new(
    _response, data: TwoFactorVoiceResponse.from_hash(decoded)
  )
end