NodeJS SDK
    • 01 Sep 2024
    • 1 Minute to read
    • Dark
      Light
    • PDF

    NodeJS SDK

    • Dark
      Light
    • PDF

    Article summary

    {{snippet.SDKAuthLink}}

    PlainID Policy Decision Point (PDP) SDK

    The PlainID PDP SDK is a library that can be used to easily integrate the PlainID's Externalized Authorization into your applications.

    The SDK utilizes the following functionality of the PlainID PDP:

    • Access Decision - Evaluates PERMIT/DENY access requests
    • Resolution Decision - Evaluates data access requests
      ### Installation
    npm i plainid-pdp-sdk
    

    Access Enforcement (API)

    const express = require('express')
    const pdp = require('plainid-pdp-sdk');
    
    const pdpClient = new pdp.v5.Client({ 
        url: 'https://demo.plainid.cloud', 
        clientId: 'P6POYCA8QY5', clientSecret: 'XRp8OXVugWlnDZjP8', authorizerId: 'APA9G5HV7WXN9C' 
    })
    
    const app = express()
    app.use(async (req, res, next) => {
        const payload = {
            method: req.method,
            headers: req.getHeaders(),
            url: req.url,
            body: req.body,
            meta: { runtimeFineTune: { includeIdentity: 'true' } }
        }
    
        const response = await pdpClient.accessDecision().evaluate(payload)
        if (!response.access) {
            throw new Error('Forbidden')
        }
    })
    

    Data Enforcement (SQL)

    const knex = require('knex');
    const pdp = require('plainid-pdp-sdk');
    const pdpClient = new pdp.v3.Client({
        url: 'https://demo.plainid.cloud',
        clientId: 'P6POYCA8QY5', clientSecret: 'XRp8OXVugWlnDZjP8', authorizerId: 'APA9G5HV7WXN9C'
    })
    
    const tableName = 'members'
    const Members = knex(tableName) // initiate query builder
    
    const payload = {
        entityId: 'john.doe@gmail.com',
        entityTypeId: 'User',
        includeAssetAttributes: true,
    }
    const response = await pdpClient.resolutionDecision().evaluate(payload)
    const tableRestrictions = response.resolution.getResourceRestrictions(tableName)!
    
    // Column-level enforcement
    const selectColumns = []
    tableRestrictions.getAssets().forEach(asset => {
        selectColumns.push(asset.isMasked() ? `${asset.getMaskAs()} as ${asset.getName()}` : asset.getName())
    })
    Members.select(selectColumns)
    
    // Row-level enforcement
    const whereClause = tableRestrictions.getConditionSql() // ( ( ('ID'  > 1 ) )  AND ( ('Country'  LIKE '%Morocco%' ) ) )
    Members.where(knex.raw(whereClause))
    
    const resultSet = await Members
    

    Was this article helpful?

    What's Next