jwt

package module
v0.0.0-...-b28a60f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

README

jwt

Go Reference

Simple yet Powerful Json Webtoken Library. Add JSON WebToken Authorisation in your web project in few clicks and codes. package jwt implements jwt in a different way it remove overhead of calculation which hashing algorithm to use by takein SHA256 by default agorithm and reduces JWT length size by removing header.



About

jwt package takes SHA256 as default algorithm. This package doesn't implement Standard json web token. It uses a compressed version of jwt (compressed by author). the client could potentionaly save few bytes of data by using this compressed version. Don't store your client's critical data as any json webtoken as jwt does only base64 encryption.

Install

go get -u github.com/Muthu-Palaniyappan-OL/jwt

Example

Sample Code
package main

import (
	"log"
	"net/http"

	"github.com/Muthu-Palaniyappan-OL/jwt"
)

func main() {
	jwt.SetPrivateKey("drjyt347yu3h87yuh")
	http.HandleFunc("/login", func(rw http.ResponseWriter, r *http.Request) {

		// validate form data of the user

		jwt.Authenticate(rw, "json-string", 30000)
		rw.WriteHeader(200) // run jwt.Authenticate before writing into wr.WriteHeader()

		// After setting token then start writing response

		// Start writing response
	})
	http.HandleFunc("/feed", func(rw http.ResponseWriter, r *http.Request) {
		json, err := jwt.Authorize(r)

		// json is the string which you assiged the client when he first used GET /login request

		if err != nil {
			log.Println(err)
		}

		rw.Write([]byte(json))
	})
	http.ListenAndServe(":8080", nil)
}

Documentation

Overview

	package jwt implements simple way to create jwt tokens for your web project in golang.

	This package takes SHA256 algorithm by default for any other algorithm download this source code and change it freely.

	Authenticate means putting username and password and getting verified and generate key.
 	Authorization means having a key which can be used to open door.

 	Functions implemented and exported in this package:

 	func Authenticate(rw http.ResponseWriter, jsonStringTOSend string, seconds int) (string, error)
 	func Authorize(r *http.Request) (string, error)
 	func SetCookiePath(path string)
 	func IsPrivateKeySet() bool

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Authenticate

func Authenticate(rw http.ResponseWriter, jsonStringTOSend string, seconds int) (string, error)

After Authenticating the user by verifying the username in database you need to create a json string by using json.Marshal() Function and send it via this function which gets converted into encoded hash and send it user via http.ResponseWriter as a token you could specify duration of cookie in seconds argument.

func Authorize

func Authorize(r *http.Request) (string, error)

reads jwt cookies in the client http.Request and checks the validity of jwt cookie signature (check whether it is signed with the same privateKey) and returns the json string which was set as a cookie by server before in Authenticate() function.

func IsPrivateKeySet

func IsPrivateKeySet() bool

returns true is pivateKey is set

func SetCookiePath

func SetCookiePath(path string)

Set jwt cookie's path using this function

func SetPrivateKey

func SetPrivateKey(key string) error

sets given string as a privateKey. privateKey can be set only once.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL