Skip to main content

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.

Image from Gyazo

Enter the following in the editor and click [Add].

Image from Gyazo

ItemSettings
TitleInternal
Version1.0
DescriptionAPI for internal use

The new API has been created.

Image from Gyazo

Security settings

Next, we set up the security. Click [Security].

Image from Gyazo

Select [Dynamic access token] and click [Save].

Image from Gyazo

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.

Image from Gyazo

configure CORS

Next, we configure CORS. Click [Operation CORS].

Image from Gyazo

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.

Image from Gyazo

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.

Image from Gyazo

Create an endpoint to retrieve login history

FieldSetting
Pathlogin_history
CategoryAuthentication
ModelLoginHistory
Operationlist
self_onlyCheck
login_type0

Image from Gyazo

Image from Gyazo

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]

Image from Gyazo

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".

Image from Gyazo

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:

FieldValue
Titleip_address_login_alert
Identifierip_address_login_alert
TriggerAfter logging in
ProcessThe 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}

Image from Gyazo

info

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.

tip

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.

Image from Gyazo


Support

If you have any other questions, please contact us or check out Our Slack Community.