auth

package
v0.0.0-...-56bfa2c Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package auth provides functions for generating and verifying JWT tokens for user authentication.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateJWT

func GenerateJWT(jwtKey []byte, username string) (string, error)

GenerateJWT generates a JSON Web Token (JWT) for a user after login.

The token is signed using the provided jwtKey and includes the username in the claims. The token expires 24 hours after it is issued.

Parameters:

  • jwtKey: A byte slice containing the key to sign the JWT.
  • username: The username to include in the JWT claims.

Returns:

  • A string representing the signed JWT.
  • An error if there is an issue generating the token.

Types

type Claims

type Claims struct {
	Username string `json:"username"`
	Role     string `json:"role"`
	jwt.RegisteredClaims
}

Claims represents the JWT claims, which includes the username and registered claims.

func VerifyJWT

func VerifyJWT(jwtKey []byte, tokenString string) (*Claims, error)

VerifyJWT verifies a JSON Web Token (JWT) and returns the claims if the token is valid.

The token is parsed and validated using the provided jwtKey. If the token is valid and the signature is correct, the claims are returned. If the token is invalid or the signature does not match, an error is returned.

Parameters:

  • jwtKey: A byte slice containing the key to verify the JWT signature.
  • tokenString: The JWT string to be verified.

Returns:

  • A pointer to the Claims struct if the token is valid.
  • An error if the token is invalid or the signature does not match.

Jump to

Keyboard shortcuts

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