Skip to content

Add auth0 source function #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions sources/auth0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Auth0
Auth0 rapidly integrates authentication and authorization for web, mobile, and legacy applications so you can focus on your core business.

You can send Auth0 logs to Segment using a Segment [Source Function](https://segment.com/docs/connections/functions/source-functions/). To get set up, take the following steps:
1. Log into your Auth0 account and follow [these instructions](https://auth0.com/docs/customize/log-streams/custom-log-streams) to create a webhook stream in Auth0.
2. Log in to Segment and create a Source Function there.
3. Provide Auth0 with the webhook URL Segment spun up for your Source Function.
172 changes: 172 additions & 0 deletions sources/auth0/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/**
* Handle incoming HTTP request
*
* @param {FunctionRequest} request
* @param {FunctionSettings} settings
*/
async function onRequest(request, settings) {
const body = request.json();

//make event name easier to understand based on Auth0's log codes
//https://auth0.com/docs/deploy-monitor/logs/log-event-type-codes
const logCodes = {
"admin_update_launch": "Auth0 Update Launched",
"api_limit": "Rate Limit on the Authentication or Management APIs",
"cls": "Code/Link Sent",
"cs": "Code Sent",
"depnote": "Deprecation Notice",
"du": "Deleted User",
"f": "Failed Login",
"fapi": "Operation on API failed",
"fc": "Failed by Connector",
"fce": "Failed Change Email",
"fco": "Failed by CORS",
"fcoa": "Failed cross-origin authentication",
"fcp": "Failed Change Password",
"fcph": "Failed Post Change Password Hook",
"fcpn": "Failed Change Phone Number",
"fcpr": "Failed Change Password Request",
"fcpro": "Failed Connector Provisioning",
"fcu": "Failed Change Username",
"fd": "Failed Delegation",
"fdeac": "Failed Device Activation",
"fdeaz": "Failed Device Authorization Request",
"fdecc": "User Canceled Device Confirmation",
"fdu": "Failed User Deletion",
"feacft": "Failed Exchange",
"feccft": "Failed Exchange",
"fede": "Failed Exchange",
"fens": "Failed Exchange",
"feoobft": "Failed Exchange",
"feotpft": "Failed Exchange",
"fepft": "Failed Exchange",
"fepotpft": "Failed Exchange",
"fercft": "Failed Exchange",
"fertft": "Failed Exchange",
"ferrt": "Failed Exchange",
"fi": "Failed invite accept",
"flo": "Failed Logout",
"fn": "Failed Sending Notification",
"fp": "Failed Login (Incorrect Password)",
"fs": "Failed Signup",
"fsa": "Failed Silent Auth",
"fu": "Failed Login (Invalid Email/Username)",
"fui": "Failed users import",
"fv": "Failed Verification Email",
"fvr": "Failed Verification Email Request",
"gd_auth_failed": "MFA Auth failed",
"gd_auth_rejected": "MFA Auth rejected",
"gd_auth_succeed": "MFA Auth success",
"gd_enrollment_complete": "MFA enrollment complete",
"gd_otp_rate_limit_exceed": "Too many failures",
"gd_recovery_failed": "Recovery failed",
"gd_recovery_rate_limit_exceed": "Too many failures",
"gd_recovery_succeed": "Recovery success",
"gd_send_email": "Email Sent",
"gd_send_pn": "Push notification sent",
"gd_send_pn_failure": "Push notification sent",
"gd_send_sms": "SMS sent",
"gd_send_sms_failure": "SMS sent failures",
"gd_send_voice": "Voice call made",
"gd_send_voice_failure": "Voice call failure",
"gd_start_auth": "Second factor started",
"gd_start_enroll": "Enroll started",
"gd_start_enroll_failed": "Enrollment failed",
"gd_tenant_update": "Guardian tenant update",
"gd_unenroll": "Unenroll device account",
"gd_update_device_account": "Update device account",
"gd_webauthn_challenge_failed": "Enrollment challenge issued",
"gd_webauthn_enrollment_failed": "Enroll failed",
"limit_delegation": "Too Many Calls to /delegation",
"limit_mu": "Blocked IP Address",
"limit_wc": "Blocked Account",
"limit_sul": "Blocked Account",
"mfar": "MFA Required",
"mgmt_api_read": "Management API read Operation",
"pla": "Pre-login assessment",
"pwd_leak": "Breached password",
"resource_cleanup": "Refresh token excess warning",
"s": "Success Login",
"sapi": "Success API Operation",
"sce": "Success Change Email",
"scoa": "Success cross-origin authentication",
"scp": "Success Change Password",
"scph": "Success Post Change Password Hook",
"scpn": "Success Change Phone Number",
"scpr": "Success Change Password Request",
"scu": "Success Change Username",
"sd": "Success Delegation",
"sdu": "Success User Deletion",
"seacft": "Success Exchange",
"seccft": "Success Exchange",
"sede": "Success Exchange",
"sens": "Success Exchange",
"seoobft": "Success Exchange",
"seotpft": "Success Exchange",
"sepft": "Success Exchange",
"sercft": "Success Exchange",
"sertft": "Success Exchange",
"si": "Successful invite accept",
"signup_pwd_leak": "Breached password",
"srrt": "Success Revocation",
"slo": "Success Logout",
"ss": "Success Signup",
"ssa": "Success Silent Auth",
"sui": "Success users import",
"sv": "Success Verification Email",
"svr": "Success Verification Email Request",
"sys_os_update_end": "Auth0 OS Update Ended",
"sys_os_update_start": "Auth0 OS Update Started",
"sys_update_end": "Auth0 Update Ended",
"sys_update_start": "Auth0 Update Started",
"ublkdu": "User login block released",
"w": "Warnings During Login"
}

let eventToSend = {
event: logCodes[body.data.type],
//assigns an identifier from Auth0's payload
anonymousId: body.data.client_id,
properties: {
...body.data
}
};

//if payload is < Segment's limit of 32KB -> send it as is (https://segment.com/docs/connections/sources/catalog/libraries/server/http-api/#max-request-size)
if (getRoughSizeOfObject(eventToSend) < 32) {
Segment.track(eventToSend);
} else {
//otherwise remove a portion and then send the pared down version
delete eventToSend.properties.details.request.auth.credentials.scopes;
Segment.track(eventToSend);
}


//checks size of outgoing payload
function getRoughSizeOfObject(object) {
let objectList = [];
let stack = [object];
let bytes = 0;

while (stack.length) {
let value = stack.pop();
if (typeof value === 'boolean') {
bytes += 4;
} else if (typeof value === 'string') {
bytes += value.length * 2;
} else if (typeof value === 'number') {
bytes += 8;
} else if (
typeof value === 'object' &&
objectList.indexOf(value) === -1
) {
objectList.push(value);
for (let i in value) {
stack.push(value[i]);
}
}
}
//returns kilobytes - Segment's limit is 32KB
return bytes / 1000;
}
}
76 changes: 76 additions & 0 deletions sources/auth0/webhook-examples/failed_signup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"payload": {
"body": {
"log_id": "90020221115155219994170187923660936391033852283976679442",
"data": {
"date": "2022-11-15T15:52:14.849Z",
"type": "fs",
"description": "Password is too weak",
"connection": "Username-Password-Authentication",
"connection_id": "con_rKY0IYI9hxvaMPxF",
"client_id": "Kx6QHMecjr7ItYzXjKKHNbwq1KWPQ7FB",
"client_name": "All Applications",
"ip": "35.167.74.121",
"user_agent": "unknown",
"details": {
"description": {
"rules": [
{
"message": "At least %d characters in length",
"format": [
8
],
"code": "lengthAtLeast",
"verified": true
},
{
"message": "Contain at least %d of the following %d types of characters:",
"code": "containsAtLeast",
"format": [
3,
4
],
"items": [
{
"message": "lower case letters (a-z)",
"code": "lowerCase",
"verified": true
},
{
"message": "upper case letters (A-Z)",
"code": "upperCase",
"verified": false
},
{
"message": "numbers (i.e. 0-9)",
"code": "numbers",
"verified": false
},
{
"message": "special characters (e.g. !@#$%^&*)",
"code": "specialCharacters",
"verified": false
}
],
"verified": false
}
],
"verified": false
},
"body": {
"client_id": "Kx6QHMecjr7ItYzXjKKHNbwq1KWPQ7FB",
"tenant": "dev-wrr7xwz1euw3aue3",
"email": "test@test.com",
"password": "*****",
"connection": "Username-Password-Authentication"
}
},
"user_id": "",
"user_name": "test@test.com",
"strategy": "auth0",
"strategy_type": "database",
"log_id": "90020221115155219994170187923660936391033852283976679442"
}
}
}
}
Loading