Class: Cpaas::Twofactor

Inherits:
Object
  • Object
show all
Defined in:
lib/cpaas-sdk/resources/twofactor.rb

Overview

CPaaS provides Authentication API where a two-factor authentication (2FA) flow can be implemented by using that.

Class Method Summary collapse

Class Method Details

.delete_code(params = {}) ⇒ Object

Delete authentication code resource.

Parameters:

  • params (Hash) (defaults to: {})

Options Hash (params):

  • :code_id (String)

    ID of the authentication code.



129
130
131
# File 'lib/cpaas-sdk/resources/twofactor.rb', line 129

def self.delete_code(params = {})
  Cpaas.api.send_request("#{base_url}/codes/#{params[:code_id]}", {}, :delete)
end

.resend_code(params = {}) ⇒ Object

Resending the authentication code via same code resource, invalidating the previously sent code.

Parameters:

  • params (Hash) (defaults to: {})

Options Hash (params):

  • :destination_address (String|Array[string])

    Destination address of the authentication code being sent. For sms type authentication codes, it should contain a E164 phone number. For e-mail type authentication codes, it should contain a valid e-mail address.

  • :message (String)

    Message text sent to the destination, containing the placeholder for the code within the text. CPaaS requires to have *code* string within the text in order to generate a code and inject into the text. For email type code, one usage is to have the *code* string located within the link in order to get a unique link.

  • :code_id (String)

    ID of the authentication code.

  • :method (String) — default: 'sms'

    optional Type of the authentication code delivery method, sms and email are supported types. Possible values: sms, email

  • :subject (String)

    optional When the method is passed as email then subject becomes a mandatory field to pass. The value passed becomes the subject line of the 2FA code email that is sent out to the destination_address.

  • :expiry (Number) — default: 120

    optional Lifetime duration of the code sent in seconds. This can contain values between 30 and 3600 seconds.

  • :length (Number) — default: 6

    optional Length of the authentication code tha CPaaS should generate for this request. It can contain values between 4 and 10.

  • :type (String) — default: 'numeric'

    optional Type of the code that is generated. If not provided, default value is numeric. Possible values: numeric, alphanumeric, alphabetic



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
# File 'lib/cpaas-sdk/resources/twofactor.rb', line 95

def self.resend_code(params = {})
  address = (params[:destination_address].is_a? String) ? [ params[:destination_address] ] : params[:destination_address]

  options = {
    body: {
      code: {
        address: address,
        method: params[:method] || 'sms',
        format: {
          length: params[:length] || 6,
          type: params[:type] || 'numeric'
        },
        expiry: params[:expiry] || 120,
        subject: params[:subject],
        message: params[:message]
      }
    }
  }

  response = Cpaas.api.send_request("#{base_url}/codes/#{params[:code_id]}", options, :put)

  process_response(response) do |res|
    {
      code_id: id_from(res.dig(:code, :resource_url))
    }
  end
end

.send_code(params = {}) ⇒ Object

Create a new authentication code.

Parameters:

  • params (Hash) (defaults to: {})

Options Hash (params):

  • :destination_address (String|Array[string])

    Destination address of the authentication code being sent. For sms type authentication codes, it should contain a E164 phone number. For e-mail type authentication codes, it should contain a valid e-mail address.

  • :message (String)

    Message text sent to the destination, containing the placeholder for the code within the text. CPaaS requires to have *code* string within the text in order to generate a code and inject into the text. For email type code, one usage is to have the *code* string located within the link in order to get a unique link.

  • :method (String) — default: 'sms'

    optional Type of the authentication code delivery method, sms and email are supported types. Possible values: sms, email

  • :subject (String)

    optional When the method is passed as email then subject becomes a mandatory field to pass. The value passed becomes the subject line of the 2FA code email that is sent out to the destination_address.

  • :expiry (Number) — default: 120

    optional Lifetime duration of the code sent in seconds. This can contain values between 30 and 3600 seconds.

  • :length (Number) — default: 6

    optional Length of the authentication code tha CPaaS should generate for this request. It can contain values between 4 and 10.

  • :type (String) — default: 'numeric'

    optional Type of the code that is generated. If not provided, default value is numeric. Possible values: numeric, alphanumeric, alphabetic



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
# File 'lib/cpaas-sdk/resources/twofactor.rb', line 21

def self.send_code(params = {})
  address = (params[:destination_address].is_a? String) ? [ params[:destination_address] ] : params[:destination_address]

  options = {
    body: {
      code: {
        address: address,
        method: params[:method] || 'sms',
        format: {
          length: params[:length] || 6,
          type: params[:type] || 'numeric'
        },
        expiry: params[:expiry] || 120,
        subject: params[:subject],
        message: params[:message]
      }
    }
  }

  response = Cpaas.api.send_request("#{base_url}/codes", options, :post)

  process_response(response) do |res|
    {
      code_id: id_from(res.dig(:code, :resource_url))
    }
  end
end

.verify_code(params = {}) ⇒ Object

Verifying authentication code

Parameters:

  • params (Hash) (defaults to: {})

Options Hash (params):

  • :code_id (String)

    ID of the authentication code.

  • :verification_code (String)

    Code that is being verified



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cpaas-sdk/resources/twofactor.rb', line 56

def self.verify_code(params = {})
  options = {
    body: {
      code: {
        verify: params[:verification_code]
      }
    }
  }

  response = Cpaas.api.send_request("#{base_url}/codes/#{params[:code_id]}/verify", options, :put)

  process_response(response) do |res|
    if res[:status_code] == 204
      {
        verified: true,
        message: 'Success'
      }
    else
      {
        verified: false,
        message: 'Code invalid or expired'
      }
    end
  end
end