151 lines
4.3 KiB
C#
151 lines
4.3 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using xCommons.Extensions;
|
|
using xIdentityModels.Models;
|
|
using xIdentityService.Interfaces;
|
|
|
|
namespace xIdentityService.Providers {
|
|
public partial class XInMemoryTokenProvider : IXTokenProvider {
|
|
private IDictionary<string, XTokenResponse> DbContext;
|
|
|
|
public XInMemoryTokenProvider () {
|
|
//
|
|
this.DbContext = new Dictionary<string, XTokenResponse> ();
|
|
}
|
|
|
|
public async Task<bool> IsTokenExists (string accessToken) {
|
|
//
|
|
// Validate Args ...
|
|
if (accessToken.IsNullOrEmpty ()) {
|
|
return false;
|
|
}
|
|
|
|
//
|
|
// Process Result ...
|
|
var result = DbContext.Keys.Any (k => k == accessToken);
|
|
return await Task.FromResult (result);
|
|
}
|
|
|
|
public async Task<bool> IsTokenExists (XTokenResponse tokens) {
|
|
//
|
|
// Validate Args ...
|
|
if (tokens.IsNull () ||
|
|
tokens.AccessToken.IsNullOrEmpty () ||
|
|
!await IsTokenExists (tokens.AccessToken)) {
|
|
return false;
|
|
};
|
|
|
|
//
|
|
// Process Result ...
|
|
var result = await IsTokenExists (tokens.AccessToken);
|
|
return result;
|
|
}
|
|
|
|
public async Task<bool> RemoveToken (string accessToken) {
|
|
//
|
|
// Validate Args ...
|
|
if (accessToken.IsNullOrEmpty ()) {
|
|
return false;
|
|
}
|
|
|
|
//
|
|
// Process Result ...
|
|
var result = DbContext.Remove (accessToken);
|
|
return await Task.FromResult (result);
|
|
}
|
|
|
|
public async Task<bool> RemoveToken (XTokenResponse tokens) {
|
|
//
|
|
// Validate Args ...
|
|
if (tokens.IsNull () ||
|
|
tokens.AccessToken.IsNullOrEmpty () ||
|
|
!await IsTokenExists (tokens.AccessToken)) {
|
|
return false;
|
|
};
|
|
|
|
//
|
|
// Process Result ...
|
|
var result = await RemoveToken (tokens.AccessToken);
|
|
return result;
|
|
}
|
|
|
|
public async Task<XTokenResponse> RetrieveToken (string accessToken) {
|
|
//
|
|
// Validate Args ...
|
|
if (accessToken.IsNullOrEmpty () ||
|
|
!await IsTokenExists (accessToken)) {
|
|
return null;
|
|
}
|
|
|
|
//
|
|
// Process Result ...
|
|
var result = DbContext[accessToken];
|
|
return result;
|
|
}
|
|
|
|
public async Task<bool> AddToken (XTokenResponse tokens) {
|
|
//
|
|
// Validate Args ...
|
|
if (tokens.IsNull () ||
|
|
tokens.AccessToken.IsNullOrEmpty () ||
|
|
await IsTokenExists (tokens.AccessToken)) {
|
|
return false;
|
|
};
|
|
|
|
//
|
|
// Process Result ...
|
|
try {
|
|
//
|
|
DbContext.Add (tokens.AccessToken, tokens);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> UpdateToken (XTokenResponse tokens) {
|
|
//
|
|
// Validate Args ...
|
|
if (tokens.IsNull () ||
|
|
tokens.AccessToken.IsNullOrEmpty () ||
|
|
!await IsTokenExists (tokens.AccessToken)) {
|
|
return false;
|
|
};
|
|
|
|
//
|
|
// Process Result ...
|
|
try {
|
|
//
|
|
DbContext[tokens.AccessToken] = tokens;
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> AddOrUpdateToken (XTokenResponse tokens) {
|
|
//
|
|
// Validate Args ...
|
|
if (tokens.IsNull () ||
|
|
tokens.AccessToken.IsNullOrEmpty ()
|
|
) {
|
|
return false;
|
|
};
|
|
|
|
//
|
|
// Process Result ...
|
|
try {
|
|
//
|
|
var isExists = await IsTokenExists (tokens);
|
|
if (isExists) {
|
|
return await UpdateToken (tokens);
|
|
} else {
|
|
return await AddToken (tokens);
|
|
}
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
} |