Authentication is the process by which a client's identity and capabilities are verified before granting access to server resources. Authentication is essential when you have content that you wish to protect and provide only to specific, approved clients.
The Ioto web server implements a powerful and flexible authentication framework that verifies username and password and controls client capabilities using a role based authorization mechanism.
Web authentication has historically used Basic or Digest authentication. However these two methods have security and usability flaws and should not be used.
A preferred method is to use a web form to capture the username and password and then transmit them securely using an encrypted TLS connection where the credentials are validated by the web server.
Once authenticated, users are authorized with a role that defines their capabilities.
In Ioto, access roles are an ordered list of user roles with increasing capabilities. Typical roles are: "user", "admin" or "support".
Authentication roles for the Ioto web server are defined in the web.json5 configuration file via the auth.roles property.
Here is a sample:
The Ioto web server controls access to resources by assigning a specific "role" on a route that provides access to a document or resource. If the authenticated user has the required role or better, then the user is granted access.
Each route specifies a matching URL prefix except for the last route which is a catchall. To restrict access, the route provides a "role" property that specifies the required user role to permit access.
Users and Passwords
Users are defined in the Ioto database together with their hashed and salted passwords and access role.
User entries are typically created by the device management or admin apps using the cryptMakePassword API.
However, during development, users may be created in the database via the dbcmd and pass programs.
To generate a hashed password for a user, use the pass program. For example:
This will print the hashed password for "ralph".
You can modify the config/seed.json5 file with the required users and hashed passwords. Then apply this file to the database using the "reset" make target.
This will run the command:
Authentication schemes define how the user credentials are captured from the user and supplied to Ioto. There are two classes of authentication schemes.
The form authentication scheme uses a HTML web form for the user to enter their username and password credentials and a HTTP Post request to submit credentials to the server for verification. Ioto manages the login/logout process and if authentication succeeds, a login session is created and a cookie is returned to the client's browser. Subsequent requests that include the cookie will be automatically authenticated using the session.
Here is a sample minimal example login page:
After the user enters their username and password and clicks OK, the form is posted and the /api/public/login URL is requested. This URL should be bound to an Action via webAddAction that will verify the password and then call webLogin.
You can use the inbuilt webLoginUser routine to validate the password against the value stored in the database by setting the auth.login property in the web.json5 config file.
If you require custom user authentication, you can define your own login action and leave the auth.login property unset. For example:
The login function will retrieve the username and password entered by the user from the form post data and then validate these in the custom CheckPassword function.
SECURITY CAUTION: The login request should only be performed over a secure TLS encrypted connection and never over plain "HTTP".
To implement the form based authentication, you should ensure the login and log out pages and all required graphics and stylesheets are accessible without authentication.
A typical set of routes for an authenticated web site is:
You can easily create a custom or hybrid authentication scheme. You can choose to pass the username and password to the login action via other means such as an "Authorization" HTTP header or JSON payload. You would then adjust your login() function accordingly.
See the the following API to manage authentication and authorization under program control.
Ioto provides a sample for user login: