Skip to content

Commit

Permalink
Authenticate methods updates (#149)
Browse files Browse the repository at this point in the history
* Authenticate methods updates

* Add UpdateUser and UpdateUserPassword

* Revert "Add UpdateUser and UpdateUserPassword"

This reverts commit 8440eb4.

* Description fix

* Add AuthenticateUserResponse and description fix

* lint
  • Loading branch information
Patrick committed Aug 30, 2023
1 parent 3c0b100 commit 482cdbf
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 52 deletions.
24 changes: 12 additions & 12 deletions src/WorkOS.net/Services/UserManagement/UserManagementService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,56 +88,56 @@ public UserManagementService(WorkOSClient client)
/// </summary>
/// <param name="options"> Parameters used to authenticate user with password.</param>
/// <param name="cancellationToken">An optional token to cancel the request.</param>
/// <returns> A User and Session record.</returns>
public async Task<(User, Session)> AuthenticateUserWithPassword(
/// <returns> A AuthenticateUserWithPasswordResponse record.</returns>
public async Task<AuthenticateUserResponse> AuthenticateUserWithPassword(
AuthenticateUserWithPasswordOptions options,
CancellationToken cancellationToken = default)
{
var request = new WorkOSRequest
{
Options = options,
Method = HttpMethod.Post,
Path = $"/users/sessions/token",
Path = $"/users/authenticate",
};
return await this.Client.MakeAPIRequest<(User, Session)>(request, cancellationToken);
return await this.Client.MakeAPIRequest<AuthenticateUserResponse>(request, cancellationToken);
}

/// <summary>
/// Authenticate user session with a code.
/// </summary>
/// <param name="options"> Parameters used to authenticate user with a code.</param>
/// <param name="cancellationToken">An optional token to cancel the request.</param>
/// <returns> A User and Session record.</returns>
public async Task<(User, Session)> AuthenticateUserWithCode(
/// <returns> A AuthenticateUserWithCodeResponse record.</returns>
public async Task<AuthenticateUserResponse> AuthenticateUserWithCode(
AuthenticateUserWithCodeOptions options,
CancellationToken cancellationToken = default)
{
var request = new WorkOSRequest
{
Options = options,
Method = HttpMethod.Post,
Path = $"/users/sessions/token",
Path = $"/users/authenticate",
};
return await this.Client.MakeAPIRequest<(User, Session)>(request, cancellationToken);
return await this.Client.MakeAPIRequest<AuthenticateUserResponse>(request, cancellationToken);
}

/// <summary>
/// Authenticate user session with Magic Auth.
/// </summary>
/// <param name="options"> Parameters used to authenticate user with Magic Auth.</param>
/// <param name="cancellationToken">An optional token to cancel the request.</param>
/// <returns> A User and Session record.</returns>
public async Task<(User, Session)> AuthenticateUserWithMagicAuth(
/// <returns> A AuthenticateUserWithMagicAuthResponse record.</returns>
public async Task<AuthenticateUserResponse> AuthenticateUserWithMagicAuth(
AuthenticateUserWithMagicAuthOptions options,
CancellationToken cancellationToken = default)
{
var request = new WorkOSRequest
{
Options = options,
Method = HttpMethod.Post,
Path = $"/users/sessions/token",
Path = $"/users/authenticate",
};
return await this.Client.MakeAPIRequest<(User, Session)>(request, cancellationToken);
return await this.Client.MakeAPIRequest<AuthenticateUserResponse>(request, cancellationToken);
}

/// <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 authenticate a user with a password.
/// </summary>
public class AuthenticateUserResponse
{
/// <summary>
/// Represents the corresponding User object.
/// </summary>
[JsonProperty("user")]
public User User { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,5 @@ public class AuthenticateUserWithCodeOptions : BaseOptions
/// </summary>
[JsonProperty("user_agent")]
public string UserAgent { get; set; }

/// <summary>
/// The length of the session in minutes. Defaults to 1 day, 1440.
/// </summary>
[JsonProperty("expires_in")]
public int ExpiresIn { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public class AuthenticateUserWithMagicAuthOptions : BaseOptions
public string Code { get; set; }

/// <summary>
/// The challenge ID returned from when the one-time code was sent to the user.
/// The ID of the User authenticating.
/// </summary>
[JsonProperty("magic_auth_challenge_id")]
public string MagicAuthChallengeId { get; set; }
[JsonProperty("user_id")]
public string UserId { get; set; }

/// <summary>
/// The IP address of the request from the user who is attempting to authenticate.
Expand All @@ -48,11 +48,5 @@ public class AuthenticateUserWithMagicAuthOptions : BaseOptions
/// </summary>
[JsonProperty("user_agent")]
public string UserAgent { get; set; }

/// <summary>
/// The length of the session in minutes. Defaults to 1 day, 1440.
/// </summary>
[JsonProperty("expires_in")]
public int ExpiresIn { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,5 @@ public class AuthenticateUserWithPasswordOptions : BaseOptions
/// </summary>
[JsonProperty("user_agent")]
public string UserAgent { get; set; }

/// <summary>
/// The length of the session in minutes. Defaults to 1 day, 1440.
/// </summary>
[JsonProperty("expires_in")]
public int ExpiresIn { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class UserManagementServiceTest

private readonly string mockToken;

private readonly AuthenticateUserResponse mockAuthenticateUserResponse;

private readonly CreateUserOptions mockCreateUserOptions;

private readonly AuthenticateUserWithPasswordOptions mockAuthenticateUserWithPasswordOptions;
Expand Down Expand Up @@ -168,6 +170,11 @@ public UserManagementServiceTest()

this.mockToken = "token_1234";

this.mockAuthenticateUserResponse = new AuthenticateUserResponse
{
User = this.mockUser,
};

this.mockListUsersOptions = new ListUsersOptions
{
Email = "marcelina.davis@gmail.com",
Expand Down Expand Up @@ -202,7 +209,6 @@ public UserManagementServiceTest()
ClientId = "client_123",
ClientSecret = "client_secret_123",
Code = "code_123",
MagicAuthChallengeId = "auth_challenge_123",
};

this.mockSendMagicAuthCodeOptions = new SendMagicAuthCodeOptions
Expand Down Expand Up @@ -311,54 +317,54 @@ public async void TestAuthenticateUserWithPassword()
{
this.httpMock.MockResponse(
HttpMethod.Post,
$"/users/sessions/token",
$"/users/authenticate",
HttpStatusCode.Created,
RequestUtilities.ToJsonString((this.mockUser, this.mockSession)));
RequestUtilities.ToJsonString(this.mockAuthenticateUserResponse));

var (user, session) = await this.service.AuthenticateUserWithPassword(this.mockAuthenticateUserWithPasswordOptions);
var response = await this.service.AuthenticateUserWithPassword(this.mockAuthenticateUserWithPasswordOptions);

this.httpMock.AssertRequestWasMade(
HttpMethod.Post,
$"/users/sessions/token");
$"/users/authenticate");
Assert.Equal(
JsonConvert.SerializeObject(session),
JsonConvert.SerializeObject(this.mockSession));
JsonConvert.SerializeObject(response),
JsonConvert.SerializeObject(this.mockAuthenticateUserResponse));
}

[Fact]
public async void TestAuthenticateUserWithCode()
{
this.httpMock.MockResponse(
HttpMethod.Post,
$"/users/sessions/token",
$"/users/authenticate",
HttpStatusCode.Created,
RequestUtilities.ToJsonString((this.mockUser, this.mockSession)));
var (user, session) = await this.service.AuthenticateUserWithCode(this.mockAuthenticateUserWithCodeOptions);
RequestUtilities.ToJsonString(this.mockAuthenticateUserResponse));
var response = await this.service.AuthenticateUserWithCode(this.mockAuthenticateUserWithCodeOptions);
this.httpMock.AssertRequestWasMade(
HttpMethod.Post,
$"/users/sessions/token");
$"/users/authenticate");
Assert.Equal(
JsonConvert.SerializeObject(session),
JsonConvert.SerializeObject(this.mockSession));
JsonConvert.SerializeObject(response),
JsonConvert.SerializeObject(this.mockAuthenticateUserResponse));
}

[Fact]
public async void TestAuthenticateUserWithMagicAuth()
{
this.httpMock.MockResponse(
HttpMethod.Post,
$"/users/sessions/token",
$"/users/authenticate",
HttpStatusCode.Created,
RequestUtilities.ToJsonString((this.mockUser, this.mockSession)));
RequestUtilities.ToJsonString(this.mockAuthenticateUserResponse));

var (user, session) = await this.service.AuthenticateUserWithMagicAuth(this.mockAuthenticateUserWithMagicAuthOptions);
var response = await this.service.AuthenticateUserWithMagicAuth(this.mockAuthenticateUserWithMagicAuthOptions);

this.httpMock.AssertRequestWasMade(
HttpMethod.Post,
$"/users/sessions/token");
$"/users/authenticate");
Assert.Equal(
JsonConvert.SerializeObject(session),
JsonConvert.SerializeObject(this.mockSession));
JsonConvert.SerializeObject(response),
JsonConvert.SerializeObject(this.mockAuthenticateUserResponse));
}

[Fact]
Expand Down

0 comments on commit 482cdbf

Please sign in to comment.