Class: Bandwidth::MultiFactorAuth::MFAController

Inherits:
BaseController show all
Defined in:
lib/bandwidth/multi_factor_auth_lib/multi_factor_auth/controllers/mfa_controller.rb

Overview

MFAController

Instance Attribute Summary

Attributes inherited from BaseController

#config, #http_call_back

Instance Method Summary collapse

Methods inherited from BaseController

#execute_request, #validate_parameters, #validate_response

Constructor Details

#initialize(config, http_call_back: nil) ⇒ MFAController



10
11
12
# 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

Instance Method Details

#create_messaging_two_factor(account_id, body) ⇒ TwoFactorMessagingResponse

Multi-Factor authentication with Bandwidth Messaging services. Allows a user to send an MFA code via a text message (SMS). Messaging service enabled



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/bandwidth/multi_factor_auth_lib/multi_factor_auth/controllers/mfa_controller.rb', line 84

def create_messaging_two_factor(,
                                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' => , '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.

  case _response.status_code
  when 400
    raise ErrorWithRequestException.new(
      'If there is any issue with values passed in by the user',
      _response
    )
  when 401
    raise UnauthorizedRequestException.new(
      'Authentication is either incorrect or not present',
      _response
    )
  when 403
    raise ForbiddenRequestException.new(
      'The user is not authorized to access this resource',
      _response
    )
  when 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) ⇒ TwoFactorVerifyCodeResponse

Allows a user to verify an MFA code. Two-Factor enabled



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/bandwidth/multi_factor_auth_lib/multi_factor_auth/controllers/mfa_controller.rb', line 148

def create_verify_two_factor(,
                             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' => , '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.

  case _response.status_code
  when 400
    raise ErrorWithRequestException.new(
      'If there is any issue with values passed in by the user',
      _response
    )
  when 401
    raise UnauthorizedRequestException.new(
      'Authentication is either incorrect or not present',
      _response
    )
  when 403
    raise ForbiddenRequestException.new(
      'The user is not authorized to access this resource',
      _response
    )
  when 429
    raise ErrorWithRequestException.new(
      'The user has made too many bad requests and is temporarily locked' \
      ' out',
      _response
    )
  when 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) ⇒ TwoFactorVoiceResponse

Multi-Factor authentication with Bandwidth Voice services. Allows for a user to send an MFA code via a phone call. Voice service enabled



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bandwidth/multi_factor_auth_lib/multi_factor_auth/controllers/mfa_controller.rb', line 20

def create_voice_two_factor(,
                            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' => , '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.

  case _response.status_code
  when 400
    raise ErrorWithRequestException.new(
      'If there is any issue with values passed in by the user',
      _response
    )
  when 401
    raise UnauthorizedRequestException.new(
      'Authentication is either incorrect or not present',
      _response
    )
  when 403
    raise ForbiddenRequestException.new(
      'The user is not authorized to access this resource',
      _response
    )
  when 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