authorizations.go
package auth
import (
"errors"
"fmt"
"log"
"net/url"
"regexp"
"time"
"github.com/nsqio/nsq/internal/http_api"
)
type Authorization struct {
Topic string `json:"topic"`
Channels []string `json:"channels"`
Permissions []string `json:"permissions"`
}
type State struct {
TTL int `json:"ttl"`
Authorizations []Authorization `json:"authorizations"`
Identity string `json:"identity"`
IdentityURL string `json:"identity_url"`
Expires time.Time
}
func (a *Authorization) HasPermission(permission string) bool {
for _, p := range a.Permissions {
if permission == p {
return true
}
}
return false
}
func (a *Authorization) IsAllowed(topic, channel string) bool {
if channel != "" {
if !a.HasPermission("subscribe") {
return false
}
} else {
if !a.HasPermission("publish") {
return false
}
}
topicRegex := regexp.MustCompile(a.Topic)
if !topicRegex.MatchString(topic) {
return false
}
for _, c := range a.Channels {
channelRegex := regexp.MustCompile(c)
if channelRegex.MatchString(channel) {
return true
}
}
return false
}
func (a *State) IsAllowed(topic, channel string) bool {
for _, aa := range a.Authorizations {
if aa.IsAllowed(topic, channel) {
return true
}
}
return false
}
func (a *State) IsExpired() bool {
if a.Expires.Before(time.Now()) {
return true
}
return false
}
func QueryAnyAuthd(authd []string, remoteIP, tlsEnabled, authSecret string,
connectTimeout time.Duration, requestTimeout time.Duration) (*State, error) {
for _, a := range authd {
authState, err := QueryAuthd(a, remoteIP, tlsEnabled, authSecret, connectTimeout, requestTimeout)
if err != nil {
log.Printf("Error: failed auth against %s %s", a, err)
continue
}
return authState, nil
}
return nil, errors.New("Unable to access auth server")
}
func QueryAuthd(authd, remoteIP, tlsEnabled, authSecret string,
connectTimeout time.Duration, requestTimeout time.Duration) (*State, error) {
v := url.Values{}
v.Set("remote_ip", remoteIP)
v.Set("tls", tlsEnabled)
v.Set("secret", authSecret)
endpoint := fmt.Sprintf("http://%s/auth?%s", authd, v.Encode())
var authState State
client := http_api.NewClient(nil, connectTimeout, requestTimeout)
if err := client.GETV1(endpoint, &authState); err != nil {
return nil, err
}
// validation on response
for _, auth := range authState.Authorizations {
for _, p := range auth.Permissions {
switch p {
case "subscribe", "publish":
default:
return nil, fmt.Errorf("unknown permission %s", p)
}
}
if _, err := regexp.Compile(auth.Topic); err != nil {
return nil, fmt.Errorf("unable to compile topic %q %s", auth.Topic, err)
}
for _, channel := range auth.Channels {
if _, err := regexp.Compile(channel); err != nil {
return nil, fmt.Errorf("unable to compile channel %q %s", channel, err)
}
}
}
if authState.TTL <= 0 {
return nil, fmt.Errorf("invalid TTL %d (must be >0)", authState.TTL)
}
authState.Expires = time.Now().Add(time.Duration(authState.TTL) * time.Second)
return &authState, nil
}
authorizations.go的更多相关文章
- WSO2 API Manager 替换mysql作为数据库,解决AuthorizationUtils Could not set authorizations for the root问题
按照wso2官网(https://docs.wso2.com/display/ADMIN44x/Changing+to+MySQL)配置AM的数据库,想从H2换成Mysql5.7,费了将近一天的时间, ...
- (转载) RESTful API 设计指南
作者: 阮一峰 日期: 2014年5月22日 网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制 ...
- 使用swagger作为restful api的doc文档生成
初衷 记得以前写接口,写完后会整理一份API接口文档,而文档的格式如果没有具体要求的话,最终展示的文档则完全决定于开发者的心情.也许多点,也许少点.甚至,接口总是需要适应新需求的,修改了,增加了,这份 ...
- .NET WebAPI 用ActionFilterAttribute实现token令牌验证与对Action的权限控制
项目背景是一个社区类的APP(求轻吐...),博主主要负责后台业务及接口.以前没玩过webAPI,但是领导要求必须用这个(具体原因鬼知道),只好硬着头皮上了. 最近刚做完权限这一块,分享出来给大家.欢 ...
- RESTful API 设计指南
转自:http://www.ruanyifeng.com/blog/2014/05/restful_api.html 网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机. ...
- RESTful API URI 设计的一些总结
非常赞的四篇文章: Resource Naming Best Practices for Designing a Pragmatic RESTful API 撰写合格的 REST API JSON 风 ...
- PHP7函数大全(4553个函数)
转载来自: http://www.infocool.net/kb/PHP/201607/168683.html a 函数 说明 abs 绝对值 acos 反余弦 acosh 反双曲余弦 addcsla ...
- CentOS搭建SVN记录
1.安装subversion(client and server) $ yum install subversion $ yum install mod_dav_svn 安装成功之后使用 svnser ...
- geotrellis使用(五)使用scala操作Accumulo
要想搞明白Geotrellis的数据处理情况,首先要弄清楚数据的存放,Geotrellis将数据存放在Accumulo中. Accumulo是一个分布式的Key Value型NOSQL数据库,官网为( ...
随机推荐
- [Zabbix3.0 ]添加Nginx监控
通过Nginx的http_stub_status_module模块提供的状态信息来监控,所以在Agent端需要配置Nginx状态获取的脚本,和添加key信息等,然后在Server端配置Nginx的监控 ...
- IT咨询顾问:一次吐血的项目救火
年后的一个合作公司上线了一个子业务系统,对接公司内部的单点系统.我收到该公司的技术咨询:项目启动后没有规律的突然无法登录了,重新启动后,登录一断时间后又无法重新登录,对方技术人员一头雾水不知道什么原因 ...
- 详解Lambda
定义好委托: public delegate int FirDelegate(int a); public delegate int SecDelegate(int a,int b); public ...
- Nginx SSL功能支持的一些资料。
http://wiki.nginx.org/HttpSslModulehttp://zou.lu/nginx-https-ssl-module/http://www.21andy.com/blog/2 ...
- PyQt5实现界面跳转
网上关于PyQt5的教程很少,特别是界面跳转这一块儿,自己研究了半天,下来和大家分享一下 一.首先是主界面 1 # -*- coding: utf-8 -*- # Form implementatio ...
- lvs+keepalive实现主从效果,以及RS健康监测和tcp,udp实现非web的负载均衡
前面文章讲到了tcp和udp负载均衡,但是没有健康监测,这几天我优化了一下上次的操作.当然,我也是用的跨网段的通讯,因为线上业务主要是海外业务,所以做了iptables流量转发 IP: lvs-mas ...
- C#的一些小知识
一.Server.MapPath E:\MyProject\GisSystem\Json\jsonlist.aspx,GisSystem项目下有个Json文件夹,文件夹下有个jsonlist.aspx ...
- Spring对象生存周期(Scope)的分析
一.Scope定义 Scope用来声明容器中管理的对象所应该处的限定场景或者说对象的存活时间,即容器在对象进入相应的Scope之前,生产并装配这些对象,在该对象不再处于这些Scope之后,容器通常会销 ...
- debain 安装nodejs
apt-get update -yapt-get install -y build-essential curl curl -sL https://deb.nodesource.com/setup_8 ...
- C++相关:C++的IO库
前言 基本的IO库设施 istream(输入流类型),提供输入操作. ostream(输出流类型),提供输出操作. cin,一个istream对象,从标准输入读取数据. cout,一个ostream对 ...