tokengen

package module
v1.0.0-beta Latest Latest
Warning

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

Go to latest
Published: May 29, 2024 License: MIT Imports: 8 Imported by: 0

README

TokenGen

TokenGen is intended to retain data integrity and authenticity shared between parties. This package is inspired by django-contrib-auth-token so that each token generated will have its timeout. Used techniques are symmetric and hashing so that you must have a secret key shared among partners to succeed on using this module.

Use cases

  • Digital signatures
  • OTP (one time password)
  • Forget password token with timeout

Installation

Use go get

go get -u github.com/hyuti/tokengen

Then import the package into your own code

import "github.com/hyuti/tokengen"

Usage

Make a token

package main

import (
	"fmt"

	"github.com/hyuti/tokengen"
)

func main() {
	data := "data"
	// this key should be stores in enviroment variables or something similar and only accessible by you
	secretKey := "a random key"
	token := tokengen.MakeToken(data, secretKey)
	fmt.Println(token)
}

Validate a token

package main

import (
	"fmt"
	"time"

	"github.com/hyuti/tokengen"
)

func main() {
	data := "data"
	secretKey := "a random key"
	token := tokengen.MakeToken(data, secretKey)
	timeout, err := time.ParseDuration("60s")
	if err != nil {
		fmt.Println(err)
		return
	}
	if err := tokengen.ValidateToken(data, secretKey, token, timeout);err != nil {
		fmt.Printf("invalid token: %s\n", err)
		return
	}
	fmt.Println("token valid")
}

Make a token with your own key salt

package main

import (
	"fmt"

	"github.com/hyuti/tokengen"
)

func main() {
	data := "data"
	secretKey := "a random key"
	salt := "your own key salt"
	token := tokengen.MakeTokenWithSalt(salt, data, secretKey)
	fmt.Println(token)
}

Validate a token with your own key salt

package main

import (
	"fmt"
	"time"

	"github.com/hyuti/tokengen"
)

func main() {
	data := "data"
	secretKey := "a random key"
	salt := "your own key salt"
	token := tokengen.MakeTokenWithSalt(salt, data, secretKey)
	timeout, err := time.ParseDuration("60s")
	if err != nil {
		fmt.Println(err)
		return
	}
	if err := tokengen.ValidateTokenWithKeySalt(salt, data, secretKey, token, timeout);err != nil {
		fmt.Printf("invalid token: %s\n", err)
		return
	}
	fmt.Println("token valid")
}

Author

Hyuti Le

Documentation

Index

Constants

View Source
const (
	KeySalt = "github.com/hyuti/tokengen"
)

Variables

This section is empty.

Functions

func MakeToken

func MakeToken(value, secretKey string) string

MakeToken uses default key salt To validate token generated by MakeToken use ValidateToken See ValidateToken

func MakeTokenWithSalt

func MakeTokenWithSalt(keySalt, value, secretKey string) string

MakeTokenWithSalt allows you to use your key salt instead of default key salt To validate token generated by MakeTokenWithSalt use ValidateTokenWithKeySalt See ValidateTokenWithKeySalt

func ValidateToken

func ValidateToken(value, secretKey, actualTk string, timeout time.Duration) error

ValidateToken validates token and uses the default key salt See MakeToken

func ValidateTokenWithKeySalt

func ValidateTokenWithKeySalt(keySalt, value, secretKey, actualTk string, timeout time.Duration) error

ValidateTokenWithKeySalt validates token and uses your key salt See MakeTokenWithSalt

Types

This section is empty.

Jump to

Keyboard shortcuts

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