Notify when logging in from a new IP address
Overview
Kuroco has an API to retrieve login history on the admin screen. This guide will introduce how to implement a feature to send a notification when logging in from a new IP address using this API.
What you'll learn
You will learn how to detect logins from a new IP address and send notifications using the following steps:
Creating an API
Create API for internal processing
It is recommended to separate APIs used only internally within Kuroco. Therefore, let's first create an API for internal use. If you have already added it, you can proceed to the next step.
Creating APIs
Click [Add] on the API page of the Kuroco admin panel.
Enter the following in the editor and click [Add].
Item | Settings |
---|---|
Title | Internal |
Version | 1.0 |
Description | API for internal use |
The new API has been created.
Security settings
Next, we set up the security. Click [Security].
Select [Dynamic access token] and click [Save].
After setting the security to [Dynamic access token], the message "Since this API is configured to use token, it is recommended to add an endpoint that implements Authentication::Login::token." will be shown. Please ignore it since the API is only for internal use.
configure CORS
Next, we configure CORS. Click [Operation CORS].
Click [Add origin] of CORS_ALLOW_ORIGINS and enter the following:
- Admin panel URL
Click [Add Method] of CORS_ALLOW_METHODS and enter the followings:
- GET
- POST
- OPTIONS
Confirm that [Allow Credentials] of CORS_ALLOW_CREDENTIALS is checked.
If there are no problems, click [Save].
Creating Endpoints
The endpoint to retrieve login history is LoginHistory::list.
Click on [Add a new endpoint] from the Internal API to create it.
Create an endpoint to retrieve login history
Field | Setting |
---|---|
Path | login_history |
Category | Authentication |
Model | LoginHistory |
Operation | list |
self_only | Check |
login_type | 0 |
Creating custom function
Once the content and endpoints are set up, we will write custom function to integrate with DeepL for translation.
In the left sidebar menu, click [Operation] -> [Custom function]
Click on [Add] and create two custom functions: "Custom function to register/delete glossary" and "Custom function to translate content and register in secondary language".
Custom Function to Send Notification When a Login is Made from a New IP Address
The operation starts by using the trigger of after logging in to retrieve the IP address when a login is made. Then, it filters by IP address to retrieve past login histories. If the result is only one record, it sends a notification indicating a login from a new IP address.
Configure as follows:
Field | Value |
---|---|
Title | ip_address_login_alert |
Identifier | ip_address_login_alert |
Trigger | After logging in |
Process | The following content |
{* Retrieve information when logged in *}
{api_internal
var='current_log'
status_var='status'
endpoint='/rcms-api/3/login_history'
method='GET'
member_id=$smarty.session.member_id}
{* Retrieve other login histories with the same IP address *}
{* Refer to the second log (login_history_list[1]) because the api_internal retains the log. *}
{assign_array var='queries' values=''}
{assign var='queries.ip_address' value=$current_log.login_history_list[1].ip_address }
{api_internal
var='log_history'
status_var='status'
endpoint='/rcms-api/3/login_history'
method='GET'
queries=$queries
member_id=$smarty.session.member_id}
{if $log_history.pageInfo.totalCnt == 1}
{capture name=mail_body}
A login from a new IP address has been detected.
Please verify if it was you.
IP Address: {$current_log.login_history_list[1].ip_address}
Date and Time: {$current_log.login_history_list[1].login_ymdhi}
Admin Panel URL: {$smarty.const.ROOT_MNG_URL}/management/
{/capture}
{sendmail
var='result'
to=$smarty.session.email
subject="Login detected from a new IP address."
contents=$smarty.capture.mail_body}
{logger msg1="Login detected from a new IP address." msg2=$current_log.login_history_list[1]}
{/if}
When logging in internally using Smarty, the IP address 127.0.0.1
or the request origin IP address of Kuroco is logged.
In the above code, we are selecting the data to be used with consideration because logs of internal logins remain with api_internal that requires authentication.
If you want to check the request origin IP address of Kuroco, please contact support.
By setting up the Smarty plugin {logout}, you can force a logout.
By implementing the authentication function for new IP addresses along with measures such as storing login-permitted IPs in member information, you can enhance security.
Verify the Operation
The setup to detect logins from new IP addresses is now complete.
Login from a different environment than usual to confirm that notifications are received.
If the setup is correct, you should receive a notification as shown below.
Related Documents
Support
If you have any other questions, please contact us or check out Our Slack Community.