🔒 JWT with RS-signing methods, designed for Gin.

Overview

GinRS GoDoc Go Report Card

套用在 gin-gonic/gin 基於 RS256 演算法的 JWT 簽署套件。

非對稱金鑰

透過 openssl 產生一個私鑰。

openssl genrsa -out private.key 2048

再透過這個私鑰產生一個公鑰,這個公鑰可以配發到其他伺服器或是第三方的手中用來驗證未來的 JWT 是否都由同人所簽署。

openssl rsa -in private.key -pubout > public.key

使用方式

產生金鑰,便能透過下列方式簽署 JWT,並以公鑰驗證其簽發正確性。

package main

import (
	"fmt"

	"github.com/teacat/ginrs"
)

type Data struct {
	Username string
}

func main() {
	// 欲簽署的資料。
	data := Data{
		Username: "YamiOdymel",
	}

	// 因為要簽署和驗證,所以必須載入公私鑰兩個檔案。
	err := ginrs.LoadKeys("./tests/public.key", "./tests/private.key")
	if err != nil {
		panic(err)
	}

	// 將資料透過 RS256 簽署成一個 JWT。
	token, err := ginrs.SignRS256(data)
	if err != nil {
		panic(err)
	}

	// 驗證這個 JWT 是否正確。
	var signedData Data
	err = ginrs.Parse(token, &signedData)
	if err != nil {
		panic(err)
	}

	fmt.Println(signedData.Username) // 輸出:YamiOdymel
}

若沒有要進行簽署,而只是要驗證 JWT 是否正確,則可以將 LoadKeys 替換成 LoadPublicKey 僅載入公鑰作為驗證用途而不需要私鑰。

用於 Gin 的中介函式

透過 Middleware 函式可以在每個請求進入時將 JWT 簽署的資料放入 *gin.Context 的變數中。

type Data struct {
	Username string
}

func main() {
	r := gin.Default()
	// 套用 GinRS 的中介函式到所有 Gin 路由。
	r.Use(ginrs.Middleware(Data{}))

	r.GET("/hello", func(c *gin.Context) {
		// 透過 ginrs.Get 取得 JWT 資料。
		if v, ok := ginrs.Get(c).(Data); ok {
			fmt.Println(v.Username)
		}
	})
}
Releases(v1.0.1)
Owner
TeaCat
Not real, but feels.
TeaCat
Go-gin-jwt - Secure web api using jwt token and caching mechanism

Project Description This project demonstrate how to create api and secure it wit

Jeremy Panjaitan 0 Jan 27, 2022
Account-jwt-go - Simple JWT api with go, gorm, gin

Account JWT on Go Go, gorm, Gin web framework 를 활용하여 만든 간단한 JWT API 입니다. Dajngo의

Deagwon Bu 1 Apr 14, 2022
Krakend-jwt-header-rewriter - Kraken Plugin - JWT Header Rewriter

Kraken Plugin - JWT Header Rewriter 1 Plugin Configuration Name Desciption Defau

null 0 Feb 15, 2022
jwt package for gin go applications

gin-jwt jwt package for gin go applications Usage Download using go module: go get github.com/ennaque/gin-jwt Import it in your code: import gwt "gith

Igor Volkov 2 Apr 21, 2022
Golang with JWT, Go Gin and MongoDB

User authentication in Golang with JWT, Go Gin and MongoDB Golang backend application that uses JWT tokens for users Locally Up Setup your .env file,

Al Mamun Khan 10 May 27, 2022
A simple user identify template with jwt token and gin, toy project

Simple Docs Register url : /api/auth/register param type value name string username password string password mailbox string mailbox response: { "sta

null 1 Dec 31, 2021
This is a jwt for Gin framework.

JWT for Gin Framework This is a jwt useful for Gin framework. It uses jwt-go to provide a jwt encode and decode token. Usage go get github.com/wyy-go/

null 0 Jan 9, 2022
A simple and lightweight library for creating, formatting, manipulating, signing, and validating JSON Web Tokens in Go.

GoJWT - JSON Web Tokens in Go GoJWT is a simple and lightweight library for creating, formatting, manipulating, signing and validating Json Web Tokens

Toby 5 Feb 7, 2022
Prototype of signing container images in the index

Prototype for inline signing of images in the image index. When designing Notary v2 there was a strong consensus for having detached signatures. These

Justin Cormack 2 Jan 8, 2022
Jose - JavaScript Object Signing and Encryption (JOSE)

jose JavaScript Object Signing and Encryption JOSE implemented in Go. RFCs RFC75

Kent 'picat' Gruber 6 Feb 21, 2022
A single sign-on solution based on go-oauth2 / oauth2 and gin-gonic/gin

A single sign-on solution based on go-oauth2 / oauth2 and gin-gonic/gin

yinhuanyi 1 Nov 17, 2021
:key: Secure alternative to JWT. Authenticated Encrypted API Tokens for Go.

branca branca is a secure alternative to JWT, This implementation is written in pure Go (no cgo dependencies) and implements the branca token specific

Wesley Hill 165 Jun 21, 2022
An implementation of JOSE standards (JWE, JWS, JWT) in Go

Go JOSE Package jose aims to provide an implementation of the Javascript Object Signing and Encryption set of standards. This includes support for JSO

Square 1.9k Jun 23, 2022
This is an implementation of JWT in golang!

jwt This is a minimal implementation of JWT designed with simplicity in mind. What is JWT? Jwt is a signed JSON object used for claims based authentic

John Rowley 99 May 9, 2022
This package provides json web token (jwt) middleware for goLang http servers

jwt-auth jwt auth middleware in goLang. If you're interested in using sessions, checkout my sessions library! README Contents: Quickstart Performance

Adam Hanna 217 Jun 7, 2022
Golang implementation of JSON Web Tokens (JWT)

jwt-go A go (or 'golang' for search engine friendliness) implementation of JSON Web Tokens NEW VERSION COMING: There have been a lot of improvements s

Dave Grijalva 10.4k Jun 23, 2022
JWT login microservice with plugable backends such as OAuth2, Google, Github, htpasswd, osiam, ..

loginsrv loginsrv is a standalone minimalistic login server providing a JWT login for multiple login backends. ** Attention: Update to v1.3.0 for Goog

tarent 1.9k Jun 23, 2022
Simple JWT Golang

sjwt Simple JSON Web Token - Uses HMAC SHA-256 Example // Set Claims claims := New() claims.Set("username", "billymister") claims.Set("account_id", 86

Brian Voelker 98 May 22, 2022
simple-jwt-provider - Simple and lightweight provider which exhibits JWTs, supports login, password-reset (via mail) and user management.

Simple and lightweight JWT-Provider written in go (golang). It exhibits JWT for the in postgres persisted user, which can be managed via api. Also, a password-reset flow via mail verification is available. User specific custom-claims also available for jwt-generation and mail rendering.

Max 25 May 18, 2022