remove unwanted function
This commit is contained in:
parent
c5a333dcef
commit
4068d2d8a3
|
|
@ -6,10 +6,6 @@ namespace OnlineSalesAutoCrop.CoreAPI.Services.Contracts.Auth;
|
|||
|
||||
public interface IRefreshTokenService
|
||||
{
|
||||
Task<RefreshTokenResponse?> GetByTokenHashAsync(string tokenHash);
|
||||
Task<bool> AddAsync(InsertRefreshTokenRequest refreshToken);
|
||||
Task<bool> RevokeAsync(RevokedRefreshTokenRequest token);
|
||||
Task<bool> RevokeAllForUserAsync(int userId);
|
||||
Task<GenerateRefreshTokenResponse> GenerateRefreshTokenByUserAsync(GenerateRefreshTokenRequest request);
|
||||
Task<string> GenerateRefreshTokenAsync(string refreshToken, string ipAddress);
|
||||
Task<UserByRefreshTokenResponse> GetUserByRefreshTokenAsync(string refreshToken);
|
||||
|
|
|
|||
|
|
@ -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<bool> 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<RefreshTokenResponse> 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<UserByRefreshTokenResponse> GetUserByRefreshTokenAsync(string refreshToken)
|
||||
{
|
||||
UserByRefreshTokenResponse response = new();
|
||||
|
|
@ -121,171 +48,6 @@ public class RefreshTokenService : IRefreshTokenService
|
|||
return response;
|
||||
}
|
||||
|
||||
private async Task<RefreshTokenResponse> 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<UserByRefreshTokenResponse> 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<bool> 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<bool> 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<GenerateRefreshTokenResponse> 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<RefreshTokenResponse> 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<UserByRefreshTokenResponse> 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<GenerateRefreshTokenResponse> 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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user