<commit_before>﻿using BetaGo.Server.DataModels.Registration;
using BetaGo.Server.Services.Authentication;
using BetaGo.Server.Utilities;
using Nancy;
using Nancy.ModelBinding;
using System;
using System.Security;

namespace BetaGo.Server.Modules
{
    /// <summary>
    /// Registration functionality
    /// </summary>
    public class RegistrationModule : NancyModule
    {
        public RegistrationModule()
        {
            Post("/register", args =>
            {
                var req = this.Bind<RegistrationRequest>();

                try
                {
                    // TODO: Validate parameters!

                    // Valdiate username length, charset
                    if (req.Username.Length < 4)
                    {
                        throw new SecurityException("Username must be at least 4 characters.");
                    }
                    // Validate phone number

                    // Validate password
                    if (req.Password.Length < 8)
                    {
                        throw new SecurityException("Password must be at least 8 characters.");
                    }

                    if (!StringUtils.IsPhoneNumber(req.PhoneNumber))
                    {
                        throw new SecurityException("Phone number was of invalid format.");
                    }

                    // Validate registration
                    var newU = WebUserManager.RegisterUser(req);

                    // Return just the 200 for now
                    return Response.AsJsonNet(new RegistrationResponse
                    {
                        User = newU,
                        ApiKey = newU.ApiKey,
                    });
                }
                catch (NullReferenceException)
                {
                    // A parameter was not provided
                    return new Response().WithStatusCode(HttpStatusCode.BadRequest);
                }
                catch (SecurityException secEx)
                {
                    // Registration blocked for security reasons
                    return Response.AsText(secEx.Message)
                        .WithStatusCode(HttpStatusCode.Unauthorized);
                }
            });
        }<commit_msg>phone number is optional<commit_after>﻿using BetaGo.Server.DataModels.Registration;
using BetaGo.Server.Services.Authentication;
using BetaGo.Server.Utilities;
using Nancy;
using Nancy.ModelBinding;
using System;
using System.Security;

namespace BetaGo.Server.Modules
{
    /// <summary>
    /// Registration functionality
    /// </summary>
    public class RegistrationModule : NancyModule
    {
        public RegistrationModule()
        {
            Post("/register", args =>
            {
                var req = this.Bind<RegistrationRequest>();

                try
                {
                    // TODO: Validate parameters!

                    // Valdiate username length, charset
                    if (req.Username.Length < 4)
                    {
                        throw new SecurityException("Username must be at least 4 characters.");
                    }
                    // Validate phone number

                    // Validate password
                    if (req.Password.Length < 8)
                    {
                        throw new SecurityException("Password must be at least 8 characters.");
                    }

                    if (req.PhoneNumber != null && !StringUtils.IsPhoneNumber(req.PhoneNumber))
                    {
                        throw new SecurityException("Phone number was of invalid format.");
                    }

                    // Validate registration
                    var newU = WebUserManager.RegisterUser(req);

                    // Return just the 200 for now
                    return Response.AsJsonNet(new RegistrationResponse
                    {
                        User = newU,
                        ApiKey = newU.ApiKey,
                    });
                }
                catch (NullReferenceException)
                {
                    // A parameter was not provided
                    return new Response().WithStatusCode(HttpStatusCode.BadRequest);
                }
                catch (SecurityException secEx)
                {
                    // Registration blocked for security reasons
                    return Response.AsText(secEx.Message)
                        .WithStatusCode(HttpStatusCode.Unauthorized);
                }
            });
        }
{'fraction_non_alphanumeric': 0.07283464566929133, 'fraction_numerical': 0.0030621172353455816, 'mean_word_length': 1.1399157697707065, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3753288', 'n_tokens_mistral': 994, 'n_tokens_neox': 888, 'n_words': 239}