{"id":19282562,"url":"https://github.com/scott-mescudi/go-jwtlib","last_synced_at":"2025-06-22T16:38:04.311Z","repository":{"id":255436635,"uuid":"849848332","full_name":"scott-mescudi/GO-jwtlib","owner":"scott-mescudi","description":"Golang jwt library for personal use","archived":false,"fork":false,"pushed_at":"2024-10-13T13:00:11.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-05T17:13:28.866Z","etag":null,"topics":["auth","golang","jwt"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/scott-mescudi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-30T11:21:19.000Z","updated_at":"2024-10-13T13:00:14.000Z","dependencies_parsed_at":"2025-01-05T17:13:14.884Z","dependency_job_id":"609dcf64-62e1-43a9-bd1a-ee2fff7f4474","html_url":"https://github.com/scott-mescudi/GO-jwtlib","commit_stats":null,"previous_names":["scott-mescudi/go-jwtlib"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scott-mescudi%2FGO-jwtlib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scott-mescudi%2FGO-jwtlib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scott-mescudi%2FGO-jwtlib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scott-mescudi%2FGO-jwtlib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scott-mescudi","download_url":"https://codeload.github.com/scott-mescudi/GO-jwtlib/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240386641,"owners_count":19793211,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auth","golang","jwt"],"created_at":"2024-11-09T21:27:22.050Z","updated_at":"2025-02-23T22:23:31.593Z","avatar_url":"https://github.com/scott-mescudi.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jwtlib\n\n`jwtlib` is a Go library for generating and validating JWT (JSON Web Tokens) using RSA keys. This library is designed to be simple, flexible, and easy to integrate into your Go applications. Note that this library was made for personal use.\n\n## Features\n\n- Load RSA private and public keys from PEM files\n- Generate JWT tokens with custom claims\n- Validate JWT tokens with custom validation logic\n\n## Installation\n\nTo install the library, run:\n\n```bash\ngo get github.com/scott-mescudi/GO-jwtlib/jwtlib\n```\n\nMake sure to replace `scott-mescudi` with your actual GitHub username or wherever the repository is hosted.\n\n## Usage\n\n### Importing the Package\n\n```go\nimport \"github.com/scott-mescudi/GO-jwtlib/jwtlib\"\n```\n\n### Loading RSA Keys\n\nYou can load your RSA private and public keys from PEM files using the `LoadRSAKeyFromPEM` function.\n\n```go\nprivateKey, err := jwtlib.LoadRSAKeyFromPEM(\"path/to/private.pem\", \"private\")\nif err != nil {\n    log.Fatalf(\"failed to load private key: %v\", err)\n}\n\npublicKey, err := jwtlib.LoadRSAKeyFromPEM(\"path/to/public.pem\", \"public\")\nif err != nil {\n    log.Fatalf(\"failed to load public key: %v\", err)\n}\n```\n\n### Generating a JWT Token\n\nYou can generate a JWT token with custom claims using the `GenerateToken` function.\n\n```go\nclaims := jwtlib.DefaultClaims(\"username\", \"admin\")\n\ntoken, err := jwtlib.GenerateToken(privateKey.(*rsa.PrivateKey), claims)\nif err != nil {\n    log.Fatalf(\"failed to generate token: %v\", err)\n}\n\nfmt.Printf(\"Generated Token: %s\\n\", token)\n```\n\n### Validating a JWT Token\n\nYou can validate a JWT token and apply custom claim validation using the `ValidateToken` function.\n\n```go\nisValid, err := jwtlib.ValidateToken(token, publicKey.(*rsa.PublicKey), func(claims jwt.MapClaims) error {\n    // Custom claim validation logic\n    if claims[\"iss\"] != \"my-auth-server\" {\n        return errors.New(\"invalid issuer\")\n    }\n\n    // Ensure the token is not expired\n    if exp, ok := claims[\"exp\"].(float64); ok {\n        if time.Now().Unix() \u003e int64(exp) {\n            return errors.New(\"token has expired\")\n        }\n    } else {\n        return errors.New(\"missing expiration claim\")\n    }\n\n    return nil\n})\n\nif err != nil || !isValid {\n    log.Fatalf(\"token validation failed: %v\", err)\n}\n\nfmt.Println(\"Token is valid!\")\n```\n\n### Example Application\n\nHere's a full example of how you might use `jwtlib` in an application:\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"log\"\n    \"time\"\n    \"crypto/rsa\"\n    \"errors\"\n    \"github.com/scott-mescudi/jwtlib\"\n)\n\nfunc main() {\n    // Load the RSA keys\n    privateKey, err := jwtlib.LoadRSAKeyFromPEM(\"path/to/private.pem\", \"private\")\n    if err != nil {\n        log.Fatalf(\"failed to load private key: %v\", err)\n    }\n\n    publicKey, err := jwtlib.LoadRSAKeyFromPEM(\"path/to/public.pem\", \"public\")\n    if err != nil {\n        log.Fatalf(\"failed to load public key: %v\", err)\n    }\n\n    // Generate a JWT token\n    claims := jwtlib.DefaultClaims(\"username\", \"admin\")\n    token, err := jwtlib.GenerateToken(privateKey.(*rsa.PrivateKey), claims)\n    if err != nil {\n        log.Fatalf(\"failed to generate token: %v\", err)\n    }\n\n    fmt.Printf(\"Generated Token: %s\\n\", token)\n\n    // Validate the JWT token\n    isValid, err := jwtlib.ValidateToken(token, publicKey.(*rsa.PublicKey), func(claims jwt.MapClaims) error {\n        if claims[\"iss\"] != \"my-auth-server\" {\n            return errors.New(\"invalid issuer\")\n        }\n        if exp, ok := claims[\"exp\"].(float64); ok {\n            if time.Now().Unix() \u003e int64(exp) {\n                return errors.New(\"token has expired\")\n            }\n        } else {\n            return errors.New(\"missing expiration claim\")\n        }\n        return nil\n    })\n\n    if err != nil || !isValid {\n        log.Fatalf(\"token validation failed: %v\", err)\n    }\n\n    fmt.Println(\"Token is valid!\")\n}\n```\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscott-mescudi%2Fgo-jwtlib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscott-mescudi%2Fgo-jwtlib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscott-mescudi%2Fgo-jwtlib/lists"}