Compare commits
1
Commits
72354dad4f
...
d43428e0b6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d43428e0b6 |
@@ -141,7 +141,7 @@ namespace xIdentityService.Extensions
|
||||
/// Retrieve All Required Tokens
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static async Task<XTokenResponse> RetrieveTokensAsXTokenResponse(HttpContext context)
|
||||
public static async Task<XTokenResponse> RetrieveTokensAsXTokenResponse(this HttpContext context)
|
||||
{
|
||||
//
|
||||
var accessToken = await GetAccessToken(context);
|
||||
|
||||
@@ -6,6 +6,16 @@ namespace xIdentityService.Extensions
|
||||
{
|
||||
public static class XRequestExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Extract ApiKey from Request Headers ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static string ExtractApiKey(this HttpContext source)
|
||||
{
|
||||
return source.Request.ExtractApiKey();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract ApiKey from Request Headers ...
|
||||
/// </summary>
|
||||
|
||||
@@ -44,6 +44,7 @@ namespace xIdentityService.Interfaces
|
||||
#region Authentication ...
|
||||
Task<DiscoveryDocumentResponse> RequestDiscoveryDocument();
|
||||
Task<XTokenResponse> RequestScopeAccessToken(string scope);
|
||||
Task<TokenIntrospectionResponse> Validate(XTokenResponse model);
|
||||
Task<TokenIntrospectionResponse> Introspection(XTokenResponse model);
|
||||
Task<XLoginResponse> Login(XLoginRequest model);
|
||||
Task Logout(XTokenResponse model);
|
||||
|
||||
@@ -9,24 +9,29 @@ using System.Collections.Generic;
|
||||
using xIdentityModels.Extensions;
|
||||
using xIdentityService.Constants;
|
||||
|
||||
namespace xIdentityService.Providers {
|
||||
public partial class XIdentityProvider {
|
||||
namespace xIdentityService.Providers
|
||||
{
|
||||
public partial class XIdentityProvider
|
||||
{
|
||||
//
|
||||
#region Authentication Handlers ...
|
||||
/// <summary>
|
||||
/// Request for Discovery Document
|
||||
/// </summary>
|
||||
/// <returns>an instance of DiscoveryDocumentResponse</returns>
|
||||
public async Task<DiscoveryDocumentResponse> RequestDiscoveryDocument () {
|
||||
public async Task<DiscoveryDocumentResponse> RequestDiscoveryDocument()
|
||||
{
|
||||
//
|
||||
var httpClient = GetHttpClient();
|
||||
|
||||
//
|
||||
try {
|
||||
try
|
||||
{
|
||||
//
|
||||
var result = httpClient
|
||||
.GetDiscoveryDocumentAsync(identityConfiguration.Authority)
|
||||
.ContinueWith (docTask => {
|
||||
.ContinueWith(docTask =>
|
||||
{
|
||||
//
|
||||
httpClient.Dispose();
|
||||
return docTask.Result;
|
||||
@@ -34,7 +39,9 @@ namespace xIdentityService.Providers {
|
||||
|
||||
//
|
||||
return await result;
|
||||
} catch {
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw XException.ActionFailed.ToException();
|
||||
}
|
||||
}
|
||||
@@ -44,14 +51,18 @@ namespace xIdentityService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="scope">a member of <see>XApiScope</see></param>
|
||||
/// <returns>an instance of <see>XTokenResponse</see></returns>
|
||||
public async Task<XTokenResponse> RequestScopeAccessToken (string scope) {
|
||||
public async Task<XTokenResponse> RequestScopeAccessToken(string scope)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
var scopeName = string.Empty;
|
||||
try {
|
||||
try
|
||||
{
|
||||
scopeName = scope; // scope.GetStringValue ();
|
||||
} catch { }
|
||||
if (scopeName.IsNullOrEmpty ()) {
|
||||
}
|
||||
catch { }
|
||||
if (scopeName.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
@@ -70,7 +81,8 @@ namespace xIdentityService.Providers {
|
||||
|
||||
//
|
||||
// Save Token ...
|
||||
if (!response.IsNull ()) {
|
||||
if (!response.IsNull())
|
||||
{
|
||||
await tokenProvider.AddOrUpdateToken(response);
|
||||
}
|
||||
|
||||
@@ -83,7 +95,8 @@ namespace xIdentityService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="model">Authentication Tokens</param>
|
||||
/// <returns>an instance of TokenIntrospectionResponse</returns>
|
||||
public async Task<TokenIntrospectionResponse> Introspection (XTokenResponse model) {
|
||||
public async Task<TokenIntrospectionResponse> Introspection(XTokenResponse model)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
await validationProvider
|
||||
@@ -96,15 +109,18 @@ namespace xIdentityService.Providers {
|
||||
var httpClient = GetHttpClient();
|
||||
|
||||
//
|
||||
try {
|
||||
try
|
||||
{
|
||||
//
|
||||
var discoDoc = await RequestDiscoveryDocument();
|
||||
if (discoDoc.IsError) {
|
||||
if (discoDoc.IsError)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var response = await httpClient.IntrospectTokenAsync (new TokenIntrospectionRequest {
|
||||
var response = await httpClient.IntrospectTokenAsync(new TokenIntrospectionRequest
|
||||
{
|
||||
//
|
||||
Address = discoDoc.IntrospectionEndpoint,
|
||||
ClientId = identityConfiguration.ClientId,
|
||||
@@ -113,23 +129,75 @@ namespace xIdentityService.Providers {
|
||||
//
|
||||
Token = model.AccessToken
|
||||
});
|
||||
if (response.IsError) {
|
||||
if (response.IsError)
|
||||
{
|
||||
throw new Exception(response.Error);
|
||||
}
|
||||
|
||||
//
|
||||
return response;
|
||||
} catch {
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw XException.ActionFailed.ToException();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validate Specified Token Model ...
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<TokenIntrospectionResponse> Validate(XTokenResponse model)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
await validationProvider
|
||||
.GroupValidationBuilder()
|
||||
.AddNotNull(model)
|
||||
.AddNotEmpty(model.AccessToken)
|
||||
.ValidateGroupAsync();
|
||||
|
||||
//
|
||||
var httpClient = GetHttpClient();
|
||||
|
||||
//
|
||||
TokenIntrospectionResponse response = null;
|
||||
try
|
||||
{
|
||||
//
|
||||
var discoDoc = await RequestDiscoveryDocument();
|
||||
if (discoDoc.IsError)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
response = await httpClient.IntrospectTokenAsync(new TokenIntrospectionRequest
|
||||
{
|
||||
//
|
||||
Address = discoDoc.IntrospectionEndpoint,
|
||||
ClientId = identityConfiguration.ClientId,
|
||||
ClientSecret = identityConfiguration.ClientSecret,
|
||||
|
||||
//
|
||||
Token = model.AccessToken
|
||||
});
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
|
||||
//
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Authenticate a User
|
||||
/// </summary>
|
||||
/// <param name="model">User Login Required Info, an instance of <see>XLoginRequest</see></param>
|
||||
/// <returns>an instance of <see>XTokenResponse</see></returns>
|
||||
public async Task<XTokenResponse> Authenticate (XLoginRequest model) {
|
||||
public async Task<XTokenResponse> Authenticate(XLoginRequest model)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
validationProvider
|
||||
@@ -155,7 +223,8 @@ namespace xIdentityService.Providers {
|
||||
|
||||
//
|
||||
// Save Token ...
|
||||
if (!response.IsNull ()) {
|
||||
if (!response.IsNull())
|
||||
{
|
||||
await tokenProvider.AddOrUpdateToken(response);
|
||||
}
|
||||
|
||||
@@ -168,7 +237,8 @@ namespace xIdentityService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="model">User Login Required Info, an instance of <see>XLoginRequest</see></param>
|
||||
/// <returns>an instance of <see>XTokenResponse</see></returns>
|
||||
public async Task<XLoginResponse> Login (XLoginRequest model) {
|
||||
public async Task<XLoginResponse> Login(XLoginRequest model)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
validationProvider
|
||||
@@ -193,7 +263,8 @@ namespace xIdentityService.Providers {
|
||||
);
|
||||
|
||||
//
|
||||
if (!response.IsNull ()) {
|
||||
if (!response.IsNull())
|
||||
{
|
||||
//
|
||||
var tokens = response.ToXTokenResponse();
|
||||
await tokenProvider.AddOrUpdateToken(tokens);
|
||||
@@ -208,7 +279,8 @@ namespace xIdentityService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public async Task Logout (XTokenResponse model) {
|
||||
public async Task Logout(XTokenResponse model)
|
||||
{
|
||||
//
|
||||
await validationProvider
|
||||
.GroupValidationBuilder()
|
||||
@@ -219,7 +291,8 @@ namespace xIdentityService.Providers {
|
||||
//
|
||||
var isExists = await tokenProvider
|
||||
.IsTokenExists(model.AccessToken);
|
||||
if (isExists) {
|
||||
if (isExists)
|
||||
{
|
||||
await tokenProvider
|
||||
.RemoveToken(model.AccessToken);
|
||||
}
|
||||
@@ -230,7 +303,8 @@ namespace xIdentityService.Providers {
|
||||
/// </summary>
|
||||
/// <param name="model">Authentication Tokens</param>
|
||||
/// <returns>an instance of <see>XTokenResponse</see></returns>
|
||||
public async Task<XTokenResponse> RefreshTokens (XTokenResponse model) {
|
||||
public async Task<XTokenResponse> RefreshTokens(XTokenResponse model)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
validationProvider
|
||||
@@ -257,7 +331,8 @@ namespace xIdentityService.Providers {
|
||||
request,
|
||||
XHttpMethod.POST
|
||||
);
|
||||
if (!response.IsNull ()) {
|
||||
if (!response.IsNull())
|
||||
{
|
||||
await tokenProvider.UpdateToken(response);
|
||||
}
|
||||
|
||||
@@ -277,7 +352,8 @@ namespace xIdentityService.Providers {
|
||||
public async Task ChangePassword(
|
||||
XTokenResponse tokens,
|
||||
XActionRequest model
|
||||
) {
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
await validationProvider
|
||||
|
||||
Reference in New Issue
Block a user