这是提交给Permit.io授权挑战的:重新构想的API优先的授权方案。
目录- 我做了什么项目
- 一步一步的操作流程
- 演示
- 我的旅程
-
API优先的授权
-
ESP32的Arduino代码
- 用于Permit.io的PHP代理(ESP32集成版)
- 结论
城堡之钥是一款互动的现实世界访问控制游戏,以中世纪城堡为灵感,并利用Permit.io实现外部授权。
玩家扮演国王、厨师或仆人的角色,努力闯入城堡的不同楼层。进入权限由外部规则决定,而不是预先设定的规则。
略
它是怎么工作的(一步一步详解)
- 玩家将RFID卡靠近连接到ESP32的读卡器。
- ESP32读取UID并将包含UID和所需操作(如
access_floor_2
)的请求发送到proxy.php
。 proxy.php
脚本使用私有令牌安全地将请求转发给Permit.io API。- Permit.io返回访问是否被允许(
"allow": true
)。 - ESP32处理响应并激活相应的LED:
-
✅ 绿色 = 允许访问
- ❌ 红色 = 拒绝访问
- 此外,ESP32将日志发送到
log.php
,用于在Web界面中进行显示或调试。
- 此外,ESP32将日志发送到
⚠️ 注意:
log.php
不是 Permit.io 运行所必需的文件。它用于在游戏中界面上显示访问记录。
🎮 试玩试试看
👉 观看完整演示(Web + 硬件演示):
🎮 或者试试网页版(无需硬件):试玩《城堡之钥》
💡 我的旅途
这个项目最初是一个将访问控制通过游戏化变得生动的想法。我想要创造的不仅仅是UI按钮或权限表,而是某种有形的、物理的且互动性强的东西。这就是为什么我决定整合如RFID读卡器等实际硬件,甚至很快会加入生物识别感应器,让体验既有趣又具有沉浸感。
🔐 API优先授权策略
以下是我ESP32上实际运行的.ino代码。它通过RC522模块读取RFID卡,将UID发送到安全的PHP代理服务器,并从Permit.io获取实时授权结果。
🔌 ESP32 Arduino 代码
#include <WiFi.h>
#include <HTTPClient.h>
#include <SPI.h>
#include <MFRC522.h>
const char* ssid = "你的WiFi名称";
const char* password = "你的WiFi密码";
const char* permitUrl = "https://example.com/api/proxy.php"; // 你的私有端点
const char* logUrl = "https://example.com/app/log.php";
#define RST_PIN 22
#define SS_PIN 21
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Serial.begin(115200);
SPI.begin();
rfid.PCD_Init();
WiFi.begin(ssid, password);
Serial.print("连接到WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi连接成功!");
}
void loop() {
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) {
return;
}
String uid = "";
for (byte i = 0; i < rfid.uid.size; i++) {
uid += String(rfid.uid.uidByte[i], HEX);
}
uid.toUpperCase();
Serial.print("检测到卡片: ");
Serial.println(uid);
// 发送 Permit.io 授权请求
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(permitUrl);
http.addHeader("Content-Type", "application/json");
String payload = "{\"user\":{\"key\":\"" + uid + "\"},\"action\":\"" + action + "\",\"resource\":{\"type\":\"document\",\"tenant\":\"devs\"},\"context\":{}}";
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Permit.io 响应: " + response);
if (response.indexOf("\"允许\":true") > 0) {
Serial.println("✅ 访问已授权");
accessGranted = true;
// 将GREEN_LED设置为高电平
} else {
Serial.println("❌ 访问被拒绝");
// 将RED_LED设置为高电平
}
} else {
Serial.print("发送请求错误: ");
Serial.println(httpResponseCode);
}
http.end();
// 发送访问日志
HTTPClient logHttp;
logHttp.begin(logUrl);
logHttp.addHeader("Content-Type", "application/json");
String logPayload = "{\"uid\":\"" + uid + "\",\"action\":\"" + action + "\",\"status\":\"" + (accessGranted ? "授予" : "拒绝") + "\"}";
logHttp.POST(logPayload);
logHttp.end();
}
delay(3000); // 防止重复扫描
}
进入全屏 / 退出全屏
PHP 代理服务器用于 Permit.io 的 ESP32 集成
为了保护你的Permit.io API密钥安全,我创建了一个简单的proxy.php
文件。这个PHP脚本接收ESP32的请求,而不是直接与Permit.io通信。
完整的代码如下:
<?php
// proxy.php - 接收来自ESP32的数据并通过Permit.io检查权限
// 从ESP32读取传入的JSON
$body = file_get_contents('php://input');
$data = json_decode($body, true);
// 为Permit.io准备payload
$payload = json_encode([
"user" => [
"key" => $data['user'] // 来自RFID卡的UID
],
"action" => $data['action'], // 示例:"access_floor_1"
"resource" => [
"type" => "document", // 请根据您的配置替换为适当的资源类型
"tenant" => "devs" // 租户ID(如有需要)
],
"context" => new stdClass() // 额外的上下文信息(目前为空)
]);
// 向Permit.io发送请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.permit.io/v2/allowed");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer YOUR_PERMIT_API_KEY", // 请使用您的Permit.io API密钥进行替换
"Content-Type: application/json"
]);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// 最后,将结果返回给ESP32
http_response_code($http_code);
echo $response;
切换到全屏,退出全屏
结语
这个项目展示了将真实世界硬件与Permit.io集成是多么简单。只需几行代码和一些外部策略,你就能安全地控制访问权限——无需硬写死任何内容。
[MCP] / [LLM] / [RAG] / [SSE]
共同學(xué)習(xí),寫下你的評論
評論加載中...
作者其他優(yōu)質(zhì)文章