-
Notifications
You must be signed in to change notification settings - Fork 0
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 #5 from marekrydlewski/chess-notation
Chess notation
- Loading branch information
Showing
122 changed files
with
3,682 additions
and
152 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
51 changes: 51 additions & 0 deletions
51
RabinChess.Server/RabinChess.Server.API/App_Start/Startup.Auth.cs
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,51 @@ | ||
using System; | ||
using Microsoft.AspNet.Identity; | ||
using Microsoft.Owin; | ||
using Microsoft.Owin.Cors; | ||
using Microsoft.Owin.Security.Cookies; | ||
using Microsoft.Owin.Security.OAuth; | ||
using Owin; | ||
using RabinChess.Server.API.Models; | ||
using RabinChess.Server.API.Providers; | ||
using RabinChess.Server.API.Stores; | ||
|
||
namespace RabinChess.Server.API | ||
{ | ||
public partial class Startup | ||
{ | ||
public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; } | ||
|
||
public static string PublicClientId { get; private set; } | ||
|
||
public static Func<UserManager<UserModel, int>> UserManagerFactory { get; private set; } | ||
|
||
static Startup() | ||
{ | ||
PublicClientId = "self"; | ||
|
||
UserManagerFactory = | ||
() => new UserManager<UserModel, int>(new UserStore()) {PasswordHasher = new Security.PasswordHasher()}; | ||
|
||
OAuthOptions = new OAuthAuthorizationServerOptions | ||
{ | ||
TokenEndpointPath = new PathString("/api/token"), | ||
Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory), | ||
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14), | ||
AllowInsecureHttp = true | ||
}; | ||
} | ||
|
||
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864 | ||
public void ConfigureAuth(IAppBuilder app) | ||
{ | ||
app.UseCors(CorsOptions.AllowAll); | ||
// Enable the application to use a cookie to store information for the signed in user | ||
// and to use a cookie to temporarily store information about a user logging in with a third party login provider | ||
app.UseCookieAuthentication(new CookieAuthenticationOptions()); | ||
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); | ||
|
||
// Enable the application to use bearer tokens to authenticate users | ||
app.UseOAuthBearerTokens(OAuthOptions); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
RabinChess.Server/RabinChess.Server.API/Controllers/AccountController.cs
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,79 @@ | ||
using System.Net.Http; | ||
using System.Web.Http; | ||
using Microsoft.AspNet.Identity; | ||
using Microsoft.Owin.Security; | ||
using Microsoft.Owin.Security.Cookies; | ||
using RabinChess.Server.API.Models; | ||
|
||
namespace RabinChess.Server.API.Controllers | ||
{ | ||
[RoutePrefix("api/Account")] | ||
public class AccountController : ApiController | ||
{ | ||
public UserManager<UserModel, int> UserManager { get; private set; } | ||
public ISecureDataFormat<AuthenticationTicket> AccessTokenFormat { get; private set; } | ||
|
||
public AccountController() : this(Startup.UserManagerFactory(), Startup.OAuthOptions.AccessTokenFormat) | ||
{ | ||
} | ||
|
||
public AccountController(UserManager<UserModel, int> userManager, | ||
ISecureDataFormat<AuthenticationTicket> accessTokenFormat) | ||
{ | ||
UserManager = userManager; | ||
AccessTokenFormat = accessTokenFormat; | ||
} | ||
|
||
[HttpPost] | ||
[Route("Logout")] | ||
public IHttpActionResult Logout() | ||
{ | ||
Authentication.SignOut(CookieAuthenticationDefaults.AuthenticationType); | ||
return Ok(); | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
UserManager.Dispose(); | ||
} | ||
|
||
base.Dispose(disposing); | ||
} | ||
|
||
private IAuthenticationManager Authentication | ||
{ | ||
get { return Request.GetOwinContext().Authentication; } | ||
} | ||
|
||
private IHttpActionResult GetErrorResult(IdentityResult result) | ||
{ | ||
if (result == null) | ||
{ | ||
return InternalServerError(); | ||
} | ||
|
||
if (!result.Succeeded) | ||
{ | ||
if (result.Errors != null) | ||
{ | ||
foreach (string error in result.Errors) | ||
{ | ||
ModelState.AddModelError("", error); | ||
} | ||
} | ||
|
||
if (ModelState.IsValid) | ||
{ | ||
// No ModelState errors are available to send, so just return an empty BadRequest. | ||
return BadRequest(); | ||
} | ||
|
||
return BadRequest(ModelState); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
RabinChess.Server/RabinChess.Server.API/Controllers/GamesController.cs
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,20 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Web.Http; | ||
using Microsoft.AspNet.Identity; | ||
using RabinChess.Server.API.Models; | ||
using RubinChess.Server.Logic; | ||
|
||
namespace RabinChess.Server.API.Controllers | ||
{ | ||
[Route("api/Games")] | ||
public class GamesController : ApiController | ||
{ | ||
[Route("api/Games")] | ||
[HttpGet] | ||
public List<GameListItemViewModel> Get() | ||
{ | ||
return ContextFactory.GetGamesContext().GetGames(User.Identity.GetUserId<int>()).Select(x => (GameListItemViewModel) x).ToList(); | ||
} | ||
} | ||
} |
39 changes: 0 additions & 39 deletions
39
RabinChess.Server/RabinChess.Server.API/Controllers/SampleController.cs
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
RabinChess.Server/RabinChess.Server.API/Models/GameListItemViewModel.cs
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,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using RabinChess.Server.DataStructures; | ||
|
||
namespace RabinChess.Server.API.Models | ||
{ | ||
public class GameListItemViewModel | ||
{ | ||
public string Name { get; set; } | ||
public string Tags { get; set; } | ||
public Guid Id { get; set; } | ||
|
||
public static explicit operator GameListItemViewModel(GameListItemVM model) | ||
{ | ||
return new GameListItemViewModel | ||
{ | ||
Id = model.Id, | ||
Name = model.Name, | ||
Tags = model.Tags | ||
}; | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
RabinChess.Server/RabinChess.Server.API/Models/UserModel.cs
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,41 @@ | ||
using Microsoft.AspNet.Identity; | ||
using RubinChess.Server.Database.Entities; | ||
|
||
namespace RabinChess.Server.API.Models | ||
{ | ||
public class UserModel : IUser<int> | ||
{ | ||
public int Id { get; private set; } | ||
public string UserName { get; set; } | ||
public string PasswordHash { get; set; } | ||
public string Email { get; set; } | ||
public string FirstName { get; set; } | ||
public string LastName { get; set; } | ||
|
||
public static explicit operator UserModel(User user) | ||
{ | ||
return new UserModel | ||
{ | ||
Email = user.Email, | ||
FirstName = user.FirstName, | ||
LastName = user.LastName, | ||
Id = user.Id, | ||
PasswordHash = user.PasswordHash, | ||
UserName = user.UserName | ||
}; | ||
} | ||
|
||
public static explicit operator User(UserModel user) | ||
{ | ||
return new User | ||
{ | ||
Email = user.Email, | ||
FirstName = user.FirstName, | ||
LastName = user.LastName, | ||
Id = user.Id, | ||
PasswordHash = user.PasswordHash, | ||
UserName = user.UserName | ||
}; | ||
} | ||
} | ||
} |
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
102 changes: 102 additions & 0 deletions
102
RabinChess.Server/RabinChess.Server.API/Providers/ApplicationOAuthProvider.cs
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,102 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNet.Identity; | ||
using Microsoft.Owin.Security; | ||
using Microsoft.Owin.Security.Cookies; | ||
using Microsoft.Owin.Security.OAuth; | ||
using RabinChess.Server.API.Models; | ||
|
||
namespace RabinChess.Server.API.Providers | ||
{ | ||
public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider | ||
{ | ||
private readonly string _publicClientId; | ||
private readonly Func<UserManager<UserModel, int>> _userManagerFactory; | ||
|
||
public ApplicationOAuthProvider(string publicClientId, Func<UserManager<UserModel, int>> userManagerFactory) | ||
{ | ||
if (publicClientId == null) | ||
{ | ||
throw new ArgumentNullException("publicClientId"); | ||
} | ||
|
||
if (userManagerFactory == null) | ||
{ | ||
throw new ArgumentNullException("userManagerFactory"); | ||
} | ||
|
||
_publicClientId = publicClientId; | ||
_userManagerFactory = userManagerFactory; | ||
} | ||
|
||
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) | ||
{ | ||
using (UserManager<UserModel, int> userManager = _userManagerFactory()) | ||
{ | ||
UserModel user = await userManager.FindAsync(context.UserName, context.Password); | ||
|
||
if (user == null) | ||
{ | ||
context.SetError("invalid_grant", "The user name or password is incorrect."); | ||
return; | ||
} | ||
|
||
ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user, | ||
context.Options.AuthenticationType); | ||
ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user, | ||
CookieAuthenticationDefaults.AuthenticationType); | ||
AuthenticationProperties properties = CreateProperties(user.UserName); | ||
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); | ||
context.Validated(ticket); | ||
context.Request.Context.Authentication.SignIn(cookiesIdentity); | ||
} | ||
} | ||
|
||
public override Task TokenEndpoint(OAuthTokenEndpointContext context) | ||
{ | ||
foreach (KeyValuePair<string, string> property in context.Properties.Dictionary) | ||
{ | ||
context.AdditionalResponseParameters.Add(property.Key, property.Value); | ||
} | ||
|
||
return Task.FromResult<object>(null); | ||
} | ||
|
||
public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) | ||
{ | ||
// Resource owner password credentials does not provide a client ID. | ||
if (context.ClientId == null) | ||
{ | ||
context.Validated(); | ||
} | ||
|
||
return Task.FromResult<object>(null); | ||
} | ||
|
||
public override Task ValidateClientRedirectUri(OAuthValidateClientRedirectUriContext context) | ||
{ | ||
if (context.ClientId == _publicClientId) | ||
{ | ||
Uri expectedRootUri = new Uri(context.Request.Uri, "/"); | ||
|
||
if (expectedRootUri.AbsoluteUri == context.RedirectUri) | ||
{ | ||
context.Validated(); | ||
} | ||
} | ||
|
||
return Task.FromResult<object>(null); | ||
} | ||
|
||
public static AuthenticationProperties CreateProperties(string userName) | ||
{ | ||
IDictionary<string, string> data = new Dictionary<string, string> | ||
{ | ||
{ "userName", userName } | ||
}; | ||
return new AuthenticationProperties(data); | ||
} | ||
} | ||
} |
Oops, something went wrong.