utils

package
v0.0.0-...-02da272 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package jwt is a custom inplementation of the well known JWT algorithm. This custom implementation is to illustrate the author's understanding on hashing and the publicly known application of hashing that is commonly used in JWT-based authentication and verification protocol in many Web-based application.

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyJWTHeader = errors.New("[JWT]: jwt header is empty")
View Source
var ErrEmptyJWTPayload = errors.New("[JWT]: jwt payload is empty")
View Source
var ErrEmptyJWTSignature = errors.New("[JWT]: jwt signature is empty")
View Source
var ErrEmptyKey = errors.New("[JWT]: signing key cannot be empty")
View Source
var ErrEmptyToken = errors.New("[JWT]: jwt cannot be empty")
View Source
var ErrWrongFormat = errors.New("[JWT]: wrong token format")

Functions

func AreAllowedCharacters

func AreAllowedCharacters(v string) bool

AreAllowedCharacters checks if the input, v contains any of the following characters that are not allowed: - / - \ - - Returns - true if v does not contain any not allowed characters; - false if v contains any of the not allowed characters.

func Generate

func Generate(header string, payload string, key string) string

Generate creates a JWT JSON string using the parameters passed in. Input parameters: - a is a JSON string, {"algo": "SHA3.512", "typ": "JWT"}, that indicates the hashing algorithm used for generating the authenticity code (used in later verification). Only SHA3.512 is supported; - b is a JSON string that contains the payload. The attributes supported are -

  • "username" (string), unique User Account Id;
  • "expiresOn" (string), a valie date time string;
  • "roles" ([]string), Roles assigned to the User;

func GenerateID

func GenerateID() string

GenerateID returns a randomly generated ID string. The randomly generated ID string is in the following format: YYYY.MM.DD.RRRRRRRR where - YYYY represents the Year of the system date - MM represents the Month of the system date - DD represents the Day of the system date - RRRRRRRR is the zero-padded random number

func GetRandomNumber

func GetRandomNumber() int

GetRandomNumber returns a random number.

func IsAlphaAndSpaceOnly

func IsAlphaAndSpaceOnly(v string) bool

IsAlphaAndSpaceOnly checks if the input, v contains only alphabets and white space characters (i.e. space or tab) only. Return true if valid; false if not valid.

func IsValidEmailFormat

func IsValidEmailFormat(v string) bool

IsValidEmailFormat checks if the input, v is in a valid email format. Return true if valid; false if not valid.

func ParseBody

func ParseBody(r *http.Request, x interface{}) error

func ParseResponseBody

func ParseResponseBody(r *http.Response, x interface{}) error

func SendBadRequestMsgToClient

func SendBadRequestMsgToClient(w *http.ResponseWriter, err error)

SendBadRequestMsgToClient prepares: - a BAD REQUEST response header; - a JSON body containing:

  • "ok" attribute, set to false;
  • "msg" attribute set to the error message passed in;
  • "data" attribute set to {}

func SendConflictMsgToClient

func SendConflictMsgToClient(w *http.ResponseWriter, err error)

func SendDataToClient

func SendDataToClient(w *http.ResponseWriter, data []byte, msg string)

SendDataToClient prepares: - a OK header; - a JSON body containing:

  • "ok" attribute, set to true;
  • "msg" attriubte;
  • "data" attribute, set to the data passed in

and send the response to the client.

func SendErrorMsgToClient

func SendErrorMsgToClient(w *http.ResponseWriter, err error)

SendErrorMsgToClient prepares: - a INTERNAL SERVER ERROR response header; - a JSON body containing:

  • "ok" attribute, set to false;
  • "msg" attribute set to the error message passed in;
  • "data" attribute set to {}

func SendForbiddenMsgToClient

func SendForbiddenMsgToClient(w *http.ResponseWriter, err error)

SendForbiddenMsgToClient prepares: - a FORBIDDEN response header; - a JSON body containing:

  • "ok" attribute, set to false;
  • "msg" attribute set to the error message passed in;
  • "data" attribute set to {}

func SendNotFoundMsgToClient

func SendNotFoundMsgToClient(w *http.ResponseWriter, err error)

SendNotFoundMsgToClient prepares: - a NOT FOUND response header; - a JSON body containing:

  • "ok" attribute, set to false;
  • "msg" attribute set to the error message passed in;
  • "data" attribute set to {}

func Verify

func Verify(jwt string, key string) (bool, error)

Verify uses the passed in jwt and key to execute a check on the integrity of the jwt. Input parameters: - jwt is a JSON string - key is the secret key used by the service to generate the jwt Returns: an error when the integrity check fails. nil when the integrity check is successful.

Types

type JWTHeader

type JWTHeader struct {
	Alg string `json:"alg"`
	Typ string `json:"typ"`
}

JWTHeader is the struct for holding the data used in generating the first segment of the JWT string.

type JWTPayload

type JWTPayload struct {
	Id    float64 `json:"id"`
	Name  string  `json:"name"`
	Phone string  `json:"phone"`
	Iss   string  `json:"iss"`
	Exp   int64   `json:"exp"`
}

func GetJWTPayload

func GetJWTPayload(r *http.Request) (JWTPayload, error)

GetJWTPayload will extract the token in the request, retrieve the payload segment of the token (in base64 encoding) and return decoded payload (in plain text).

Jump to

Keyboard shortcuts

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