What is Adaptive Authentication ??
Why you should select WSO2 IS for adaptive authentication?
- Ability to write complex authentication policies in a simple manner using embedded script editor in management console. It removes the barriers enforced by traditional UI tools to generate adaptive authentication flows.
- Shipping a set of well written templates on use cases such as role-based, user-age-based, tenant-based, user store based, IP-based, new device based, risk-based by default with the product. It makes the identity admin’s life comfortable and easier.
- Open and future-proof adaptive authentication platform which provides more value for less cost.
- Designed in such as a way to quickly integrate with risk engines and external systems.
adaptive authentication to handle adaptive authentication scripts in a
hassle free manner.
Advantages of function library support for adaptive authentication
- The common Java script functions which are useful for adaptive authentication scripts can be stored as collections.
- Enhance the re-usability of common functions.
- Make adaptive authentication scripts more clear by importing function
libraries and using their functions instead of writing everything in the same script. - Make the users’ life easier by reducing the effort to modify and maintain scripts.
- Mechanism of importing libraries to adaptive authentication scripts is developer friendly as it uses a similar way of
in NodeJS.require()
How to use function libraries ?
.js
extension is added to the name you specified. You need to use that .js
extension when importing a function library. (Can see later)How to write function library scripts ?
this.<function>
as similar to this.convertIpToLong
usage inside isCorporateIP
function).// Function to convert ip address string to long value
var convertIpToLong = function(ip) {
var components = ip.split('.');
if (components) {
var ipAddr = 0, pow = 1;
for (var i = 3; i >= 0; i -= 1) {
ipAddr += pow * parseInt(components[i]);
pow *= 256;
}
return ipAddr;
} else {
return -1;
}
};
// Function to check if the ip address is within the given subnet
var isCorporateIP = function(ip, subnets) {
var subnetLength = subnets.length;
for (var i = 0; i < subnetLength; i++) {
var subnetComponents = subnets[i].split('/');
var minHost = this.convertIpToLong(subnetComponents[0]);
var ipAddr = this.convertIpToLong(ip);
var mask = subnetComponents[1];
if (subnetComponents && minHost >= 0) {
var numHosts = Math.pow(2, 32 - parseInt(mask));
if ((ipAddr >= minHost) && (ipAddr <= minHost + numHosts - 1)) {
return true;
}
}
}
return false;
};
module.exports.convertIpToLong = convertIpToLong;
module.exports.isCorporateIP = isCorporateIP;
var networkUtils = {
// Function to convert ip address string to long value
convertIpToLong : function(ip) {
var components = ip.split('.');
if (components) {
var ipAddr = 0, pow = 1;
for (var i = 3; i >= 0; i -= 1) {
ipAddr += pow * parseInt(components[i]);
pow *= 256;
}
return ipAddr;
} else {
return -1;
}
},
// Function to check if the ip address is within the given subnet
isCorporateIP : function(ip, subnets) {
var subnetLength = subnets.length;
for (var i = 0; i < subnetLength; i++) {
var subnetComponents = subnets[i].split('/');
var minHost = this.convertIpToLong(subnetComponents[0]);
var ipAddr = this.convertIpToLong(ip);
var mask = subnetComponents[1];
if (subnetComponents && minHost >= 0) {
var numHosts = Math.pow(2, 32 - parseInt(mask));
if ((ipAddr >= minHost) && (ipAddr <= minHost + numHosts - 1)) {
return true;
}
}
}
return false;
}
};
module.exports = networkUtils;
var networkUtils = { };
// Function to convert ip address string to long value
networkUtils.convertIpToLong = function(ip) {
var components = ip.split('.');
if (components) {
var ipAddr = 0, pow = 1;
for (var i = 3; i >= 0; i -= 1) {
ipAddr += pow * parseInt(components[i]);
pow *= 256;
}
return ipAddr;
} else {
return -1;
}
};
// Function to check if the ip address is within the given subnet
networkUtils.isCorporateIP = function(ip, subnets) {
var subnetLength = subnets.length;
for (var i = 0; i < subnetLength; i++) {
var subnetComponents = subnets[i].split('/');
var minHost = this.convertIpToLong(subnetComponents[0]);
var ipAddr = this.convertIpToLong(ip);
var mask = subnetComponents[1];
if (subnetComponents && minHost >= 0) {
var numHosts = Math.pow(2, 32 - parseInt(mask));
if ((ipAddr >= minHost) && (ipAddr <= minHost + numHosts - 1)) {
return true;
}
}
}
return false;
};
module.exports = networkUtils;
IMPORTANT : You must export the functions in a library in order to use them in any adaptive authentication script.
- Add
var <module_name> = require('<function library name>');
- Use the functions in the loaded function library,
var networkUtilsModule = require('networkUtils.js');
// IP-Based from Template...
// This script will step up authentication for any user who are trying to log in outside from the configured network
// Configure the network ranges here
var corpNetwork = ['192.168.1.0/24', '10.100.0.0/16'];
var onLoginRequest = function(context) {
executeStep(1, {
onSuccess: function (context) {
var user = context.currentKnownSubject;
// Extracting the origin IP of the request
var loginIp = context.request.ip;
Log.info('User: ' + user.username + ' logged in from IP: ' + loginIp);
// Checking if the IP is within the allowed range
if (!networkUtilsModule.isCorporateIP(loginIp, corpNetwork)) {
executeStep(2);
}
}
});
};
// End of IP-Based.......
var networkUtilsModule = require(‘networkUtils.js’);
on top of the script/ before the usage of functions in the function library.networkUtilsModule.isCorporateIP(loginIp, corpNetwork)
require()
functionality is not supported inside function libraries. (i.e You won’t be able to import one function library into another function library)