Advertisement
dc_warlock

Oauth2Controller

Nov 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Cors;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Http.Extensions;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.Extensions.Caching.Memory;
  10. using Oauth2Server.Models;
  11. using OauthDemo.Utitlities;
  12.  
  13. namespace Oauth2Server.Controllers
  14. {
  15. public class Oauth2Controller : Controller
  16. {
  17. private readonly Oauth2ServerContext _context;
  18. private readonly IMemoryCache _memoryCache;
  19. private readonly string OAUTH2_COOKIE = "oauth2token";
  20.  
  21.  
  22. public Oauth2Controller(Oauth2ServerContext context, IMemoryCache memoryCache)
  23. {
  24. _context = context;
  25. _memoryCache = memoryCache;
  26. }
  27.  
  28. [HttpGet]
  29. public IActionResult CheckToken(string tokenKey, string scopes)
  30. {
  31. //return Json(new {tokenKey, scopes});
  32. var token = _context.Credential.SingleOrDefault(t => t.AccessToken == tokenKey);
  33. if (token != null && token.IsValid() && token.IsValid(scopes))
  34. {
  35. return Ok(token);
  36. }
  37. return StatusCode(403);
  38. }
  39.  
  40. [HttpGet]
  41. public IActionResult Authentication(string redirectUrl)
  42. {
  43. if (Request.Cookies.ContainsKey(OAUTH2_COOKIE))
  44. {
  45. var tokenString = Request.Cookies[OAUTH2_COOKIE];
  46. var token = _context.Credential.Find(tokenString);
  47. if (token != null && token.IsValid())
  48. {
  49. return Redirect(redirectUrl);
  50. }
  51. }
  52. LoginInformation loginInformation = new LoginInformation
  53. {
  54. RedirectUrl = redirectUrl
  55. };
  56. return View("Login", loginInformation);
  57. }
  58.  
  59. [HttpPost]
  60. public IActionResult Authentication(LoginInformation loginInformation)
  61. {
  62. if (!ModelState.IsValid)
  63. {
  64. return View("Login", loginInformation);
  65. }
  66.  
  67. Account existAccount = _context.Account.FirstOrDefault(m => m.Email == loginInformation.Email);
  68. if (existAccount == null)
  69. {
  70. return View("Login", loginInformation);
  71. }
  72.  
  73. if (PasswordHandle.GetInstance().EncryptPassword(loginInformation.Password, existAccount.Salt) != existAccount.Password)
  74. {
  75. return View("Login", loginInformation);
  76. }
  77.  
  78. Credential credential = Credential.GenerateCredential(existAccount.Id, new List<CredentialScope>() {
  79. CredentialScope.Basic
  80. });
  81. _context.Credential.Add(credential);
  82. _context.SaveChanges();
  83. Response.Cookies.Append(
  84. OAUTH2_COOKIE,
  85. credential.AccessToken,
  86. new CookieOptions()
  87. {
  88. Path = "/"
  89. }
  90. );
  91. return Redirect(loginInformation.RedirectUrl);
  92. }
  93.  
  94. [HttpGet]
  95. public IActionResult Authorization(string clientId)
  96. {
  97. string scopes = "http://basicscope.com, http://songresourcescope.com";
  98. if (Request.Cookies.ContainsKey(OAUTH2_COOKIE))
  99. {
  100. var tokenString = Request.Cookies[OAUTH2_COOKIE];
  101. var token = _context.Credential.Find(tokenString);
  102. if (token != null && token.IsValid())
  103. {
  104. var client = _context.RegisteredClient.Find(clientId);
  105. if (client == null)
  106. {
  107. return NotFound("Invalid client.");
  108. }
  109.  
  110. Dictionary<string, Oauth2Scope> scopeItems = AvailableScopes.GetOauth2Scopes(scopes);
  111. if (scopeItems.Count == 0)
  112. {
  113. return NotFound("Invalid scopes.");
  114. }
  115.  
  116. var authorizationInformation = new AuthorizationInformation
  117. {
  118. RegisteredClient = client,
  119. Oauth2Scopes = scopeItems
  120. };
  121. return View(authorizationInformation);
  122. }
  123. }
  124. return RedirectToAction("Authentication", new { redirectUrl = Request.GetDisplayUrl() });
  125. }
  126.  
  127. [HttpPost]
  128. public IActionResult GetAuthorizationExchangeCode(string clientId)
  129. {
  130. string scopes = "http://basicscope.com, http://songresourcescope.com";
  131. if (Request.Cookies.ContainsKey(OAUTH2_COOKIE))
  132. {
  133. var tokenString = Request.Cookies[OAUTH2_COOKIE];
  134. var token = _context.Credential.Find(tokenString);
  135. if (token != null && token.IsValid())
  136. {
  137. var client = _context.RegisteredClient.Find(clientId);
  138. if (client == null)
  139. {
  140. return NotFound();
  141. }
  142.  
  143. var exchange = new ExchangeToken()
  144. {
  145. ExchangeCode = Guid.NewGuid().ToString(),
  146. Credential = Credential.GenerateCredential(token.AccountId, scopes)
  147. };
  148. _memoryCache.Set(exchange.ExchangeCode, exchange,
  149. new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(180)));
  150. return Redirect("https://oauth2server20181119112903.azurewebsites.net/oauth2/exchangetoken?exchangecode=" + exchange.ExchangeCode);
  151. }
  152. }
  153. return RedirectToAction("Authentication", new { redirectUrl = Request.GetDisplayUrl() });
  154. }
  155.  
  156. [EnableCors("MyPolicy")]
  157. [HttpGet]
  158. public IActionResult ExchangeToken(string exchangeCode)
  159. {
  160. if (_memoryCache.TryGetValue(exchangeCode, out ExchangeToken exchangeToken))
  161. {
  162. _context.Credential.Add(exchangeToken.Credential);
  163. _context.SaveChanges();
  164. _memoryCache.Remove(exchangeCode);
  165. return Json(exchangeToken.Credential);
  166. }
  167. return NotFound();
  168. }
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement