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

⚡ refactor: When HttpContext is empty, use ClientCredentials #594

Merged
merged 1 commit into from
Sep 12, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the Apache License. See LICENSE.txt in the project root for license information.

namespace Masa.Mc.Contracts.Admin.Infrastructure;

public class TokenGenerater : ITokenGenerater
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly HttpClient _httpClient;
private readonly IMasaStackConfig _masaStackConfig;
private const string SCHEME = "Bearer ";

public TokenGenerater(IHttpContextAccessor httpContextAccessor, HttpClient httpClient, IMasaStackConfig masaStackConfig)
{
_httpContextAccessor = httpContextAccessor;
_httpClient = httpClient;
_masaStackConfig = masaStackConfig;
}

public TokenProvider Generater()
{
StringValues authenticationHeaderValue;

if (_httpContextAccessor.HttpContext?.Request.Headers.TryGetValue("Authorization", out authenticationHeaderValue) == true)
{
var accessToken = authenticationHeaderValue.ToString();

if (!string.IsNullOrEmpty(accessToken) && accessToken.StartsWith(SCHEME, StringComparison.OrdinalIgnoreCase))
{
accessToken = accessToken.Substring(SCHEME.Length).Trim();
}

return new TokenProvider { AccessToken = accessToken };
}

if (_httpContextAccessor.HttpContext == null)
{
var request = new ClientCredentialsTokenRequest
{
Address = _masaStackConfig.GetSsoDomain() + "/connect/token",
GrantType = BuildingBlocks.Authentication.OpenIdConnect.Models.Constans.GrantType.CLIENT_CREDENTIALS,
ClientId = _masaStackConfig.GetWebId(MasaStackProject.MC),
Scope = "MasaStack"
};
var tokenResponse = _httpClient.RequestClientCredentialsTokenAsync(request).Result;
return new TokenProvider { AccessToken = tokenResponse.AccessToken };
}

return new TokenProvider();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Masa.BuildingBlocks.StackSdks.Auth.Contracts" Version="$(MasaStackSdksPackageVersion)" />
<PackageReference Include="Masa.BuildingBlocks.SearchEngine.AutoComplete" Version="$(MasaFrameworkPackageVersion)" />
<PackageReference Include="Masa.BuildingBlocks.Authentication.OpenIdConnect.Models" Version="$(MasaFrameworkPackageVersion)" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 6 additions & 1 deletion src/Contracts/Masa.Mc.Contracts.Admin/_Imports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@
global using System.Reflection;
global using System.Collections.ObjectModel;
global using System.ComponentModel.DataAnnotations;
global using System.Collections.Concurrent;
global using System.Collections.Concurrent;
global using IdentityModel.Client;
global using Masa.BuildingBlocks.StackSdks.Config;
global using Masa.Contrib.StackSdks.Caller;
global using Masa.Contrib.StackSdks.Config;
global using Microsoft.Extensions.Primitives;
3 changes: 1 addition & 2 deletions src/Services/Masa.Mc.Service/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the Apache License. See LICENSE.txt in the project root for license information.

using Masa.Mc.Infrastructure.Weixin.Work.Extensions;

var builder = WebApplication.CreateBuilder(args);

ValidatorOptions.Global.LanguageManager = new MasaLanguageManager();
Expand Down Expand Up @@ -97,6 +95,7 @@
Password = masaStackConfig.RedisModel.RedisPassword
};
var configuration = builder.Services.GetMasaConfiguration().ConfigurationApi.GetDefault();
builder.Services.AddScoped<ITokenGenerater, TokenGenerater>();
builder.Services.AddAuthClient(masaStackConfig.GetAuthServiceDomain(), redisOptions);
builder.Services.AddMcClient(masaStackConfig.GetMcServiceDomain());
builder.Services.AddPmClient(masaStackConfig.GetPmServiceDomain());
Expand Down
4 changes: 4 additions & 0 deletions src/Services/Masa.Mc.Service/_Imports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
global using Masa.Mc.Infrastructure.Weixin.Work.Sender;
global using Masa.Mc.Infrastructure.Weixin.Work.Work;
global using Masa.Mc.Infrastructure.Weixin.Work.WorkWebhook;
global using Masa.Mc.Infrastructure.Weixin.Work.Extensions;
global using Masa.Mc.Service.Admin.Application.Channels.Commands;
global using Masa.Mc.Service.Admin.Application.Channels.Queries;
global using Masa.Mc.Service.Admin.Application.MessageInfos.Commands;
Expand Down Expand Up @@ -181,7 +182,10 @@
global using Microsoft.Extensions.DependencyInjection.Extensions;
global using Microsoft.Extensions.Options;
global using Microsoft.OpenApi.Models;
global using Microsoft.Extensions.Primitives;
global using Moq;
global using static AlibabaCloud.SDK.Dysmsapi20170525.Models.QuerySmsTemplateListResponseBody;
global using ICsvExporter = Masa.Mc.Infrastructure.ExporterAndImporter.Csv.ICsvExporter;
global using ICsvImporter = Masa.Mc.Infrastructure.ExporterAndImporter.Csv.ICsvImporter;
global using IdentityModel.Client;
global using Masa.Contrib.StackSdks.Caller;
1 change: 1 addition & 0 deletions src/Web/Masa.Mc.Web.Admin.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
mcBaseAddress = masaStackConfig.GetMcServiceDomain();
}

builder.Services.AddScoped<ITokenGenerater, TokenGenerater>();
await builder.Services.AddMasaStackComponentsAsync(MasaStackProject.MC, "wwwroot/i18n", authBaseAddress);

builder.Services.AddHttpContextAccessor();
Expand Down
3 changes: 2 additions & 1 deletion src/Web/Masa.Mc.Web.Admin.Server/_Imports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@
global using FluentValidation.Resources;
global using Masa.BuildingBlocks.StackSdks.Config;
global using Masa.Contrib.Configuration.ConfigurationApi.Dcc;
global using Masa.Contrib.StackSdks.Caller;
global using Masa.Contrib.StackSdks.Caller;
global using Masa.Mc.Contracts.Admin.Infrastructure;