Skip to content

Commit

Permalink
fix: Edited CodeController route mechanism and edited readme file.
Browse files Browse the repository at this point in the history
- The readme file now includes key features and Azure publish flow.
- "api/code" part added as a global route in controller.
  • Loading branch information
Andronovo-bit committed Jul 31, 2024
1 parent b38ba8e commit 58d4244
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 7 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,41 @@

GenerateCampaignCode is a .NET 8.0 project designed to generate and validate campaign codes using different hashing algorithms (SHA1 and HMACSHA256). The project includes API endpoints to generate and validate codes, as well as extensive testing to ensure reliability and performance.

#### You can try this
- Click this link -> https://generatecode.azurewebsites.net/swagger (maybe open slowly you should wait a little first open)

### CodeGenerator

The `CodeGenerator` class provides methods to generate and validate secure campaign codes using SHA1 and HMACSHA256 hashing algorithms. It includes caching functionality to optimize performance and reduce redundant computations. This service is designed for applications that require generating unique, secure codes for various campaigns or promotions.

#### Key Features:

1. **Configuration and Dependencies**:
- `CampaignCodeSettings`: Configuration settings for the campaign code, including characters set and code length.
- `IMemoryCache`: Caching service to store and retrieve generated codes.
- `ILogger<CodeGenerator>`: Logging service to record informational messages and errors.

2. **Main Methods**:
- `GenerateCodeSHA1(string id, string salt)`: Generates a campaign code using the SHA1 hashing algorithm, with optional caching to improve performance.
- `ValidateCodeSHA1(string id, string salt, string code)`: Validates a given code by comparing it with a newly generated code using SHA1.
- `GenerateCodeWithHMACSHA256(string id, string salt)`: Generates a campaign code using the HMACSHA256 hashing algorithm.
- `ValidateCodeHMACSHA256(string id, string salt, string code)`: Validates a given code by comparing it with a newly generated code using HMACSHA256.

3. **Caching**:
- Caches generated SHA1 codes to reduce redundant computations and improve performance. Cached codes have a sliding expiration of one day.

4. **Logging**:
- Logs the generation and retrieval of codes, as well as any errors encountered during the process.


## Table of Contents

- [Getting Started](#getting-started)
- [Usage](#usage)
- [Configuration](#configuration)
- [Running the Tests](#running-the-tests)
- [Contributing](#contributing)
- [Azure Web App Setup and GitHub Secrets](#azure-web-app-setup-and-github-secrets)
- [License](#license)

## Getting Started
Expand Down Expand Up @@ -119,6 +147,27 @@ The solution includes extensive unit tests to ensure code reliability. To run th

The tests cover code generation, validation, and uniqueness checks.

## Azure Web App Setup and GitHub Secrets

### Creating an Azure Web App

1. Sign in to the [Azure Portal](https://portal.azure.com/).
2. From the Azure portal menu, select "Create a resource".
3. In the "Search the Marketplace" field, type 'Web App' and press enter.
4. Select "Web App" from the results, then click "Create".
5. Fill in the details for your web app, such as Subscription, Resource Group, Name, Publish (Code), and Runtime Stack (.NET Core).
6. Click "Review + create" and then "Create" after verifying your details.
### Configuring GitHub Secrets for Azure Deployment
1. Navigate to your GitHub repository.
2. Go to "Settings" > "Secrets" > "Actions".
3. Click on "New repository secret".
4. Add the following secrets required for Azure deployment:
- `AZURE_PUBLISH_PROFILE`: The publish profile XML content. (You can download this from your Azure Web App's "Deployment Center".)
5. Use these secrets in your `publish.yml` GitHub Action workflow to deploy your application.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
Expand Down
11 changes: 6 additions & 5 deletions src/Api/Controllers/CodeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace GenerateCampaignCode.Api.Controllers;

[ApiController]

[Route("api/code")]
public class CodeController : ControllerBase
{
private readonly ICodeGenerator _codeGenerator;
Expand All @@ -17,31 +19,30 @@ public CodeController(ICodeGenerator codeGenerator, ILogger<CodeController> logg
_logger = logger;
}

[HttpPost("api/code/generate/sha1")]
[HttpPost("generate/sha1")]
public IActionResult GenerateCode([FromBody] GenerateCodeRequest request)
{
_logger.LogInformation("Generating code with HMACSHA1: {Id} with unique key: {Salt}", request.Id, request.Salt);
var code = _codeGenerator.GenerateCodeSHA1(request.Id, request.Salt);
return Ok(code);
}

[HttpPost("api/code/validate/sha1")]
[HttpPost("validate/sha1")]
public IActionResult ValidateCode([FromBody] ValidateCodeRequest request)
{
_logger.LogInformation("Validating code with HMACSHA1: {Id} with unique key: {Salt}", request.Id, request.Salt);
var isValid = _codeGenerator.ValidateCodeSHA1(request.Id, request.Salt, request.Code);
return Ok(isValid);
}

[HttpPost("api/code/generate/hmacsha256")]
[HttpPost("generate/hmacsha256")]
public IActionResult GenerateCodeWithHMACSHA256([FromBody] GenerateCodeRequest request)
{
_logger.LogInformation("Generating code with HMACSHA256: {Id} with unique key: {Salt}", request.Id, request.Salt);
var code = _codeGenerator.GenerateCodeWithHMACSHA256(request.Id, request.Salt);
return Ok(code);
}

[HttpPost("api/code/validate/hmacsha256")]
[HttpPost("validate/hmacsha256")]
public IActionResult ValidateCodeWithHMACSHA256([FromBody] ValidateCodeRequest request)
{
_logger.LogInformation("Validating code with HMACSHA256: {Id} with unique key: {Salt}", request.Id, request.Salt);
Expand Down
1 change: 0 additions & 1 deletion src/Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
var builder = WebApplication.CreateBuilder(args);

// Configure Serilog
//Add support to logging with SERILOG
builder.Host.UseSerilog((context, configuration) =>
configuration.ReadFrom.Configuration(context.Configuration));

Expand Down
32 changes: 31 additions & 1 deletion src/Application/Interfaces/ICodeGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
namespace GenerateCampaignCode.Application.Interfaces;
using System;

namespace GenerateCampaignCode.Application.Interfaces;

public interface ICodeGenerator
{

/// <summary>
/// Generates a unique code using the SHA1 algorithm.
/// </summary>
/// <param name="id">A string representing an identifier.</param>
/// <param name="salt">A string used to add complexity to the input.</param>
/// <returns>A unique code as a string, generated based on the input id and salt.</returns>
/// <remarks>
/// The generated code is cached for 1 day
/// </remarks>
/// <example>
/// This sample shows how to call the <see cref="GenerateCodeSHA1"/> method.
/// <code>
/// var code = GenerateCode SHA1("id", "salt");
/// </code>
/// </example>
string GenerateCodeSHA1(string id, string salt);
/// <summary>
/// Generates a unique code using the HMACSHA256 algorithm.
/// </summary>
/// <param name="id">A string representing an identifier.</param>
/// <param name="salt">A string used to add complexity to the input.</param>
/// <returns>A unique code as a string, generated based on the input id and salt.</returns>
/// <example>
/// This sample shows how to call the <see cref="GenerateCodeWithHMACSHA256"/> method.
/// <code>
/// var code = GenerateCodeWithHMACSHA256("id", "salt");
/// </code>
/// </example>
string GenerateCodeWithHMACSHA256(string id, string salt);
bool ValidateCodeSHA1(string id, string salt, string code);
bool ValidateCodeHMACSHA256(string id, string salt, string code);
Expand Down

0 comments on commit 58d4244

Please sign in to comment.