Module: WorkOS::MFA

Extended by:
Client, Deprecation
Defined in:
lib/workos/mfa.rb

Overview

The MFA module provides convenience methods for working with the WorkOS MFA platform. You’ll need a valid API key

Class Method Summary collapse

Methods included from Client

client, delete_request, execute_request, get_request, handle_error_response, post_request, put_request, user_agent

Methods included from Deprecation

warn_deprecation

Class Method Details

.challenge_factor(authentication_factor_id: nil, sms_template: nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/workos/mfa.rb', line 81

def challenge_factor(
  authentication_factor_id: nil,
  sms_template: nil
)
  if authentication_factor_id.nil?
    raise ArgumentError, "Incomplete arguments: 'authentication_factor_id' is a required argument"
  end

  request = post_request(
    auth: true,
    body: {
      sms_template: sms_template,
    },
    path: "/auth/factors/#{authentication_factor_id}/challenge",
  )

  response = execute_request(request: request)
  WorkOS::Challenge.new(response.body)
end

.delete_factor(id:) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/workos/mfa.rb', line 13

def delete_factor(id:)
  response = execute_request(
    request: delete_request(
      path: "/auth/factors/#{id}",
      auth: true,
    ),
  )
  response.is_a? Net::HTTPSuccess
end

.enroll_factor(type:, totp_issuer: nil, totp_user: nil, phone_number: nil) ⇒ Object

rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity



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

def enroll_factor(
  type:,
  totp_issuer: nil,
  totp_user: nil,
  phone_number: nil
)
  validate_args(
    type: type,
    totp_issuer: totp_issuer,
    totp_user: totp_user,
    phone_number: phone_number,
  )
  response = execute_request(request: post_request(
    auth: true,
    body: {
      type: type,
      totp_issuer: totp_issuer,
      totp_user: totp_user,
      phone_number: phone_number,
    },
    path: '/auth/factors/enroll',
  ))
  WorkOS::Factor.new(response.body)
end

.get_factor(id:) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/workos/mfa.rb', line 23

def get_factor(
  id:
)
  response = execute_request(
    request: get_request(
      path: "/auth/factors/#{id}",
      auth: true,
    ),
  )
  WorkOS::Factor.new(response.body)
end

.validate_args(type:, totp_issuer: nil, totp_user: nil, phone_number: nil) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/workos/mfa.rb', line 37

def validate_args(
  type:,
  totp_issuer: nil,
  totp_user: nil,
  phone_number: nil
)
  if type != 'sms' && type != 'totp' && type != 'generic_otp'
    raise ArgumentError, "Type argument must be either 'sms' or 'totp'"
  end
  if (type == 'totp' && totp_issuer.nil?) || (type == 'totp' && totp_user.nil?)
    raise ArgumentError, 'Incomplete arguments. Need to specify both totp_issuer and totp_user when type is totp'
  end
  return unless type == 'sms' && phone_number.nil?

  raise ArgumentError, 'Incomplete arguments. Need to specify phone_number when type is sms'
end

.verify_challenge(authentication_challenge_id: nil, code: nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/workos/mfa.rb', line 113

def verify_challenge(
  authentication_challenge_id: nil,
  code: nil
)

  if authentication_challenge_id.nil? || code.nil?
    raise ArgumentError, "Incomplete arguments: 'authentication_challenge_id' and 'code' are required arguments"
  end

  options = {
    "code": code,
  }

  response = execute_request(
    request: post_request(
      path: "/auth/challenges/#{authentication_challenge_id}/verify",
      auth: true,
      body: options,
    ),
  )
  WorkOS::VerifyChallenge.new(response.body)
end

.verify_factor(authentication_challenge_id: nil, code: nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/workos/mfa.rb', line 101

def verify_factor(
  authentication_challenge_id: nil,
  code: nil
)
  warn_deprecation '`verify_factor` is deprecated. Please use `verify_challenge` instead.'

  verify_challenge(
    authentication_challenge_id: authentication_challenge_id,
    code: code,
  )
end