Skip to content

Commit

Permalink
Merge pull request #41 from shawnwildermuth/sessionize
Browse files Browse the repository at this point in the history
Integrate Sessionize
  • Loading branch information
shawnwildermuth authored Mar 25, 2020
2 parents 0bdb2c2 + cc6f930 commit f2567a7
Show file tree
Hide file tree
Showing 40 changed files with 937 additions and 437 deletions.
14 changes: 8 additions & 6 deletions src/CoreCodeCamp/Areas/Admin/Controllers/RootController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public RootController(ICodeCampRepository repo, ILogger<RootController> logger,
}

[HttpGet("[area]")]
public IActionResult Index()
public async Task<IActionResult> Index()
{
return View(_repo.GetAllEventInfo());
return View(await _repo.GetAllEventInfoAsync());
}

[HttpGet("[area]/users")]
Expand Down Expand Up @@ -54,9 +54,11 @@ public IActionResult EventInfo(string moniker)
}

[HttpGet("{moniker}/[area]/speakerlist")]
public FileContentResult SpeakerList(string moniker)
public async Task<FileContentResult> SpeakerList(string moniker)
{
var speakers = _repo.GetSpeakers(moniker).Where(s => s.Talks.Count(t => t.Approved) > 0).ToList();
var speakers = (await _repo.GetSpeakersAsync(moniker))
.Where(s => s.Talks.Count(t => t.Approved) > 0)
.ToList();

var csv = new StringBuilder();
csv.AppendLine("\"Name\",\"Email\",\"CompanyName\",\"PhoneNumber\",\"TwitterHandle\",\"TShirtSize\"");
Expand All @@ -75,9 +77,9 @@ public FileContentResult SpeakerList(string moniker)
}

[HttpGet("{moniker}/[area]/submissionlist")]
public FileContentResult SubmissionList(string moniker)
public async Task<FileContentResult> SubmissionList(string moniker)
{
var talks = _repo.GetTalks(moniker).ToList();
var talks = (await _repo.GetTalksAsync(moniker)).ToList();

var csv = new StringBuilder();
csv.AppendLine(@"""Title"",""SpeakerName"",""Email"", ""SpeakerCompanyName"",""SpeakerPhoneNumber"",""SpeakerTwitterHandle"",""SpeakerTitle"",""SpeakerWebsite"",""SpeakerBlog"",""TShirtSize"",""Audience"",""Category"",""Level"",""Prerequisites"",""Approved"",""Abstract""");
Expand Down
16 changes: 16 additions & 0 deletions src/CoreCodeCamp/Authentication/CoreCampAuthSchemes.cs
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;
}
}
16 changes: 16 additions & 0 deletions src/CoreCodeCamp/Authentication/CoreCampTokenOptions.cs
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;
}
}
62 changes: 62 additions & 0 deletions src/CoreCodeCamp/Authentication/JwtTokenFactory.cs
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
};
}
}
}
14 changes: 7 additions & 7 deletions src/CoreCodeCamp/Controllers/Api/EventsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public EventsController(ICodeCampRepository repo, ILogger<EventsController> logg

[HttpGet("")]
[AllowAnonymous]
public IActionResult GetEvents()
public async Task<IActionResult> GetEvents()
{
try
{
var events = _repo.GetAllEventInfo()
var events = (await _repo.GetAllEventInfoAsync())
.OrderByDescending(e => e.Moniker)
.ToArray();

Expand All @@ -51,11 +51,11 @@ public IActionResult GetEvents()

[HttpGet("{moniker}")]
[AllowAnonymous]
public IActionResult GetEvent(string moniker)
public async Task<IActionResult> GetEvent(string moniker)
{
try
{
var info = _repo.GetAllEventInfo()
var info = (await _repo.GetAllEventInfoAsync())
.Where(e => e.Moniker == moniker)
.FirstOrDefault();

Expand All @@ -76,7 +76,7 @@ public async Task<IActionResult> Insert(string moniker, [FromBody]EventInfoViewM
{

if (moniker != vm.Moniker) return BadRequest("Wrong Event with Moniker");
var info = _repo.GetEventInfo(vm.Moniker);
var info = await _repo.GetEventInfoAsync(vm.Moniker);
if (info != null)
{
return BadRequest("Cannot add duplicate moniker");
Expand Down Expand Up @@ -109,7 +109,7 @@ public async Task<IActionResult> Update(string moniker, [FromBody]EventInfoViewM
if (ModelState.IsValid)
{
if (moniker != vm.Moniker) return BadRequest("Wrong Event with Moniker");
var info = _repo.GetEventInfo(moniker);
var info = await _repo.GetEventInfoAsync(moniker);
if (info == null)
{
return BadRequest("Cannot add update new event");
Expand Down Expand Up @@ -139,7 +139,7 @@ public async Task<IActionResult> UpdateLocation(string moniker, [FromBody]EventL
{
try
{
var info = _repo.GetEventInfo(moniker);
var info = await _repo.GetEventInfoAsync(moniker);
if (info == null)
{
return BadRequest("Cannot update a location on a missing event");
Expand Down
57 changes: 52 additions & 5 deletions src/CoreCodeCamp/Controllers/Api/MeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,81 @@
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using CoreCodeCamp.Authentication;
using CoreCodeCamp.Data;
using CoreCodeCamp.Data.Entities;
using CoreCodeCamp.Models;
using CoreCodeCamp.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.V4.Pages.Account.Internal;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace CoreCodeCamp.Controllers.Api
{
[Route("{moniker}/api/me")]
[Authorize]
[Authorize(AuthenticationSchemes = CoreCampAuthSchemes.AnyAuthScheme)]
[ApiController]
public class MeController : Controller
{
private ILogger<MeController> _logger;
private readonly IMapper _mapper;
private readonly UserManager<CodeCampUser> _userManager;
private readonly SignInManager<CodeCampUser> _signInManager;
private readonly CoreCodeCampTokenFactory _tokenFactory;
private ICodeCampRepository _repo;

public MeController(ICodeCampRepository repo, ILogger<MeController> logger, IMapper mapper)
public MeController(ICodeCampRepository repo,
ILogger<MeController> logger,
IMapper mapper,
UserManager<CodeCampUser> userManager,
SignInManager<CodeCampUser> signInManager,
CoreCodeCampTokenFactory tokenFactory)
{
_repo = repo;
_logger = logger;
_mapper = mapper;
_userManager = userManager;
_signInManager = signInManager;
_tokenFactory = tokenFactory;
}

[AllowAnonymous]
[HttpPost("token")]
public async Task<IActionResult> CreateToken(TokenRequestModel model)
{
try
{
// Allow by username or email
var user = await _userManager.FindByNameAsync(model.Username);
if (user == null) user = await _userManager.FindByEmailAsync(model.Username);

if (user == null) return BadRequest("Invalid User");

// Check Password
if ((await _signInManager.CheckPasswordSignInAsync(user, model.Password, false)) == Microsoft.AspNetCore.Identity.SignInResult.Success)
{
// Generate the token
var token = await _tokenFactory.GenerateForUser(user);

return Created("", token);
}
}
catch (Exception ex)
{
_logger.LogError($"Failed to login for Token: {ex}");
}

return BadRequest();

}

[HttpGet("favorites")]
public IActionResult GetFavorites(string moniker)
public async Task<IActionResult> GetFavorites(string moniker)
{
var favs = _repo.GetUserWithFavoriteTalksForEvent(User.Identity.Name, moniker);
var favs = await _repo.GetUserWithFavoriteTalksForEventAsync(User.Identity.Name, moniker);
return Ok(_mapper.Map<IEnumerable<FavoriteTalkViewModel>>(favs));
}

Expand All @@ -40,7 +87,7 @@ public async Task<IActionResult> ToggleStar(string moniker, int talkId)
{
try
{
var state = _repo.ToggleTalkForUser(moniker, User.Identity.Name, talkId);
var state = await _repo.ToggleTalkForUserAsync(moniker, User.Identity.Name, talkId);
await _repo.SaveChangesAsync();
return Ok(state);
}
Expand Down
10 changes: 5 additions & 5 deletions src/CoreCodeCamp/Controllers/Api/RoomsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public RoomsController(ICodeCampRepository repo, ILogger<RoomsController> logger
}

[HttpGet()]
public IActionResult Get(string moniker)
public async Task<IActionResult> Get(string moniker)
{
return Ok(_repo.GetRooms(moniker));
return Ok(await _repo.GetRoomsAsync(moniker));
}

[HttpPost()]
Expand All @@ -39,7 +39,7 @@ public async Task<IActionResult> Create(string moniker, [FromBody]Room model)
{
try
{
var eventInfo = _repo.GetEventInfo(moniker);
var eventInfo = await _repo.GetEventInfoAsync(moniker);
if (eventInfo != null)
{
model.Event = eventInfo;
Expand All @@ -63,7 +63,7 @@ public async Task<IActionResult> Update(string moniker, int id, [FromBody] Room
{
try
{
var room = _repo.GetRoom(moniker, id);
var room = await _repo.GetRoomAsync(moniker, id);
if (room.Id != id || string.IsNullOrWhiteSpace(model.Name)) return BadRequest();
room.Name = model.Name;
await _repo.SaveChangesAsync();
Expand All @@ -82,7 +82,7 @@ public async Task<IActionResult> Delete(string moniker, int id)
{
try
{
var room = _repo.GetRoom(moniker, id);
var room = await _repo.GetRoomAsync(moniker, id);
_repo.Delete(room);
await _repo.SaveChangesAsync();

Expand Down
18 changes: 9 additions & 9 deletions src/CoreCodeCamp/Controllers/Api/SpeakersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public SpeakersController(ICodeCampRepository repo,
}

[HttpGet("")]
public ActionResult<IEnumerable<Speaker>> Get(string moniker)
public async Task<ActionResult<IEnumerable<Speaker>>> Get(string moniker)
{
try
{
return Ok(_mapper.Map<IEnumerable<SpeakerViewModel>>(_repo.GetSpeakers(moniker)));
return Ok(_mapper.Map<IEnumerable<SpeakerViewModel>>(await _repo.GetSpeakersAsync(moniker)));
}
catch (Exception ex)
{
Expand All @@ -54,11 +54,11 @@ public ActionResult<IEnumerable<Speaker>> Get(string moniker)

[HttpGet("me")]
[Authorize]
public ActionResult<SpeakerViewModel> GetCurrent(string moniker)
public async Task<ActionResult<SpeakerViewModel>> GetCurrent(string moniker)
{
try
{
var speaker = _repo.GetSpeakerForCurrentUser(moniker, User.Identity.Name);
var speaker = await _repo.GetSpeakerForCurrentUserAsync(moniker, User.Identity.Name);
if (speaker == null)
{
speaker = new Speaker()
Expand All @@ -78,11 +78,11 @@ public ActionResult<SpeakerViewModel> GetCurrent(string moniker)
}

[HttpGet("{id:int}")]
public IActionResult Get(string moniker, int id)
public async Task<IActionResult> Get(string moniker, int id)
{
try
{
return Ok(_mapper.Map<SpeakerViewModel>(_repo.GetSpeaker(id)));
return Ok(_mapper.Map<SpeakerViewModel>(await _repo.GetSpeakerAsync(id)));
}
catch (Exception ex)
{
Expand All @@ -96,14 +96,14 @@ public IActionResult Get(string moniker, int id)
[Authorize]
public async Task<IActionResult> UpsertMySpeaker(string moniker, [FromBody]SpeakerViewModel model)
{
return await UpsertSpeaker(model, _repo.GetSpeakerForCurrentUser(moniker, User.Identity.Name), moniker, User.Identity.Name);
return await UpsertSpeaker(model, await _repo.GetSpeakerForCurrentUserAsync(moniker, User.Identity.Name), moniker, User.Identity.Name);
}

[HttpPost("")]
[Authorize]
public async Task<IActionResult> UpsertSpeaker(string moniker, [FromBody]SpeakerViewModel model)
{
return await UpsertSpeaker(model, _repo.GetSpeakerByName(moniker, model.Name), moniker, User.Identity.Name);
return await UpsertSpeaker(model, await _repo.GetSpeakerByNameAsync(moniker, model.Name), moniker, User.Identity.Name);
}

async Task<IActionResult> UpsertSpeaker(SpeakerViewModel model, Speaker speaker, string moniker, string userName)
Expand All @@ -116,7 +116,7 @@ async Task<IActionResult> UpsertSpeaker(SpeakerViewModel model, Speaker speaker,
{
speaker = _mapper.Map<Speaker>(model);
speaker.UserName = userName;
speaker.Event = _repo.GetEventInfo(moniker);
speaker.Event = await _repo.GetEventInfoAsync(moniker);

// Send confirmation email on new speaker
var user = await _userMgr.FindByNameAsync(userName);
Expand Down
Loading

0 comments on commit f2567a7

Please sign in to comment.