jwt

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package jwt provides functionality for generating and parsing JSON Web Tokens (JWT) specifically for application authentication in the go-api project.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateAppToken

func GenerateAppToken(App *auth.App, expireTime time.Duration) (token string, err error)

GenerateAppToken creates a new JWT token for an application.

Parameters:

  • App: A pointer to the auth.App struct containing application details.
  • expireTime: The duration for which the token should be valid, in seconds.

Returns:

  • token: The generated JWT token as a string.
  • err: An error if token generation fails, nil otherwise.

Example:

app := &auth.App{ID: 1, AppName: "MyApp", AppID: "APP123"}
token, err := GenerateAppToken(app, 3600) // Token valid for 1 hour
if err != nil {
    log.Fatalf("Failed to generate token: %v", err)
}

Types

type ServerClaims

type ServerClaims struct {
	ID      uint   `json:"id"`       // Unique identifier for the app
	AppName string `json:"app_name"` // Name of the application
	AppID   string `json:"app_id"`   // Unique ID of the application
	jwt.RegisteredClaims
}

ServerClaims represents the custom claims structure for the JWT. It extends the standard RegisteredClaims with application-specific fields.

func ParseAppAuth

func ParseAppAuth(token string) (*ServerClaims, error)

ParseAppAuth parses and validates a JWT token string.

Parameters:

  • token: The JWT token string to be parsed and validated.

Returns:

  • *ServerClaims: A pointer to the parsed ServerClaims if the token is valid.
  • error: An error if parsing fails or the token is invalid, nil otherwise.

Example:

tokenString := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
claims, err := ParseAppAuth(tokenString)
if err != nil {
    log.Fatalf("Failed to parse token: %v", err)
}
fmt.Printf("App ID: %s\n", claims.AppID)

Jump to

Keyboard shortcuts

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