Skip to content

Commit

Permalink
Add deprecated MFA API module (#58)
Browse files Browse the repository at this point in the history
* Fix timestamp mapping

* Add deprecated response structs

* Add deprecated API

* Add tests
  • Loading branch information
LauraBeatris authored Dec 9, 2023
1 parent b356821 commit 4a206eb
Show file tree
Hide file tree
Showing 13 changed files with 545 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/workos/audit_logs/export.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule WorkOS.AuditLogs.Export do
object: map["object"],
state: map["state"],
url: map["url"],
updated_at: map["created_at"],
updated_at: map["updated_at"],
created_at: map["created_at"]
}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/workos/directory_sync/directory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule WorkOS.DirectorySync.Directory do
name: map["name"],
organization_id: map["organization_id"],
domain: map["domain"],
updated_at: map["created_at"],
updated_at: map["updated_at"],
created_at: map["created_at"]
}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/workos/directory_sync/directory/group.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule WorkOS.DirectorySync.Directory.Group do
directory_id: map["directory_id"],
organization_id: map["organization_id"],
raw_attributes: map["raw_attributes"],
updated_at: map["created_at"],
updated_at: map["updated_at"],
created_at: map["created_at"]
}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/workos/directory_sync/directory/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ defmodule WorkOS.DirectorySync.Directory.User do
last_name: map["last_name"],
job_title: map["job_title"],
state: map["state"],
updated_at: map["created_at"],
updated_at: map["updated_at"],
created_at: map["created_at"]
}
end
Expand Down
122 changes: 122 additions & 0 deletions lib/workos/mfa.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
defmodule WorkOS.MFA do
@moduledoc """
This module is deprecated.
"""

@deprecated "MFA has been replaced by the User Management Multi-Factor API."

alias WorkOS.Empty
alias WorkOS.MFA.AuthenticationFactor
alias WorkOS.MFA.AuthenticationChallenge
alias WorkOS.MFA.VerifyChallenge

@doc """
Enrolls an Authentication Factor.
Parameter options:
* `:type` - The type of the factor to enroll. Only option available is `totp`. (required)
* `:totp_issuer` - For `totp` factors. Typically your application or company name, this helps users distinguish between factors in authenticator apps.
* `:totp_user` - For `totp` factors. Used as the account name in authenticator apps. Defaults to the user's email.
* `:phone_number` - A valid phone number for an SMS-enabled device. Required when type is sms.
"""
@spec enroll_factor(map()) :: WorkOS.Client.response(AuthenticationFactor.t())
@spec enroll_factor(WorkOS.Client.t(), map()) ::
WorkOS.Client.response(AuthenticationFactor.t())
def enroll_factor(client \\ WorkOS.client(), opts) when is_map_key(opts, :type) do
WorkOS.Client.post(
client,
AuthenticationFactor,
"/auth/factors/enroll",
%{
type: opts[:type],
totp_issuer: opts[:totp_issuer],
totp_user: opts[:totp_user],
phone_number: opts[:phone_number]
}
)
end

@doc """
Creates a Challenge for an Authentication Factor.
Parameter options:
* `:sms_template` - A valid phone number for an SMS-enabled device. Required when type is sms.
"""
@spec challenge_factor(String.t(), map()) :: WorkOS.Client.response(AuthenticationChallenge.t())
@spec challenge_factor(WorkOS.Client.t(), String.t(), map()) ::
WorkOS.Client.response(AuthenticationChallenge.t())
def challenge_factor(client \\ WorkOS.client(), authentication_factor_id, opts) do
WorkOS.Client.post(
client,
AuthenticationChallenge,
"/auth/factors/:id/challenge",
%{
sms_template: opts[:sms_template]
},
opts: [
path_params: [id: authentication_factor_id]
]
)
end

@doc """
Verifies Authentication Challenge.
Parameter options:
* `:code` - The 6 digit code to be verified. (required)
"""
@spec verify_challenge(String.t(), map()) :: WorkOS.Client.response(VerifyChallenge.t())
@spec verify_challenge(WorkOS.Client.t(), String.t(), map()) ::
WorkOS.Client.response(VerifyChallenge.t())
def verify_challenge(client \\ WorkOS.client(), authentication_challenge_id, opts)
when is_map_key(opts, :code) do
WorkOS.Client.post(
client,
VerifyChallenge,
"/auth/challenges/:id/verify",
%{
code: opts[:code]
},
opts: [
path_params: [id: authentication_challenge_id]
]
)
end

@doc """
Gets an Authentication Factor.
"""
@spec get_factor(String.t()) ::
WorkOS.Client.response(AuthenticationFactor.t())
@spec get_factor(WorkOS.Client.t(), String.t()) ::
WorkOS.Client.response(AuthenticationFactor.t())
def get_factor(client \\ WorkOS.client(), authentication_factor_id) do
WorkOS.Client.get(
client,
AuthenticationFactor,
"/auth/factors/:id",
opts: [
path_params: [id: authentication_factor_id]
]
)
end

@doc """
Deletes an Authentication Factor.
"""
@spec delete_factor(String.t()) :: WorkOS.Client.response(nil)
@spec delete_factor(WorkOS.Client.t(), String.t()) :: WorkOS.Client.response(nil)
def delete_factor(client \\ WorkOS.client(), authentication_factor_id) do
WorkOS.Client.delete(client, Empty, "/auth/factors/:id", %{},
opts: [
path_params: [id: authentication_factor_id]
]
)
end
end
35 changes: 35 additions & 0 deletions lib/workos/mfa/authentication_challenge.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule WorkOS.MFA.AuthenticationChallenge do
@moduledoc """
This response struct is deprecated. Use the User Management Multi-Factor API instead.
"""

@behaviour WorkOS.Castable

@type t() :: %__MODULE__{
id: String.t(),
authentication_factor_id: String.t(),
expires_at: String.t(),
updated_at: String.t(),
created_at: String.t()
}

@enforce_keys [:id, :authentication_factor_id, :expires_at, :updated_at, :created_at]
defstruct [
:id,
:authentication_factor_id,
:expires_at,
:updated_at,
:created_at
]

@impl true
def cast(map) do
%__MODULE__{
id: map["id"],
authentication_factor_id: map["authentication_factor_id"],
expires_at: map["expires_at"],
updated_at: map["updated_at"],
created_at: map["created_at"]
}
end
end
41 changes: 41 additions & 0 deletions lib/workos/mfa/authentication_factor.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
defmodule WorkOS.MFA.AuthenticationFactor do
@moduledoc """
This response struct is deprecated. Use the User Management Multi-Factor API instead.
"""

@behaviour WorkOS.Castable

alias WorkOS.MFA.SMS
alias WorkOS.MFA.TOTP

@type t() :: %__MODULE__{
id: String.t(),
type: String.t(),
sms: SMS.t() | nil,
totp: TOTP.t() | nil,
updated_at: String.t(),
created_at: String.t()
}

@enforce_keys [:id, :type, :sms, :totp, :updated_at, :created_at]
defstruct [
:id,
:type,
:sms,
:totp,
:updated_at,
:created_at
]

@impl true
def cast(map) do
%__MODULE__{
id: map["id"],
type: map["type"],
sms: map["sms"],
totp: map["totp"],
updated_at: map["updated_at"],
created_at: map["created_at"]
}
end
end
25 changes: 25 additions & 0 deletions lib/workos/mfa/sms.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule WorkOS.MFA.SMS do
@moduledoc """
This response struct is deprecated. Use the User Management Multi-Factor API instead.
"""

@behaviour WorkOS.Castable

@type t() :: %__MODULE__{
phone_number: String.t()
}

@enforce_keys [
:phone_number
]
defstruct [
:phone_number
]

@impl true
def cast(map) do
%__MODULE__{
phone_number: map["phone_number"]
}
end
end
41 changes: 41 additions & 0 deletions lib/workos/mfa/totp.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
defmodule WorkOS.MFA.TOTP do
@moduledoc """
This response struct is deprecated. Use the User Management Multi-Factor API instead.
"""

@behaviour WorkOS.Castable

@type t() :: %__MODULE__{
issuer: String.t(),
user: String.t(),
secret: String.t(),
qr_code: String.t(),
uri: String.t()
}

@enforce_keys [
:issuer,
:user,
:secret,
:qr_code,
:uri
]
defstruct [
:issuer,
:user,
:secret,
:qr_code,
:uri
]

@impl true
def cast(map) do
%__MODULE__{
issuer: map["issuer"],
user: map["user"],
secret: map["secret"],
qr_code: map["qr_code"],
uri: map["uri"]
}
end
end
28 changes: 28 additions & 0 deletions lib/workos/mfa/verify_challenge.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule WorkOS.MFA.VerifyChallenge do
@moduledoc """
This response struct is deprecated. Use the User Management Multi-Factor API instead.
"""

@behaviour WorkOS.Castable

alias WorkOS.MFA.AuthenticationChallenge

@type t() :: %__MODULE__{
challenge: AuthenticationChallenge.t(),
valid: boolean()
}

@enforce_keys [:challenge, :valid]
defstruct [
:challenge,
:valid
]

@impl true
def cast(map) do
%__MODULE__{
challenge: map["challenge"],
valid: map["valid"]
}
end
end
2 changes: 1 addition & 1 deletion lib/workos/sso/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule WorkOS.SSO.Connection do
connection_type: map["connection_type"],
state: map["state"],
domains: Castable.cast_list(Domain, map["domains"]),
updated_at: map["created_at"],
updated_at: map["updated_at"],
created_at: map["created_at"],
organization_id: map["organization_id"]
}
Expand Down
Loading

0 comments on commit 4a206eb

Please sign in to comment.