Documentation ¶
Index ¶
- Variables
- func AddRootToPath(path string) string
- func CheckPasswordHash(password, hash string) bool
- func CreateToken(username string) (string, error)
- func FindCookieInResponse(cookie string, resp *http.Response) error
- func GenerateUUID() string
- func GetDatabase() *gorm.DB
- func HashPassword(password string) (string, error)
- func IsAuth(r *http.Request) bool
- func IsPasswordValid(password string) bool
- func IsUsernameValid(username string) bool
- func SetDatabase(database *gorm.DB)
- func ValidatePasswordStrength(password string) bool
- func ValidateToken(tokenString string) (string, error)
- func WriteResponseJSON(ctx *fasthttp.RequestCtx, code int, obj interface{})
- type C
- type ErrorPageContent
- type JSON
Constants ¶
This section is empty.
Variables ¶
var ( // InternalServerErrorPage is used when some internal code execution has failed. InternalServerErrorPage = ErrorPageContent{ StatusCode: fasthttp.StatusInternalServerError, MainMessage: fasthttp.StatusMessage(fasthttp.StatusInternalServerError), Description: "There was a server error while processing your request, this is due to the server.", } // BadRequestErrorPage is used when the user's request had bad properties. BadRequestErrorPage = ErrorPageContent{ StatusCode: fasthttp.StatusBadRequest, MainMessage: fasthttp.StatusMessage(fasthttp.StatusBadRequest), Description: "There was an error without request input.", } // NotFoundErrorPage is used when something the user is searching for is not found. NotFoundErrorPage = ErrorPageContent{ StatusCode: fasthttp.StatusNotFound, MainMessage: fasthttp.StatusMessage(fasthttp.StatusNotFound), Description: "The thing you're searching for has not been found", } // ForbiddenErrorPage is used when the user is trying to do something without permission. ForbiddenErrorPage = ErrorPageContent{ StatusCode: fasthttp.StatusForbidden, MainMessage: fasthttp.StatusMessage(fasthttp.StatusForbidden), Description: "You're not allowed to view the content on this page.", } // ConflictErrorPage is used when the user tries to create information into the database that // already exists. ConflictErrorPage = ErrorPageContent{ StatusCode: fasthttp.StatusConflict, MainMessage: fasthttp.StatusMessage(fasthttp.StatusConflict), Description: "The username is already taken.", } )
Functions ¶
func AddRootToPath ¶
func CheckPasswordHash ¶
CheckPasswordHash checks if the given password's bcrypt hash matches the given hash.
func CreateToken ¶
CreateToken creates a jwt token which stores a username and is valid for 24 hours.
func FindCookieInResponse ¶
FindCookieInResponse loops over the cookies in a request and checks if those cookies include the given cookie.
func GenerateUUID ¶
func GenerateUUID() string
GenerateUUID uses the 'github.com/satori/go.uuid' package to construct a new V4 unique id.
func GetDatabase ¶
GetDatabase returns a pointer, which points to the database instance.
func HashPassword ¶
HashPassword creates a bcrypt hash from a given password.
func IsAuth ¶
IsAuth takes in a request pointer and returns a boolean telling if a user has a valid token in their cookies.
func IsPasswordValid ¶
IsPasswordValid checks if the password a user has chosen is valid.
func IsUsernameValid ¶
IsUsernameValid checks if the username a user has chosen is valid.
func SetDatabase ¶
SetDatabase sets the global variable in this file to database instance created in the main function.
func ValidateToken ¶
ValidateToken takes a token as an argument and checks if that token is valid. If the token is valid, then the function returns the usernanem stored in the token.
func WriteResponseJSON ¶
func WriteResponseJSON(ctx *fasthttp.RequestCtx, code int, obj interface{})
WriteResponseJSON takes in a request context, status code, and a golang interface. The functions sets the request's response with the given status code and content type. And then golang's json package encodes the data from the interface into json format.
Types ¶
type C ¶
type C struct { Username string `json:"username"` jwt.StandardClaims }
C is a simple struct to keep the username and the standard claims of a jwt, such as the expiration time
type ErrorPageContent ¶
type ErrorPageContent struct { StatusCode int `json:"status"` MainMessage string `json:"message"` Description string `json:"description"` }
ErrorPageContent defines the fields that are on the error site.
func CreateDetailedErrorContent ¶
func CreateDetailedErrorContent(err error, title string, code int) *ErrorPageContent
CreateDetailedErrorContent makes a more detailed error page with fully custom content
func CreateSimpleErrorContent ¶
func CreateSimpleErrorContent(statusCode int) *ErrorPageContent
CreateSimpleErrorContent makes a simpler and more common version of the above constants. This is used for status codes that are not as common as the ones above.