Skip to content

Commit 3152b8b

Browse files
committed
feat: make code clearer
1 parent 0afae53 commit 3152b8b

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

src/content/docs/machine-to-machine-applications/organization-scoped-m2m-apps/enforce-org-m2m-access-in-your-api.mdx

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ relatedArticles:
1111

1212
If you're using [org-scoped machine-to-machine (M2M) apps](/machine-to-machine-applications/organization-scoped-m2m-apps/m2m-applications-for-organizations/), Kinde will automatically include a trusted `org_code` claim in the token. You can then enforce access control in your own APIs using this claim.
1313

14-
1514
## When to enforce org context
1615

1716
You should validate `org_code` in any API route or resource that is **tenant-specific**, such as:
@@ -33,35 +32,31 @@ When you receive a token, decode it and check:
3332
## Example in Node.js (Express)
3433

3534
```js
36-
const jwt = require('jsonwebtoken')
37-
35+
const jwt = require("jsonwebtoken");
3836
function verifyOrgAccess(req, res, next) {
39-
const authHeader = req.headers.authorization
40-
const token = authHeader?.split(' ')[1]
41-
42-
if (!token) return res.status(401).send('Missing token')
43-
44-
const decoded = jwt.decode(token)
45-
37+
const authHeader = req.headers.authorization;
38+
const token = authHeader?.split(" ")[1];
39+
if (!token) return res.status(401).send("Missing token");
40+
// If Kinde is **not** validating the token for you,
41+
// verify the signature instead of a raw decode.
42+
// const PUBLIC_KEY = ... // fetch from /.well-known/openid-configuration
43+
const decoded = jwt.verify(token, PUBLIC_KEY, {algorithms: ["RS256"]});
4644
// Check for required claims
47-
if (!decoded?.org_code) return res.status(403).send('Token not scoped to an organization')
48-
49-
const orgFromRoute = req.params.org_code
50-
45+
if (!decoded?.org_code) return res.status(403).send("Token not scoped to an organization");
46+
const orgFromRoute = req.params.org_code;
5147
if (decoded.org_code !== orgFromRoute) {
52-
return res.status(403).send('Token does not match organization')
48+
return res.status(403).send("Token does not match organization");
5349
}
54-
55-
next()
50+
next();
5651
}
5752
```
5853
5954
Then apply the middleware:
6055
6156
```js
62-
app.get('/orgs/:org_code/users', verifyOrgAccess, (req, res) => {
57+
app.get("/orgs/:org_code/users", verifyOrgAccess, (req, res) => {
6358
// safe to fetch users for this org
64-
})
59+
});
6560
```
6661
6762
## Notes
@@ -76,6 +71,7 @@ app.get('/orgs/:org_code/users', verifyOrgAccess, (req, res) => {
7671
If the token has no `org_code`, treat it as **not authorized** to access org-specific resources unless explicitly allowed.
7772

7873
You may choose to:
74+
7975
- Reject the request
8076
- Allow access only to system-level endpoints
8177
- Prompt developers to use org-scoped apps instead

0 commit comments

Comments
 (0)