Backend for the Teso project written in 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.9 KiB

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using Microsoft.Net.Http.Headers;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Teso_API.Methods;
using Teso_API.Models;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace Teso_API.Controllers
{
[AllowAnonymous]
[ApiController]
public class StatusController : ControllerBase
{
readonly ITokenService tokenService;
public StatusController( ITokenService tokenService)
{
this.tokenService = tokenService ?? throw new ArgumentNullException(nameof(tokenService));
}
// GET: api/<StatusController>
[Authorize]
[HttpGet, Route("serverstatus")]
public ActionResult Get()
{
var accessToken = Request.Headers[HeaderNames.Authorization];
string token = accessToken;
token = token.Substring(6).Trim();
var handler = new JwtSecurityTokenHandler();
var jwtToken = handler.ReadToken(token) as JwtSecurityToken;
string userID = jwtToken.Claims.First(claim => claim.Type == "userGUID").Value;
string Username = jwtToken.Claims.First(claim => claim.Type == "username").Value;
string DeviceToken = jwtToken.Claims.First(claim => claim.Type == "deviceToken").Value;
//return Ok("Developed by Bacware Tech");
int timestamp_issued = (int)new DateTimeOffset(DateTimeOffset.UtcNow.DateTime).ToUnixTimeMilliseconds();
int timestamp_expires = (int)new DateTimeOffset(DateTimeOffset.UtcNow.DateTime.AddDays(14)).ToUnixTimeMilliseconds();
int timestamp_issuednbf = (int)new DateTimeOffset(DateTimeOffset.UtcNow.DateTime.AddMinutes(1)).ToUnixTimeMilliseconds();
//create claims details based on the user information
var claims = new[] {
new Claim(JwtRegisteredClaimNames.Iat, timestamp_issued.ToString()),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(JwtRegisteredClaimNames.Iss, ServerLocation.issuer),
new Claim(JwtRegisteredClaimNames.Exp, timestamp_expires.ToString()),
new Claim(JwtRegisteredClaimNames.Nbf, timestamp_issuednbf.ToString()),
new Claim(JwtRegisteredClaimNames.Sub, userID.ToString()),
new Claim("username", Username.ToString()),
new Claim("userGUID", userID.ToString()),
new Claim("deviceToken", DeviceToken),
};
string newtoken = this.tokenService.GenerateAccessToken(claims);
return Ok(newtoken);
}
}
}