From 25e226d219f5654b668f81dd425e5c953d07f33b Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Fri, 7 Dec 2018 14:37:07 +0900 Subject: [PATCH] Clarify the code --- BTCPayServer/Security/BitpayAuthentication.cs | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/BTCPayServer/Security/BitpayAuthentication.cs b/BTCPayServer/Security/BitpayAuthentication.cs index 56646b405..e71c9f2db 100644 --- a/BTCPayServer/Security/BitpayAuthentication.cs +++ b/BTCPayServer/Security/BitpayAuthentication.cs @@ -57,39 +57,37 @@ namespace BTCPayServer.Security List claims = new List(); var bitpayAuth = Context.Request.HttpContext.GetBitpayAuth(); string storeId = null; - // Careful, those are not the opposite. failedAuth says if a the tentative failed. - // successAuth, ensure that at least one succeed. - var failedAuth = false; - var successAuth = false; + + bool? success = null; if (!string.IsNullOrEmpty(bitpayAuth.Signature) && !string.IsNullOrEmpty(bitpayAuth.Id)) { var result = await CheckBitId(Context.Request.HttpContext, bitpayAuth.Signature, bitpayAuth.Id, claims); storeId = result.StoreId; - successAuth = result.SuccessAuth; - failedAuth = !successAuth; + success = result.SuccessAuth; } else if (!string.IsNullOrEmpty(bitpayAuth.Authorization)) { storeId = await CheckLegacyAPIKey(Context.Request.HttpContext, bitpayAuth.Authorization); - successAuth = storeId != null; - failedAuth = !successAuth; + success = storeId != null; } - if (failedAuth) + if (success.HasValue) { - return AuthenticateResult.Fail("Invalid credentials"); - } - - if (successAuth) - { - if (storeId != null) + if (success.Value) { - claims.Add(new Claim(Policies.CanCreateInvoice.Key, storeId)); - var store = await _StoreRepository.FindStore(storeId); - store.AdditionalClaims.AddRange(claims); - Context.Request.HttpContext.SetStoreData(store); + if (storeId != null) + { + claims.Add(new Claim(Policies.CanCreateInvoice.Key, storeId)); + var store = await _StoreRepository.FindStore(storeId); + store.AdditionalClaims.AddRange(claims); + Context.Request.HttpContext.SetStoreData(store); + } + return AuthenticateResult.Success(new AuthenticationTicket(new ClaimsPrincipal(new ClaimsIdentity(claims, Policies.BitpayAuthentication)), Policies.BitpayAuthentication)); + } + else + { + return AuthenticateResult.Fail("Invalid credentials"); } - return AuthenticateResult.Success(new AuthenticationTicket(new ClaimsPrincipal(new ClaimsIdentity(claims, Policies.BitpayAuthentication)), Policies.BitpayAuthentication)); } } return AuthenticateResult.NoResult();