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

  1. 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,费了将近一天的时间, ...

  2. (转载) RESTful API 设计指南

    作者: 阮一峰 日期: 2014年5月22日 网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制 ...

  3. 使用swagger作为restful api的doc文档生成

    初衷 记得以前写接口,写完后会整理一份API接口文档,而文档的格式如果没有具体要求的话,最终展示的文档则完全决定于开发者的心情.也许多点,也许少点.甚至,接口总是需要适应新需求的,修改了,增加了,这份 ...

  4. .NET WebAPI 用ActionFilterAttribute实现token令牌验证与对Action的权限控制

    项目背景是一个社区类的APP(求轻吐...),博主主要负责后台业务及接口.以前没玩过webAPI,但是领导要求必须用这个(具体原因鬼知道),只好硬着头皮上了. 最近刚做完权限这一块,分享出来给大家.欢 ...

  5. RESTful API 设计指南

    转自:http://www.ruanyifeng.com/blog/2014/05/restful_api.html 网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机. ...

  6. RESTful API URI 设计的一些总结

    非常赞的四篇文章: Resource Naming Best Practices for Designing a Pragmatic RESTful API 撰写合格的 REST API JSON 风 ...

  7. PHP7函数大全(4553个函数)

    转载来自: http://www.infocool.net/kb/PHP/201607/168683.html a 函数 说明 abs 绝对值 acos 反余弦 acosh 反双曲余弦 addcsla ...

  8. CentOS搭建SVN记录

    1.安装subversion(client and server) $ yum install subversion $ yum install mod_dav_svn 安装成功之后使用 svnser ...

  9. geotrellis使用(五)使用scala操作Accumulo

    要想搞明白Geotrellis的数据处理情况,首先要弄清楚数据的存放,Geotrellis将数据存放在Accumulo中. Accumulo是一个分布式的Key Value型NOSQL数据库,官网为( ...

随机推荐

  1. 我的sql数据库存储过程分页- -

    以前用到数据库存储过程分页的时候都是用 not in 但是最近工作的时候,随着数据库记录的不断增大,发现not in的效率 真的不行 虽然都设置了索引,但是当记录达到10w的时候就发现不行了,都是需要 ...

  2. linux上安装redis的踩坑过程2

    昨天在linux上安装redis后马上发现了其它问题,服务器很卡,cpu使用率上升,top命令查看下,原来有恶意程序在挖矿,此程序入侵了很多redis服务器,马上用kill杀掉它 然后开始一些安全策略 ...

  3. sublime使用技巧之集成VI

    熟悉开发工具,减少多余的操作流程有助于提高开发效率,而Sublime Text 2是sublime产品的经典版本,因此本文基于Sublime Text 2讲解sublime的使用技巧. VI的主要作用 ...

  4. TypeScript 的声明文件的使用与编写

    https://fenying.net/2016/09/19/typings-for-typescript/ TypeScript 是 JavaScript 的超集,相比 JavaScript,其最关 ...

  5. 关于JavaScript的那些话

    1.初学者动手环境----推荐Chrome的控制台(F12调用)2.JS中两个非常重要的数据类型是对象和数组.3.JavaScript 程序是用Unicode字符集编写的.4.JavaScript是区 ...

  6. 全屏slider--swiper

    这两年,这种滑动器技术在互联网产品介绍页设计上非常常用,最近某个项目需要这种设计,在网上找了一下,有个老外产的这种设计组件swiper已经非常成熟,原来这个东西设计初衷是pad之类的移动触摸交互设备, ...

  7. codeforces——961B. Lecture Sleep

    本文为博主原创文章,未经允许不得转载. 我在csdn也同步发布了此文,链接 https://blog.csdn.net/umbrellalalalala/article/details/7989196 ...

  8. python笔记:#002#第一个python程序

    第一个 Python 程序 目标 第一个 HelloPython 程序 Python 2.x 与 3​​.x 版本简介 执行 Python 程序的三种方式 解释器 -- python / python ...

  9. HTML知识点总结

    HTML知识点总结 一.需要熟悉的基本快捷键 ctrl+c            复制 ctrl+v            粘贴 ctrl+x            剪切 ctrl+tab       ...

  10. ArcGIS 产品体系结构

    1. 开篇 本文主要从以下几个方面介绍 ArcGIS 的产品体系 2. 详细介绍 2.1 ArcGIS Desktop 参考:[https://blog.csdn.net/hphone/article ...