Create Account with Password Strength Checker using OpenWhisk and Hypi

In this post, we will construct the common use case of creating an account with a password strength checker. It consists of two actions: Checking the strength of the password and then creating the account if the password is strong.

We will create these two actions using Python scripts. Two actions can be combined using an action sequence. The result of one action becomes the input to the second action. An action sequence can be integrated with Hypi as a serverless function. And when you execute Hypi’s serverless function, it will create an account with the credentials only if the password is strong.

Before proceeding to action sequence, let’s configure OpenWhisk to point towards Hypi’s endpoint.

wsk property set --apihost "https://fn.hypi.app" --auth “controversially.apps.hypi.app:<Auth_Key>"

Let’s create first action pwdstrength that checks the password strength.
Here is the python script pwdstrength.py that categorizes the password as strong or weak.

import re
# Function to categorize password
def main(args):
    # the password should not be a
    # newline or space
    v = args.get("pwd")
    u = args.get("user")
    if v == "\n" or v == " ":
        return {"strength": "weak","result":"Password cannot be a newline or space!","user":u,"pwd":v}
   
    # the password length should be in
    # between 9 and 20
    if 9 <= len(v) <= 20:
   
        # checks for occurrence of a character 
        # three or more times in a row
        if re.search(r'(.)\1\1', v):
            return {"strength": "weak","result":"Weak Password: Same character repeats three or more times in a row","user":u,"pwd":v}
   
        # checks for occurrence of same string 
        # pattern( minimum of two character length)
        # repeating
        if re.search(r'(..)(.*?)\1', v):
            return {"strength": "weak","result":"Weak password: Same string pattern repetition","user":u,"pwd":v}
   
        else:
            return {"strength": "strong","result":"Strong Password!","user":u,"pwd":v}
   
    else:
        return {"strength": "weak","result":"Password length must be 9-20 characters!","user":u,"pwd":v}

The script returns the strength of the password, result of the action, username and password. Credentials are forwarded as input parameters to the second action for creating an account.

Now, create the OpenWhisk action pwdstrength

wsk action create pwdstrength pwdstrength.py
ok: created action pwdstrength

Invoke the action

wsk action invoke pwdstrength --param pwd "[email protected] " --result
{
    "result": "Strong Password!"
}

Now create another action createAcnt that creates the Hypi account using username and password. The second action checks the strength of password provided as an input parameter. If the password is weak, account does not get created.

Below is the createaccount.py python script that will perform this function.

import json
import requests
from string import Template
# Function to create Hypi User Account
def main(args):
    password = args.get("pwd")
    username = args.get("user")
    srgth = args.get("strength")
    reslt= args.get("result")
    if srgth == "weak":
        return {"strength": "weak","result":reslt}
    query_value = Template('mutation {createAccount(value:{username:"${username}",password:{value:"${password}"}}){id}}').substitute(username=username,password=password)
    my_headers = {'hypi-domain' : 'controversially.apps.hypi.app' , 'content-type' : 'application/json' , 'authorization' : 'Auth-Token'}
    data1 = {'query': query_value}
    data2 = json.dumps(data1)
    response = requests.post("https://controversially.apps.hypi.app/graphql",data=data2,headers=my_headers)
    text = response.text
    return json.loads(text)

Create the OpenWhisk action createAcnt

wsk action create createAcnt createaccount.py
ok: created action createAcnt

Let’s create an Action Sequence with the above two actions.

wsk action create pwdSequence --sequence pwdstrength,createAcnt

Invoke the sequence providing username and password.

#1
wsk action invoke --result pwdSequence --param pwd qwe --param user wesadasd
{
    "result": "Password length must be 9-20 characters!",
    "strength": "weak"
}
#2
wsk action invoke --result pwdSequence --param pwd aPo7*sdHdns --param user mnbvclkd
{
    "data": {
        "createAccount": {
            "id": "01FAJHRM4K5FVKC7FFEFT9QJSV"
        }
    }
}

So, our action sequence of creating an account with a password strength checker is ready!
Open up schema editor in the release console of Hypi and declare the serverless function createAcntwithPwdCheck.

createAcntwithPwdCheck(user:String, pwd: String): Json @tan(type:OpenWhisk, name:"pwdSequence")

Now is the time to execute our serverless function.

#1
{
  createAcntwithPwdCheck(user:"vfghdjlpw",pwd:"")
}
#result
{
  "data": {
    "createAcntwithPwdCheck": {
      "result": "Password length must be 9-20 characters!",
      "strength": "weak"
    }
  }
}
#2
{
  createAcntwithPwdCheck(user:"wedrffksdfw",pwd:"pshduasd&d67")
}
#result
{
  "data": {
    "createAcntwithPwdCheck": {
      "data": {
        "createAccount": {
          "id": "01FAJJ9CFZK4AG732ZH2XE8GRP"
        }
      }
    }
  }
}

You can see that if the password strength is weak, the account doesn’t get created. If it is strong the account gets created with provided credentials.

Concluding Note:

Creating Serverless functions using OpenWhisk and Hypi is an effortless way to implement common use cases. The implementation can be reused in various applications as well.