Class: WorkOS::MultiFactorAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/workos/multi_factor_auth.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ MultiFactorAuth

Returns a new instance of MultiFactorAuth.



9
10
11
# File 'lib/workos/multi_factor_auth.rb', line 9

def initialize(client)
  @client = client
end

Instance Method Details

#challenge_factor(id:, sms_template: nil, request_options: {}) ⇒ WorkOS::AuthenticationChallenge

Challenge Factor

Parameters:

  • id (String)

    The unique ID of the Authentication Factor to be challenged.

  • sms_template (String, nil) (defaults to: nil)

    A custom template for the SMS message. Use the {code} placeholder to include the verification code.

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

    (see WorkOS::Types::RequestOptions)

Returns:



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

def challenge_factor(
  id:,
  sms_template: nil,
  request_options: {}
)
  body = {
    "sms_template" => sms_template
  }.compact
  response = @client.request(
    method: :post,
    path: "/auth/factors/#{WorkOS::Util.encode_path(id)}/challenge",
    auth: true,
    body: body,
    request_options: request_options
  )
  result = WorkOS::AuthenticationChallenge.new(response.body)
  result.last_response = WorkOS::Types::ApiResponse.new(http_status: response.code.to_i, http_headers: response.each_header.to_h, request_id: response["x-request-id"])
  result
end

#create_user_auth_factor(userland_user_id:, type:, totp_issuer: nil, totp_user: nil, totp_secret: nil, request_options: {}) ⇒ WorkOS::UserAuthenticationFactorEnrollResponse

Enroll an authentication factor

Parameters:

  • userland_user_id (String)
  • type (String)

    The type of the factor to enroll.

  • totp_issuer (String, nil) (defaults to: nil)

    Your application or company name displayed in the user’s authenticator app.

  • totp_user (String, nil) (defaults to: nil)

    The user’s account name displayed in their authenticator app.

  • totp_secret (String, nil) (defaults to: nil)

    The Base32-encoded shared secret for TOTP factors. This can be provided when creating the auth factor, otherwise it will be generated. The algorithm used to derive TOTP codes is SHA-1, the code length is 6 digits, and the timestep is 30 seconds – the secret must be compatible with these parameters.

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

    (see WorkOS::Types::RequestOptions)

Returns:



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/workos/multi_factor_auth.rb', line 189

def create_user_auth_factor(
  userland_user_id:,
  type:,
  totp_issuer: nil,
  totp_user: nil,
  totp_secret: nil,
  request_options: {}
)
  body = {
    "type" => type,
    "totp_issuer" => totp_issuer,
    "totp_user" => totp_user,
    "totp_secret" => totp_secret
  }.compact
  response = @client.request(
    method: :post,
    path: "/user_management/users/#{WorkOS::Util.encode_path(userland_user_id)}/auth_factors",
    auth: true,
    body: body,
    request_options: request_options
  )
  result = WorkOS::UserAuthenticationFactorEnrollResponse.new(response.body)
  result.last_response = WorkOS::Types::ApiResponse.new(http_status: response.code.to_i, http_headers: response.each_header.to_h, request_id: response["x-request-id"])
  result
end

#delete_factor(id:, request_options: {}) ⇒ void

This method returns an undefined value.

Delete Factor

Parameters:

  • id (String)

    The unique ID of the Factor.

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

    (see WorkOS::Types::RequestOptions)



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/workos/multi_factor_auth.rb', line 96

def delete_factor(
  id:,
  request_options: {}
)
  @client.request(
    method: :delete,
    path: "/auth/factors/#{WorkOS::Util.encode_path(id)}",
    auth: true,
    request_options: request_options
  )
  nil
end

#enroll_factor(type:, phone_number: nil, totp_issuer: nil, totp_user: nil, user_id: nil, request_options: {}) ⇒ WorkOS::AuthenticationFactorEnrolled

Enroll Factor

Parameters:

  • type (WorkOS::Types::AuthenticationFactorsCreateRequestType)

    The type of factor to enroll.

  • phone_number (String, nil) (defaults to: nil)

    Required when type is ‘sms’.

  • totp_issuer (String, nil) (defaults to: nil)

    Required when type is ‘totp’.

  • totp_user (String, nil) (defaults to: nil)

    Required when type is ‘totp’.

  • user_id (String, nil) (defaults to: nil)

    The ID of the user to associate the factor with.

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

    (see WorkOS::Types::RequestOptions)

Returns:



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
# File 'lib/workos/multi_factor_auth.rb', line 46

def enroll_factor(
  type:,
  phone_number: nil,
  totp_issuer: nil,
  totp_user: nil,
  user_id: nil,
  request_options: {}
)
  body = {
    "type" => type,
    "phone_number" => phone_number,
    "totp_issuer" => totp_issuer,
    "totp_user" => totp_user,
    "user_id" => user_id
  }.compact
  response = @client.request(
    method: :post,
    path: "/auth/factors/enroll",
    auth: true,
    body: body,
    request_options: request_options
  )
  result = WorkOS::AuthenticationFactorEnrolled.new(response.body)
  result.last_response = WorkOS::Types::ApiResponse.new(http_status: response.code.to_i, http_headers: response.each_header.to_h, request_id: response["x-request-id"])
  result
end

#get_factor(id:, request_options: {}) ⇒ WorkOS::AuthenticationFactor

Get Factor

Parameters:

  • id (String)

    The unique ID of the Factor.

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

    (see WorkOS::Types::RequestOptions)

Returns:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/workos/multi_factor_auth.rb', line 77

def get_factor(
  id:,
  request_options: {}
)
  response = @client.request(
    method: :get,
    path: "/auth/factors/#{WorkOS::Util.encode_path(id)}",
    auth: true,
    request_options: request_options
  )
  result = WorkOS::AuthenticationFactor.new(response.body)
  result.last_response = WorkOS::Types::ApiResponse.new(http_status: response.code.to_i, http_headers: response.each_header.to_h, request_id: response["x-request-id"])
  result
end

#list_user_auth_factors(userland_user_id:, before: nil, after: nil, limit: nil, order: "desc", request_options: {}) ⇒ WorkOS::Types::ListStruct<WorkOS::AuthenticationFactor>

List authentication factors

Parameters:

  • userland_user_id (String)
  • before (String, nil) (defaults to: nil)

    An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.

  • after (String, nil) (defaults to: nil)

    An object ID that defines your place in the list. When the ID is not present, you are at the end of the list.

  • limit (Integer, nil) (defaults to: nil)

    Upper limit on the number of objects to return, between ‘1` and `100`.

  • order (WorkOS::Types::UserManagementMultiFactorAuthenticationOrder, nil) (defaults to: "desc")

    Order the results by the creation time.

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

    (see WorkOS::Types::RequestOptions)

Returns:



142
143
144
145
146
147
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
# File 'lib/workos/multi_factor_auth.rb', line 142

def list_user_auth_factors(
  userland_user_id:,
  before: nil,
  after: nil,
  limit: nil,
  order: "desc",
  request_options: {}
)
  params = {
    "before" => before,
    "after" => after,
    "limit" => limit,
    "order" => order
  }.compact
  response = @client.request(
    method: :get,
    path: "/user_management/users/#{WorkOS::Util.encode_path(userland_user_id)}/auth_factors",
    auth: true,
    params: params,
    request_options: request_options
  )
  fetch_next = ->(cursor) {
    list_user_auth_factors(
      userland_user_id: userland_user_id,
      before: before,
      after: cursor,
      limit: limit,
      order: order,
      request_options: request_options
    )
  }
  WorkOS::Types::ListStruct.from_response(
    response,
    model: WorkOS::AuthenticationFactor,
    filters: {userland_user_id: userland_user_id, before: before, limit: limit, order: order},
    fetch_next: fetch_next
  )
end

#verify_challenge(id:, code:, request_options: {}) ⇒ WorkOS::AuthenticationChallengeVerifyResponse

Verify Challenge

Parameters:

  • id (String)

    The unique ID of the Authentication Challenge.

  • code (String)

    The one-time code to verify.

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

    (see WorkOS::Types::RequestOptions)

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/workos/multi_factor_auth.rb', line 18

def verify_challenge(
  id:,
  code:,
  request_options: {}
)
  body = {
    "code" => code
  }.compact
  response = @client.request(
    method: :post,
    path: "/auth/challenges/#{WorkOS::Util.encode_path(id)}/verify",
    auth: true,
    body: body,
    request_options: request_options
  )
  result = WorkOS::AuthenticationChallengeVerifyResponse.new(response.body)
  result.last_response = WorkOS::Types::ApiResponse.new(http_status: response.code.to_i, http_headers: response.each_header.to_h, request_id: response["x-request-id"])
  result
end