Help with your scripting

Email and Password Validation

Email and Password Validation

Postby Shando » 21 Aug 2017, 10:41

Hi All,

In preparation for the release of Raknet in Skyline, I thought I would share these two snippets that show how to validate both an email address and a password. Both are written in pure Lua.

EMAIL ADDRESS
Code: Select all
function validemail ( str )
   if ( str == nil ) then
      return nil, "Empty string"
   end

   if ( type ( str ) ~= "string" ) then
      return nil, "Expected a string"
   end

   local lastAt = str:find ( "[^%@]+$" )
   local localPart = str:sub ( 1, ( lastAt - 2 ) ) -- Returns the substring before '@' symbol
   local domainPart = str:sub ( lastAt, #str ) -- Returns the substring after '@' symbol

   -- we werent able to split the email properly
   if ( localPart == nil ) then
      return nil, "Local name is invalid"
   end

   if ( domainPart == nil ) then
      return nil, "Domain is invalid"
   end

   -- local part is maxed at 64 characters
   if ( #localPart > 64 ) then
      return nil, "Local name must be less than 64 characters"
   end

   -- domains are maxed at 253 characters
   if ( #domainPart > 253 ) then
      return nil, "Domain must be less than 253 characters"
   end

   -- somthing is wrong
   if ( lastAt >= 65 ) then
      return nil, "Invalid @ symbol usage"
   end

   -- quotes are only allowed at the beginning of a the local name
   local quotes = localPart:find ( "[\"]" )

   if ( type ( quotes ) == 'number' and quotes > 1 ) then
      return nil, "Invalid quote symbol usage"
   end

   -- no @ symbols allowed outside quotes
   if ( localPart:find ( "%@+" ) and quotes == nil ) then
      return nil, "Invalid @ symbol usage in local part"
   end

   -- no dot found in domain name
   if ( not domainPart:find ( "%." ) ) then
      return nil, "No period (dot) found in domain"
   end

   -- only 1 dot in succession allowed
   if ( domainPart:find ( "%.%." ) ) then
      return nil, "Too many periods (dots) in domain"
   end

   if ( localPart:find ( "%.%." ) ) then
      return nil, "Too many periods (dots) in local part"
   end

   -- just a general match
   if ( not str:match ( '[%w]*[%p]*%@+[%w]*[%.]?[%w]*' ) ) then
      return nil, "Email pattern test failed"
   end

   -- all our tests passed, so we are ok
   return true
end


If the email is valid the function returns true and nil, but if it fails, it will return nil and the reason that the validation failed. This code was originally from http://ohdoylerules.com/content/snippets/validate-email-with-lua/

PASSWORD
Code: Select all
blockedWords = { "password", "letmein", "computer" }

function checkPassword ( Username, Password, minLength, maxLength )
   local tStr = ""
   local tInt = string.len ( Password )

   if ( tInt < minLength ) then
      tStr = "Password MUST be longer than " .. minLength .. " characters in length"

      return false, tStr
   elseif ( tInt > maxLength ) then
      tStr = "Password CANNOT be longer than " .. maxLength .. " characters in length"

      return false, tStr
      end

   tStr = string.lower ( Password )

   if ( Password == Username or tStr == string.lower ( Username ) ) then
      return false, "Password CANNOT be the same (ignoring case) as the Username"
   end

   for _, value in pairs ( blockedWords ) do
      if ( tStr == value ) then
         tStr = "Password CANNOT equal " .. value

         return false, tStr
      end
   end

   tStr = string.sub ( Password, 1, 1 )

   if ( not string.find ( Password, "%a" ) ) then
      return false, "Password MUST start with a letter"
   end

   if ( not string.find ( Password, "%d" ) ) then
      return false, "Password MUST contain at least 1 digit"
   end

   if ( not string.find ( Password, "%l" ) ) then
      return false, "Password MUST contain at least 1 lower case letter"
   end

   if ( not string.find ( Password, "%u" ) ) then
      return false, "Password MUST contain at least 1 upper case letter"
   end

   if ( not string.find ( Password, "[^%a%d]" ) ) then
      return false, "Password MUST contain at least 1 special character"
   end
end


As with the Email code, this returns true & nil or false & reason. This code was originally from http://wiki.pscs.co.uk/reference:password_strength_checking_script

A couple of notes for the Password code:

    1) Password must contain at least 1 of each of the following:
      Upper Case Letter
      Lower Case Letter
      Number
      Special Character
    2) Password must be between minLength & maxLength in length
    3) Password must start with a Letter
    4) Password cannot be equal to one of the blockedWords
It should be fairly easy to work out how to change these rules, but if you have any questions, let me know.

Hope this is helpful.

Shando

PS: I'm hoping to be able to put together an xml or json file that contains profanities in an assortment of languages, which can be loaded into Skyline before running a check on such things as User Names etc. Using a file will mean that you will be able to select which (if any) language(s) to check.
PPS: Raknet has the ability to register a Profanity Table, so this file can potentially be used with Raknet.
Ryzen 7 4800H 16GB GTX1650 Win 11 64
Love, Hope, Strength http://www.lovehopestrength.co.uk
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Email and Password Validation

Postby SolarPortal » 21 Aug 2017, 13:09

Thanks for the info and code Shando, this could be useful and would be good to implement into one of skyline utility lua files.

The json profanity table is just lol :P But a very good idea haha :P
Raknet and lua is usable in the next version, so you should be able to start in the next version of skyline. We are still polishing the workflow and fixing any errors we come across :)
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: Email and Password Validation

Postby StarFire » 21 Aug 2017, 18:28

Thanks Shando these should be very useful. :D
I have copied them into the scripts networking folder with links to this topic. :D
Dream the Journey, Live the Experience!
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer


Return to Lua Scripting

Who is online

Users browsing this forum: No registered users and 5 guests

cron