hasura graphql auth-webhook api 说明
hasura graphql 生产的使用是推荐使用webhook 进行角色访问控制的,官方同时提供了一个nodejs
的简单demo
代码
git clone https://github.com/hasura/sample-auth-webhook
代码说明
项目结构
api 格式说明
auth0 auth0/auth0Handler.js
var express = require('express');
var auth0Router = express.Router();
var requestClient = require('request');
var auth0Domain = process.env.AUTH_ZERO_DOMAIN;
/*
Auth webhook handler for auth0
Flow:
1) Expects access_token to be sent as 'Authorization: Bearer <access-token>
2) Verified access_token by fetching /userinfo endpoint from auth0
Usage:
1) From your application, when you call Hasura's GraphQL APIs remember to send the access_token from auth0 as an authorization header
2) Replace the url (https://test-hasura.auth0.com/userinfo) in the code below with your own auth0 app url
*/
auth0Router.route('/webhook').get((request, response) => {
// Throw 500 if auth0 domain is not configured
if (!auth0Domain) {
response.status(500).send('Auth0 domain not configured');
return;
}
var token = request.get('Authorization');
if (!token) {
response.json({'x-hasura-role': 'anonymous'});
return;
} else {
// Fetch information about this user from
// auth0 to validate this token
// NOTE: Replace the URL with your own auth0 app url
var options = {
url: `https://${auth0Domain}/userinfo`,
headers: {
Authorization: token,
'Content-Type': 'application/json'
}
};
requestClient(options, (err, res, body) => {
if (!err && res.statusCode == 200) {
var userInfo = JSON.parse(body);
console.log(userInfo); //debug
var hasuraVariables = {
'X-Hasura-User-Id': userInfo.sub,
'X-Hasura-Role': 'user'
};
console.log(hasuraVariables); // For debug
response.json(hasuraVariables);
} else {
// Error response from auth0
console.log(err, res, body);
response.json({'x-hasura-role': 'anonymous'});
return;
}
});
}
});
module.exports = auth0Router;
普通rest api: server.js
app.get('/simple/webhook', (request, response) => {
// Extract token from request
var token = request.get('Authorization');
// Fetch user_id that is associated with this token
fetchUserInfo(token, (result) => {
// Return appropriate response to Hasura
var hasuraVariables = {
'X-Hasura-Role': 'user', // result.role
'X-Hasura-User-Id': '1' // result.user_id
};
response.json(hasuraVariables);
});
});
上边的代码比较简单就是提供一个webhook 的rest api 地址,获取请求中的token (Authorization)
之后进行判定,并返回使用json表示,用户对应的role 以及user-id (X-Hasura-User-Id 、X-Hasura-Role)
参考资料
https://github.com/hasura/sample-auth-webhook
https://docs.hasura.io/1.0/graphql/manual/auth/index.html
hasura graphql auth-webhook api 说明的更多相关文章
- sofa graphql 2 rest api webhook 试用
sofa 的webhook实际上就是将graphql 的subscription 进行了扩展,当接受到sub 请求的时候 再做一次http 的转发处理,方便rest api 的访问 环境准备 环境还是 ...
- 通过torodb && hasura graphql 让mongodb 快速支持graphql api
torodb 可以方便的将mongo 数据实时同步到pg,hasura graphql 可以方便的将pg 数据暴露为graphql api,集成在一起真的很方便 环境准备 docker-compose ...
- Hasura GraphQL schema 生成是如何工作的
不像大部分的graphql 引擎,使用标准的graphql 规范的处理模型,Hasura graphql 不存在resolver 的概念(实际上是有的,只是转换为了sql语法) 以下是Hasura g ...
- hasura graphql server event trigger 试用
hasura graphql server 是一个很不错的graphql 引擎,当前版本已经支持event triiger 了 使用此功能我们可以方便的集成webhook功能,实现灵活,稳定,快捷的消 ...
- 记一次通过c#运用GraphQL调用Github api
阅读目录 GraphQL是什么 .net下如何运用GraphQL 运用GraphQL调用Github api 结语 一.Graphql是什么 最近在折腾使用Github api做个微信小程序练练手,本 ...
- hasura graphql server 集成gatsby
hasura graphql server 社区基于gatsby-source-graphql 开发了gatsby-postgres-graphql 插件, 可以快速的开发丰富的网站 基本使用 安装h ...
- hasura graphql subscriptions 使用
subscriptions graphql 的一项实时数据推送的功能,还是很方便的,自己在直接使用subscriptions-transport-ws npm 包 的时候运行一直有错误(主要是依赖 ...
- hasura graphql pg 自定义函数的使用
hasura graphql 的安装可以参考相关项目 创建函数 数据表创建 CREATE TABLE sql_function_table ( id SERIAL PRIMARY KEY, inp ...
- 人人都是 API 设计师:我对 RESTful API、GraphQL、RPC API 的思考
原文地址:梁桂钊的博客 博客地址:http://blog.720ui.com 欢迎关注公众号:「服务端思维」.一群同频者,一起成长,一起精进,打破认知的局限性. 有一段时间没怎么写文章了,今天提笔写一 ...
随机推荐
- C++11多线程教学
转自:http://www.cnblogs.com/lidabo/p/3908705.html 本篇教学代码可在GitHub获得:https://github.com/sol-prog/threads ...
- 微信小程序:本地资源图片无法通过 WXSS 获取,可以使用网络图片或者 base64或者使用image标签
微信小程序:本地资源图片无法通过 WXSS 获取,可以使用网络图片或者 base64或者使用image标签 一.问题 报错信息: VM696:2 pages/user/user.wxss 中的本地资源 ...
- Kali2018.1
目录 制作U盘启动盘 安装 Kali Linux 之后的事 更新源 配置 Zsh 配置 Vim 修改 Firefox 语言为中文 安装 Gnome 扩展 美化 安装 Google 拼音输入法 安装常用 ...
- System.data.sqlite安装
在这个页面可以下载安装不同版本的 http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki x86的版本可以在64位机 ...
- 安全之路:Web渗透技术及实战案例解析(第2版)
安全之路:Web渗透技术及实战案例解析(第2版)
- python2.7和python3.6共存,使用pip安装第三方库
因为一般情况下,window命令行运行pip,默认的情况是运行python3.6的pip,安装第三方库的路径也是python3.6,安装路径是: 如何运行在2.7环境下安装PIP呢?有网上的教程说需要 ...
- linux文件锁的应用,POSIX,unix标准,linux标准
1. perl,flock加锁.java也能加锁. 2. 先创建文件并打开,才能加锁(写打开?). 3. 可以用于判断进程是否一直在运行(用另一进程判断),如果锁一直在,则进程在:锁不在,则原进程或意 ...
- 使用由 Intel MKL 支持的 R
我们通常使用的 R 版本是单线程的,即只使用一个 CPU 线程运行所有 R 代码.这样的好处是运行模型比较简单且安全,但是它并没有利用多核计算.Microsoft R Open(MRO,https:/ ...
- 记一次加载js不全报错的原因总结
1.运营商广告 dns 劫持2.浏览器本身并发数3.服务器配置keepAliveTimeout=04.加载文件过大5.
- ADSL拨号上网或者光纤上网设置概要(原创)
不管是在梧州设置光纤还是在太平设置ADSL拨号上网每次设置上网一体机的时候都是遇到各种麻烦...这次又是弄了N久,每次问题各不一样.总结一下操作过程,方便以后又遇问题回头查询自个微博.一.设置电话线的 ...