node-red bearer token

Node-RED Tutorial: Secure Flows with Bearer Tokens

By Emily Martinez
AD

Node-RED, an open-source, flow-based, lightweight programming tool, enables users to wire together flows by dragging and connecting nodes using a web browser interface. When it comes to securing these flows, particularly when dealing with APIs, understanding and implementing bearer tokens becomes vital. This article walks you through the concept of bearer tokens and their integration in Node-RED.

In the context of web APIs, a bearer token is a type of access token that allows access to the API resources server. A client, after obtaining a bearer token, can make an API request by including the token in the 'Authorization' header, proving that it's authorized to access the resource. This is what makes bearer tokens 'bearer agnostic' - they don't care about who the bearer is, only that they can present the token.

Understanding Bearer Tokens in Node-RED

In Node-RED, the node-red-bearer-authentication node provides a way to protect flows using JSON Web Tokens (JWT), which are a type of bearer token. Here's how you can use it.

AD
Token 12
Token 12
Barkeep
Barkeep
a woman with red hair and blue eyes in a round frame on a white background
a woman with red hair and blue eyes in a round frame on a white background

Firstly, you need to understand how to obtain a valid JWT and how to use it in your flows. The basic process involves requesting a JWT by authenticating with a valid user account, and then using that JWT to access your secure flows.

Obtaining a JWT

The request-http-out and function nodes, when set up correctly, can be used to make an authentication request to your chosen API and return a JWT. Further configuration of the function node is required to parse and decode the JSON response, extracting the JWT.

"DND Token Border Frame: Crimson Vampire Portraits"
"DND Token Border Frame: Crimson Vampire Portraits"
Roll20 Token
Roll20 Token
The roaring knight deltarune dnd token border
The roaring knight deltarune dnd token border

For instance, imagine your API uses standard OpenID Connect for authentication. You'd first configure the request-http-out node to make a POST request to your API's token endpoint. Then, you'd feed the response into a function node, which decodes the JSON response and extracts the JWT:

```javascript let response = msg.payload; let decoded = JSON.parse(response); msg.payload = decoded.access_token; return msg; ```

Configuring the bearer-auth node

Once you have a valid JWT, you can configure the bearer-auth node to accept it. This node uses the 'Authorization' header in incoming requests to check that the included token is valid and authorized to access a particular flow or node. You need to configure this node correctly to point to the API's JWT secret, and specify the audience and issuer.

AD

For a typical usage, the secret is a public key obtained from your token endpoint, the audience is the client ID of your Node-RED instance, and the issuer is the token endpoint URL.

Samurai Gear
Samurai Gear
Peasant
Peasant
Token Hoard | Caeora Drider Token, Spider Token 5e, Spider Swarm Token, Giant Spider Token 5e, Giant Spider Token Dnd, Phase Spider Token, Night Hag Token, Dnd Web Token, Dnd Spider Token
Token Hoard | Caeora Drider Token, Spider Token 5e, Spider Swarm Token, Giant Spider Token 5e, Giant Spider Token Dnd, Phase Spider Token, Night Hag Token, Dnd Web Token, Dnd Spider Token
an image of a circular frame made out of red and black dragon like branches with sharp teeth
an image of a circular frame made out of red and black dragon like branches with sharp teeth
a purple circular frame with red eyes and spikes on the edges, in front of a white background
a purple circular frame with red eyes and spikes on the edges, in front of a white background
Roll20 Token
Roll20 Token

Securing Node-RED Flows with Bearer Tokens

Now, let's look at two practical use-cases for bearer tokens in Node-RED: securing an entire flow and securing a single node.

Securing an entire flow is done by wrapping it in a `route` node, with the bearer-auth node as one of its children. This way, all incoming messages to that flow must first pass through the bearer-auth node and provide a valid JWT.

AD

Protecting single nodes with bearer tokens

Securing a single node involves inserting the bearer-auth node in the flow, before the node you want to protect. This ensures the message containing the request hits the bearer-auth node first, and only passes if the token is valid.

an image of a man with a staff in front of a circular frame that has lightning bolts on it
an image of a man with a staff in front of a circular frame that has lightning bolts on it
Roll20 Tokens, Bandit Captain Token, Epic Cleric Tokens D&d, Hucek's Tavern Tokens, Pathfinder Wounded Tokens, Party Token Dnd, Commoner Tokens, Roll20 Token Packs, Dnd Npc Tokens
Roll20 Tokens, Bandit Captain Token, Epic Cleric Tokens D&d, Hucek's Tavern Tokens, Pathfinder Wounded Tokens, Party Token Dnd, Commoner Tokens, Roll20 Token Packs, Dnd Npc Tokens
Roll20 Token
Roll20 Token

In most cases, you'd want to configure your API to return a JWT with an appropriate lifespan, and refresh the token as necessary. Depending on your API's implementation, this may involve normalizing the lifespan of your JWT with the `access-token-refresh` node, or using a Node-RED dashboard with an 'eye' symbol to force access token renewal.

Managing bearer tokens in Node-RED requires careful configuration but ultimately provides a robust way to secure your flows. Staying informed about best practices and continuously updating your configurations ensures the highest level of protection for your data.

AD
AD