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 ¶
- Variables
- func AreAllowedCharacters(v string) bool
- func Generate(header string, payload string, key string) string
- func GenerateID() string
- func GetRandomNumber() int
- func IsAlphaAndSpaceOnly(v string) bool
- func IsValidEmailFormat(v string) bool
- func ParseBody(r *http.Request, x interface{}) error
- func ParseResponseBody(r *http.Response, x interface{}) error
- func SendBadRequestMsgToClient(w *http.ResponseWriter, err error)
- func SendConflictMsgToClient(w *http.ResponseWriter, err error)
- func SendDataToClient(w *http.ResponseWriter, data []byte, msg string)
- func SendErrorMsgToClient(w *http.ResponseWriter, err error)
- func SendForbiddenMsgToClient(w *http.ResponseWriter, err error)
- func SendNotFoundMsgToClient(w *http.ResponseWriter, err error)
- func Verify(jwt string, key string) (bool, error)
- type JWTHeader
- type JWTPayload
Constants ¶
This section is empty.
Variables ¶
var ErrEmptyJWTHeader = errors.New("[JWT]: jwt header is empty")
var ErrEmptyJWTPayload = errors.New("[JWT]: jwt payload is empty")
var ErrEmptyJWTSignature = errors.New("[JWT]: jwt signature is empty")
var ErrEmptyKey = errors.New("[JWT]: signing key cannot be empty")
var ErrEmptyToken = errors.New("[JWT]: jwt cannot be empty")
var ErrWrongFormat = errors.New("[JWT]: wrong token format")
Functions ¶
func AreAllowedCharacters ¶
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 ¶
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 IsAlphaAndSpaceOnly ¶
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 ¶
IsValidEmailFormat checks if the input, v is in a valid email format. Return true if valid; false if not valid.
func ParseResponseBody ¶
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 {}
Types ¶
type JWTHeader ¶
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).