Skip to main content
Authenticator servisi src/DiyanetCleanArchitecture.Infrastructure.Services.Authenticator projesindedir. Google Authenticator / Microsoft Authenticator gibi uygulamalarla uyumlu TOTP (Time-based One-Time Password, RFC 6238) üretir ve doğrular. Altında OtpNet kütüphanesi çalışır.

Arayüz

public interface IAuthenticationTotpService
{
    string GenerateSecret();
    string GenerateQrCodeUri(string accountName, string secret);
    bool   VerifyCode(string secret, string code, DateTime utcNow, out long timeStep);
}

Secret üretimi

GenerateSecret, config’teki SecretLength (default 20 byte = 160 bit) uzunluğunda rastgele anahtar üretir ve Base32 kodlar:
public string GenerateSecret()
{
    var key = KeyGeneration.GenerateRandomKey(_options.SecretLength);
    return Base32Encoding.ToString(key);
}

QR Code URI — otpauth://

GenerateQrCodeUri, authenticator uygulamalarının okuduğu standart otpauth://totp/... URI’sini üretir. Issuer ve account adı URL-encode edilir; algoritma, basamak ve periyot config’ten gelir:
return
    $"otpauth://totp/{encodedIssuer}:{encodedAccount}" +
    $"?secret={secret}" +
    $"&issuer={encodedIssuer}" +
    $"&algorithm={_options.Algorithm}" +   // SHA1
    $"&digits={_options.Digits}" +         // 6
    $"&period={_options.TimeStepSeconds}"; // 30

Doğrulama — drift toleransı

VerifyCode, kodu Base32 secret ile doğrular. Saat kayması için AllowedDriftSteps kadar (default ±1 time step) pencere uygulanır. Eşleşen timeStep out parametresiyle döner (replay/önceki kullanım kontrolü için):
var totp = new Totp(secretBytes,
    step: _options.TimeStepSeconds,
    totpSize: _options.Digits,
    mode: _hashMode);

var window = new VerificationWindow(
    previous: _options.AllowedDriftSteps,
    future:   _options.AllowedDriftSteps);

return totp.VerifyTotp(code, out timeStep, window);
Servis fail-closed’dur: geçersiz Base32, sayısal olmayan kod ya da beklenmedik herhangi bir hata false döner — asla “doğru” kabul etmez.
Constructor’da seçenekler validate edilir: Issuer boş olamaz, TimeStepSeconds > 0, SecretLength >= 16, AllowedDriftSteps >= 0, Digits ∈ {4, 6, 8}, Algorithm ∈ {SHA1, SHA256, SHA512}.

User aggregate ile ilişki

Servis yalnızca saf TOTP hesaplaması yapar; durum User aggregate’inde tutulur. Akış (ConfigureTotpCommandHandler):
var plainSecret   = _totpService.GenerateSecret();
var encryptedSecret = TotpSecret.Create(encrypted: _secretProtector.Protect(plainSecret));

user.ConfigureTotp(encryptedSecret);                          // aggregate'e işle
var qrCodeUri = _totpService.GenerateQrCodeUri(user.Phone.Value, plainSecret);

await _unitOfWork.SaveEntitiesAsync(cancellationToken);
// dön: { ManualEntryKey = plainSecret, QrCodeUri = qrCodeUri }
User aggregate metodları: ConfigureTotp (secret atar, henüz aktif değil), EnableTotp (ilk doğru kod ile etkinleştirir), VerifyTotp, DisableTotp.
TotpSecret DataProtection ile şifreli saklanır. ITotpSecretProtector (impl: TotpSecretProtector) ASP.NET Core IDataProtectionProvider’ı "TotpSecret" purpose’u ile kullanır. DB’ye düz secret asla yazılmaz; QR/manuel giriş için düz secret yalnızca configure anında, response’da bir kez döner.

Config — Services:Authenticator

{
  "Services": {
    "Authenticator": {
      "Issuer": "DiyanetCleanArchitecture",
      "TimeStepSeconds": 30,
      "AllowedDriftSteps": 1,
      "SecretLength": 20,
      "Algorithm": "SHA1",
      "Digits": 6
    }
  }
}
Issuer
string
default:"DiyanetCleanArchitecture"
Authenticator uygulamasında görünen sağlayıcı adı.
TimeStepSeconds
int
default:"30"
TOTP periyodu (RFC 6238 standardı = 30 sn).
AllowedDriftSteps
int
default:"1"
Saat kayması toleransı (±N adım).
SecretLength
int
default:"20"
Secret uzunluğu (byte). Min 16.
Algorithm
string
default:"SHA1"
SHA1 / SHA256 / SHA512. Uyumluluk için SHA1.
Digits
int
default:"6"
Kod basamak sayısı (4, 6 veya 8).

DI kaydı

public static IServiceCollection AddAuthenticatorService(this IServiceCollection services, IConfiguration configuration)
{
    services.Configure<AuthenticatorServiceOptions>(o =>
        configuration.GetSection("Services:Authenticator").Bind(o));

    services.AddScoped<IAuthenticationTotpService, AuthenticationTotpService>();
    return services;
}

User Aggregate

ConfigureTotp / EnableTotp / VerifyTotp metodları ve TotpSecret value object.

Güvenlik

Çok faktörlü kimlik doğrulama ve DataProtection kullanımı.