Merge pull request 'Fixed deploy, Added HH Auth on back-end' (#22) from dev into main
Reviewed-on: #22
This commit is contained in:
commit
cb481e6af6
23
JOBot.Backend/Controllers/HeadHunterHookController.cs
Normal file
23
JOBot.Backend/Controllers/HeadHunterHookController.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using JOBot.Backend.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace JOBot.Backend.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("auth")]
|
||||
public class HeadHunterHookController(HeadHunterService hhService)
|
||||
: ControllerBase
|
||||
{
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Get(int userId, string? error, string code)
|
||||
{
|
||||
var res = await hhService.AuthUser(userId, code, error);
|
||||
return res switch
|
||||
{
|
||||
HeadHunterService.Status.Success => Ok("Авторизация завершена успешно. Вернитесь в Telegram для продолжения."),
|
||||
HeadHunterService.Status.UserNotFoundError => NotFound("Пользователь не найден."),
|
||||
_ => BadRequest("Авторизация завершена с ошибкой. Вернитесь в Telegram для продолжения.") //TODO: Add resource
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace JOBot.Backend.DTOs.HeadHunterHook;
|
||||
|
||||
public record HeadHunterTokenResponseDto(
|
||||
[property:JsonPropertyName("access_token")]
|
||||
string AccessToken,
|
||||
[property:JsonPropertyName("expires_in")]
|
||||
int ExpiresIn,
|
||||
[property:JsonPropertyName("refresh_token")]
|
||||
string RefreshToken,
|
||||
[property:JsonPropertyName("token_type")]
|
||||
string TokenType
|
||||
);
|
@ -10,5 +10,4 @@ WORKDIR /app
|
||||
COPY --from=build /app .
|
||||
EXPOSE 5001
|
||||
EXPOSE 5000
|
||||
ENV ASPNETCORE_ENVIRONMENT Staging
|
||||
ENTRYPOINT ["dotnet", "JOBot.Backend.dll"]
|
18
JOBot.Backend/Infrastructure/Config/HeadHunterConfig.cs
Normal file
18
JOBot.Backend/Infrastructure/Config/HeadHunterConfig.cs
Normal file
@ -0,0 +1,18 @@
|
||||
namespace JOBot.Backend.Infrastructure.Config;
|
||||
|
||||
public class HeadHunterConfig
|
||||
{
|
||||
public const string SectionName = "HeadHunter";
|
||||
public required HeadHunterLinksConfig Links { get; init; }
|
||||
public required string ClientId { get; init; }
|
||||
public required string Secret { get; init; }
|
||||
|
||||
public class HeadHunterLinksConfig
|
||||
{
|
||||
public required string AuthLink { get; init; }
|
||||
public required string HookDomain { get; init; }
|
||||
public required string HookRoute { get; init; }
|
||||
public required string HeadHunterApiDomain { get; init; }
|
||||
public required string HeadHunterTokenRoute { get; init; }
|
||||
}
|
||||
}
|
@ -14,6 +14,7 @@
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.4">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
113
JOBot.Backend/Services/HeadHunterService.cs
Normal file
113
JOBot.Backend/Services/HeadHunterService.cs
Normal file
@ -0,0 +1,113 @@
|
||||
using System.Text.Json;
|
||||
using System.Web;
|
||||
using JOBot.Backend.DAL.Context;
|
||||
using JOBot.Backend.DTOs.HeadHunterHook;
|
||||
using JOBot.Backend.Infrastructure.Config;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace JOBot.Backend.Services;
|
||||
|
||||
public class HeadHunterService(ILogger<HeadHunterService> logger, IOptions<HeadHunterConfig> config, AppDbContext dbContext)
|
||||
{
|
||||
private readonly HeadHunterConfig _config = config.Value;
|
||||
|
||||
/// <summary>
|
||||
/// Generate HeadHunter oauth authorization link
|
||||
/// </summary>
|
||||
/// <param name="userId">Telegram UserId</param>
|
||||
/// <returns>Link for auth</returns>
|
||||
public string GenerateAuthLink(long userId)
|
||||
{
|
||||
|
||||
return string.Format(_config.Links.AuthLink, [_config.ClientId, GetRedirectUrl(userId)]);
|
||||
}
|
||||
|
||||
public async Task<Status> AuthUser(int userId, string authorizationCode, string? error)
|
||||
{
|
||||
logger.LogInformation($"Authorization for user {userId} in process...");
|
||||
|
||||
if (!string.IsNullOrEmpty(error))
|
||||
{
|
||||
logger.LogWarning($"User {userId} auth completed with error {error}");
|
||||
return Status.UserAuthRejectedError;
|
||||
}
|
||||
|
||||
using var client = new HttpClient();
|
||||
var form = new Dictionary<string, string>
|
||||
{
|
||||
{ "client_id", _config.ClientId },
|
||||
{ "client_secret", _config.Secret },
|
||||
{ "code", authorizationCode },
|
||||
{ "grant_type", "authorization_code" },
|
||||
{ "redirect_uri", GetRedirectUrl(userId) }
|
||||
};
|
||||
client.BaseAddress = new UriBuilder(_config.Links.HeadHunterApiDomain)
|
||||
{
|
||||
Port = -1,
|
||||
Scheme = "https"
|
||||
}.Uri;
|
||||
client.DefaultRequestHeaders.UserAgent.ParseAdd("Jobot BackEnd Service");
|
||||
|
||||
using var res = await client.SendAsync(
|
||||
new HttpRequestMessage(
|
||||
HttpMethod.Post,
|
||||
_config.Links.HeadHunterTokenRoute)
|
||||
{
|
||||
Content = new FormUrlEncodedContent(form)
|
||||
});
|
||||
|
||||
if (!res.IsSuccessStatusCode)
|
||||
{
|
||||
logger.LogWarning($"Response of HttpRequest {_config.Links.HeadHunterApiDomain}" +
|
||||
$"{_config.Links.HeadHunterTokenRoute} has unsuccessful status code {res.StatusCode}");
|
||||
logger.LogWarning($"{res.Content.ReadAsStringAsync().Result}");
|
||||
return Status.HeadHunterAuthRejectedError;
|
||||
}
|
||||
|
||||
var responseDto = JsonSerializer.Deserialize<HeadHunterTokenResponseDto>(await res.Content.ReadAsStringAsync());
|
||||
|
||||
if (responseDto == null)
|
||||
{
|
||||
logger.LogWarning($"User {userId} auth completed with error " +
|
||||
$"{nameof(Status.HeadHunterResponseDeserializationFailedError)}");
|
||||
return Status.HeadHunterResponseDeserializationFailedError;
|
||||
}
|
||||
|
||||
var user = await dbContext.Users.FirstOrDefaultAsync(x => x.UserId == userId);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
logger.LogWarning($"User {userId} search completed with error {nameof(Status.UserNotFoundError)}");
|
||||
return Status.UserNotFoundError;
|
||||
}
|
||||
|
||||
user.AccessToken = responseDto.AccessToken;
|
||||
user.RefreshToken = responseDto.RefreshToken;
|
||||
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
logger.LogInformation($"User {userId} auth completed!");
|
||||
return Status.Success;
|
||||
}
|
||||
|
||||
private string GetRedirectUrl(long userId)
|
||||
{
|
||||
return new UriBuilder(_config.Links.HookDomain)
|
||||
{
|
||||
Port = -1,
|
||||
Scheme = "https",
|
||||
Path = _config.Links.HookRoute,
|
||||
Query = $"?userId={userId}"
|
||||
}.ToString();
|
||||
}
|
||||
|
||||
public enum Status
|
||||
{
|
||||
UserAuthRejectedError,
|
||||
HeadHunterAuthRejectedError,
|
||||
UserNotFoundError,
|
||||
HeadHunterResponseDeserializationFailedError,
|
||||
Success
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
using Grpc.Core;
|
||||
using JOBot.Backend.DAL.Context;
|
||||
using JOBot.Backend.DAL.Models;
|
||||
@ -7,7 +8,7 @@ using User = JOBot.Backend.DAL.Models.User;
|
||||
|
||||
namespace JOBot.Backend.Services.gRPC;
|
||||
|
||||
public class UserService(AppDbContext dbContext) : Proto.User.UserBase
|
||||
public class UserService(AppDbContext dbContext, HeadHunterService hhService) : Proto.User.UserBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Create user
|
||||
@ -62,6 +63,16 @@ public class UserService(AppDbContext dbContext) : Proto.User.UserBase
|
||||
return new AcceptEulaResponse { Success = true };
|
||||
}
|
||||
|
||||
public override Task<GetHeadHunterAuthHookResponse> GetHeadHunterAuthHook(
|
||||
GetHeadHunterAuthHookRequest request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
return Task.Run(() => new GetHeadHunterAuthHookResponse
|
||||
{
|
||||
RegistrationUrl = hhService.GenerateAuthLink(request.UserId)
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throw RPCException if user not found
|
||||
/// </summary>
|
||||
|
@ -1,30 +1,34 @@
|
||||
using JOBot.Backend.DAL.Context;
|
||||
using JOBot.Backend.Infrastructure.Config;
|
||||
using JOBot.Backend.Services;
|
||||
using JOBot.Backend.Services.gRPC;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace JOBot.Backend;
|
||||
|
||||
public class Startup
|
||||
public class Startup(IConfiguration configuration)
|
||||
{
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
private IConfiguration Configuration { get; } = configuration;
|
||||
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddGrpc();
|
||||
services.AddGrpcReflection();
|
||||
services.AddControllers();
|
||||
services.AddLogging();
|
||||
|
||||
services.AddDbContext<AppDbContext>(options =>
|
||||
options.UseNpgsql(Configuration.GetConnectionString("PostgreSQL")));
|
||||
|
||||
services.Configure<HeadHunterConfig>(Configuration.GetSection(HeadHunterConfig.SectionName));
|
||||
|
||||
services.AddScoped<HeadHunterService>();
|
||||
}
|
||||
|
||||
public void Configure(WebApplication app, IWebHostEnvironment env)
|
||||
{
|
||||
app.MapGrpcReflectionService().AllowAnonymous();
|
||||
app.MapGrpcService<UserService>();
|
||||
app.MapControllers();
|
||||
}
|
||||
}
|
@ -9,6 +9,17 @@
|
||||
"ConnectionStrings": {
|
||||
"PostgreSQL": "Host=postgres;Port=5432;Database=jobot;Username=postgres;Password=LocalDbPass"
|
||||
},
|
||||
"HeadHunter": {
|
||||
"Links": {
|
||||
"AuthLink": "https://hh.ru/oauth/authorize?response_type=code&client_id={0}&redirect_uri={1}",
|
||||
"HookDomain": "jobot.lisoveliy.su",
|
||||
"HookRoute": "/auth",
|
||||
"HeadHunterApiDomain": "api.hh.ru",
|
||||
"HeadHunterTokenRoute": "/token"
|
||||
},
|
||||
"ClientId": "",
|
||||
"Secret": ""
|
||||
},
|
||||
"Kestrel": {
|
||||
"Endpoints": {
|
||||
"gRPC": {
|
||||
|
@ -12,6 +12,7 @@ public class EulaAgreementButtonCommand(PrepareUserState prepareUserState) : IAu
|
||||
{
|
||||
public async Task ExecuteAsync(Update update, GetUserResponse user, CancellationToken ct)
|
||||
{
|
||||
await prepareUserState.AcceptEula(update, ct);
|
||||
if (!user.Eula)
|
||||
await prepareUserState.AcceptEula(user, update, ct);
|
||||
}
|
||||
}
|
@ -57,4 +57,10 @@
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -6,12 +6,10 @@ namespace JOBot.TClient.Services;
|
||||
|
||||
public class MenuService(ITelegramBotClient bot)
|
||||
{
|
||||
public Task RenderMenu(Update update, CancellationToken ct = default)
|
||||
public async Task RenderMenu(Update update, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(update.Message?.From);
|
||||
|
||||
bot.SendMessage(update.Message.From.Id,"PrepareUser stage is done.", cancellationToken: ct);
|
||||
|
||||
return Task.CompletedTask;
|
||||
await bot.SendMessage(update.Message.From.Id,"PrepareUser stage is done.", cancellationToken: ct);
|
||||
}
|
||||
}
|
@ -83,4 +83,19 @@ public class PrepareUserService(ITelegramBotClient bot, User.UserClient userClie
|
||||
if (!result.Success)
|
||||
throw new FallbackException(TextResource.FallbackMessage, update.Message, _bot);
|
||||
}
|
||||
|
||||
public async Task Auth(Update update, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(update.Message?.From);
|
||||
|
||||
var url = await _userClient.GetHeadHunterAuthHookAsync(new GetHeadHunterAuthHookRequest()
|
||||
{
|
||||
UserId = update.Message.From.Id
|
||||
});
|
||||
|
||||
await _bot.SendMessage(
|
||||
update.Message.From.Id,
|
||||
string.Format(TextResource.AskForAuth, [url.RegistrationUrl]),
|
||||
cancellationToken: ct);
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
using JOBot.Proto;
|
||||
using JOBot.TClient.Services;
|
||||
using Telegram.Bot.Types;
|
||||
using User = JOBot.Proto.User;
|
||||
@ -21,27 +22,34 @@ public class PrepareUserState(PrepareUserService prepareUserService, MenuService
|
||||
return; //interrupt while eula isn't accepted
|
||||
}
|
||||
|
||||
await OnUserEulaValidStage(update, ct);
|
||||
await OnUserEulaValidStage(user, update, ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Signal for accepted eula
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="update"></param>
|
||||
/// <param name="ct"></param>
|
||||
public async Task AcceptEula(Update update, CancellationToken ct = default)
|
||||
public async Task AcceptEula(GetUserResponse user, Update update, CancellationToken ct = default)
|
||||
{
|
||||
await prepareUserService.AcceptEula(update, ct: ct);
|
||||
await OnUserEulaValidStage(update, ct);
|
||||
await OnUserEulaValidStage(user, update, ct);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Continue prepare stage
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="update"></param>
|
||||
/// <param name="ct"></param>
|
||||
private async Task OnUserEulaValidStage(Update update, CancellationToken ct = default)
|
||||
private async Task OnUserEulaValidStage(GetUserResponse user, Update update, CancellationToken ct = default)
|
||||
{
|
||||
if (!user.IsLogged)
|
||||
{
|
||||
await prepareUserService.Auth(update, ct);
|
||||
return;
|
||||
}
|
||||
await menuService.RenderMenu(update, ct); //boilerplate
|
||||
}
|
||||
}
|
9
JOBot.TClient/TextResource.Designer.cs
generated
9
JOBot.TClient/TextResource.Designer.cs
generated
@ -59,6 +59,15 @@ namespace JOBot.TClient {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Авторизируйтесь на сайте HeadHunter для получения доступа к резюме и вакансиям {0}.
|
||||
/// </summary>
|
||||
public static string AskForAuth {
|
||||
get {
|
||||
return ResourceManager.GetString("AskForAuth", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Команда не найдена, попробуйте что-то другое.
|
||||
/// </summary>
|
||||
|
@ -34,4 +34,7 @@ https://hh.ru/account/agreement?backurl=%2Faccount%2Fsignup%3Fbackurl%3D%252F%26
|
||||
<data name="Info" xml:space="preserve">
|
||||
<value>Это бот для упрощения поиска работы на HH.ru</value>
|
||||
</data>
|
||||
<data name="AskForAuth" xml:space="preserve">
|
||||
<value>Авторизируйтесь на сайте HeadHunter для получения доступа к резюме и вакансиям {0}</value>
|
||||
</data>
|
||||
</root>
|
@ -1,14 +1,16 @@
|
||||
syntax = "proto3";
|
||||
option csharp_namespace = "JOBot.Proto";
|
||||
|
||||
import "google/protobuf/wrappers.proto";
|
||||
import "google/protobuf/empty.proto";
|
||||
|
||||
service User {
|
||||
rpc Register (RegisterRequest) returns (RegisterResponse);
|
||||
rpc GetUser (GetUserRequest) returns (GetUserResponse);
|
||||
rpc AcceptEula (AcceptEulaRequest) returns (AcceptEulaResponse);
|
||||
rpc GetHeadHunterAuthHook(GetHeadHunterAuthHookRequest) returns (GetHeadHunterAuthHookResponse);
|
||||
}
|
||||
|
||||
import "google/protobuf/wrappers.proto";
|
||||
|
||||
message RegisterRequest{
|
||||
int64 user_id = 1;
|
||||
google.protobuf.StringValue username = 2;
|
||||
@ -37,4 +39,12 @@ message AcceptEulaRequest {
|
||||
|
||||
message AcceptEulaResponse{
|
||||
bool success = 1;
|
||||
}
|
||||
|
||||
message GetHeadHunterAuthHookRequest{
|
||||
int64 user_id = 1;
|
||||
}
|
||||
|
||||
message GetHeadHunterAuthHookResponse{
|
||||
string registration_url = 1;
|
||||
}
|
@ -20,6 +20,7 @@ services:
|
||||
depends_on:
|
||||
- postgres
|
||||
ports:
|
||||
- "5000:5000"
|
||||
- "5001:5001"
|
||||
networks:
|
||||
- jobot
|
||||
|
@ -17,6 +17,8 @@ services:
|
||||
dockerfile: JOBot.Backend/Dockerfile
|
||||
depends_on:
|
||||
- postgres
|
||||
ports:
|
||||
- "5000:5000"
|
||||
networks:
|
||||
- jobot
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user