jowt

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

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

Go to latest
Published: Sep 3, 2019 License: MIT Imports: 9 Imported by: 1

README

JOWT (JWT Simple Package for Golang)

GoDoc Go Report Card Build Status CII Best Practices

JOWT is a Simple Package for Golang to generate and verify JWT Token using HMAC + RSA256 OR RSA512.

How to Install ?

$ go get github.com/johansetia/jowt

How to Use ?

1. Importing Package
package main

import (
    "github.com/johansetia/jowt"
)
2. Make a Payload and Key

you can use a structure from this package to create payload data.

key := "qwertyuiopasdfghjklzxcvbnm123456"
data := jowt.Payload{
	"iss": "johan.com",
        "iat": 1566959060,
        "exp": 1598495060,
        "aud": "www.johan.com",
        "sub": "admin@johan.com",
        "UserName": "Johan",
        "Email": "johan@example.com",
        "userData": jowt.Payload{
                "Level":     "Administrator",
                "Role":      "UserAdministrator",
            },
	}
3. Getting value

Using HS512 Function to get a JWT Token

    token, ok := jowt.HS512(key).SetPayload(dataPayload).Get()
    if !ok {
        panic("ERROR GET TOKEN")
    }
    fmt.Println(token)
    

Your HS512 token is

eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJFbWFpbCI6ImpvaGFuQGV4YW1wbGUuY29tIiwiVXNlck5hbWUiOiJKb2hhbiIsImF1ZCI6Ind3dy5qb2hhbi5jb20iLCJleHAiOjE1OTg0OTUwNjAsImlhdCI6MTU2Njk1OTA2MCwiaXNzIjoiam9oYW4uY29tIiwic3ViIjoiYWRtaW5Aam9oYW4uY29tIiwidXNlckRhdGEiOnsiTGV2ZWwiOiJBZG1pbmlzdHJhdG9yIiwiUm9sZSI6IlVzZXJBZG1pbmlzdHJhdG9yIn19.M1xhZDFeXwWj9dxVyMYGBgZ45NFqVobe8ZoPm6JVDBjSc6TSQCA-Ja9_DnIqzhNP1JMCMCdam5SIY6xn5ijLIw

Using HS256 Function to get a JWT Token

    token, ok := jowt.HS256(key).SetPayload(dataPayload).Get()
    if !ok {
        panic("ERROR GET TOKEN")
    }
    fmt.Println(token)
    

Your HS256 token is

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJFbWFpbCI6ImpvaGFuQGV4YW1wbGUuY29tIiwiVXNlck5hbWUiOiJKb2hhbiIsImF1ZCI6Ind3dy5qb2hhbi5jb20iLCJleHAiOjE1OTg0OTUwNjAsImlhdCI6MTU2Njk1OTA2MCwiaXNzIjoiam9oYW4uY29tIiwic3ViIjoiYWRtaW5Aam9oYW4uY29tIiwidXNlckRhdGEiOnsiTGV2ZWwiOiJBZG1pbmlzdHJhdG9yIiwiUm9sZSI6IlVzZXJBZG1pbmlzdHJhdG9yIn19.yoFC0whrAh80A0bc7bGMlDR3XRW_dL-YwwEXQ3qRlJQ
4. Verify your JWT using key
    verify := jowt.Verify(key).SetToken(jwt).Status()
    if verify {
        fmt.Println("USER ACCEPTED")
    }else{
        fmt.Println("USER DECIDED")
    }

Structure

.
├── example
│   ├── go.mod
│   ├── go.sum
│   ├── main.go
│   └── README.md
├── go.mod
├── helper
│   └── helper.go
├── jowt.go
├── LICENSE
├── README.md
└── security.go

2 directories, 10 files

Documentation

Index

Constants

View Source
const (
	// HS512alg is used to implement HS512 Algorithm
	HS512alg string = "HS512"
	// HS256alg is used to implement HS512 Algorithm
	HS256alg string = "HS256"
)

Variables

This section is empty.

Functions

func Jwt

func Jwt(m *Security, next http.Handler) http.Handler

Jwt is a middleware to verify token that you set before.

Types

type JWT

type JWT struct {
	Payload
	// contains filtered or unexported fields
}

JWT struct is used to store header and payload data to be created as a JWT Token Based on the first function name.

func HS256

func HS256(secret string) *JWT

HS256 is used to start make an easy HS512 JWT Token

func HS512

func HS512(secret string) *JWT

HS512 is used to start make an easy HS512 JWT Token

func (*JWT) Get

func (jwt *JWT) Get() (string, bool)

Get is used to generate a JWT token.

func (*JWT) SetPayload

func (jwt *JWT) SetPayload(data Payload) *JWT

SetPayload is used to fill data into the payload to be encrypted, this function only can be passed using payload type from this library.

type Payload

type Payload map[string]interface{}

Payload is a JWT data

type Security

type Security struct {
	SecretKey         string
	WhiteListURI      []string
	BlackListURI      []string
	Message           interface{}
	MiddlewarePayload map[string]interface{}
}

Security is used to store JWT to be encrypted.

func (*Security) JWTMiddleware

func (s *Security) JWTMiddleware(next http.Handler) http.Handler

JWTMiddleware is implemented from middleware. this function are implemented with http.Handle param and it will return http.Handler

func (*Security) SetPayloadFromMiddleware

func (s *Security) SetPayloadFromMiddleware(DecrypedPayload Payload)

SetPayloadFromMiddleware :

type VerifyToken

type VerifyToken struct {
	Payload
	// contains filtered or unexported fields
}

VerifyToken struct is used to store header and payload data that has been given from Verify function.

func Verify

func Verify(secret string) *VerifyToken

Verify is used to verify your last JWT token based on secret key.

func (*VerifyToken) SetToken

func (verif *VerifyToken) SetToken(token string) *VerifyToken

SetToken is used to fill JWT token to be filled to VerifyToken Struct.

func (*VerifyToken) Status

func (verif *VerifyToken) Status() bool

Status function is used to get a status from JWT Token is original or fake.

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

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