From 4068d2d8a30270266b27c5f524784c1837d74a35 Mon Sep 17 00:00:00 2001 From: dibakor Date: Mon, 22 Jun 2026 11:53:49 +0600 Subject: [PATCH] remove unwanted function --- .../Contracts/Auth/IRefreshTokenService.cs | 4 - .../Services/Auth/RefreshTokenService.cs | 349 ++++++------------ 2 files changed, 111 insertions(+), 242 deletions(-) diff --git a/Api/OnlineSalesAutoCrop.CoreAPI.Services/Contracts/Auth/IRefreshTokenService.cs b/Api/OnlineSalesAutoCrop.CoreAPI.Services/Contracts/Auth/IRefreshTokenService.cs index 38bc176..9f7730c 100644 --- a/Api/OnlineSalesAutoCrop.CoreAPI.Services/Contracts/Auth/IRefreshTokenService.cs +++ b/Api/OnlineSalesAutoCrop.CoreAPI.Services/Contracts/Auth/IRefreshTokenService.cs @@ -6,10 +6,6 @@ namespace OnlineSalesAutoCrop.CoreAPI.Services.Contracts.Auth; public interface IRefreshTokenService { - Task GetByTokenHashAsync(string tokenHash); - Task AddAsync(InsertRefreshTokenRequest refreshToken); - Task RevokeAsync(RevokedRefreshTokenRequest token); - Task RevokeAllForUserAsync(int userId); Task GenerateRefreshTokenByUserAsync(GenerateRefreshTokenRequest request); Task GenerateRefreshTokenAsync(string refreshToken, string ipAddress); Task GetUserByRefreshTokenAsync(string refreshToken); diff --git a/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Auth/RefreshTokenService.cs b/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Auth/RefreshTokenService.cs index e39b090..4453fb3 100644 --- a/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Auth/RefreshTokenService.cs +++ b/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Auth/RefreshTokenService.cs @@ -3,7 +3,6 @@ using Ease.NetCore.DataAccess.SQL; using Microsoft.Data.SqlClient; using Microsoft.Extensions.Options; using OnlineSalesAutoCrop.CoreAPI.Models.Global; -using OnlineSalesAutoCrop.CoreAPI.Models.Objects.Systems; using OnlineSalesAutoCrop.CoreAPI.Models.Requests.Integrations; using OnlineSalesAutoCrop.CoreAPI.Models.Responses.Integrations; using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.Auth; @@ -23,78 +22,6 @@ public class RefreshTokenService : IRefreshTokenService _settings = settings.Value; } - - public async Task AddAsync(InsertRefreshTokenRequest refreshToken) - { - bool returnValue = false; - try - { - using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode, true); - AddAsync(tc, refreshToken); - - tc.End(); - } - catch (Exception e) - { - throw new InvalidOperationException(e.Message, e); - } - - return returnValue; - } - - private bool AddAsync( TransactionContext tc, InsertRefreshTokenRequest refreshToken) - { - bool returnValue = false; - - try - { - SqlParameter[] p = - [ - SqlHelperExtension.CreateInParam(pName: "@UserId", pType: SqlDbType.VarChar, pValue: refreshToken.UserId), - SqlHelperExtension.CreateInParam(pName: "@TokenHash", pType: SqlDbType.VarChar, pValue: refreshToken.TokenHash), - SqlHelperExtension.CreateInParam(pName: "@IpAddress", pType: SqlDbType.VarChar, pValue: refreshToken.IpAddress), - SqlHelperExtension.CreateInParam(pName: "@CreatedAt", pType: SqlDbType.DateTime, pValue: DateTime.Now), - SqlHelperExtension.CreateInParam(pName: "@ExpiredAt", pType: SqlDbType.DateTime, pValue: DateTime.Now.AddMinutes(_settings.RefreshTokenDuration)), - ]; - _ = tc.ExecuteNonQuerySp(spName: "dbo.InsertRefreshToken", parameterValues: p); - - returnValue = true; - } - catch (Exception e) - { - throw new InvalidOperationException(e.Message, e); - } - - return returnValue; - } - - - public async Task GetByTokenHashAsync(string tokenHash) - { - RefreshTokenResponse response = new(); - try - { - using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode); - try - { - response= await GetByTokenHashAsync(tc, tokenHash); - tc.End(); - } - catch (Exception ie) - { - tc?.HandleError(); - - throw DBCustomError.GenerateCustomError(ie); - } - } - catch (Exception e) - { - throw new InvalidOperationException(e.Message, e); - } - - return response; - } - public async Task GetUserByRefreshTokenAsync(string refreshToken) { UserByRefreshTokenResponse response = new(); @@ -121,171 +48,6 @@ public class RefreshTokenService : IRefreshTokenService return response; } - private async Task GetByTokenHashAsync(TransactionContext tc, string tokenHash) - { - RefreshTokenResponse response = new(); - try - { - SqlParameter[] p = - [ - SqlHelperExtension.CreateInParam(pName: "@RefreshToken", pType: SqlDbType.VarChar, pValue: tokenHash), - ]; - using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetRefreshTokenByTokenHash", parameterValues: p)) - { - if (dr.Read()) - { - response.UserId = dr.GetInt32(0); - response.TokenHash = dr.GetString(1); - response.IpAddress = dr.GetString(2); - response.ExpiredAt = dr.GetDateTime(3); - response.RevokedAt = dr.IsDBNull(4) ? null : dr.GetDateTime(4); - } - dr.Close(); - } - } - catch (Exception e) - { - throw new InvalidOperationException(e.Message, e); - } - - return response; - } - - private async Task GetUserByRefreshTokenAsync(TransactionContext tc, string refreshToken) - { - UserByRefreshTokenResponse response = new(); - try - { - SqlParameter[] p = - [ - SqlHelperExtension.CreateInParam(pName: "@RefreshToken", pType: SqlDbType.VarChar, pValue: refreshToken), - ]; - using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetUserByRefreshToken", parameterValues: p)) - { - if (dr.Read()) - { - response.UserId = dr.GetInt32(0); - response.LoginId = dr.GetString(1); - response.EmailAddress = dr.IsDBNull(2) ? string.Empty : dr.GetString(2); - response.AuthKey = dr.IsDBNull(3) ? string.Empty : dr.GetString(3); - response.IsActive = dr.IsDBNull(4) ? true : false; - } - dr.Close(); - } - } - catch (Exception e) - { - throw new InvalidOperationException(e.Message, e); - } - - return response; - } - - public async Task RevokeAllForUserAsync(int userId) - { - bool returnValue = false; - try - { - using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode, true); - try - { - SqlParameter[] p = - [ - SqlHelperExtension.CreateInParam(pName: "@UserId", pType: SqlDbType.Int, pValue: userId) - ]; - _ = await tc.ExecuteNonQuerySpAsync(spName: "dbo.RevokedAllRefreshToken", parameterValues: p); - - returnValue = true; - - tc.End(); - } - catch (Exception ie) - { - tc?.HandleError(); - - throw DBCustomError.GenerateCustomError(ie); - } - } - catch (Exception e) - { - throw new InvalidOperationException(e.Message, e); - } - - return returnValue; - } - - public async Task RevokeAsync(RevokedRefreshTokenRequest token) - { - bool returnValue = false; - try - { - using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode, true); - try - { - returnValue = RevokeAsync(tc, token); - - tc.End(); - } - catch (Exception ie) - { - tc?.HandleError(); - - throw DBCustomError.GenerateCustomError(ie); - } - } - catch (Exception e) - { - throw new InvalidOperationException(e.Message, e); - } - - return returnValue; - } - - private bool RevokeAsync(TransactionContext tc, RevokedRefreshTokenRequest token) - { - bool returnValue = false; - try - { - SqlParameter[] p = - [ - SqlHelperExtension.CreateInParam(pName: "@RefreshToken", pType: SqlDbType.NVarChar, pValue: token.RefreshToken) - ]; - _ = tc.ExecuteNonQuerySp(spName: "dbo.RevokedAllRefreshToken", parameterValues: p); - - returnValue = true; - - } - catch (Exception e) - { - throw new InvalidOperationException(e.Message, e); - } - - return returnValue; - } - - private bool RevokeRefreshTokenByUserIdAsync(TransactionContext tc, int userId) - { - bool returnValue = false; - try - { - SqlParameter[] p = - [ - SqlHelperExtension.CreateInParam(pName: "@UserId", pType: SqlDbType.Int, pValue: userId) - ]; - _ = tc.ExecuteNonQuerySp(spName: "dbo.RevokedAllRefreshTokenByUserId", parameterValues: p); - - returnValue = true; - - } - catch (Exception e) - { - throw new InvalidOperationException(e.Message, e); - } - - return returnValue; - } - - public async Task GenerateRefreshTokenByUserAsync(GenerateRefreshTokenRequest request) { GenerateRefreshTokenResponse refreshTokenResponse = new(); @@ -354,6 +116,7 @@ public class RefreshTokenService : IRefreshTokenService return refreshTokenResponse; } + #region Private Methods // ----- private helpers ----- private bool IsValidRefreshTokenAsync(TransactionContext tc, string refreshToken ) @@ -370,6 +133,114 @@ public class RefreshTokenService : IRefreshTokenService } } + private bool AddAsync(TransactionContext tc, InsertRefreshTokenRequest refreshToken) + { + bool returnValue = false; + + try + { + SqlParameter[] p = + [ + SqlHelperExtension.CreateInParam(pName: "@UserId", pType: SqlDbType.VarChar, pValue: refreshToken.UserId), + SqlHelperExtension.CreateInParam(pName: "@TokenHash", pType: SqlDbType.VarChar, pValue: refreshToken.TokenHash), + SqlHelperExtension.CreateInParam(pName: "@IpAddress", pType: SqlDbType.VarChar, pValue: refreshToken.IpAddress), + SqlHelperExtension.CreateInParam(pName: "@CreatedAt", pType: SqlDbType.DateTime, pValue: DateTime.Now), + SqlHelperExtension.CreateInParam(pName: "@ExpiredAt", pType: SqlDbType.DateTime, pValue: DateTime.Now.AddMinutes(_settings.RefreshTokenDuration)), + ]; + _ = tc.ExecuteNonQuerySp(spName: "dbo.InsertRefreshToken", parameterValues: p); + + returnValue = true; + } + catch (Exception e) + { + throw new InvalidOperationException(e.Message, e); + } + + return returnValue; + } + + private async Task GetByTokenHashAsync(TransactionContext tc, string tokenHash) + { + RefreshTokenResponse response = new(); + try + { + SqlParameter[] p = + [ + SqlHelperExtension.CreateInParam(pName: "@RefreshToken", pType: SqlDbType.VarChar, pValue: tokenHash), + ]; + using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetRefreshTokenByTokenHash", parameterValues: p)) + { + if (dr.Read()) + { + response.UserId = dr.GetInt32(0); + response.TokenHash = dr.GetString(1); + response.IpAddress = dr.GetString(2); + response.ExpiredAt = dr.GetDateTime(3); + response.RevokedAt = dr.IsDBNull(4) ? null : dr.GetDateTime(4); + } + dr.Close(); + } + } + catch (Exception e) + { + throw new InvalidOperationException(e.Message, e); + } + + return response; + } + + private bool RevokeRefreshTokenByUserIdAsync(TransactionContext tc, int userId) + { + bool returnValue = false; + try + { + SqlParameter[] p = + [ + SqlHelperExtension.CreateInParam(pName: "@UserId", pType: SqlDbType.Int, pValue: userId) + ]; + _ = tc.ExecuteNonQuerySp(spName: "dbo.RevokedAllRefreshTokenByUserId", parameterValues: p); + + returnValue = true; + + } + catch (Exception e) + { + throw new InvalidOperationException(e.Message, e); + } + + return returnValue; + } + + private async Task GetUserByRefreshTokenAsync(TransactionContext tc, string refreshToken) + { + UserByRefreshTokenResponse response = new(); + try + { + SqlParameter[] p = + [ + SqlHelperExtension.CreateInParam(pName: "@RefreshToken", pType: SqlDbType.VarChar, pValue: refreshToken), + ]; + using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetUserByRefreshToken", parameterValues: p)) + { + if (dr.Read()) + { + response.UserId = dr.GetInt32(0); + response.LoginId = dr.GetString(1); + response.EmailAddress = dr.IsDBNull(2) ? string.Empty : dr.GetString(2); + response.AuthKey = dr.IsDBNull(3) ? string.Empty : dr.GetString(3); + response.IsActive = dr.IsDBNull(4) ? true : false; + } + dr.Close(); + } + } + catch (Exception e) + { + throw new InvalidOperationException(e.Message, e); + } + + return response; + } + private async Task IssueTokensAsync(TransactionContext tc, int userId, string ipAddress) { string rawRefreshToken = GenerateRowToken(); @@ -405,4 +276,6 @@ public class RefreshTokenService : IRefreshTokenService rng.GetBytes(bytes); return Convert.ToBase64String(bytes); } + + #endregion }