项目作者: vaijab

项目描述 :
TokenAuth is an authorization middleware for gin.
高级语言: Go
项目地址: git://github.com/vaijab/gin-tokenauth.git
创建时间: 2017-10-27T09:39:31Z
项目社区:https://github.com/vaijab/gin-tokenauth

开源协议:MIT License

下载


tokenauth

TokenAuth is a authorization middleware for gin.

Installation

  1. go get github.com/vaijab/gin-tokenauth

Usage

This example uses file based token store, see below for more
details how filestore works.

  1. package main
  2. import (
  3. "log"
  4. "github.com/gin-gonic/gin"
  5. "github.com/vaijab/gin-tokenauth"
  6. "github.com/vaijab/gin-tokenauth/filestore"
  7. )
  8. func main() {
  9. store, err := filestore.New("tokens.yaml")
  10. if err != nil {
  11. log.Fatalln(err)
  12. }
  13. r := gin.Default()
  14. r.Use(tokenauth.New(store))
  15. r.GET("/secrets", func(c *gin.Context) {
  16. c.String(200, "p4ssw0rd\n")
  17. })
  18. r.Run()
  19. }
  1. > curl -i http://localhost:8080/secrets -H 'Authorization: Bearer jUyaoAFFZ5Ay3fxXG2boT5'
  2. HTTP/1.1 200 OK
  3. Content-Type: text/plain; charset=utf-8
  4. Date: Fri, 27 Oct 2017 12:11:16 GMT
  5. Content-Length: 9
  6. p4ssw0rd

Token Stores

Different token stores can be implemented quite easily, the API stays the same.

filestore

This store is based on a yaml file. During initialization, a file watcher is
attached which ensures that changes to the tokens file are reflected
immediately.

Tokens file does not have to exist at first. It can be created or removed and
filestore will either create tokens or remove them entirely. Only token and
is_disabled fields are used at the moment.

  1. # tokens.yaml
  2. ---
  3. tokens:
  4. - name: foo
  5. token: 'jUyaoAFFZ5Ay3fxXG2boT5'
  6. is_disabled: false
  7. description: 'Token for user foo'
  8. - name: bar
  9. token: 'jUyaoAFFZ5Ay3fxXG2boT5'
  10. is_disabled: true
  11. description: 'Disabled token for user bar'