Skip to content

Commit

Permalink
Merge pull request #200 from eatskolnikov/development
Browse files Browse the repository at this point in the history
Added claim for userid to user claims
  • Loading branch information
eatskolnikov committed Jul 25, 2020
2 parents 6b7f5e5 + 9dbd025 commit f2643ba
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 47 deletions.
3 changes: 2 additions & 1 deletion Web/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task<IActionResult> Login([FromForm] string provider, [FromForm] st
return BadRequest();
}

return Challenge(new AuthenticationProperties { RedirectUri = Url.Action("OnPostConfirmation", "Account", new { returnUrl, provider } )}, provider);
return Challenge(new AuthenticationProperties { RedirectUri = returnUrl }, provider); // Url.Action("OnPostConfirmation", "Account", new { returnUrl, provider } )}, provider);
}

[HttpGet]
Expand All @@ -67,6 +67,7 @@ public IActionResult LogOut()

public async Task<IActionResult> OnPostConfirmation(string returnUrl, string provider)
{

try
{
if (string.IsNullOrWhiteSpace(provider))
Expand Down
2 changes: 1 addition & 1 deletion Web/Controllers/BaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Web.Controllers
{
public class BaseController : Controller
{
protected ApplicationUser _currentUser => new ApplicationUser(HttpContext.User, HttpContext.Session);
protected ApplicationUser _currentUser => new ApplicationUser(HttpContext.User);


public override void OnActionExecuting(ActionExecutingContext context)
Expand Down
18 changes: 2 additions & 16 deletions Web/Framework/ApplicationUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,14 @@ namespace Web.Framework
{
public class ApplicationUser
{
ISession _session;
private ClaimsPrincipal _user;
public ApplicationUser(ClaimsPrincipal user, ISession session)
public ApplicationUser(ClaimsPrincipal user)
{
_user = user;
_session= session;
}

public void SetUserId(int value)
{
var userIdClaim = _user.FindFirst("UserId");
var claimIdentity = ((ClaimsIdentity)_user.Identity);
if (userIdClaim != null)
claimIdentity.RemoveClaim(userIdClaim);
claimIdentity.AddClaim(new Claim("UserId", value.ToString()));
}
public int UserId {
get { return _session.GetInt32("UserId")??0; }
}
public int UserId { get { return Convert.ToInt32(_user.FindFirst("UserId").Value); } }
public string SocialId { get { return _user.FindFirst(ClaimTypes.NameIdentifier).Value; } }
public string Email { get { return _user.FindFirst(ClaimTypes.Email).Value; } }
public string Name { get { return _user.FindFirst(ClaimTypes.Name).Value; } }

}
}
57 changes: 57 additions & 0 deletions Web/Framework/Configurations/AuthConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Threading.Tasks;
using AppServices.Services;
using Microsoft.FeatureManagement;
using System;
using Domain.Entities;

namespace Web.Framework.Configurations
{
Expand Down Expand Up @@ -41,33 +43,88 @@ public static void Init(IConfiguration configuration, IServiceCollection service
googleOptions.ClaimActions.MapJsonKey("urn:google:profile", "link");
googleOptions.ClaimActions.MapJsonKey(ClaimTypes.Email, "email");
googleOptions.SaveTokens = true;
googleOptions.Events.OnCreatingTicket = ctx => {
return ProcessUser(ctx, "Google", services);
};
});

if(featureManager.IsEnabledAsync(FeatureFlags.UseFacebookAuthentication).Result)
services.AddAuthentication().AddFacebook(facebookOptions =>
{
facebookOptions.AppId = configuration["Authentication:Facebook:AppId"];
facebookOptions.AppSecret = configuration["Authentication:Facebook:AppSecret"];
facebookOptions.Events.OnCreatingTicket = ctx => {
return ProcessUser(ctx, "Facebook", services);
};
});
if(featureManager.IsEnabledAsync(FeatureFlags.UseMicrosoftAuthentication).Result)
services.AddAuthentication().AddMicrosoftAccount(microsoftOptions =>
{
microsoftOptions.ClientId = configuration["Authentication:Microsoft:ClientId"];
microsoftOptions.ClientSecret = configuration["Authentication:Microsoft:ClientSecret"];
microsoftOptions.Events.OnCreatingTicket = ctx => {
return ProcessUser(ctx, "Microsoft", services);
};
});
if(featureManager.IsEnabledAsync(FeatureFlags.UseLinkedInAuthentication).Result)
services.AddAuthentication().AddLinkedIn(linkedinOptions =>
{
linkedinOptions.ClientId = configuration["Authentication:LinkedIn:ClientId"];
linkedinOptions.ClientSecret = configuration["Authentication:LinkedIn:ClientSecret"];
linkedinOptions.Events.OnCreatingTicket = ctx => {
return ProcessUser(ctx, "linkedin", services);
};
});
if(featureManager.IsEnabledAsync(FeatureFlags.UseGithubAuthentication).Result)
services.AddAuthentication().AddGitHub(githubOptions =>
{
githubOptions.ClientId = configuration["Authentication:Github:ClientId"];
githubOptions.ClientSecret = configuration["Authentication:Github:ClientSecret"];
githubOptions.Scope.Add("user:email");
githubOptions.Events.OnCreatingTicket = ctx => {
return ProcessUser(ctx, "github", services);
};
});
}


public static Task ProcessUser(OAuthCreatingTicketContext ctx, string provider, IServiceCollection services)
{
var serviceProvider = services.BuildServiceProvider();
var loginService = serviceProvider.GetService<ILoginsService>();
var userService = serviceProvider.GetService<IUsersService>();

var currentUser = ctx.Identity;
var socialId = currentUser.FindFirst(ClaimTypes.NameIdentifier).Value;
var loginInfo = loginService.GetLogin(provider.ToLower(), socialId);
if (loginInfo == null) //Create new account
{
var newUser = new User
{
Email = currentUser.FindFirst(ClaimTypes.Email).Value,
Name = currentUser.FindFirst(ClaimTypes.Name).Value,
};
var result = userService.Create(newUser);

if (result.Success)
{
var newLogin = new Login
{
LoginProvider = provider.ToLower(),
ProviderKey = socialId,
UserId = newUser.Id
};
loginService.Create(newLogin);
var userIdClaim = new Claim("UserId", newUser.Id.ToString());
ctx.Identity.AddClaim(userIdClaim);
}
}
else
{
var userIdClaim = new Claim("UserId", loginInfo.UserId.ToString());
ctx.Identity.AddClaim(userIdClaim);
}
return Task.CompletedTask;
}
}
}
2 changes: 1 addition & 1 deletion Web/Framework/ViewPageBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public abstract class ViewPageBase<TModel> : RazorPage<TModel>
{
public String Title { get; set; }

protected ApplicationUser CurrentUser => new ApplicationUser(Context.User, Context.Session);
protected ApplicationUser CurrentUser => new ApplicationUser(Context.User);

}
}
68 changes: 40 additions & 28 deletions Web/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
"DefaultConnection": "Data Source=../mydb.db;"
},
"Authentication": {
"Google": {
"ClientId": "",
"ClientSecret": ""
},
"Facebook": {
"AppId": "",
"AppSecret": ""
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"Github": {
"ClientId": "rtyrty",
"ClientSecret": "ertert"
"ConnectionStrings": {
"DefaultConnection": "Data Source=../mydb.db;"
},
"LinkedIn": {
"ClientId": "",
"ClientSecret": ""
"GoogleMapsApiKey": "AIzaSyCbhsqfnYFGPid88_Ncdm7IPcAN2Joj_VY",
"FeatureManagement": {
"use-facebook-authentication": false,
"use-github-authentication": false,
"use-linkedin-authentication": false,
"use-google-authentication": true,
"use-microsoft-authentication": false,
"jobscontroller-enable-search": false,
"use-mock-data": false,
"jobscontroller-enable-apply-for-job": false,
"jobscontroller-show-preview-warning": false
},
"Microsoft": {
"ClientId": "rtret",
"ClientSecret": "ertert"
"Authentication": {
"Google": {
"ClientId": "184368955865-eq4m1mec3kr504jqhr1tbi9mf2hgskhl.apps.googleusercontent.com",
"ClientSecret": "lLkrYzans4kwKYUARz7cBxKj"
},
"Facebook": {
"AppId": "",
"AppSecret": ""
},
"Github": {
"ClientId": "rtyrty",
"ClientSecret": "ertert"
},
"LinkedIn": {
"ClientId": "",
"ClientSecret": ""
},
"Microsoft": {
"ClientId": "rtret",
"ClientSecret": "ertert"
}
}
}
}

0 comments on commit f2643ba

Please sign in to comment.