Skip to content

Commit

Permalink
Merge pull request #5 from marekrydlewski/chess-notation
Browse files Browse the repository at this point in the history
Chess notation
  • Loading branch information
marekrydlewski committed May 26, 2016
2 parents 52c3a3d + 964f89b commit 1bc3c46
Show file tree
Hide file tree
Showing 122 changed files with 3,682 additions and 152 deletions.
6 changes: 6 additions & 0 deletions RabinChess.Server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,9 @@ FakesAssemblies/
GeneratedArtifacts/
_Pvt_Extensions/
ModelManifest.xml

# Config files
**/connectionStrings.config

# Publish profiles
RabinChess.Server.API/Properties/PublishProfiles/
51 changes: 51 additions & 0 deletions RabinChess.Server/RabinChess.Server.API/App_Start/Startup.Auth.cs
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);
}
}
}
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;
}
}
}
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();
}
}
}

This file was deleted.

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 RabinChess.Server/RabinChess.Server.API/Models/UserModel.cs
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
};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.Owin;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
Expand Down Expand Up @@ -33,3 +34,6 @@
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

// Additional assemblies
[assembly: OwinStartup(typeof(RabinChess.Server.API.Startup))]
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);
}
}
}
Loading

0 comments on commit 1bc3c46

Please sign in to comment.