Skip to content

Latest commit

 

History

History
59 lines (42 loc) · 1.1 KB

File metadata and controls

59 lines (42 loc) · 1.1 KB

Getting started

Install

go get github.com/fgrzl/claims

Build a principal

import (
    "time"

    "github.com/fgrzl/claims"
    "github.com/fgrzl/claims/jwtkit"
)

cs := claims.NewClaimsSet("user123").
    SetIssuer("myapp").
    SetEmail("user@example.com").
    SetRoles("admin", "user").
    Set("department", "engineering")

principal := claims.NewPrincipal(cs)

Set custom claims on ClaimSet before NewPrincipal — the principal holds a copy; mutating cs afterward does not change the principal.

Sign a JWT (HMAC)

secret := []byte(os.Getenv("JWT_SECRET"))
signer := &jwtkit.HMAC256Signer{Secret: secret}

token, err := signer.CreateToken(principal, time.Hour)

Validate

validator := &jwtkit.HMAC256Validator{Secret: secret}
validated, err := validator.Validate(token)
if err != nil {
    return err
}
_ = validated.Subject()

Custom claims on principal

dept := principal.CustomClaimValue("department")

Next steps