using System;
using System.Collections.Generic;
using Crestron.SimplSharp.CrestronAuthentication;
using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json;
using PepperDash.Core.Web.RequestHandlers;
namespace PepperDash.Essentials.Core.Web.RequestHandlers
{
///
/// Represents a LoginRequestHandler
///
public class LoginRequestHandler : WebApiBaseRequestHandler
{
///
/// Constructor
///
///
/// base(true) enables CORS support by default
///
public LoginRequestHandler()
: base(true)
{
}
///
/// Handles POST method requests for user login and token generation
///
/// The HTTP context for the request.
protected override void HandlePost(HttpCwsContext context)
{
try
{
if (context.Request.ContentLength < 0)
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End();
return;
}
var data = context.Request.GetRequestBody();
if (string.IsNullOrEmpty(data))
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End();
return;
}
var loginRequest = JsonConvert.DeserializeObject(data);
if (loginRequest == null || string.IsNullOrEmpty(loginRequest.Username) || string.IsNullOrEmpty(loginRequest.Password))
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End();
return;
}
Authentication.UserToken token;
try
{
token = Authentication.GetAuthenticationToken(loginRequest.Username, loginRequest.Password);
}
catch (ArgumentException)
{
context.Response.StatusCode = 401;
context.Response.StatusDescription = "Bad Request";
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Write(JsonConvert.SerializeObject(new { Error = "Unauthorized" }, Formatting.Indented), false);
context.Response.End();
return;
}
if (!token.Valid)
{
context.Response.StatusCode = 401;
context.Response.StatusDescription = "Unauthorized";
context.Response.End();
return;
}
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Write(JsonConvert.SerializeObject(
new
{
Token = new LoginResponse
{
UserName = token.UserName,
Access = token.Access,
State = token.State,
Groups = token.Groups,
ADConnect = token.ADConnect,
Valid = token.Valid
}
}, Formatting.Indented), false);
context.Response.End();
}
catch (System.Exception ex)
{
context.Response.StatusCode = 500;
context.Response.StatusDescription = "Internal Server Error";
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Write(JsonConvert.SerializeObject(new { Error = ex.Message }, Formatting.Indented), false);
context.Response.End();
}
}
}
///
/// Represents a LoginRequest
///
public class LoginRequest
{
///
/// Gets or sets the username.
///
public string Username { get; set; }
///
/// Gets or sets the password.
///
public string Password { get; set; }
}
///
/// Represents a LoginResponse
///
internal class LoginResponse
{
///
/// Gets or sets the username.
///
public string UserName { get; set; }
///
/// Gets or sets the access level.
///
public Authentication.UserAuthenticationLevelEnum Access { get; set; }
///
/// Gets or sets the token authenticated state.
///
public Authentication.eTokenAuthenticatedState State { get; set; }
///
/// Gets or sets the list of groups.
///
public List Groups { get; set; }
///
/// Gets or sets the active directory connection flag.
///
public int ADConnect { get; set; }
///
/// Gets or sets the valid flag indicating whether the token is valid.
///
public bool Valid { get; set; }
}
}