Skip to content

Commit

Permalink
Add SendMagicAuthCode (#144)
Browse files Browse the repository at this point in the history
* Add SendMagicAuthCode

* Add Test

* Description fix

* lint

* Revise description

* Revised Send Magic Auth

* Update user object to not include UserType

* Add SendMagicAuthCodeResponse

* Rename description

* Rename description

* Newline

* Remove MagicAuthChallenge file

---------

Co-authored-by: Marshall Bowers <elliott.codes@gmail.com>
  • Loading branch information
Patrick and maxdeviant committed Aug 30, 2023
1 parent ec3ec8b commit 3c0b100
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace WorkOS
{
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

/// <summary>
/// Represents a response for a Magic Auth code.
/// </summary>
public class SendMagicAuthCodeResponse
{
/// <summary>
/// The corresponding User object.
/// </summary>
[JsonProperty("user")]
public User User { get; set; }
}
}
19 changes: 19 additions & 0 deletions src/WorkOS.net/Services/UserManagement/UserManagementService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ public UserManagementService(WorkOSClient client)
return await this.Client.MakeAPIRequest<(User, Session)>(request, cancellationToken);
}

/// <summary>
/// Creates a one-time Magic Auth code and emails it to the user.
/// </summary>
/// <param name="options"> Parameters used to send magic auth code.</param>
/// <param name="cancellationToken">An optional token to cancel the request.</param>
/// <returns> A Magic Auth Challenge.</returns>
public async Task<SendMagicAuthCodeResponse> SendMagicAuthCode(
SendMagicAuthCodeOptions options,
CancellationToken cancellationToken = default)
{
var request = new WorkOSRequest
{
Options = options,
Method = HttpMethod.Post,
Path = $"/users/magic_auth/send",
};
return await this.Client.MakeAPIRequest<SendMagicAuthCodeResponse>(request, cancellationToken);
}

/// <summary>
/// Creates an email verification challenge and emails verification token to user.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace WorkOS
{
using Newtonsoft.Json;

/// <summary>
/// The parameters to send Magic Auth code.
/// </summary>
public class SendMagicAuthCodeOptions : BaseOptions
{
/// <summary>
/// The email address of the user.
/// </summary>
[JsonProperty("email")]
public string Email { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ public class UserManagementServiceTest

private readonly UserManagementService service;

private readonly ListUsersOptions listUsersOptions;
private readonly ListUsersOptions mockListUsersOptions;

private readonly User mockUser;

private readonly User mockUser2;

private readonly SendMagicAuthCodeResponse mockSendMagicAuthCodeResponse;

private readonly Session mockSession;

private readonly Organization mockOrganization;
Expand All @@ -41,10 +43,14 @@ public class UserManagementServiceTest

private readonly AuthenticateUserWithMagicAuthOptions mockAuthenticateUserWithMagicAuthOptions;

private readonly SendMagicAuthCodeOptions mockSendMagicAuthCodeOptions;

private readonly CreateEmailVerificationChallengeOptions mockCreateEmailVerificationChallengeOptions;

private readonly CompleteEmailVerificationOptions mockCompleteEmailVerificationOptions;

private readonly AddUserToOrganizationOptions mockAddUserToOrganizationOptions;

private readonly CreatePasswordResetChallengeOptions mockCreatePasswordResetChallengeOptions;

private readonly CompletePasswordResetOptions mockCompletePasswordResetOptions;
Expand Down Expand Up @@ -80,9 +86,9 @@ public UserManagementServiceTest()
UpdatedAt = "2022-08-27T19:07:33.155Z",
};

this.listUsersOptions = new ListUsersOptions
this.mockSendMagicAuthCodeResponse = new SendMagicAuthCodeResponse
{
Organization = "org_1234",
User = this.mockUser,
};

this.mockOrganization = new Organization
Expand Down Expand Up @@ -162,6 +168,11 @@ public UserManagementServiceTest()

this.mockToken = "token_1234";

this.mockListUsersOptions = new ListUsersOptions
{
Email = "marcelina.davis@gmail.com",
};

this.mockCreateUserOptions = new CreateUserOptions
{
Email = "marcelina.davis@gmail.com",
Expand Down Expand Up @@ -194,6 +205,11 @@ public UserManagementServiceTest()
MagicAuthChallengeId = "auth_challenge_123",
};

this.mockSendMagicAuthCodeOptions = new SendMagicAuthCodeOptions
{
Email = "marcelina.davis@gmail.com",
};

this.mockCreateEmailVerificationChallengeOptions = new CreateEmailVerificationChallengeOptions
{
VerificationUrl = "verify_url_1234",
Expand Down Expand Up @@ -279,7 +295,7 @@ public async void TestListUsers()
HttpStatusCode.OK,
RequestUtilities.ToJsonString(mockResponse));

var response = await this.service.ListUsers(this.listUsersOptions);
var response = await this.service.ListUsers(this.mockListUsersOptions);

this.httpMock.AssertRequestWasMade(
HttpMethod.Get,
Expand Down Expand Up @@ -345,6 +361,25 @@ public async void TestAuthenticateUserWithMagicAuth()
JsonConvert.SerializeObject(this.mockSession));
}

[Fact]
public async void TestSendMagicAuthCode()
{
this.httpMock.MockResponse(
HttpMethod.Post,
$"/users/magic_auth/send",
HttpStatusCode.Created,
RequestUtilities.ToJsonString(this.mockSendMagicAuthCodeResponse));

var user = await this.service.SendMagicAuthCode(this.mockSendMagicAuthCodeOptions);

this.httpMock.AssertRequestWasMade(
HttpMethod.Post,
$"/users/magic_auth/send");
Assert.Equal(
JsonConvert.SerializeObject(user),
JsonConvert.SerializeObject(this.mockSendMagicAuthCodeResponse));
}

[Fact]
public async void TestCreateEmailVerificationChallenge()
{
Expand Down

0 comments on commit 3c0b100

Please sign in to comment.