Skip to content

gojwt is a barebones JSON Web Token implementation in Go

Notifications You must be signed in to change notification settings

nickvellios/gojwt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

gojwt v0.0.1

gojwt is a barebones JSON Web Token implementation in Go. I wrote this as a test for a future project. As I get closer to that project I will begin to build this project out as needed. Currently gojwt only supports the HMAC SHA Algorithm because that's all I needed. Please feel free to contribute and add other algorithms.

Error handling needs some massaging.

More info on what a JSON Web Token is can be found here: https://jwt.io/

Usage

Create JWT:

claims := map[string]string{
  "user": "nick",
  "email": "[email protected]",
}
expiration := 3600 // seconds
token := jwt.Generate(claims, expiration)

Decode JWT

payload := jwt.Decode(token)
fmt.Println("Hello " + payload["user"])

You can also decode directly from a cookie (neat?):

func SomeHttpHandler(w http.ResponseWriter, r *http.Request) {
	payload, err := jwt.DecodeFromCookie(r, "user")
	if err != nil {
		fmt.Fprint(w, "Something bad happened")
		return
	}
	fmt.Fprint(w, "Logged in.  Welcome back "+payload["user"])
}

License

Unlicense. For more information, please refer to http://unlicense.org/

About

gojwt is a barebones JSON Web Token implementation in Go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages