Code Monkey home page Code Monkey logo

active-directory-b2c-node-sign-up-user-flow-captcha's Introduction

page_type languages products description urlFragment
sample
javascript
nodejs
azure-active-directory
A sample to demonstrate how to use a CAPTCHA servicein Azure AD B2C sign-up user flows.
active-directory-node-b2c-sign-up-user-flow-captcha

active-directory-node-b2c-user-flow-captcha

Contents

File/folder Description
Assets/selfAsserted.html Sample custom HTML and JS script file for user flow.
Assets Contains UI/UX assets used by the user flows.
CaptchaAzureFunction(AzureFunction.cs) Sample source code for Node.js HTTP trigger.
README.md This README file.
.gitignore Define what to ignore at commit time.
CONTRIBUTING.md Guidance on how to contribute to this repository.
LICENSE.md The license for the sample.
SECURITY.md The security notice for the sample.

Key Concepts

CAPTCHA services are often used in authentication scenarios to protect against bots or other automated abuse. This sample demonstrates how to integrate an Azure AD B2C sign-up user flow with a CAPTCHA service.

Key components:

  • reCAPTCHA - a CAPTCHA service for protecting against bots and other automated abuse.
  • Azure AD B2C sign-up user flow - The sign-up experience that will be using the CAPTCHA service. Will utilize the custom HTML & JavaScript and API connectors to integrate with the CAPTCHA service.
  • Azure Functions - API endpoint hosted by you that works in conjunction with the API connectors feature. This API is responsible for doing the server-side validation of the CAPTCHA challenge.

This same pattern can be used for other CAPTCHA services and with other API hosting services.

Create an API key pair for reCAPTCHA V2

  • Follow the reCAPTCHA documentation to create an API key pair for your scenario.
  • Use your Azure AD B2C tenant as the domain: <tenantname>.b2clogin.com
  • You will receive a site_key and secret_key. The values of these are referred to as CAPTCHA_SITE_KEY and CAPTCHA_SECRET_KEY in the sample code and correspond to keys used in client-side and server-side CAPTCHA API calls.

Create a "CaptchaUserResponseToken" Custom Attribute

  1. From the Azure Portal, go to Azure AD B2C
  2. Select User Attributes
  3. Select Add
  4. Enter CaptchaUserResponseToken as the attribute Name
  5. Create

Custom attribute creation

Learn more about custom attributes.

Create a user flow

This can be either be a sign up and sign in or a just sign up or user flow. Either way, the CAPTCHA will only be shown during sign up.

  1. Follow these instructions. If using an existing user flow, note that user flows must be of the "Recommended (next-generation preview)" version type.
  2. In the user flow settings, navigate to User attributes and select the CaptchaUserResponseToken claim.

Select custom attribute in user flow

Configure custom HTML, JavaScript, and Page Layouts

The Assets > selfAsserted.html file contains an HTML template with JavaScript (<script> tags) that will do three things:

  • Load the reCAPTCHA script (https://www.google.com/recaptcha/api.js), which renders the reCAPTCHA widget and performs client-side CAPTCHA validation.
  • Hide the extension_CaptchaUserResponseToken input element and label, corresponding to the CaptchaUserResponseToken custom attribute, from the UI shown to the user.
  • When a user completes the CAPTCHA challenge, reCAPTCHA verifies the user's response and generates a token. When this happens, the callback captchaCallback in the custom JavaScript sets the value of extension_CaptchaUserResponseToken to the generated token value. This value will be submitted to the API endpoint as described in the "Create and deploy your API" section.

Read more about reCAPTCHA V2 checkbox client-side validation here.

Follow the instructions below to use this custom HTML and JS for your user flow.

Modify the selfAsserted.html page

Modify the Assets > selfAsserted.html file so that <CAPTCHA_SITE_KEY> matches the value you generated in the first step, which is used by the reCAPTCHA script.

Host the HTML page

Host the html page on a CORS enabled web endpoint. For example, follow steps 2 and 3 of these instructions ("Create an Azure Blob storage account" and "Configure CORS").

If you have your own custom HTML, just copy and paste the <script> elements onto your HTML page.

Configure page layouts

  1. From the Azure Portal, go to Azure AD B2C
  2. Navigate to User flows and select your user flow
  3. Select Page layouts
  4. Select Local account sign up page layout
  5. Toggle Use custom page content to "YES"
  6. Paste the URI where your custom HTML lives in Use custom page content
  7. Do the previous two steps for Social account sign up page layout if using social identity providers.

Select custom attribute in user flow

Enable JavaScript

  1. From your user flow, go to Properties and select Enable JavaScript enforcing page layout (preview)

Learn more here.

Create and deploy your API

These steps assume you use Visual Studio Code, but deploying the Azure Function via the Azure Portal, terminal or command prompt, or any other code editor will also work.

Prerequisite: Install the Azure Functions extension for Visual Studio Code.

Run the API locally

  1. Navigate to the Azure extension in Visual Studio code on the left navigation bar. You should see a 'Local Project' folder representing your local Azure Function.
  2. Press F5 (or use the Debug > Start Debugging menu command) to launch the debugger and attach to the Azure Functions host. (This command automatically uses the single debug configuration that Azure Functions created.)
  3. The Azure Function extension will automatically generate a few files for local development, install dependencies, and install the Function Core tools if not already present. These tools help with the debugging experience.
  4. Output from the Functions Core tools appears in the VS Code Terminal panel. Once the host has started, Alt+click the local URL shown in the output to open the browser and run the function. You can also see the url of the locally-hosted function by right clicking on the function on the Azure Functions explorer.
  5. To redeploy the local instance during testing, just repeat these steps.

Add environment variables

This sample protects the web API endpoint using HTTP Basic authentication.

Here, username and password are stored as environment variables so they're not stored as part of the repository. Read more about the local.settings.json file.

  1. Create a local.settings.json file in your root folder
  2. Copy and paste the below code onto the file:
{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "BASIC_AUTH_USERNAME": "<USERNAME>",
    "BASIC_AUTH_PASSWORD": "<PASSWORD>",
    "CAPTCHA_SECRET_KEY": "<CAPTCHA_SECRET_KEY>",
    "B2C_EXTENSIONS_APP_ID": "<B2C_EXTENSIONS_APP_ID>"
  }
}

The BASIC_AUTH_USERNAME and BASIC_AUTH_PASSWORD are going to be the credentials used to authenticate the API call to your Azure Function. Choose your desired values.

The <CAPTCHA_SECRET_KEY> is the server-side secret you generated in the reCAPTCHA service. It's used to call the reCAPTCHA server-side validation API to validate the value of the CaptchaUserResponseToken generated by the front-end.

THE <B2C_EXTENSIONS_APP_ID> is the application ID of the app used by Azure AD B2C to store custom attributes in the directory. You can find this application ID by navigating to App registrations, searching for b2c-extensions-app and copying the Application (client) ID from the Overview pane. Remove the - characters.

Find extensions app

Deploy the application to the web

  1. Follow steps of this guide #1-7 to deploy your Azure Function to the cloud. Copy the endpoint web URL of your Azure Function.
  2. Once deployed, you'll see an 'Upload settings' option. Select this. It will upload your environment variables onto the Application settings of the App service. These application settings can also be configured or managed via the Azure portal.

To learn more about Visual Studio Code development for Azure Functions, see this.

Configure and enable the API connector

Follow the steps outlined in "Add an API connector" to create an API connector and enable it your user flow. The configuration should look like the below.

API connector configuration

Your API connector configuration should look like the following:

API connector configuration

  • Endpoint URL is the Function URL you copied earlier if the deployed Azure Function.
  • Username and Password are the Username and Passwords you defined as environment variables earlier.

Enable the API connector

In the API connector settings for your user flow, select the API connector to be invoked at the Before creating the user step. This will invoke the API when a user hits 'Create' in the sign-up flow. The API will do a server-side validation of the CaptchaUserResponseToken value, which was is set when a user completed the captcha challenge during sign-up.

API connector selected

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

active-directory-b2c-node-sign-up-user-flow-captcha's People

Contributors

microsoft-github-operations[bot] avatar mirkon90 avatar nickgmicrosoft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

active-directory-b2c-node-sign-up-user-flow-captcha's Issues

Invisible mode example

We are having problems with invisible mode since we need to explicitly get token before form submission and it is not easy to do that with custom policies. Azure AD B2C adds their own event handlers to the form so it is not clear how to achieve invisible mode captcha integration.

Can you share an example using invisible mode?

Error Code AADB2C / Message: Please Complete the Captcha / Status: 400

Hello,

So I followed the steps from the description, by adding the html page and the azure function code in the http trigger.
After doing this, I go up to the Sign Up page, I complete it with the email than do the captcha and I get the check mark and after I press to create the account, but it doesn't go further and asks me to complete the captcha which is already completed.

I did look on the network to see the request and I see the following:
Error Code: AADB2C
Message: Please complete the Captcha
Status: 400

What is this ?
Or where should I look into ? to figure it out where is the problem.
What to check more exactly ?

I cannot figure it out where I missed something.
Thank you
@nickgmicrosoft
@mirkon90

Captcha Azure function

Hi Team,

I have followed this Git multiple times and tested the recaptcha with Azure function, However its not working.

This time I have imported the git repository and modified the local settings.json, selfassertion.html and index.js. Then ran the project .net code, its executing without any errors. However when I copy the local project URL and put it in browser, I am getting 404 error.

Even I tried to publish to Azure, I copied the Azure function URL that is also giving the same kind of error.

Are there any clear instructions on how to achieve the integration of recaptcha with Azure AD b2c, either through user flows or custom policies.

Kindly advice.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.