Implementation Walkthrough
Detailed walkthrough of an implementation of ZBD Login.
Client Side
When setting up OAuth2 from client side, you have two options:
- Build the entire flow yourself
- Make a request for building the authorization URL
- Handling the Webview with the created URL
- Getting the
code
after authenticating with ZEBEDE - Making another request for getting the token
- Make use of some of the available OAuth libraries available for the language you are working on, which would handle the initial steps of the flow
- Install and set up configs accordingly to the library
- If PKCE is available on the library you might receive the
code_verifier
from the response, after authenticating. - If PKCE is not available, you might need to build the code_challenge and code_verifier yourself.
- If PKCE is available on the library you might receive the
- Getting
code
as response - Making the request for getting the token
- Install and set up configs accordingly to the library
If you chose the second option, there are open-source libraries for handling OAuth2 on Mobile using AppAuth (iOS and Android), as well as for React Native using react-native-app-auth.
Server Side
Node.js
This flow go over all the steps for Logging in with ZBD, from Building the authorization URL, with proper PKCE Keys, to getting the accessToken and calling ZBD API.
If you are using a library that provides PKCE guidelines, it will most likely be able to handle the initial flow (building auth URL and getting the code) just fine. In this case, feel free to jump to the Token Section.
Building Authorization URL
In case you are setting up the PKCE flow yourself (no usage of libraries), we will need to setup the code_challenge
from a code_verifier
which will be used later on, on token request.
Code explanation
getZBDLoginUrl
function- In this function, we initially Generate PKCE keys:
verifier
andcode_challenge
- Then,
findOneAndUpdate
represents an generic ORM call for updating the User model, on the database. This way we can, afterwards, retrieve that value for getting the token. - We then Build the URL suffix
- Url prefix and generated from
createZBDOauth
, which usesoauth
library to do so
- In this function, we initially Generate PKCE keys:
generatePKCE
- This is where we setup PKCE Keys.
- initially we set the code verifier:
const verifier = base64URLEncode(crypto.randomBytes(32));
- From the code verifier, the code_challenge is created:
const challenge = base64URLEncode(sha256(verifier));
- initially we set the code verifier:
- This is where we setup PKCE Keys.
When logging in using the provided URL, you will get a response including state
and code
which should be user now for getting the token, and user data:
Getting the token and accessing ZBD API
Code explanation
Once the user is successfully authenticated on ZBD Client, there is a redirect back to your app, sending code
and state
as query params.
Those values are send as payload on this next request:
-
First of all, remember we registered the verifier on the User (generic ORM update)? now we retrieve that value:
const user = await User.findOne({ userId: state });
-
With the proper verifier, we can now make a post request to the token endpoint, and we should have the token returned.
const res = await getAccessToken({…body})
-
Now, by adding the accessToken to request headers, we can fetch data from ZBD API
const response = await getUserData(access_token);
You are now able to proceed with your login logic and send the user data accordingly ⚡
Code Examples
Was this page helpful?