Categorygithub.com/kotlin2018/jwt
modulepackage
0.1.0
Repository: https://github.com/kotlin2018/jwt.git
Documentation: pkg.go.dev

# README

gf-jwt

基于GoFrame框架的JWT认证

使用

下载安装

$ go get github.com/kotlin2018/jwt

导入

import "github.com/kotlin2018/jwt"

参数说明

claims["expire"] token的过期时间.

Claims["current_time"] 生成token的当前时间.

r.SetParam("JWT_Payload", claims) 将有效载荷设置到请求中.

r.GetString("Jwt_Token").

请求头中的Authorization字段保存token的值.

例子

查看示例 example/auth/auth.go , 使用 ExtractClaims 可以自定义用户数据.

Demo

运行 example/server/server.go8000端口.

$ go run example/server/server.go

api screenshot

通过 httpie ,在命令行来测试下效果.

登录接口:

$ http -v --form  POST localhost:8000/login username=admin password=admin

命令行输出

api screenshot

刷新 token 接口:

$ http -v -f GET localhost:8000/user/refresh_token "Authorization:Bearer xxxxxxxxx"  "Content-Type: application/json"

命令行输出

api screenshot

info 接口

我们使用用户名 admin 和密码 admin 测试一下 info 接口的返回

$ http -f GET localhost:8000/user/info "Authorization:Bearer xxxxxxxxx"  "Content-Type: application/json"

命令行输出

api screenshot

用户验证接口

我们用未授权的 token 来测试 info 接口的返回

$ http -f GET localhost:8000/user/info "Authorization:Bearer xxxxxxxxx"  "Content-Type: application/json"

命令行输出

api screenshot

再次感谢https://github.com/appleboy/gin-jwt

# Packages

No description provided by the author

# Functions

返回值interface{} 就是 JWT_Payload对应的值,即: interface{} = r.Get("JWT_Payload").
No description provided by the author
ExtractClaims 帮助提取JWT claims.
IdentityHandler从JWT获取身份并为每个请求设置身份使用此函数,通过r.GetParam(“ id”)获取身份。.
JWTAuth JwtToken中间件。 可选参数<pattern> 不能为空,<pattern>用法如下: pattern[0] = "POST:/login" pattern[1] = "ALL:/refresh_token" pattern[2] = "ALL:/logout".
============ ============ ============ ============ LoginResponse 用于定义自定义的登录成功回调函数。.
LogoutResponse is used to set token blacklist.
No description provided by the author
No description provided by the author
PayloadFunc是一个回调函数,将在登录期间被调用。 使用此功能可以向网络令牌添加其他有效载荷数据。 然后在请求期间通过c.Get(“ JWT_PAYLOAD”)使数据可用。 请注意,有效负载未加密。 jwt.io上提到的属性不能用作map的键。 可选,默认情况下不会设置其他数据。.
No description provided by the author
Unauthorized 用于定义自定义的未经授权的回调函数。.

# Variables

No description provided by the author
ErrEmptyAuthHeader can be thrown if authing with a HTTP header, the Auth header needs to be set.
ErrEmptyCookieToken can be thrown if authing with a cookie, the token cokie is empty.
ErrEmptyParamToken can be thrown if authing with parameter in path, the parameter in path is empty.
ErrEmptyQueryToken can be thrown if authing with URL Query, the query token variable is empty.
ErrExpiredToken indicates JWT token has expired.
ErrFailedAuthentication indicates authentication failed, could be faulty username or password.
ErrFailedTokenCreation indicates JWT Token failed to create, reason unknown.
ErrForbidden when HTTP status 403 is given.
ErrInvalidAuthHeader indicates auth header is invalid, could for example have the wrong Realm name.
ErrInvalidPrivKey indicates that the given private key is invalid.
ErrInvalidPubKey indicates the the given public key is invalid.
ErrInvalidSigningAlgorithm indicates signing algorithm is invalid, needs to be HS256, HS384, HS512, RS256, RS384 or RS512.
ErrInvalidToken indicates JWT token has invalid.
ErrMissingAuthenticatorFunc indicates Authenticator is required.
ErrMissingExpField missing exp field in token.
ErrMissingIdentity identity key and identity value is null.
ErrMissingLoginValues indicates a user tried to authenticate without username or password.
ErrMissingSecretKey 表示需要密钥.
ErrNoPrivKeyFile indicates that the given private key is unreadable.
ErrNoPubKeyFile indicates that the given public key is unreadable.
ErrWrongFormatOfExp field must be float64 format.
默认身份密钥.
用于签名的密钥。 这个参数必须要有。.
此字段允许客户端刷新令牌,直到MaxRefresh通过。.
显示给用户的名称,这个参数必须要有。.
jwt令牌有效的持续时间。 可选,默认为一小时,时间单位为:小时。.

# Structs

GfJWTMiddleware 提供了Json-Web-Token身份验证实现。 失败时,将返回401 HTTP响应;成功后,将调用包装的中间件,并以c.Get(“ userID”)。(string)的形式提供userID。 用户可以通过将json请求发布到LoginHandler来获得令牌,然后需要在Authentication标头中传递令牌。 示例:授权:承载者XXX_TOKEN_XXX.

# Type aliases

MapClaims 如果您不提供,这是默认的声明类型。使用map [string] interface {}进行JSON解码.