Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vaijab/gin-tokenauth
TokenAuth is an authorization middleware for gin.
https://github.com/vaijab/gin-tokenauth
gin golang http middleware package
Last synced: about 1 month ago
JSON representation
TokenAuth is an authorization middleware for gin.
- Host: GitHub
- URL: https://github.com/vaijab/gin-tokenauth
- Owner: vaijab
- License: mit
- Created: 2017-10-27T09:39:31.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-11-28T10:44:57.000Z (almost 7 years ago)
- Last Synced: 2024-06-20T02:12:28.982Z (5 months ago)
- Topics: gin, golang, http, middleware, package
- Language: Go
- Homepage: https://github.com/vaijab/gin-tokenauth
- Size: 4.88 KB
- Stars: 2
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tokenauth
TokenAuth is a authorization middleware for [gin](https://github.com/gin-gonic/gin).
## Installation
```
go get github.com/vaijab/gin-tokenauth
```## Usage
This example uses file based token store, see [below](#filestore) for more
details how filestore works.```Go
package mainimport (
"log""github.com/gin-gonic/gin"
"github.com/vaijab/gin-tokenauth"
"github.com/vaijab/gin-tokenauth/filestore"
)func main() {
store, err := filestore.New("tokens.yaml")
if err != nil {
log.Fatalln(err)
}r := gin.Default()
r.Use(tokenauth.New(store))r.GET("/secrets", func(c *gin.Context) {
c.String(200, "p4ssw0rd\n")
})r.Run()
}
``````shell
> curl -i http://localhost:8080/secrets -H 'Authorization: Bearer jUyaoAFFZ5Ay3fxXG2boT5'
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Fri, 27 Oct 2017 12:11:16 GMT
Content-Length: 9p4ssw0rd
```## 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.```yaml
# tokens.yaml
---
tokens:
- name: foo
token: 'jUyaoAFFZ5Ay3fxXG2boT5'
is_disabled: false
description: 'Token for user foo'
- name: bar
token: 'jUyaoAFFZ5Ay3fxXG2boT5'
is_disabled: true
description: 'Disabled token for user bar'
```