diff --git a/Project-Unite/Controllers/OAuth2Controller.cs b/Project-Unite/Controllers/OAuth2Controller.cs index 39aa75c..f8bc0fd 100644 --- a/Project-Unite/Controllers/OAuth2Controller.cs +++ b/Project-Unite/Controllers/OAuth2Controller.cs @@ -1,17 +1,92 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; using System.Web; using System.Web.Mvc; +using Project_Unite.Models; namespace Project_Unite.Controllers { public class OAuth2Controller : Controller { - // GET: OAuth2 - public ActionResult Index() + private ApplicationSignInManager _signInManager = null; + private ApplicationUserManager _userManager = null; + + public ApplicationSignInManager SignInManager { - return View(); + get + { + return _signInManager ?? HttpContext.GetOwinContext().Get(); + } + private set + { + _signInManager = value; + } + } + + public ApplicationUserManager UserManager + { + get + { + return _userManager ?? HttpContext.GetOwinContext().GetUserManager(); + } + private set + { + _userManager = value; + } + } + + + [AllowAnonymous] + public async Task Login(string appname, string appdesc, string version) + { + try + { + string authHeader = Request.Headers["Authentication"]; + string b64_auth = authHeader.Remove(0, 6); //get rid of the "Basic " text. + byte[] data = Convert.FromBase64String(b64_auth); + string plaintext = Encoding.UTF8.GetString(data); + string[] split = plaintext.Split(':'); + string username = split[0]; + string password = split[1]; + var result = await SignInManager.PasswordSignInAsync(username, password, false, false); + if(result == Microsoft.AspNet.Identity.Owin.SignInStatus.Success) + { + var db = new ApplicationDbContext(); + var user = db.Users.FirstOrDefault(x => x.UserName == username); + var auth_token = db.OAuthTokens.Where(x => x.UserId == user.Id).FirstOrDefault(x => x.AppName == appname && x.AppDescription == appdesc && x.Version == version); + if(auth_token == null) + { + auth_token = new Models.OAuthToken + { + Id = Guid.NewGuid().ToString(), + UserId = user.Id, + AppName = appname, + AppDescription = appdesc, + Version = version + }; + db.OAuthTokens.Add(auth_token); + db.SaveChanges(); + return Content(auth_token.Id); + } + else + { + return Content(auth_token.Id); + } + } + else + { + return new HttpStatusCodeResult(403); + } + } + catch + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + } } } \ No newline at end of file diff --git a/Project-Unite/Models/IdentityModels.cs b/Project-Unite/Models/IdentityModels.cs index f864527..e11d675 100644 --- a/Project-Unite/Models/IdentityModels.cs +++ b/Project-Unite/Models/IdentityModels.cs @@ -247,8 +247,18 @@ public static ApplicationDbContext Create() public DbSet ForumPosts { get; set; } public DbSet Stories { get; set; } public DbSet Views { get; set; } + public DbSet OAuthTokens { get; set; } } + public class OAuthToken + { + public string Id { get; set; } + public string UserId { get; set; } + public string AppName { get; set; } + public string AppDescription { get; set; } + public string Version { get; set; } + } + public class ReadPost { public string Id { get; set; }