Only Allow Signups from Inside the Corporate Network using Hooks
Last updated
Last updated
Authgear Webhook is a feature that sends notifications in the form of HTTP requests to an external URL that you specify when certain events occur. For example, Webhook can send the user.pre_create event just before a new user is created.
Webhooks also listen for response from your external services. For blocking events, this response determines if the process that triggered the event should continue to completion or be terminated. In this post, we'll use a blocking event and a custom webhook endpoint to restrict user signup to only devices from inside a corporate network.
Configuring a webhook for the user.pre-created
event will prevent Authgear from creating new users if your webhook endpoint returns is_allowed: false
in its response. In contrast, a new user will be created successfully when the endpoint returns is_allowed: true
in its response.
We will be using this behavior to prevent users from outside a corporate network from signing up by checking their IP address (which is provided in the payload of the webhook HTTP request from Authgear). When the IP address matches a specified allowed IP address, we set the value for is_allowed
to true
. Otherwise, we return false
.
The following tutorial shows detailed steps for allowing signups from inside a corporate network only.
In this step, we'll create a webhook for the User pre-create event in the Authgear Portal. To do that, login to the Authgear Portal and navigate to Advance > Hooks. Next, under Blocking Events section, click on Add, then select Webhook as the type and User pre-create as the Event.
Enter the full URL that points to the page that will be receiving and processing the webhook requests in the Endpoint text field.
Once you're done, click on the Save button at the top.
You'll need to create an endpoint in your preferred programming language or framework and host it online. For this tutorial, we'll be creating a basic Express.js endpoint.
Create a new Express.js project and add the following code to app.js
:
Let's walk through what the above code will do:
Express creates an endpoint that accepts POST requests. (Note that the Authgear webhook only sends POST HTTP requests and your endpoint must support HTTPS).
Deploying the above code and pointing your Authgear webhook endpoint to it should prevent any user from signing up.
This is because the code returns is_allowed: false
as a response to the Webhook. Hence terminating the signup process.
You can also state a more descriptive reason for termination in the endpoint response using the reason
field.
Authgear webhooks send data to the endpoint URL to enable developers to build custom features and more. In this step, we'll read the data from the User pre-created event call and get the IP address for the user trying to register.
To read the request body and output the value to the console add the following code to app.js on a new line just above response.status(200)...
Attempting to register a new user again should print JSON data similar to this to your endpoints console:
The data we're interested in for this example is ip_address
in the context
object (context.ip_address). We'll use this IP address to determine whether a user is inside the corporate network.
Now, update app.js so that it returns is_allowed: true
when the user's IP address is within the corporate network's IP address.
The code for app.js should look like this after the modification:
You should replace the value for allowedIp
with the IP address you wish to allow.
At this point, if you deploy your endpoint and try signing up from the allowed IP the process should complete without issue, and you've successfully implemented signups only from a corporate network.
Authgear offers an alternative for regular webhooks called the JavaScript/TypeScript hooks. This kind of hook makes it possible to implement our earlier solution without hosting an external endpoint. In the following section, we'll walk through the steps for allowing signups from inside a corporate network only using the Typescript hook.
Navigate to Advance > Hooks in the Authgear portal. Next, click on Add under the Blocking Events section. Or, modify the previous webhook.
Select TypeScript as the type and User pre-create as the Event.
The third field in a TypeScript hook is different from a regular webhook. The field allows you to enter JavaScript code that the hook can execute when an event is triggered.
You can click on the Edit Script button near the Script field to open the hook script editor.
Modify the code in the script editor to the following:
Make sure to update the value for allowedIp
to the IP address you wish to allow.
Once you're done, click on Finish Editing to return to the Hooks configuration page. Save your new hook by clicking on the Save button at the top of the configuration page.
To test that your TypeScript hook is working properly, try signing up a new user from the IP address you specified earlier. Also, try signing up outside that IP address to see if everything works as expected.