项目作者: LdDl

项目描述 :
JWT for fiber
高级语言: Go
项目地址: git://github.com/LdDl/fiber-jwt.git
创建时间: 2020-06-27T19:56:05Z
项目社区:https://github.com/LdDl/fiber-jwt

开源协议:MIT License

下载


GoDoc Build Status Sourcegraph Go Report Card GitHub tag

JWT Middleware for Fiber Framework

preamble: this is port of appleyboy’s JWT middleware adapted for Fiber framework

This is a middleware for Fiber framework, which built on top of fasthttp

It uses golang-jwt/jwt to provide a jwt authentication middleware. It provides additional handler functions to provide the doauth api that will generate the token and an additional refresh_token handler that can be used to refresh tokens.

Usage

Download and install using go module:

  1. export GO111MODULE=on
  2. go get github.com/LdDl/fiber-jwt

Import it in your code:

  1. import (
  2. jwt "github.com/LdDl/fiber-jwt/v2"
  3. )

Example

Please see the example file

  1. go run example/main.go

Demo server will start on port 8080.

Login API

Correct username/password and user access

  1. curl -X POST 'http://localhost:8080/api/doauth' -d '{"username": "user", "password": "pass"}'
  2. curl -X GET 'http://localhost:8080/api/v0.0.1/secret_page?token=PUT_RECIEVED_TOKEN'

Correct username/password but user has no access (banned)

  1. curl -X POST 'http://localhost:8080/api/doauth' -d '{"username": "user2", "password": "pass"}'

Wrong user or password

  1. curl -X POST 'http://localhost:8080/api/doauth' -d '{"username": "user", "password": "pass333"}'

Refresh token API

  1. curl -X GET 'http://localhost:8080/api/v0.0.1/refresh_token?token=PUT_RECIEVED_TOKEN'

Login Flow

  1. Authenticator: handles the login logic. On success LoginResponse is called, on failure Unauthorized is called.
  2. LoginResponse: optional, allows setting a custom response such as a redirect.

JWT Flow

  1. PayloadFunc: maps the claims in the JWT.
  2. IdentityHandler: extracts identity from claims.
  3. Authorizator: receives identity and handles authorization logic.
  4. Unauthorized: handles unauthorized logic.