使用 Permit.io🔏,只需几分钟即可为您的应用添加授权层⏱️
还在为编写权限和授权码而苦恼吗?兄弟,我真替你感到难过!我之前也为此苦恼不已,直到我发现了 Permit.io 🥹
在这篇博客中,我将介绍 Permit 的一些基础知识,以及它如何顺利处理应用程序中的授权部分。
但在开始之前,我们先来了解一下“什么是授权? ”。
授权(简称 Authorization AuthZ)是身份访问管理 (IAM) 中最关键、最先进的部分。它控制着产品内部的访问权限。在这里,您最常遇到的角色、权限、所有权、应用程序用户、邀请、审批流程以及许多其他常见模式。您可以访问Permit 创始人分享的官方博客,了解更多关于授权的信息。
了解Permit.io 🔏
授权是每个应用程序的必要条件。没有授权,您的应用程序就无法控制许多参数的访问权限,从而变得不安全。🔑
授权机制至关重要,它确保合适的人员和服务能够访问合适的资源。Permit允许您将授权策略与应用程序代码解耦,为您提供构建完美应用程序授权层所需的一切——集中式控制面板、SDK、API 和用于授权的微服务。这些组件使您能够轻松地在应用程序中设置决策点和执行点。更重要的是,所有授权决策均由您自行完成,零延迟。
🧑💻 Permit 的云服务为您提供了一整套无需代码的工具,让您以及您组织中的每个相关人员(例如,产品、安全、支持或销售人员)都能安全地管理应用程序中的授权,而无需任何编码知识。
🍩 Permit 为您提供了一整套预构建的 UI 组件,这些组件提供功能齐全的访问控制,使您能够安全地将它们委派给最终用户。
👷♂️使用 Permit.io,现在可以非常轻松地配置ABAC(基于属性的访问控制)和 ReBAC(基于关系的访问控制)等策略模型,甚至它们的组合。
Permit.io 快速入门教程🧑💻
准备好亲自动手使用 Permit 为你的应用程序添加授权功能了吗?在本教程中,我们将通过几个步骤将 Permit.io 集成到你的应用程序中。
开始之前,请先在 Permit.io 创建一个帐户,以便获取 API 密钥并访问 Permit.io 控制面板,您可以在其中根据您的选择和决定为用户分配角色。如果您遇到任何问题,请点击此链接查看快速指南:Permit 帐户设置指南
您还可以通过单击“用户菜单”>“复制环境密钥”来复制活动环境的环境 API 密钥。
2️⃣设置您的 PDP(策略决策点)容器
PDP 作为您的授权微服务,以 Docker 容器的形式提供,供您使用;或者以云版本的形式提供,以便快速进行实验。
我们将使用 Docker 容器来实现这一点。
- 拉取 Permit.io 镜像(假设您已安装 Docker,如果没有,请点击安装)
docker pull permitio/pdp-v2:latest
- 请在以下命令中将 API 密钥替换为您的密钥。
docker run -it -p 7766:7000 --env PDP_DEBUG=True --env PDP_API_KEY=<YOUR_API_KEY> permitio/pdp-v2:latest
- 现在,您的 PDP 容器正在运行,请使用以下命令检查或验证正在运行的容器。
docker ps
3️⃣是时候将 Permit.io 添加到您的应用中了!
我们将使用 Permit SDK。您可以将 Permit.io 集成到多种编程语言中。
在本教程中,我们将使用nodeJs Sdk。
- 安装 Permit.io SDK
npm install permitio
- 将 SDK 导入到您的代码中
import { Permit } from "permitio";
- 创建 SDK 的新实例。
// This line initializes the SDK and connects your Node.js app
// to the Permit.io PDP container you've set up in the previous step.
const permit = new Permit({
// your API Key
token: "[YOUR_API_KEY]",
// in production, you might need to change this url to fit your deployment
pdp: "http://localhost:7766",
// if you want the SDK to emit logs, uncomment this:
// log: {
// level: "debug",
// },
// The SDK returns false if you get a timeout / network error
// if you want it to throw an error instead, and let you handle this, uncomment this:
// throwOnError: true,
});
希望您现在添加 API 密钥时不会遇到任何问题 😉
4️⃣使用 SDK 检查权限
您可以使用 SDK 运行权限检查permit.check(),并传入 3 个参数:
user:用于识别执行操作的用户的唯一字符串 ID。这通常是用户密钥。action:所执行的动作。resource:执行操作的资源(对象)。
const permitted = await permit.check("rohan@xyz.co", "modify", "document");
if (permitted) {
console.log("Rohan is PERMITTED to modify a document");
} else {
console.log("Rohan is NOT PERMITTED to modify a document");
}
通常情况下,您会使用所选身份验证解决方案提供的唯一标识符,而不是电子邮件。
如果你的应用程序中涉及多个租户,permit.check()可以将租户作为资源的一部分传递。
传入的租户信息必须是“租户”tenant id或“租户” tenant key。所有租户 API 列表
例子:
const permitted = await permit.check(
// the key of the user
"rohan@xyz.co",
// the action
"modify",
{
type: "document",
tenant: "permit_io",
}
);
您还可以根据 ABAC 策略检查权限。ABAC策略由用户集和资源集组成。
如果我们正在运行permit.check()ABAC 策略,我们可以将即时属性附加到用户user和资源resource。这些属性是合并(并覆盖)了持久化到允许的 API 的用户和资源属性。
const permitted = await permit.check(
// the user object
{
// the user key
key: "check@permit.io",
// just-in-time attributes on the user
attributes: {
location: "India",
department: "Engineering",
},
},
// the action the user is trying to do
"action",
// Resource
{
// the type of the resource (the resource key)
type: "resource",
// just-in-time attributes on the resource
attributes: {
hasApproval: "true",
},
// the tenant the resource belong to
tenant: "tenant",
}
);
5️⃣完整申请示例:
const { Permit } = require("permitio");
const express = require("express");
const app = express();
const port = 4000;
// This line initializes the SDK and connects your Node.js app
// to the Permit.io PDP container you've set up in the previous step.
const permit = new Permit({
// in production, you might need to change this url to fit your deployment
pdp: "http://localhost:7766",
// your secret API Key
token: "[YOUR_API_KEY]",
});
// You can open http://localhost:4000 to invoke this http
// endpoint, and see the outcome of the permission check.
app.get("/", async (req, res) => {
// Example user object
// You would usually get the user from your authentication layer (e.g. Auth0, Cognito, etc) via a JWT token or a database.
const user = {
key: "[A_USER_ID]",
firstName: "John",
lastName: "Smith",
email: "john@permit.io",
};
// check for permissions to a resource and action (in this example, create a document)
const permitted = await permit.check(user.key, "create", "document");
if (permitted) {
res.status(200).send(`${user.firstName} ${user.lastName} is PERMITTED to create document!`);
} else {
res.status(403).send(`${user.firstName} ${user.lastName} is NOT PERMITTED to create document!`);
}
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
导航至审核日志页面,查看您的权限请求结果。
如果您想用您熟悉的语言运行本教程,或者想用其他语言测试 Permit.io,请点击此链接:选择您的语言
结束啦……🥹
Permit 是一个全栈授权即服务解决方案,可让您应用安全、精细且用户友好的权限管理。
Permit Slack 社区是寻求帮助的好地方,您可以在这里规划实施方案、使用 Permit 功能,或解答您可能遇到的任何其他相关问题。
在本博客的第二部分,我将介绍 OPAL——开放策略管理层(Open Policy Administration Layer,简称 OPAL)。OPAL 是一个由 Permit.io 团队开发和维护的开源项目,它作为策略引擎的管理层,能够实时检测策略及其数据的变更,并将实时更新推送给您的代理。
Permit.io 官方网站:https://www.permit.io/
Permit.io 官方文档:https://docs.permit.io/quickstart
感谢你读到这里!你真棒!祝你今天工作顺利!💖
文章来源:https://dev.to/rohan_sharma/add-an-authorization-layer-to-your-app-with-permitio-in-a-few-minutes-32d6


