Intro
The purpose of this Knowledge Base Article is to show you how to properly encrypt your passwords when using a database user source. If your user source Mode is set to automatic then Ignition will create the database tables for you. In doing so the password field is set to be encrypted. It is often helpful to allow users of the Ignition clients to change their passwords. This article will help you do this.
Encrypting your password
Assuming you already have a Database User Source, and, its mode is configured to be Automatic, I will walk you through an update query that will update a user’s password in the database. When the User Source mode is set to Automatic Ignition will encrypt the user’s password in the database. Ignition uses SHA1 when doing this. To do this we will import ‘SecurityUtils’ from ‘com.inductiveautomation.ignition.common.util’.
The script above will take your password string, encrypt it, and update the password in the database. Here is a line by line breakdown:
- Get the current logged in user’s username.
- Take the user’s new password from a component. I used the ‘Password Field’ component.
- Import ‘SecurityUtils’ from ‘com.inductiveautomation.ignition.common.util’.
- Encrypt the password from Step 2 using SecurityUtils. This will encrypt your password as SHA1.
- Write your update query.
- Execute your query to update the password in the database.
This is a very simplified script that will update the password in the database. You can add additional logic to your script that could validate the user’s current password before allowing them to update their password. I have included a script example of that also.
Here is a line by line explanation of the script:
- Get the current logged in user’s username.
- Create a variable for the user’s current password. In the client this is where the user will enter their current password into a component.
- If the component does not contain an empty string the script will move to line 4, else, it will move to line 19 where a warning box will open instructing the user to enter their current password.
- Here we are validating the username and password for the current logged in user.
- If the user is validated we will move to line 6, else, the script moves to line 17 warning the user that an incorrect password was entered.
- In the client I have provided fields for the user to enter and confirm their new passwords. Here I am creating a variable for the new password.
- The user will reenter their new password.
- Here we are comparing the two new passwords entered to make sure they are equal to one another. If the passwords match the script continues, else, the script moves to line 15 and warn the user of non-matching passwords.
- Import ‘SecurityUtils’ from ‘com.inductiveautomation.ignition.common.util’.
- Encrypt the password from Step 9 using SecurityUtils. This will encrypt your password as SHA1.
- Write your update query.
- Execute your query to update the password in the database.
- Show a message indicating the password has been updated.
Comments
0 comments
Please sign in to leave a comment.