timaan

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2021 License: MIT Imports: 6 Imported by: 1

README

timaan The timaan package is a token generator for your user's authentication process in your app whether it's a WEB, CLI, or Mobile applications.

Installation

go get -u github.com/itrepablik/timaan

Usage

This is the sample usage for the timaan package.

package main

import (
	"fmt"
	"time"

	"github.com/itrepablik/itrlog"
	"github.com/itrepablik/timaan"
)

func main() {
	//*************************************************************************************
	// Add new token request for token key e.g username: 'politz' after successful login
	// This will serve as your session token stored in memory
	//*************************************************************************************
	tokenPayload := timaan.TP{
		"LOG_LEVEL":  "INFO",
		"API_KEY":    timaan.RandomToken(),
		"USERNAME":   "politz",
		"EMAIL":      "[email protected]",
		"FIRST_NAME": "Juan",
		"LAST_NAME":  "Dela Cruz",
	}
	tok := timaan.TK{
		TokenKey: "politz",
		Payload:  tokenPayload,
		ExpireOn: time.Now().Add(time.Minute * 30).Unix(),
	}
	newToken, err := timaan.GenerateToken("politz", tok)
	if err != nil {
		itrlog.Fatal(err)
	}
	fmt.Println("newToken: ", newToken)

	//*****************************************************************************
	// Extract the token payload for any specific user, e.g username: 'politz'
	// or any token key e.g: '54f2067c42ee4587bf239a58bcdd438a'
	//*****************************************************************************
	tok, err = timaan.DecodePayload("politz")
	if err != nil {
		itrlog.Fatal(err)
	}
	fmt.Println("TokenKey: ", tok.TokenKey)

	payLoad := tok.Payload
	for field, val := range payLoad {
		fmt.Println(field, " : ", val)
	}

	//************************************************
	// Example for email confirmation token
	//************************************************
	rt := timaan.RandomToken()
	emailConfirmPayload := timaan.TP{
		"USERNAME": "juan",
		"EMAIL":    "[email protected]",
	}
	tok = timaan.TK{
		TokenKey: rt,
		Payload:  emailConfirmPayload,
		ExpireOn: time.Now().Add(time.Minute * 30).Unix(),
	}
	newToken, err = timaan.GenerateToken(rt, tok)
	if err != nil {
		itrlog.Fatal(err)
	}
	confirmURL := "https://itrepablik.com/confirm/" + newToken
	fmt.Println(confirmURL)

	//*********************************************************************************
	// Remove token for any specific user, e.g username: 'politz'
	// Or remove any token key for e.g token key: '54f2067c42ee4587bf239a58bcdd438a'
	//*********************************************************************************
	isTokenRemove, err := timaan.UT.Remove("politz")
	if err != nil {
		itrlog.Fatal(err)
	}
	fmt.Println("isTokenRemove: ", isTokenRemove)
}

Subscribe to Maharlikans Code Youtube Channel:

Please consider subscribing to my Youtube Channel to recognize my work on this package. Thank you for your support! https://www.youtube.com/channel/UCdAVUmldU9Jn2VntuQChHqQ/

License

Code is distributed under MIT license, feel free to use it in your proprietary projects as well.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var UT = UserTokens{Token: make(map[string][]byte)}

UT is a user's token methods

Functions

func EncodePayload

func EncodePayload(payLoad TK) ([]byte, error)

EncodePayload encodes the token payload using gob

func GenerateToken

func GenerateToken(tokenKey string, payLoad TK) ([]byte, error)

GenerateToken generate a new timaan token which can be used mostly after successful authentication process, mostly use after successful login, this can also be used as you sessions for the entire duration of the token validity period.

func RandomToken added in v1.0.1

func RandomToken() string

RandomToken uses the lower-case letters from 'a' to 'z' and positive numeric digits combination

Types

type TK

type TK struct {
	TokenKey string
	Payload  TP
	ExpireOn int64
}

TK is a successful auth token requests after successful login the TokenKey might be anything, e.g username, random unique alpha-numeric strings, etc.

func DecodePayload added in v1.0.1

func DecodePayload(tokenKey string) (TK, error)

DecodePayload extracts the token payload

type TP

type TP map[string]interface{}

TP type stands for 'Token Payload' that uses the map[string]interface{} as the custom token payload structure

type UserTokens

type UserTokens struct {
	Token map[string][]byte
	// contains filtered or unexported fields
}

UserTokens is a users auth token requests stored in memory

func (*UserTokens) Add

func (t *UserTokens) Add(tokenKey string, encBytes []byte)

Add insert the new token request to the 'UserTokens' map

func (*UserTokens) Get added in v1.0.1

func (t *UserTokens) Get(tokenKey string) ([]byte, bool)

Get gets the specific user's token filtered by the tokenKey

func (*UserTokens) Remove

func (t *UserTokens) Remove(tokenKey string) (bool, error)

Remove any single stored token from the 'UserTokens' map

Jump to

Keyboard shortcuts

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