-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
azure storage integration + stripe integration
- Loading branch information
Showing
20 changed files
with
881 additions
and
31 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System.Security.Claims; | ||
using MicrobotApi.Database; | ||
using MicrobotApi.Services; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace MicrobotApi.Controllers; | ||
|
||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class AuthController : Controller | ||
{ | ||
private readonly IConfiguration _configuration; | ||
private readonly DiscordService _discordService; | ||
private readonly MicrobotContext _microbotContext; | ||
|
||
public AuthController(IConfiguration configuration, DiscordService discordService, MicrobotContext microbotContext) | ||
{ | ||
_configuration = configuration; | ||
_discordService = discordService; | ||
_microbotContext = microbotContext; | ||
} | ||
|
||
[HttpGet("discord/user")] | ||
public async Task<IActionResult> DiscordUserInfo([FromQuery] String code) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(code)) | ||
{ | ||
var clientId = _configuration["Discord:ClientId"] ?? string.Empty; | ||
var clientSecret = _configuration["Discord:ClientSecret"] ?? string.Empty; | ||
var redirectUri = _configuration["Discord:RedirectUri"] ?? string.Empty; | ||
var tokenResponse = await _discordService.GetToken(clientId, clientSecret, code, redirectUri); | ||
|
||
if (tokenResponse == null) | ||
return BadRequest("Invalid code!"); | ||
|
||
var userInfo = await _discordService.GetUserInfo(tokenResponse.Access_Token); | ||
|
||
if (userInfo == null) | ||
return BadRequest("userinfo is empty"); | ||
|
||
var discordUser = await _microbotContext.DiscordUsers.FirstOrDefaultAsync(x => x.DiscordId == userInfo.Id); | ||
|
||
if (discordUser == null) | ||
{ | ||
_microbotContext.Users.Add(new DiscordUser() | ||
{ | ||
DiscordId = userInfo.Id, | ||
Token = tokenResponse.Access_Token, | ||
RefreshToken = tokenResponse.Refresh_Token, | ||
TokenExpiry = DateTime.UtcNow.AddSeconds(tokenResponse.Expires_In), | ||
}); | ||
await _microbotContext.SaveChangesAsync(); | ||
} | ||
|
||
return Ok(tokenResponse.Access_Token); | ||
|
||
} | ||
|
||
return BadRequest("Code is missing!"); | ||
} | ||
|
||
[HttpGet("test")] | ||
[Authorize] | ||
public async Task<IActionResult> Test() | ||
{ | ||
return Ok("hello world"); | ||
} | ||
// [HttpGet("discord/token/{userId}")] | ||
// public async Task<IActionResult> Token(string userId = "126659209642246144") | ||
// { | ||
// var discordUser = await _microbotContext.DiscordUsers.FirstOrDefaultAsync(x => x.DiscordId == userId); | ||
// | ||
// if (discordUser == null) | ||
// return BadRequest("User not found"); | ||
// | ||
// if (discordUser.TokenExpiry < DateTime.UtcNow) | ||
// return Ok(discordUser.Token); | ||
// | ||
// var clientId = _configuration["Discord:ClientId"]; | ||
// var clientSecret = _configuration["Discord:ClientSecret"]; | ||
// var redirectUri = _configuration["Discord:RedirectUri"]; | ||
// | ||
// var token = await _discordService.RefreshAccessToken(clientId, clientSecret, discordUser.RefreshToken, redirectUri); | ||
// | ||
// if (string.IsNullOrWhiteSpace(token)) | ||
// return BadRequest("Invalid code!"); | ||
// | ||
// return Ok(token); | ||
// | ||
// } | ||
|
||
[HttpGet("userinfo")] | ||
[Authorize] | ||
public async Task<IActionResult> UserInfo() | ||
{ | ||
var userId = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value; | ||
|
||
var discordUser = await _microbotContext.DiscordUsers.FirstOrDefaultAsync(x => x.DiscordId == userId); | ||
|
||
if (discordUser != null) | ||
{ | ||
var userInfo = await _discordService.GetUserInfo(discordUser.Token); | ||
|
||
return Ok(userInfo); | ||
} | ||
|
||
|
||
return NotFound("User not found."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Stripe; | ||
using Stripe.Checkout; | ||
using Stripe.Identity; | ||
|
||
namespace MicrobotApi.Controllers; | ||
|
||
[Route("create-checkout-session")] | ||
[ApiController] | ||
public class CheckoutApiController : Controller | ||
{ | ||
private readonly ILogger<CheckoutApiController> _logger; | ||
private readonly IConfiguration _configuration; | ||
|
||
public CheckoutApiController(ILogger<CheckoutApiController> logger, IConfiguration configuration) | ||
{ | ||
_logger = logger; | ||
_configuration = configuration; | ||
} | ||
|
||
[HttpPost] | ||
[Authorize] | ||
public ActionResult Create([FromBody] CreateCheckOutRequest createCheckOutRequest) | ||
{ | ||
var domain = _configuration["Discord:RedirectUri"]; | ||
var options = new SessionCreateOptions | ||
{ | ||
LineItems = new List<SessionLineItemOptions> | ||
{ | ||
new() | ||
{ | ||
// Provide the exact Price ID (for example, pr_1234) of the product you want to sell | ||
Price = _configuration["Stripe:PriceSecret"], | ||
Quantity = 1, | ||
}, | ||
}, | ||
Metadata = new Dictionary<string, string> | ||
{ | ||
{ "userId", createCheckOutRequest.UserId } // Add user ID as metadata | ||
}, | ||
Mode = "payment", | ||
SuccessUrl = domain + "/success", | ||
CancelUrl = domain + "/cancel", | ||
AutomaticTax = new SessionAutomaticTaxOptions { Enabled = true }, | ||
}; | ||
var service = new SessionService(); | ||
Session session = service.Create(options); | ||
|
||
Response.Headers.Append("Location", session.Url); | ||
|
||
return Ok(session); | ||
} | ||
} | ||
|
||
public class CreateCheckOutRequest | ||
{ | ||
public string UserId { get; set; } | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using MicrobotApi.Database; | ||
using MicrobotApi.Services; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace MicrobotApi.Controllers; | ||
|
||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class FileController : Controller | ||
{ | ||
private readonly AzureStorageService _azureStorageService; | ||
private readonly MicrobotContext _microbotContext; | ||
|
||
public FileController(AzureStorageService azureStorageService, MicrobotContext microbotContext) | ||
{ | ||
_azureStorageService = azureStorageService; | ||
_microbotContext = microbotContext; | ||
} | ||
|
||
[Authorize] | ||
[HttpGet("download/{blobName}/{key}/{hwid}")] | ||
public async Task<IActionResult> Download(string blobName, string key, string hwid) | ||
{ | ||
var exists = await _microbotContext.Keys.AnyAsync(x => x.Key == key && x.HWID == hwid); | ||
|
||
if (!exists) | ||
return Unauthorized(); | ||
|
||
var file = await _azureStorageService.DownloadFile(blobName); | ||
|
||
return File(file.Value.Content, "application/octet-stream", blobName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using MicrobotApi.Database; | ||
using MicrobotApi.Extensions; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace MicrobotApi.Controllers; | ||
|
||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class ScriptKeysController(MicrobotContext microbotContext) : Controller | ||
{ | ||
[HttpPost] | ||
[Authorize] | ||
public async Task<IActionResult> Create([FromBody] HmacRequest request) | ||
{ | ||
var key = await microbotContext.Keys.FirstOrDefaultAsync(x => x.Key == request.Key); | ||
|
||
if (key == null) | ||
return NotFound("Key not found!"); | ||
|
||
key.Active = true; | ||
key.HWID = request.HWID; | ||
|
||
await microbotContext.SaveChangesAsync(); | ||
|
||
return Ok(); | ||
} | ||
|
||
[HttpGet] | ||
[Authorize] | ||
public async Task<IActionResult> Get() | ||
{ | ||
var keys = await microbotContext.DiscordUsers | ||
.Include(x => x.Keys) | ||
.Where(x => x.DiscordId == User.GetUserId()) | ||
.Select(x => x.Keys) | ||
.FirstOrDefaultAsync(); | ||
|
||
return Ok(keys); | ||
} | ||
|
||
public class HmacRequest | ||
{ | ||
public string Key { get; set; } | ||
public string HWID { get; set; } | ||
} | ||
} |
Oops, something went wrong.