OTP Verification in Python
You can also make OTP calls to the API without oAuth tokens. This can be done by means of setting up OTP providers as well as SMTP/SMS gateways in the webconsole. To do it, you can use the instructions in the document by the link.
Sending an email verification code
Proceed with writing a program to send an email verification code, as shown below.
import requestsimport jsonurl = "http://your-openiam-instance/selfservice/send-verification-code-by-email"payload = json.dumps({"email": "youremail@openiam.com"})headers = {'Content-Type': 'application/json'}response = requests.request("POST", url, headers=headers, data=payload)print(response.text)
After running the program above, a response similar to this should be printed in the terminal.
PS C:\Users\maxim\OneDrive\Desktop\openiam\restapi> python sendemailotp.py{"primaryKey":null,"status":200,"errorList":null,"redirectURL":null,"successToken":{"message":"EMAIL_SENT","params":null},"successMessage":null,"contextValues":{"emailMovingFactor":1723562999236,"emailAddressForCode":{"id":null,"beanType":"EmailBean","operation":"NO_CHANGE","name":null,"type":null,"typeId":null,"active":true,"userId":null,"published":false,"email":"maximustheneuf@gmail.com","description":null,"default":false,"public":false}},"stackTrace":null,"possibleErrors":null,"error":false}
Make note of the numbers after emailMovingFactor
, you will need these for checking the email verification.
Checking the email verification code
To check the email verification code, use the following program.
import requestsimport jsonurl = "http://your-openiam-instance/selfservice/check-verification-code"payload = json.dumps({"emailCode": "471607","emailMovingFactor": "1721971968761"})headers = {'Content-Type': 'application/json'}response = requests.request("POST", url, headers=headers, data=payload)print(response.text)
For the parameters, the emailCode
is the code sent to the email you specified and emailMovingFactor
is what you need to copy from the response above. If both the emailCode
and emailMovingFactor
are correct, you will get a 200 status response similar to one as follows.
PS C:\Users\maxim\OneDrive\Desktop\openiam\restapi> python checkemailotp.py{"primaryKey":null,"status":200,"errorList":null,"redirectURL":null,"successToken":null,"successMessage":null,"contextValues":null,"stackTrace":null,"possibleErrors":null,"error":false}If the code is incorrect, you will receive this message:PS C:\Users\maxim\OneDrive\Desktop\openiam\restapi> python checkemailotp.py{"primaryKey":null,"status":500,"errorList":[{"i18nError":null,"error":"CODE_NOT_EQUAL","validationError":null,"params":null,"message":"Sent code and input do not match"}],"redirectURL":null,"successToken":null,"successMessage":null,"contextValues":null,"stackTrace":null,"possibleErrors":null,"error":true}
Sending an SMS verification code
To send an SMS verification code, use the code provided below.
import requestsimport jsonurl = "http://your-openiam-instance/selfservice/send-sms-verification-code"payload = json.dumps({"areaCd": "1","phoneNbr": "123456789","phoneExt": "","mdTypeId": "CELL_PHONE","countryCd": "1","usedForSMS": True,"published": True})headers = {'Content-Type': 'application/json'}response = requests.request("POST", url, headers=headers, data=payload)print(response.text)Check SMS verification code:import requestsimport jsonurl = "http://your-openiam-instance/selfservice/check-verification-code"payload = json.dumps({"smsCode": "190074","smsMovingFactor": "1721970770048"})headers = {'Content-Type': 'application/json'}response = requests.request("POST", url, headers=headers, data=payload)print(response.text)
Logging out
To logout (or revoke oAuth token), use the following code.
import requestsurl = "https://your-openiam-instance/idp/oauth2/revoke"payload='token_type_hint=access_token&token={{authtoken}}'headers = {'Content-Type': 'application/x-www-form-urlencoded','Cookie': 'OPENIAM_AUTH_TOKEN={{authtokenId}}'}response = requests.request("POST", url, headers=headers, data=payload)print(response.text)