Skip to content

Commit

Permalink
Fixed invitation api
Browse files Browse the repository at this point in the history
  • Loading branch information
aruss committed Jan 19, 2018
1 parent d669b6e commit cf8c78a
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 116 deletions.
19 changes: 19 additions & 0 deletions src/IdentityBase.Shared/Extensions/ClientExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace IdentityBase.Extensions
{
using System;
using System.Linq;
using IdentityBase.Models;
using IdentityServer4.Models;
using ServiceBase.Extensions;
Expand All @@ -10,5 +12,22 @@ public static ClientProperties GetClientProperties(this Client client)
{
return client.Properties.ToObject<ClientProperties>();
}

public static string TryGetReturnUri(
this Client client,
string returnUri)
{
if (String.IsNullOrWhiteSpace(returnUri) &&
client.RedirectUris.Count > 0)
{
return client.RedirectUris.First();
}
else if (client.RedirectUris.Contains(returnUri))
{
return returnUri;
}

return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,90 @@
namespace IdentityBase
{
using System;
using System.Linq;
using System.Threading.Tasks;
using IdentityBase.Configuration;
using IdentityBase.Models;
using IdentityServer4.Models;
using IdentityServer4.Services;
using IdentityServer4.Stores;
using Microsoft.AspNetCore.Http;
using ServiceBase.Extensions;

public class IdentityBaseContextFactory :
IServiceFactory<IdentityBaseContext>
public class IdentityBaseContextBasicFactory
: IServiceFactory<IdentityBaseContext>
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IClientStore _clientStore;

public IdentityBaseContextBasicFactory(
IHttpContextAccessor httpContextAccessor,
IClientStore clientStore,
ApplicationOptions appOptions)
{
this._httpContextAccessor = httpContextAccessor;
this._clientStore = clientStore;
}

public IdentityBaseContext Build()
{
return this.GetIdentityBaseContextAsync().Result;
}

internal virtual async Task<IdentityBaseContext>
GetIdentityBaseContextAsync()
{
var context = new IdentityBaseContext();

string clientId = this._httpContextAccessor
.HttpContext.Request.Query["ClientId"];

if (String.IsNullOrWhiteSpace(clientId))
{
clientId = this._httpContextAccessor
.HttpContext.Request.Headers["X-ClientId"].FirstOrDefault();
}

if (!String.IsNullOrWhiteSpace(clientId))
{
await this.SetClientInfoAsync(context, clientId);
return context;
}

return context;
}

internal async Task SetClientInfoAsync(
IdentityBaseContext context,
string clientId)
{
Client client = await this._clientStore
.FindClientByIdAsync(clientId);

if (client != null && client.Enabled == true)
{
context.Client = client;

context.ClientProperties = client.Properties
.ToObject<ClientProperties>();
}

// fill other junk
}
}

public class IdentityBaseContextIdSrvFactory : IdentityBaseContextBasicFactory
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IIdentityServerInteractionService _interactionService;
private readonly IClientStore _clientStore;

public IdentityBaseContextFactory(
public IdentityBaseContextIdSrvFactory(
IHttpContextAccessor httpContextAccessor,
IIdentityServerInteractionService interactionService,
IClientStore clientStore,
ApplicationOptions appOptions)
: base(httpContextAccessor, clientStore, appOptions)
{
this._httpContextAccessor = httpContextAccessor;
this._interactionService = interactionService;
Expand All @@ -29,10 +93,11 @@ public class IdentityBaseContextFactory :

public IdentityBaseContext Build()
{
return this.GetIdentityBaseContext().Result;
return this.GetIdentityBaseContextAsync().Result;
}

private async Task<IdentityBaseContext> GetIdentityBaseContext()
internal override async Task<IdentityBaseContext>
GetIdentityBaseContextAsync()
{
var context = new IdentityBaseContext();

Expand All @@ -43,7 +108,7 @@ private async Task<IdentityBaseContext> GetIdentityBaseContext()
{
await this.SetAuthorizationRequest(context, returnUrl);

await this.SetClientInfo(context,
await this.SetClientInfoAsync(context,
context.AuthorizationRequest.ClientId);

return context;
Expand All @@ -56,24 +121,15 @@ private async Task<IdentityBaseContext> GetIdentityBaseContext()
{
await this.SetLogoutRequest(context, logoutId);

await this.SetClientInfo(context,
await this.SetClientInfoAsync(context,
context.LogoutRequest.ClientId);

return context;
}

string clientId = this._httpContextAccessor
.HttpContext.Request.Query["ClientId"];

if (!String.IsNullOrWhiteSpace(clientId))
{
await this.SetClientInfo(context, clientId);
return context;
}

return context;

return await base.GetIdentityBaseContextAsync();
}

/// <summary>
/// Creates <see cref="IdentityBaseContext"/>from logoutId.
/// </summary>
Expand Down Expand Up @@ -121,23 +177,5 @@ await this._interactionService
context.ReturnUrl = returnUrl;
context.AuthorizationRequest = request;
}

private async Task SetClientInfo(
IdentityBaseContext context,
string clientId)
{
Client client = await this._clientStore
.FindClientByIdAsync(clientId);

if (client != null && client.Enabled == true)
{
context.Client = client;

context.ClientProperties = client.Properties
.ToObject<ClientProperties>();
}

// fill other junk
}
}
}
44 changes: 44 additions & 0 deletions src/IdentityBase.Shared/Services/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,50 @@ public class NotificationService
);
}

public async Task SendUserAccountInvitationEmailAsync(
UserAccount userAccount)
{
string baseUrl = this._httpContextAccessor
.HttpContext
.GetBaseUrl()
.RemoveTrailingSlash();

UrlHelper urlHelper =
new UrlHelper(_actionContextAccessor.ActionContext);

IdentityBaseContext idbContext = this._httpContextAccessor
.HttpContext.GetIdentityBaseContext();

await this._emailService.SendEmailAsync(
EmailTemplates.UserAccountInvited,
userAccount.Email,
new
{
ConfirmUrl = this.GetUrl(
baseUrl,
"/register/confirm",
userAccount.VerificationKey,
idbContext.Client.ClientId),

CancelUrl = baseUrl + this.GetUrl(
baseUrl,
"/register/cancel",
userAccount.VerificationKey,
idbContext.Client.ClientId),
},
true
);
}

private string GetUrl(
string baseUrl,
string path,
string verificationKey,
string clientId)
{
return $"{baseUrl}{path}?key={verificationKey}&clientId={clientId}&culture={CultureInfo.CurrentUICulture.Name}";
}

private string GetUrl(
UrlHelper urlHelper,
string actionName,
Expand Down
6 changes: 1 addition & 5 deletions src/IdentityBase.Shared/Services/UserAccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -590,11 +590,7 @@ public async Task SetEmailVerifiedAsync(UserAccount userAccount)

return userAccount;
}






public async Task SetEmailChangeVirificationKeyAsync(
UserAccount userAccount,
string email,
Expand Down
28 changes: 0 additions & 28 deletions src/IdentityBase.Shared/ThemeHelper.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,11 @@
namespace IdentityBase
{
using System.Collections.Generic;
using System.IO;
using IdentityBase.Configuration;
using IdentityBase.Models;
using Microsoft.AspNetCore.Http;
using ServiceBase.Extensions;

public static class ObjectExtensions
{
public static TResult ToObject<TResult>(this IDictionary<string, string> source)
where TResult : class, new()
{
var someObject = new TResult();
var someObjectType = someObject.GetType();

foreach (var item in source)
{
try
{
someObjectType
.GetProperty(item.Key)
.SetValue(someObject, item.Value, null);
}
catch (System.Exception)
{

}
}

return someObject;
}

}

public class ThemeHelper
{
private readonly IHttpContextAccessor _httpContextAccessor;
Expand Down

0 comments on commit cf8c78a

Please sign in to comment.