Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented create user with scrypt modified password #244

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
```

## ⌛ Progress
![Server & Client - 64 / 291](https://img.shields.io/badge/Server_&_Client-64%20%2F%20291-red?style=for-the-badge)
![Server & Client - 65 / 291](https://img.shields.io/badge/Server_&_Client-65%20%2F%20291-red?style=for-the-badge)

![Server - 20 / 201](https://img.shields.io/badge/Server-20%20%2F%20201-red?style=for-the-badge)
![Server - 21 / 201](https://img.shields.io/badge/Server-21%20%2F%20201-red?style=for-the-badge)

![Client - 44 / 90](https://img.shields.io/badge/Client-44%20%2F%2090-gold?style=for-the-badge)

Expand Down Expand Up @@ -207,7 +207,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Create Phone Verification (Confirmation)](https://appwrite.io/docs/references/1.6.x/client-rest/account#updatePhoneVerification) | ✅ | ❌ | |

### Users
![Account - 9 / 41](https://img.shields.io/badge/Users-9%20%2F%2041-red?style=for-the-badge)
![Account - 10 / 41](https://img.shields.io/badge/Users-10%20%2F%2041-red?style=for-the-badge)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand All @@ -220,7 +220,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Create User with MD5 Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createMD5User) | ❌ | ✅ |
| [Create User with PHPass Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createPHPassUser) | ❌ | ✅ |
| [Create User with Scrypt Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createScryptUser) | ❌ | ✅ |
| [Create User with Scrypt Modified Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createScryptModifiedUser) | ❌ | |
| [Create User with Scrypt Modified Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createScryptModifiedUser) | ❌ | |
| [Create User with SHA Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#createSHAUser) | ❌ | ⬛ |
| [Get User](https://appwrite.io/docs/references/1.6.x/server-rest/users#get) | ❌ | ⬛ |
| [Delete User](https://appwrite.io/docs/references/1.6.x/server-rest/users#delete) | ❌ | ⬛ |
Expand Down
10 changes: 4 additions & 6 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server,

public async Task Run(string[] args)
{
var request = new CreateUserWithScryptPasswordRequest()
var request = new CreateUserWithScryptModifiedPasswordRequest()
{
Email = "[email protected]",
Password = "dbb97714c09bad417bb51288abd2c049c557e5e617839a7bc8c615492db859b57624366de72f04b9d4c3e6452a497a67a36f1afcc481d6d69f6da9e5f03598d7",
PasswordSalt = "MySuperSalt",
PasswordCpu = 16384,
PasswordMemory = 8,
PasswordParallel = 1,
PasswordLength = 64
PasswordSaltSeparator = ";",
PasswordSignerKey = "MySuperSignerKey"
};

var response = await _server.Users.CreateUserWithScryptPassword(request);
var response = await _server.Users.CreateUserWithScryptModifiedPassword(request);

Console.WriteLine(response.Result.Match(
result => result.ToString(),
Expand Down
8 changes: 7 additions & 1 deletion src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ public interface IUsersClient
/// <param name="request">The request content</param>
/// <returns>The user</returns>
Task<AppwriteResult<User>> CreateUserWithScryptPassword(CreateUserWithScryptPasswordRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]

/// <summary>
/// Create a new user. Password provided must be hashed with the <see href="https://gist.github.com/Meldiron/eecf84a0225eccb5a378d45bb27462cc">Scrypt Modified</see> algorithm. Use <see cref="CreateUser(CreateUserRequest)"/> to create users with a plain text password.
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/users#createScryptUser">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The user</returns>
Task<AppwriteResult<User>> CreateUserWithScryptModifiedPassword(CreateUserWithScryptModifiedPasswordRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]
Task<AppwriteResult<User>> CreateUserWithShaPassword(CreateUserWithShaPasswordRequest request);
Expand Down
17 changes: 15 additions & 2 deletions src/PinguApps.Appwrite.Server/Clients/UsersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,22 @@ public async Task<AppwriteResult<User>> CreateUserWithScryptPassword(CreateUserW
}
}

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
public Task<AppwriteResult<User>> CreateUserWithScryptModifiedPassword(CreateUserWithScryptModifiedPasswordRequest request) => throw new NotImplementedException();
public async Task<AppwriteResult<User>> CreateUserWithScryptModifiedPassword(CreateUserWithScryptModifiedPasswordRequest request)
{
try
{
request.Validate(true);

var result = await _usersApi.CreateUserWithScryptModifiedPassword(request);

return result.GetApiResponse();
}
catch (Exception e)
{
return e.GetExceptionResponse<User>();
}
}

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.Net;
using PinguApps.Appwrite.Shared.Requests.Users;
using PinguApps.Appwrite.Shared.Tests;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Server.Tests.Clients.Users;
public partial class UsersClientTests
{
[Fact]
public async Task CreateUserWithScryptModifiedPassword_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new CreateUserWithScryptModifiedPasswordRequest
{
Email = "[email protected]",
Password = "password123",
PasswordSalt = "MySalt",
PasswordSaltSeparator = "Separator",
PasswordSignerKey = "SignerKey"
};

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/users/scrypt-modified")
.WithJsonContent(request)
.ExpectedHeaders()
.Respond(Constants.AppJson, Constants.UserResponse);

// Act
var result = await _appwriteClient.Users.CreateUserWithScryptModifiedPassword(request);

// Assert
Assert.True(result.Success);
}

[Fact]
public async Task CreateUserWithScryptModifiedPassword_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new CreateUserWithScryptModifiedPasswordRequest
{
Email = "[email protected]",
Password = "password123",
PasswordSalt = "MySalt",
PasswordSaltSeparator = "Separator",
PasswordSignerKey = "SignerKey"
};

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/users/scrypt-modified")
.WithJsonContent(request)
.ExpectedHeaders()
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError);

// Act
var result = await _appwriteClient.Users.CreateUserWithScryptModifiedPassword(request);

// Assert
Assert.True(result.IsError);
Assert.True(result.IsAppwriteError);
}

[Fact]
public async Task CreateUserWithScryptModifiedPassword_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new CreateUserWithScryptModifiedPasswordRequest
{
Email = "[email protected]",
Password = "password123",
PasswordSalt = "MySalt",
PasswordSaltSeparator = "Separator",
PasswordSignerKey = "SignerKey"
};

_mockHttp.Expect(HttpMethod.Post, $"{Constants.Endpoint}/users/scrypt-modified")
.WithJsonContent(request)
.ExpectedHeaders()
.Throw(new HttpRequestException("An error occurred"));

// Act
var result = await _appwriteClient.Users.CreateUserWithScryptModifiedPassword(request);

// Assert
Assert.False(result.Success);
Assert.True(result.IsInternalError);
Assert.Equal("An error occurred", result.Result.AsT2.Message);
}
}