public string GetToken([FromBody] string code, [FromBody] string clientId, [FromBody] string clientSecret, [FromBody] string redirectUri, [FromBody] string grantType, [FromBody] string state) { if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret) || string.IsNullOrEmpty(redirectUri) || string.IsNullOrEmpty(code) || string.IsNullOrEmpty(grantType)) { throw new HttpException(400, BuildInvalidRquestMessage(code, clientId, clientSecret, redirectUri, grantType)); } if (grantType != "authorization_code") { throw new HttpException(400, "Unsupported Grant Type"); } OAuthEntity registeredApp; try { registeredApp = _service.Get(clientId, clientSecret); } catch { throw new HttpException(500, "Server Error"); } if (registeredApp == null) { throw new HttpException(403, "Unauthorized Client"); } if (registeredApp.Code != code) { throw new HttpException(400, "Invalid Grant"); } var accessToken = ShortGuid.NewGuid().ToString(); const string tokenType = "Bearer"; var expiresIn = TimeSpan.FromHours(1).TotalSeconds; var token = new TokenEntity(clientId, accessToken, tokenType, expiresIn); _service.CreateToken(token); var tokenUri = string.Format("{0}#access_token={1}&token_type={2}&expires_in={3}{4}", redirectUri, accessToken, tokenType, expiresIn, string.IsNullOrEmpty(state) ? "" : "&state=" + state); return tokenUri; }
/// <summary> /// Create an OAuth token. /// </summary> /// <param name="token">Token to create</param> public void CreateToken(TokenEntity token) { _repository.CreateToken(token); _unitOfWork.SubmitChanges(); }
/// <summary> /// Creates an entity in the OAuthTokens Azure Table. /// </summary> /// <param name="token">TokenEntity to be created</param> public void CreateToken(TokenEntity token) { _unitOfWork.Create(token, "OAuthTokens"); }