summaryrefslogtreecommitdiff
path: root/Project-Unite/Controllers
diff options
context:
space:
mode:
authorMichael <[email protected]>2017-04-30 10:55:47 -0400
committerMichael <[email protected]>2017-04-30 10:55:47 -0400
commitc9716799cc49c24d691a612712bdbd860e46e8b6 (patch)
treef1385413e902da6d35dab806de47ee19f504cb7f /Project-Unite/Controllers
parent3d7126efd77deadac5f6be00c18e06eb62be49e3 (diff)
downloadproject-unite-c9716799cc49c24d691a612712bdbd860e46e8b6.tar.gz
project-unite-c9716799cc49c24d691a612712bdbd860e46e8b6.tar.bz2
project-unite-c9716799cc49c24d691a612712bdbd860e46e8b6.zip
this shouldn't be a POST
Diffstat (limited to 'Project-Unite/Controllers')
-rw-r--r--Project-Unite/Controllers/OAuth2Controller.cs81
1 files changed, 78 insertions, 3 deletions
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<ApplicationSignInManager>();
+ }
+ private set
+ {
+ _signInManager = value;
+ }
+ }
+
+ public ApplicationUserManager UserManager
+ {
+ get
+ {
+ return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
+ }
+ private set
+ {
+ _userManager = value;
+ }
+ }
+
+
+ [AllowAnonymous]
+ public async Task<ActionResult> 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