Last active
January 21, 2022 11:23
-
-
Save phi1ipp/1d146e2e2fbcce2c32e4dffdf140670a to your computer and use it in GitHub Desktop.
Okta event hook lambda example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | |
}; | |
} | |
}; |
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
For a simplicity only first event is taken care of