Integrate Microsoft OAuth with Web3Auth
This guide will cover the basics of how to integrate Web3Auth with OAuth 2.0, using Microsoft Github authentication. Trying to explain all the flow and covering all the problems that can happen during the integration. Web3Auth is employed to provide Ethereum private key access and public address.
Full example: https://github.com/Web3Auth/web3auth-core-kit-examples/tree/main/single-factor-auth-node/microsoft-oauth-connection
We use two web3auth libraries in this project: @web3auth/ethereum-provider
and
@web3auth/node-sdk
.
To install them, run: npm install @web3auth/ethereum-provider @web3auth/node-sdk
How it works?
When integrating Web3Auth with Microsoft Login the flow looks something like this:
-
When a user logs in with
Microsoft
, Microsoft sends a JWTid_token
to the app. This JWT token is sent to the Web3Auth SDK's login function. -
Finally, on successful validation of the JWT token, Web3Auth SDK will generate a private key for the user, in a self custodial way, resulting in easy onboarding for your user to the application.
Create a JWKS file
A JWKS stands for JSON Web Key Set. It is a set of keys containing the public keys that should be used to verify any JSON Web Token (JWT) issued by the authorization server and signed using the RS256 signing algorithm.
If you don't know how to create a JWKS, you can follow this web3auth tutorial. This file must be located on a public endpoint. To test the example, you will need to modify the file located in the root directory of the project.
Both the private and the public keys, that you used to create the JWKS, must be in the server
directory. In our project, they are called private.pem
and public.pem
.
Set up Custom JWT Verifier
To create a custom verifier for your JWT Provider, you'll need the follow this steps:
-
The verifier Identifier, which would be the name. This name should be used in your .ENV file as
WEB3AUTH_VERIFIER
. -
JWT Verifier ID: JWT Verifier ID is the unique identifier to publicly represent a user on a verifier. e.g:
sub
,email
, or even a custom field of your JWT payload that is unique in your system for each user. In this case, we are using thesub
field. -
JWK Endpoint: An endpoint containing the JWKS used to sign the JWT. In my example I'm using a static github url file.
-
In the Select JWT Validation. You will add
iss
andaud
fields.- The
iss
field is the issuer of the JWT. In this case "https://login.microsoftonline.com/" - The
aud
field is the audience of the JWT. In this case "http://login.microsoftonline.com/{AZURE_TENANT_ID}/v2.0/token"
- The
Configure a new Application in Microsoft Entra.
After creating the app, you'll need the Application (client) ID and the Directory (tenant) ID into the .ENV file. Then you can add a redirect URI (in our example is http://localhost:5005/ms/callback)
Finally in "Certificates & secrets" -> Client Secret -> New client secret and copy the value to the
.ENV file as AZURE_CLIENT_SECRET
.