-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from shawnwildermuth/sessionize
Integrate Sessionize
- Loading branch information
Showing
40 changed files
with
937 additions
and
437 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Microsoft.AspNetCore.Authentication.Cookies; | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace CoreCodeCamp.Authentication | ||
{ | ||
public static class CoreCampAuthSchemes | ||
{ | ||
// HACK | ||
public const string AnyAuthScheme = "Identity.Application," + JwtBearerDefaults.AuthenticationScheme; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace CoreCodeCamp.Authentication | ||
{ | ||
public class CoreCodeCampTokenOptions | ||
{ | ||
public string Issuer { get; set; } = "http://localhost:5000"; | ||
public string Audience { get; set; } = "users"; | ||
public string SigningKey { get; set; } | ||
public int ExpirationLength { get; set; } = 30; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using CoreCodeCamp.Data.Entities; | ||
using CoreCodeCamp.Models; | ||
using CoreCodeCamp.Services; | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.IdentityModel.Tokens; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace CoreCodeCamp.Authentication | ||
{ | ||
public class CoreCodeCampTokenFactory | ||
{ | ||
private readonly IConfiguration _config; | ||
private readonly UserManager<CodeCampUser> _userManager; | ||
private CoreCodeCampTokenOptions _tokenOptions = new CoreCodeCampTokenOptions(); | ||
|
||
public CoreCodeCampTokenFactory(IConfiguration config, UserManager<CodeCampUser> userManager) | ||
{ | ||
_config = config; | ||
_userManager = userManager; | ||
} | ||
|
||
public async Task<TokenModel> GenerateForUser(CodeCampUser user, string optionsKey = "TokenOptions") | ||
{ | ||
_config.Bind(optionsKey, _tokenOptions); | ||
|
||
// Create the token | ||
var claims = new List<Claim>() | ||
{ | ||
new Claim(JwtRegisteredClaimNames.Sub, user.Email), | ||
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), | ||
new Claim(JwtRegisteredClaimNames.UniqueName, user.UserName) | ||
}; | ||
|
||
var userClaims = await _userManager.GetClaimsAsync(user); | ||
|
||
claims.AddRange(userClaims); | ||
|
||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_tokenOptions.SigningKey)); | ||
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512); | ||
|
||
var token = new JwtSecurityToken( | ||
_tokenOptions.Issuer, | ||
_tokenOptions.Audience, | ||
claims, | ||
expires: DateTime.Now.AddMinutes(_tokenOptions.ExpirationLength), | ||
signingCredentials: creds); | ||
|
||
return new TokenModel() | ||
{ | ||
Token = new JwtSecurityTokenHandler().WriteToken(token), | ||
Expiration = token.ValidTo | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.