66 lines
2.6 KiB
C#
66 lines
2.6 KiB
C#
|
|
using System;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using Microsoft.AspNetCore.Authorization;
|
|||
|
|
using Sozsoft.Mcp.Dtos;
|
|||
|
|
using Sozsoft.Mcp.Services;
|
|||
|
|
using Sozsoft.Platform.Localization;
|
|||
|
|
using Volo.Abp;
|
|||
|
|
using Volo.Abp.Application.Services;
|
|||
|
|
|
|||
|
|
namespace Sozsoft.Mcp;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Dinamik istemci kaydi (RFC 7591). Uc kimlik dogrulamasizdir; istemci ne alirsa alsin token
|
|||
|
|
/// kullanicinin kendi girisiyle ve kendi yetkileriyle uretilir. Kural ve kayit
|
|||
|
|
/// <see cref="IMcpOAuthClientManager"/> icindedir.
|
|||
|
|
/// </summary>
|
|||
|
|
[AllowAnonymous]
|
|||
|
|
public class McpOAuthAppService : ApplicationService, IMcpOAuthAppService
|
|||
|
|
{
|
|||
|
|
private const string AuthorizationCodeGrant = "authorization_code";
|
|||
|
|
private const string RefreshTokenGrant = "refresh_token";
|
|||
|
|
|
|||
|
|
private readonly IMcpOAuthClientManager clientManager;
|
|||
|
|
|
|||
|
|
public McpOAuthAppService(IMcpOAuthClientManager clientManager)
|
|||
|
|
{
|
|||
|
|
this.clientManager = clientManager;
|
|||
|
|
LocalizationResource = typeof(PlatformResource);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task<McpClientRegistrationResultDto> RegisterClientAsync(McpClientRegistrationRequestDto input)
|
|||
|
|
{
|
|||
|
|
Check.NotNull(input, nameof(input));
|
|||
|
|
|
|||
|
|
var redirectUris = (input.RedirectUris ?? []).Distinct(StringComparer.Ordinal).ToList();
|
|||
|
|
var registration = await clientManager.RegisterAsync(input.ClientName, redirectUris);
|
|||
|
|
|
|||
|
|
return registration.Error switch
|
|||
|
|
{
|
|||
|
|
McpClientRegistrationError.InvalidRedirectUri => new McpClientRegistrationResultDto
|
|||
|
|
{
|
|||
|
|
Error = "invalid_redirect_uri",
|
|||
|
|
ErrorDescription = L[McpErrorCodes.RedirectUriNotAllowed, registration.ErrorValue ?? string.Empty]
|
|||
|
|
},
|
|||
|
|
McpClientRegistrationError.LimitReached => new McpClientRegistrationResultDto
|
|||
|
|
{
|
|||
|
|
Error = "invalid_client_metadata",
|
|||
|
|
ErrorDescription = L[McpErrorCodes.RegistrationLimitReached, registration.ErrorValue ?? string.Empty]
|
|||
|
|
},
|
|||
|
|
_ => new McpClientRegistrationResultDto
|
|||
|
|
{
|
|||
|
|
ClientId = registration.ClientId,
|
|||
|
|
ClientName = input.ClientName,
|
|||
|
|
RedirectUris = redirectUris,
|
|||
|
|
// Istemci hangi akislari isterse istesin gecerli olanlar bunlardir; RFC 7591
|
|||
|
|
// sunucunun istenen degerleri daraltmasina izin verir.
|
|||
|
|
GrantTypes = [AuthorizationCodeGrant, RefreshTokenGrant],
|
|||
|
|
ResponseTypes = ["code"],
|
|||
|
|
Scope = string.Join(' ', McpConsts.OAuthScopes),
|
|||
|
|
ClientIdIssuedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
}
|