Skip to content

Instantly share code, notes, and snippets.

@phi1ipp
Last active January 21, 2022 11:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phi1ipp/1d146e2e2fbcce2c32e4dffdf140670a to your computer and use it in GitHub Desktop.
Save phi1ipp/1d146e2e2fbcce2c32e4dffdf140670a to your computer and use it in GitHub Desktop.
Okta event hook lambda example
const okta = require('@okta/okta-sdk-nodejs');
const client = new okta.Client({
orgUrl: 'https://dev-xxxxx.oktapreview.com/',
token: 'xxxxxx'
});
exports.handler = async (event) => {
if (event.requestContext.http.method === 'POST') {
console.log('data event with body: ', event.body);
const oktaEvents = JSON.parse(event.body).data.events;
const targetUserId = oktaEvents[0].target[0].id;
console.log('getting user ', targetUserId);
const user = await client.getUser(targetUserId);
console.log('got user ', targetUserId);
user.profile.costCenter = 'Data from lambda';
console.log('updating user ', user.profile.login);
await user.update();
console.log('user ', user.profile.login, ' updated');
return {
statusCode: 200,
body: {'message' : 'OK'},
};
} else {
console.log('verification request, header value is: ' + event.headers['x-okta-verification-challenge']);
return {
"verification" : event.headers["x-okta-verification-challenge"]
};
}
};
@phi1ipp
Copy link
Author

phi1ipp commented Nov 12, 2020

For a simplicity only first event is taken care of

@phi1ipp
Copy link
Author

phi1ipp commented Nov 19, 2020

An example of a hook event

{
    "eventType": "com.okta.event_hook",
    "eventTypeVersion": "1.0",
    "cloudEventsVersion": "0.1",
    "source": "https://.oktapreview.com/api/v1/eventHooks/whor3gffo4lymILsW0h7",
    "eventId": "3346b9da-5678-4e21-944b-d84e359dbb8c",
    "data": {
        "events": [
            {
                "uuid": "720e1293-2aa4-11eb-94dc-318f61a7da54",
                "published": "2020-11-19T20:18:54.146Z",
                "eventType": "user.lifecycle.activate",
                "version": "0",
                "displayMessage": "Activate Okta user",
                "severity": "INFO",
                "client": {
                    "ipChain": []
                },
                "actor": {
                    "id": "00uj47huuzrkVuVnb0h7",
                    "type": "User",
                    "alternateId": "philipp.grigoryev@okta.com",
                    "displayName": "Philipp Grigoryev"
                },
                "outcome": {
                    "result": "SUCCESS"
                },
                "target": [
                    {
                        "id": "00uk80g4no7xbUllU0h7",
                        "type": "User",
                        "alternateId": "melissa.bonnette@okta.com",
                        "displayName": "Melissa Bonnette"
                    }
                ],
                "transaction": {
                    "type": "JOB",
                    "id": "pujv7ra3ljGWCaxay0h7",
                    "detail": {}
                },
                "debugContext": {
                    "debugData": {
                        "threatSuspected": "false",
                        "targetEventHookIds": "whor3gffo4lymILsW0h7"
                    }
                },
                "legacyEventType": "core.user.config.user_activated",
                "authenticationContext": {
                    "authenticationStep": 0,
                    "externalSessionId": "trsb7R0Jm2NTw-JLlsgo-BtMg"
                },
                "securityContext": {}
            }
        ]
    },
    "eventTime": "2020-11-19T20:18:58.690Z",
    "contentType": "application/json"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment