Protecting app logins

In this how-to, you will learn how to integrate the Incognia Transaction API to improve the recognition of trusted users at login without adding friction to the user experience. You will be able to automatically approve trusted login attempts, trigger step-up authentication, and block suspicious logins.

Requirements

  • An Incognia account. Sign up here if you don't have an account.

  • An app with the Incognia SDK initialized. Follow SDK getting started guide for this.

  • API credentials to be able to make requests to Incognia APIs.

Step-by-step

Setting the User ID on the app

In order to verify a login attempt, Incognia needs to link a device and a unique account identifier. The Account ID is used for this purpose.

This association must happen in two moments:

1. Whenever the application initializes

This covers the scenario where the application is updated with the Incognia SDK and the user is already logged in. The Application.onCreate() on Android and the [AppDelegate application:didFinishLaunchingWithOptions:] on iOS are good places to do this.

If the Account Id is not available in those locations, it is important to forward the value to the Incognia SDK as soon as it is available. The important action here is to call setUserId whenever the application initializes so it is guaranteed that we always know that a specific user is associated with a specific device.

2. Whenever the user logs in and out

It is necessary to call the setUserId method when the user finishes the login and clearUserId when the user logs out. This is usually done in the callback of these two actions.

Kotlin
Java
Swift
Objective-C
Kotlin
Incognia.setUserId(context, <userId>)
Java
Incognia.setUserId(context, <userId>);
Swift
ICGIncognia.setUserId(<userId>)
Objective-C
[ICGIncognia setUserId:<userId>];

While data such as e-mail and phone numbers are examples of identifiers, their use in the SDK is forbidden. Consider using non-personal information, such as UUIDs, and Hashing to securely generate and set the User ID.

Forwarding the device's Installation ID to your server

The Incognia SDK collects location and device data to build a behavioral profile for mobile users. This data is tagged with an identifier called Installation ID, which is automatically generated by the SDK.

To verify a login attempt, Incognia needs to receive an Installation ID to identify the device from which the login originates. Since your server will request the Incognia API to assess the risk of this login, it needs to receive this information from your mobile application.

The installation ID can be forwarded to your server in two ways.

Option 1: Sending the Installation ID as a header

Before sending a login request from your mobile application to your server, call Incognia.getInstallationId and set its value as a header of the request. We encourage you to choose a clear name for this header, like Incognia-Installation-ID, so you can easily identify it on the server-side.

This option has a clear benefit if your application will use more than one Incognia solution because you won't need to change each request's properties, like signup, login, payment, password change, etc.

Kotlin
Java
Swift
Objective-C
Kotlin
Incognia.getInstallationId(context) { result ->
if (result.isSuccessful) {
val installationId = result.result
// HttpURLConnection
httpUrlConnection.setRequestProperty("Incognia-Installation-ID", installationId)
}
// Send the request with the installationId to your backend server
}
Java
Incognia.getInstallationId(context, new IncogniaListener<String>() {
@Override
public void onResult(final Result<String> result) {
if (result.isSuccessful()) {
String installationId = result.getResult();
// HttpURLConnection
httpUrlConnection.setRequestProperty("Incognia-Installation-ID", installationId);
}
// Send the request with the installationId to your backend server
}
});
Swift
ICGIncognia.getInstallationId({ installationId in
if installationId != nil {
// NSURLRequest
request.setValue(
installationId,
forHTTPHeaderField: "Incognia-Installation-ID"
)
}
// Send the request with the installationId to your backend server
})
Objective-C
[ICGIncognia getInstallationId:^(NSString *installationId) {
if (installationId != nil) {
// NSURLRequest
[request setValue:installationId forHTTPHeaderField:@"Incognia-Installation-ID"];
}
// Send the request with the installationId to your backend server
}];

Option 2: Sending the Installation ID in the body of the request

Before sending the login request from your mobile application to your server, call Incognia.getInstallationId and send it as additional information about this login. We suggest that you choose a clear name for this property like Incognia-Installation-ID, so you can easily identify it on the server-side.

Handling the user's login request

When your server receives a login request, you can use Incognia intelligence to assess the risk of this login attempt inside this request/response cycle.

To evaluate this login attempt risk, your server will request the Transaction API informing that a login attempt was made alongside its Installation ID and the Account ID of the account that the device is attempting to access.

A sample implementation of a controller/handler

Let's consider a toy example as back-end with the controller below:

Ruby/Rails
Ruby/Rails
# sessions_controller.rb
class SessionsController < ApplicationController
def create
@login_form = LoginForm.new(params)
if @user = @login_form.submit
sign_in @user
redirect_to @user, notice: "Welcome!"
else
render action: :new
end
end
end
# login_form.rb
class LoginForm < BaseForm
attr_accessor :email, :password
validates :email, :password, presence: true
# Other validations...
def submit
return nil if invalid?
user&.authenticate(password)
end
private
def user
@user ||= User.find_by(email: email.downcase)
end
end

You are required to authenticate when using the Incognia API. For authentication details, see Authenticating in Incognia APIs.

Considering that the authentication logic is implemented, you can add risk assessment requests to your login handler:

Ruby/Rails
Ruby/Rails
# login_form.rb
class LoginForm < BaseForm
attr_accessor :email, :password, :incognia_installation_id
validates :email, :password, presence: true
validate :device_risk
# Other validations...
def submit
return nil if invalid?
user&.authenticate(password)
end
private
def user
@user ||= User.find_by(email: email.downcase)
end
def device_risk
return unless user
api = Incognia::Api.instance
risk_assessment = api.register_login(
installation_id: incognia_installation_id,
account_id: user.id
)
# Automatically denies if Incognia gives high risk!
if risk_assessment == 'high_risk'
errors.add(:incognia_installation_id, 'considered unsafe!')
end
end
end
# incognia/api.rb
require 'faraday'
require 'json'
module Incognia
class Api
include Singleton
API_HOST = 'https://api.us.incognia.com/api/'.freeze
def register_login(installation_id:, account_id:)
transactions_endpoint = 'v2/authentication/transactions'
params = {
installation_id: installation_id,
account_id: account_id,
type: 'login'
}
response = Faraday.post(
"#{API_HOST}#{transactions_endpoint}",
params.to_json,
headers
)
if response.status == 200
parsed_body = JSON.parse(response.body)
parsed_body['risk_assessment']
else
# Error handling
end
end
private
def headers
{
'Content-Type': 'application/json',
# Read more about how to generate a fresh token at our
# Authenticating in Incognia APIs section.
Authorization: "Bearer #{fresh_token}"
}
end
end
end

Using the Incognia risk assessment

When your server makes a request to the Transaction API endpoint, it will receive a response like the following:

{
"evidence": {
"account_integrity": {...},
"addresses": [...],
"device_behavior_reputation": "allowed",
"device_fraud_reputation": "allowed",
"device_integrity": {
"emulator": false,
"from_official_store": true,
"gps_spoofing": false,
"probable_root": false
},
"device_model": "SM-G9600",
"distance_to_trusted_location": 4.213,
"known_account": true,
"last_location_ts": "2019-08-24T14:15:22Z",
"location_events_quantity": 10,
"location_services": {
"location_permission_enabled": true,
"location_sensors_enabled": true
},
"sensor_match_type": "gps"
},
"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
"risk_assessment": "low_risk"
}

The response contains the id of the created entity (in this case, a login), the risk_assessment provided by Incognia based on device behavior, and evidence that supports this assessment. You can learn more about all the returned data in this article: Understanding risk assessments.

The returned assessment can be used with other risk-related data, such as in a risk engine, to decide if this login should be accepted. Incognia's risk assessments include:

  • high_risk: the login performed by the device may be fraudulent and we advise you to take preventive actions in these scenarios, such as requiring additional step-up authentication or simply blocking this login attempt;

  • low_risk: the login performed by the device seems to be safe to accept;

  • unknown_risk: we are unable to provide a precise assessment at the time of the request. You should require additional authentication (e.g. 2FA).

Wrapping Up

After these steps, your application is ready to recognize trusted users at login without friction and preventing account takeovers.