Skip to content

Commit

Permalink
Merge pull request #127 from PinguApps/93-get-mfa-recovery-codes
Browse files Browse the repository at this point in the history
Implemented get mfa recovery codes
  • Loading branch information
pingu2k4 authored Aug 10, 2024
2 parents a008c8b + 30a4fef commit ceabee2
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 8 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
```

## ⌛ Progress
<!-- ![26 / 288](https://progress-bar.dev/26/?scale=288&suffix=%20/%20288&width=500) -->
![Server & Client - 26 / 288](https://img.shields.io/badge/Server_&_Client-26%20%2F%20288-red?style=for-the-badge)
<!-- ![27 / 288](https://progress-bar.dev/27/?scale=288&suffix=%20/%20288&width=500) -->
![Server & Client - 27 / 288](https://img.shields.io/badge/Server_&_Client-27%20%2F%20288-red?style=for-the-badge)

<!-- ![2 / 195](https://progress-bar.dev/2/?scale=195&suffix=%20/%20195&width=300) -->
![Server - 2 / 195](https://img.shields.io/badge/Server-2%20%2F%20195-red?style=for-the-badge)

<!-- ![24 / 93](https://progress-bar.dev/24/?scale=93&suffix=%20/%2093&width=300) -->
![Client - 24 / 93](https://img.shields.io/badge/Client-24%20%2F%2093-red?style=for-the-badge)
<!-- ![25 / 93](https://progress-bar.dev/25/?scale=93&suffix=%20/%2093&width=300) -->
![Client - 25 / 93](https://img.shields.io/badge/Client-25%20%2F%2093-red?style=for-the-badge)

### 🔑 Key
| Icon | Definition |
Expand All @@ -155,8 +155,8 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
|| There is currently no intention to implement the endpoint for the given SDK type (client or server) |

### Account
<!-- ![26 / 52](https://progress-bar.dev/26/?scale=52&suffix=%20/%2052&width=120) -->
![Account - 26 / 52](https://img.shields.io/badge/Account-26%20%2F%2052-yellow?style=for-the-badge)
<!-- ![27 / 52](https://progress-bar.dev/27/?scale=52&suffix=%20/%2052&width=120) -->
![Account - 27 / 52](https://img.shields.io/badge/Account-27%20%2F%2052-yellow?style=for-the-badge)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand All @@ -174,7 +174,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Create 2FA Challenge](https://appwrite.io/docs/references/1.5.x/client-rest/account#createMfaChallenge) |||
| [Create MFA Challenge (confirmation)](https://appwrite.io/docs/references/1.5.x/client-rest/account#updateMfaChallenge) |||
| [List Factors](https://appwrite.io/docs/references/1.5.x/client-rest/account#listMfaFactors) |||
| [Get MFA Recovery Codes](https://appwrite.io/docs/references/1.5.x/client-rest/account#getMfaRecoveryCodes) | ||
| [Get MFA Recovery Codes](https://appwrite.io/docs/references/1.5.x/client-rest/account#getMfaRecoveryCodes) | ||
| [Create MFA Recovery Codes](https://appwrite.io/docs/references/1.5.x/client-rest/account#createMfaRecoveryCodes) |||
| [Regenerate MFA Recovery Codes](https://appwrite.io/docs/references/1.5.x/client-rest/account#updateMfaRecoveryCodes) |||
| [Update Name](https://appwrite.io/docs/references/1.5.x/client-rest/account#updateName) |||
Expand Down
15 changes: 15 additions & 0 deletions src/PinguApps.Appwrite.Client/Clients/AccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,19 @@ public async Task<AppwriteResult<MfaRecoveryCodes>> CreateMfaRecoveryCodes()
return e.GetExceptionResponse<MfaRecoveryCodes>();
}
}

/// <inheritdoc/>
public async Task<AppwriteResult<MfaRecoveryCodes>> GetMfaRecoveryCodes()
{
try
{
var result = await _accountApi.GetMfaRecoveryCodes(GetCurrentSessionOrThrow());

return result.GetApiResponse();
}
catch (Exception e)
{
return e.GetExceptionResponse<MfaRecoveryCodes>();
}
}
}
7 changes: 7 additions & 0 deletions src/PinguApps.Appwrite.Client/Clients/IAccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,11 @@ public interface IAccountClient
/// </summary>
/// <returns>The Mfa Recovery Codes</returns>
Task<AppwriteResult<MfaRecoveryCodes>> CreateMfaRecoveryCodes();

/// <summary>
/// Get recovery codes that can be used as backup for MFA flow. Before getting codes, they must be generated using <see cref="CreateMfaRecoveryCodes"/> method. An OTP challenge is required to read recovery codes
/// <para><see href="https://appwrite.io/docs/references/1.5.x/client-rest/account#getMfaRecoveryCodes">Appwrite Docs</see></para>
/// </summary>
/// <returns>The Mfa Recovery Codes</returns>
Task<AppwriteResult<MfaRecoveryCodes>> GetMfaRecoveryCodes();
}
3 changes: 3 additions & 0 deletions src/PinguApps.Appwrite.Client/Internals/IAccountApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,7 @@ internal interface IAccountApi : IBaseApi

[Post("/account/mfa/recovery-codes")]
Task<IApiResponse<MfaRecoveryCodes>> CreateMfaRecoveryCodes([Header("x-appwrite-session")] string session);

[Get("/account/mfa/recovery-codes")]
Task<IApiResponse<MfaRecoveryCodes>> GetMfaRecoveryCodes([Header("x-appwrite-session")] string session);
}
2 changes: 1 addition & 1 deletion src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task Run(string[] args)
{
_client.SetSession(_session);

var response = await _client.Account.CreateMfaRecoveryCodes();
var response = await _client.Account.GetMfaRecoveryCodes();

Console.WriteLine(response.Result.Match(
account => account.ToString(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Net;
using PinguApps.Appwrite.Client.Clients;
using PinguApps.Appwrite.Shared.Tests;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Client.Tests.Clients.Account;
public partial class AccountClientTests
{
[Fact]
public async Task GetMfaRecoveryCodes_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
_mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/account/mfa/recovery-codes")
.ExpectedHeaders(true)
.Respond(Constants.AppJson, Constants.JwtResponse);

_appwriteClient.SetSession(Constants.Session);

// Act
var result = await _appwriteClient.Account.GetMfaRecoveryCodes();

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

[Fact]
public async Task GetMfaRecoveryCodes_ShouldReturnError_WhenSessionIsNull()
{
// Act
var result = await _appwriteClient.Account.GetMfaRecoveryCodes();

// Assert
Assert.True(result.IsError);
Assert.True(result.IsInternalError);
Assert.Equal(ISessionAware.SessionExceptionMessage, result.Result.AsT2.Message);
}

[Fact]
public async Task GetMfaRecoveryCodes_ShouldHandleException_WhenApiCallFails()
{
// Arrange
_mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/account/mfa/recovery-codes")
.ExpectedHeaders(true)
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError);

_appwriteClient.SetSession(Constants.Session);

// Act
var result = await _appwriteClient.Account.GetMfaRecoveryCodes();

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

[Fact]
public async Task GetMfaRecoveryCodes_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
_mockHttp.Expect(HttpMethod.Get, $"{Constants.Endpoint}/account/mfa/recovery-codes")
.ExpectedHeaders(true)
.Throw(new HttpRequestException("An error occurred"));

_appwriteClient.SetSession(Constants.Session);

// Act
var result = await _appwriteClient.Account.GetMfaRecoveryCodes();

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

0 comments on commit ceabee2

Please sign in to comment.