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 说明的更多相关文章

  1. sofa graphql 2 rest api webhook 试用

    sofa 的webhook实际上就是将graphql 的subscription 进行了扩展,当接受到sub 请求的时候 再做一次http 的转发处理,方便rest api 的访问 环境准备 环境还是 ...

  2. 通过torodb && hasura graphql 让mongodb 快速支持graphql api

    torodb 可以方便的将mongo 数据实时同步到pg,hasura graphql 可以方便的将pg 数据暴露为graphql api,集成在一起真的很方便 环境准备 docker-compose ...

  3. Hasura GraphQL schema 生成是如何工作的

    不像大部分的graphql 引擎,使用标准的graphql 规范的处理模型,Hasura graphql 不存在resolver 的概念(实际上是有的,只是转换为了sql语法) 以下是Hasura g ...

  4. hasura graphql server event trigger 试用

    hasura graphql server 是一个很不错的graphql 引擎,当前版本已经支持event triiger 了 使用此功能我们可以方便的集成webhook功能,实现灵活,稳定,快捷的消 ...

  5. 记一次通过c#运用GraphQL调用Github api

    阅读目录 GraphQL是什么 .net下如何运用GraphQL 运用GraphQL调用Github api 结语 一.Graphql是什么 最近在折腾使用Github api做个微信小程序练练手,本 ...

  6. hasura graphql server 集成gatsby

    hasura graphql server 社区基于gatsby-source-graphql 开发了gatsby-postgres-graphql 插件, 可以快速的开发丰富的网站 基本使用 安装h ...

  7. hasura graphql subscriptions 使用

      subscriptions graphql 的一项实时数据推送的功能,还是很方便的,自己在直接使用subscriptions-transport-ws npm 包 的时候运行一直有错误(主要是依赖 ...

  8. hasura graphql pg 自定义函数的使用

      hasura graphql 的安装可以参考相关项目 创建函数 数据表创建 CREATE TABLE sql_function_table ( id SERIAL PRIMARY KEY, inp ...

  9. 人人都是 API 设计师:我对 RESTful API、GraphQL、RPC API 的思考

    原文地址:梁桂钊的博客 博客地址:http://blog.720ui.com 欢迎关注公众号:「服务端思维」.一群同频者,一起成长,一起精进,打破认知的局限性. 有一段时间没怎么写文章了,今天提笔写一 ...

随机推荐

  1. uva672

      Gangsters  N gangsters are going to a restaurant. The i-th gangster comes at the time Ti and has t ...

  2. SharedObject对象聊天室

    本博推荐文章快速导航: Sql Server2005 Transact-SQL 新兵器学习MCAD学习 代码阅读总结 ASP.NET状态管理 DB(数据库)WAPWinFormFlex,Fms aie ...

  3. 远程连接软件TeamViewer

    (1)先在windows下安装Teamviewer软件,地址:https://pan.baidu.com/s/1rWxRBtNbn3OMmg-8YaYWRQ (2)再在linux下安装Teamview ...

  4. 【图像处理】计算Haar特征个数

    http://blog.csdn.net/xiaowei_cqu/article/details/8216109 Haar特征/矩形特征 Haar特征本身并不复杂,就是用图中黑色矩形所有像素值的和减去 ...

  5. c++第二十二天

    p120~p124: 表达式 1.表达式由一个或者多个运算对象组成. 2.最简单的表达式是字面值和变量. 3.一元运算符作用于一个运算对象,二元则作用于两个.一个运算符到底是几元由上下文决定. 4.重 ...

  6. HDU 4300 Clairewd’s message(扩展KMP)题解

    题意:先给你一个密码本,再给你一串字符串,字符串前面是密文,后面是明文(明文可能不完成整),也就是说这个字符串由一个完整的密文和可能不完整的该密文的明文组成,要你找出最短的密文+明文. 思路:我们把字 ...

  7. Coursera SDN M1.1 SDN History: Central Control

    source Structure 1.讨论SDN的时间线,从1980s至今. 2.认识到SDN背后的原则和idea. 3.识别SDN起源的架构主题. NOTE Four Chapter in SDN ...

  8. 【转载】解决telnet无法连接 Connection refused

    原文:解决telnet无法连接 Connection refused telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户提供了在本地计算机上完成 ...

  9. python 随机整数

    # Program to generate a random number between and # import the random module import random print(ran ...

  10. 【原创】遇到:Invalid layout of java.lang.String at value 这样的问题,该怎么办呢?

    Invalid layout of java.lang.String at value## A fatal error has been detected by the Java Runtime En ...