namespace NATS.Server.Auth;

public sealed class ProxyAuthenticator(ProxyAuthOptions options) : IAuthenticator
{
    public AuthResult? Authenticate(ClientAuthContext context)
    {
        if (!options.Enabled)
            return null;

        var username = context.Opts.Username;
        if (string.IsNullOrEmpty(username))
            return null;

        if (!username.StartsWith(options.UsernamePrefix, StringComparison.Ordinal))
            return null;

        var identity = username[options.UsernamePrefix.Length..];
        if (identity.Length == 0)
            return null;

        return new AuthResult
        {
            Identity = identity,
            AccountName = options.Account,
        };
    }
}
