Initial Commit ...
This commit is contained in:
@@ -0,0 +1,211 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using xCommons.Configurations;
|
||||
using xCommons.Constants;
|
||||
using xCommons.Controllers;
|
||||
using xCommons.Extensions;
|
||||
using xCommons.Providers;
|
||||
using xExceptions.Constants;
|
||||
using xIdentityModels.Constants;
|
||||
using xIdentityModels.Models;
|
||||
using xIdentityService.Interfaces;
|
||||
|
||||
namespace xIdentityService.Controllers {
|
||||
public abstract class XIBaseController : XBaseController {
|
||||
private readonly IXIdentityProvider identityProvider;
|
||||
|
||||
public XIBaseController (
|
||||
ILogger logger,
|
||||
XAppConfiguration appConfiguration,
|
||||
IXIdentityProvider identityProvider,
|
||||
XValidationProvider validationProvider
|
||||
) : base (
|
||||
logger,
|
||||
appConfiguration,
|
||||
validationProvider
|
||||
) {
|
||||
this.identityProvider = identityProvider;
|
||||
}
|
||||
|
||||
//
|
||||
#region User Handlers NonActions ...
|
||||
/// <summary>
|
||||
/// Retrieve User Identifier Base on XActionRequest
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <param name="forceNotNull"></param>
|
||||
/// <param name="excludes"></param>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public string GetUserSelectByParam (
|
||||
XActionRequest model,
|
||||
bool forceNotNull = true,
|
||||
ICollection<XUserSelectBy> excludes = null) {
|
||||
//
|
||||
// Validate Args ...
|
||||
ValidationProvider.NotNull (model);
|
||||
|
||||
//
|
||||
var result = identityProvider.GetUserSelectByParam (
|
||||
model,
|
||||
forceNotNull,
|
||||
excludes);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Access Token
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<string> GetAccessToken () {
|
||||
//
|
||||
var accessToken = Request.Headers[XAuthorization.Header]
|
||||
.ToString ();
|
||||
if (accessToken.IsNullOrEmpty ()) {
|
||||
accessToken = await HttpContext
|
||||
.GetTokenAsync (XAuthorization.AccessToken);
|
||||
}
|
||||
|
||||
//
|
||||
if (accessToken
|
||||
.ToNormalString ()
|
||||
.Contains (XAuthorization.TokenIdentifier
|
||||
.ToNormalString ())) {
|
||||
accessToken = accessToken
|
||||
.Remove (0, XAuthorization.TokenIdentifier.Length);
|
||||
}
|
||||
|
||||
//
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Refresh Token
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<string> GetRefreshToken () {
|
||||
//
|
||||
var refreshToken = Request.Headers[XAuthorization.RefreshToken]
|
||||
.ToString ();
|
||||
if (refreshToken.IsNullOrEmpty ()) {
|
||||
refreshToken = await HttpContext
|
||||
.GetTokenAsync (XAuthorization.RefreshToken);
|
||||
}
|
||||
|
||||
//
|
||||
return refreshToken;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Access Token Expiration Date
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<long> GetTokenExpiresAt () {
|
||||
//
|
||||
var expiresAtStr = Request.Headers[XAuthorization.ExpiresAt]
|
||||
.ToString ();
|
||||
if (expiresAtStr.IsNullOrEmpty ()) {
|
||||
expiresAtStr = await HttpContext
|
||||
.GetTokenAsync (XAuthorization.ExpiresAt);
|
||||
}
|
||||
|
||||
//
|
||||
var expiresAt = expiresAtStr.ConvertTo<long> ();
|
||||
|
||||
//
|
||||
return expiresAt;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve All Required Tokens
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<XLoginResponse> RetrieveTokensAsXLoginResponse () {
|
||||
//
|
||||
var accessToken = await GetAccessToken ();
|
||||
var refreshToken = await GetRefreshToken ();
|
||||
var expiresAt = await GetTokenExpiresAt ();
|
||||
|
||||
//
|
||||
return new XLoginResponse {
|
||||
AccessToken = accessToken,
|
||||
RefreshToken = refreshToken,
|
||||
ExpiresAt = expiresAt
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve All Required Tokens
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<XTokenResponse> RetrieveTokensAsXTokenResponse () {
|
||||
//
|
||||
var accessToken = await GetAccessToken ();
|
||||
var refreshToken = await GetRefreshToken ();
|
||||
var expiresAt = await GetTokenExpiresAt ();
|
||||
|
||||
//
|
||||
return new XTokenResponse {
|
||||
AccessToken = accessToken,
|
||||
RefreshToken = refreshToken,
|
||||
ExpiresAt = expiresAt
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrive UserInfo
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<XUserClaimsInfoDto> GetUserInfo () {
|
||||
//
|
||||
var claims = User.Claims ?? null;
|
||||
if (!claims.HasChild ()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var xTokens = await RetrieveTokensAsXTokenResponse ();
|
||||
var result = new XUserClaimsInfoDto (
|
||||
xTokens.AccessToken,
|
||||
xTokens.RefreshToken,
|
||||
xTokens.ExpiresAt,
|
||||
User.Claims
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validate User Authenticated and Retrieve User Info
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<XUserClaimsInfoDto> ValidateAndGetUserInfo () {
|
||||
//
|
||||
if (!User.Identity.IsAuthenticated) {
|
||||
XException.NotAuthorized.Throw ();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await GetUserInfo ();
|
||||
if (result.IsNull ()) {
|
||||
XException.NotAuthorized.Throw ();
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user