Skip to content

Commit

Permalink
Add Auth0 integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Bardin08 committed May 15, 2024
1 parent c72592a commit f2e6c72
Show file tree
Hide file tree
Showing 14 changed files with 211 additions and 3 deletions.
9 changes: 9 additions & 0 deletions EduAutomation/Application/Auth0/IAuthService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using EduAutomation.Domain.Auth0;

namespace EduAutomation.Application.Auth0;

public interface IAuthService
{
Task<string?> Login(LoginDto dto);
Task<string?> Register(RegisterDto dto);
}
6 changes: 5 additions & 1 deletion EduAutomation/Application/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
using EduAutomation.Application.GitHub.Services;
using EduAutomation.Application.Auth0;
using EduAutomation.Application.GitHub.Services;
using EduAutomation.Application.Telegram.Formatters;
using EduAutomation.Application.Trello.Formatters;
using EduAutomation.Infrastructure.Auth0;

namespace EduAutomation.Application;

public static class DependencyInjection
{
public static IServiceCollection AddApplication(this IServiceCollection services)
{
services.AddScoped<IAuthService, AuthService>();

services.AddTransient<ITrelloCardFormatter, MarkdownCardFormatter>();
services.AddTransient<ITelegramMessageFormatter, MarkdownMessageFormatter>();

Expand Down
3 changes: 3 additions & 0 deletions EduAutomation/Domain/Auth0/LoginDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace EduAutomation.Domain.Auth0;

public record LoginDto(string Login, string Password);
7 changes: 7 additions & 0 deletions EduAutomation/Domain/Auth0/RegisterDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EduAutomation.Domain.Auth0;

public record RegisterDto(
string Login,
string FullName,
string Password,
string Email);
3 changes: 3 additions & 0 deletions EduAutomation/EduAutomation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Auth0.AuthenticationApi" Version="7.26.2" />
<PackageReference Include="Auth0Net.DependencyInjection" Version="3.2.0" />
<PackageReference Include="GithubWebhook" Version="1.1.0" />
<PackageReference Include="Manatee.Trello" Version="4.4.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.4" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.3"/>
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
Expand Down
13 changes: 13 additions & 0 deletions EduAutomation/Infrastructure/Auth0/Auth0Options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace EduAutomation.Infrastructure.Auth0;

public class Auth0Options
{
public const string SectionName = "Auth0";

public required string ClientId { get; init; }
public required string ClientSecret { get; init; }
public required string Domain { get; init; }
public required string Authority { get; init; }
public required string Audience { get; init; }
public required string Realm { get; init; }
}
67 changes: 67 additions & 0 deletions EduAutomation/Infrastructure/Auth0/AuthService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Auth0.AuthenticationApi;
using Auth0.AuthenticationApi.Models;
using EduAutomation.Application.Auth0;
using EduAutomation.Domain.Auth0;
using Microsoft.Extensions.Options;

namespace EduAutomation.Infrastructure.Auth0;

public class AuthService(
IAuthenticationApiClient authenticationApiClient,
IOptions<Auth0Options> auth0Options) : IAuthService
{
private readonly Auth0Options _auth0Options = auth0Options.Value;

public async Task<string?> Login(LoginDto dto)
{
try
{
var auth0Response = await authenticationApiClient.GetTokenAsync(new ResourceOwnerTokenRequest
{
Username = dto.Login,
Password = dto.Password,
Realm = _auth0Options.Realm,

Audience = _auth0Options.Audience,
ClientId = _auth0Options.ClientId,
ClientSecret = _auth0Options.ClientSecret,
Scope = "openid"
});

return !string.IsNullOrEmpty(auth0Response?.AccessToken ?? null)
? auth0Response!.AccessToken : null;
}
catch (Exception e)
{
Console.WriteLine(e);
return null;
}
}

public async Task<string?> Register(RegisterDto dto)
{
try
{
var username = $"{dto.FullName[0]}-{Guid.NewGuid().ToString()[..6]}";
await authenticationApiClient.SignupUserAsync(new SignupUserRequest
{
Username = username,
Nickname = username,
Name = dto.FullName ,
Password = dto.Password,
Email = dto.Email,

Connection = "edu-automation",
ClientId = _auth0Options.ClientId
});

var jwtToken = await Login(new LoginDto(username, dto.Password));
return jwtToken;
}
catch (Exception e)
{
Console.WriteLine(e);
return null;
}
}
}
28 changes: 27 additions & 1 deletion EduAutomation/Infrastructure/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using EduAutomation.Application.GitHub;
using Auth0Net.DependencyInjection;
using EduAutomation.Application.GitHub;
using EduAutomation.Application.Telegram;
using EduAutomation.Application.Trello;
using EduAutomation.Infrastructure.Auth0;
using EduAutomation.Infrastructure.GitHub;
using EduAutomation.Infrastructure.Telegram;
using EduAutomation.Infrastructure.Trello;
using GitHubUtils.Core;
using Manatee.Trello;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Telegram.Bot;

namespace EduAutomation.Infrastructure;
Expand All @@ -15,13 +18,36 @@ public static class DependencyInjection
public static IServiceCollection AddInfrastructure(
this IServiceCollection services, IConfiguration configuration)
{
services.AddAuth0(configuration);

services.AddTrello(configuration);
services.AddGitHub(configuration);
services.AddTelegram(configuration);

return services;
}

private static void AddAuth0(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<Auth0Options>(configuration.GetSection(Auth0Options.SectionName));
services.AddAuth0AuthenticationClient(c =>
{
c.Domain = configuration["Auth0:Domain"]!;
c.ClientId = configuration["Auth0:ClientId"]!;
c.ClientSecret = configuration["Auth0:ClientSecret"]!;
});

services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Authority = configuration["Auth0:Authority"];
options.Audience = configuration["Auth0:Audience"];
});
}

private static void AddTrello(this IServiceCollection services, IConfiguration configuration)
{
TrelloAuthorization.Default.AppKey = configuration[TrelloOptions.SectionName + ":AppKey"];
Expand Down
9 changes: 8 additions & 1 deletion EduAutomation/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using EduAutomation.Application;
using EduAutomation.Infrastructure;
using EduAutomation.Rest.Auth0;
using EduAutomation.Rest.GitHub;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthorization();

builder.Services.AddInfrastructure(builder.Configuration);
builder.Services.AddApplication();

Expand All @@ -18,8 +21,12 @@
app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.UseAuthentication();

app.MapPost("/api/github-repo-webhook", GitHubEndpoints.RepositoryCreatedWebhook);

app.MapPost("/api/auth/login", AuthEndpoints.Login);
app.MapPost("/api/auth/register", AuthEndpoints.Register);

app.Run();
35 changes: 35 additions & 0 deletions EduAutomation/Rest/Auth0/AuthEndpoints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using EduAutomation.Application.Auth0;
using EduAutomation.Rest.Auth0.Mappers;
using EduAutomation.Rest.Auth0.Models;
using Microsoft.AspNetCore.Mvc;

namespace EduAutomation.Rest.Auth0;

public static class AuthEndpoints
{
public static async Task<IResult> Login(
LoginRequest payload,
CancellationToken cancellationToken,
[FromServices] IAuthService authService)
{
var loginDto = payload.ToDomainModel();
var jwtToken = await authService.Login(loginDto);

return !string.IsNullOrEmpty(jwtToken)
? Results.Ok(jwtToken)
: Results.Unauthorized();
}

public static async Task<IResult> Register(
RegisterRequest payload,
CancellationToken cancellationToken,
[FromServices] IAuthService authService)
{
var registerDto = payload.ToDomainModel();
var jwtToken = await authService.Register(registerDto);

return !string.IsNullOrEmpty(jwtToken)
? Results.Ok(jwtToken)
: Results.Unauthorized();
}
}
12 changes: 12 additions & 0 deletions EduAutomation/Rest/Auth0/Mappers/LoginRequestMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using EduAutomation.Domain.Auth0;
using EduAutomation.Rest.Auth0.Models;

namespace EduAutomation.Rest.Auth0.Mappers;

public static class LoginRequestMapper
{
public static LoginDto ToDomainModel(this LoginRequest r)
{
return new LoginDto(r.Login, r.Password);
}
}
12 changes: 12 additions & 0 deletions EduAutomation/Rest/Auth0/Mappers/RegisterRequestMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using EduAutomation.Domain.Auth0;
using EduAutomation.Rest.Auth0.Models;

namespace EduAutomation.Rest.Auth0.Mappers;

public static class RegisterRequestMapper
{
public static RegisterDto ToDomainModel(this RegisterRequest r)
{
return new RegisterDto(r.Login, r.FullName, r.Password, r.Email);
}
}
3 changes: 3 additions & 0 deletions EduAutomation/Rest/Auth0/Models/LoginRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace EduAutomation.Rest.Auth0.Models;

public record LoginRequest(string Login, string Password);
7 changes: 7 additions & 0 deletions EduAutomation/Rest/Auth0/Models/RegisterRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EduAutomation.Rest.Auth0.Models;

public record RegisterRequest(
string Login,
string FullName,
string Password,
string Email);

0 comments on commit f2e6c72

Please sign in to comment.