{"id":15394385,"url":"https://github.com/xyproto/simplejwt","last_synced_at":"2025-04-15T23:53:21.065Z","repository":{"id":153917449,"uuid":"630820095","full_name":"xyproto/simplejwt","owner":"xyproto","description":"A simple JWT package","archived":false,"fork":false,"pushed_at":"2023-06-16T08:45:52.000Z","size":568,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-29T03:03:53.408Z","etag":null,"topics":["authentication","go","http","jwt","simple"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xyproto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2023-04-21T08:18:43.000Z","updated_at":"2024-09-13T06:11:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"45fa019a-30cd-4fe3-8ff4-5ad008a6191d","html_url":"https://github.com/xyproto/simplejwt","commit_stats":{"total_commits":58,"total_committers":2,"mean_commits":29.0,"dds":"0.10344827586206895","last_synced_commit":"fa067764bf6095f02b54422cd86c2fb056d9db28"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xyproto%2Fsimplejwt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xyproto%2Fsimplejwt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xyproto%2Fsimplejwt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xyproto%2Fsimplejwt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xyproto","download_url":"https://codeload.github.com/xyproto/simplejwt/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249173061,"owners_count":21224481,"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":["authentication","go","http","jwt","simple"],"created_at":"2024-10-01T15:23:26.754Z","updated_at":"2025-04-15T23:53:21.047Z","avatar_url":"https://github.com/xyproto.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# simplejwt [![Build](https://github.com/xyproto/simplejwt/actions/workflows/build.yml/badge.svg)](https://github.com/xyproto/simplejwt/actions/workflows/build.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/xyproto/simplejwt)](https://goreportcard.com/report/github.com/xyproto/simplejwt) [![License](https://img.shields.io/badge/license-BSD-green.svg?style=flat)](https://raw.githubusercontent.com/xyproto/simplejwt/main/LICENSE)\n\nA simple JWT package.\n\n## Generate and verify JWT tokens\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n\n    \"github.com/xyproto/simplejwt\"\n)\n\nfunc main() {\n    // Set the secret that is used for generating and validating JWT tokens\n    simplejwt.SetSecret(\"server-secret-goes-here\")\n\n    // Generate a token by passing in a subject and for how many seconds the token should last\n    token := simplejwt.SimpleGenerate(\"bob@zombo.com\", 3600)\n    if token == \"\" {\n        fmt.Println(\"Failed to generate token\")\n        return\n    }\n    fmt.Printf(\"Generated token: %s\\n\", token)\n\n    // Validate the token\n    decodedSubject := simplejwt.SimpleValidate(token)\n    if decodedSubject == \"\" {\n        fmt.Println(\"Failed to validate token\")\n        return\n    }\n    fmt.Printf(\"Decoded payload, got subject: %s\\n\", decodedSubject)\n}\n```\n\n## Using a Payload struct\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"time\"\n\n    \"github.com/xyproto/simplejwt\"\n)\n\nfunc main() {\n    // Set the JWT secret\n    simplejwt.SetSecret(\"server-secret-goes-here\")\n\n    // Generate a token\n    payload := simplejwt.Payload{\n        Subject: \"1234567890\",\n        Expires: time.Now().Add(time.Hour),\n    }\n\n    token, err := simplejwt.Generate(payload, nil)\n    if err != nil {\n        fmt.Printf(\"Failed to generate token: %v\\n\", err)\n        return\n    }\n\n    fmt.Printf(\"Generated token: %s\\n\", token)\n\n    // Validate the token\n    decodedPayload, err := simplejwt.Validate(token)\n    if err != nil {\n        fmt.Printf(\"Failed to validate token: %v\\n\", err)\n        return\n    }\n\n    fmt.Printf(\"Decoded payload: %+v\\n\", decodedPayload)\n}\n```\n\n* `Subject` is the user or system that the token is about.\n* `Expires` is the expiration time of the token.\n* The secret key is used when JWT tokens are generated or verified, together with the HMAC SHA256 algorithm.\n\nThis example is also available as `cmd/simple/main.go`.\n\n## Set up a simple HTTP server\n\nThis is a simple HTTP server that can be accessed in a browser as `http://localhost:4000`.\n\nIt has the following endpoints:\n\n* `/` - a HTML page with instructions for how to use `curl`.\n* `/generate`  - for generating a JWT token.\n* `/protected` - for retrieving protected data, but only if it is also given a valid JWT token.\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"net/http\"\n    \"strings\"\n    \"time\"\n\n    \"github.com/xyproto/simplejwt\"\n)\n\nfunc generateHandler(w http.ResponseWriter, r *http.Request) {\n    if r.Method != http.MethodPost {\n        http.Error(w, \"Method not allowed\", http.StatusMethodNotAllowed)\n        return\n    }\n\n    payload := simplejwt.Payload{\n        Subject: \"1234567890\",\n        Expires: time.Now().Add(time.Hour),\n    }\n    token, err := simplejwt.Generate(payload, nil)\n    if err != nil {\n        http.Error(w, \"Error generating token\", http.StatusInternalServerError)\n        return\n    }\n\n    w.Header().Set(\"Content-Type\", \"text/plain\")\n    w.Write([]byte(token))\n}\n\nfunc protectedHandler(w http.ResponseWriter, r *http.Request) {\n    authHeader := r.Header.Get(\"Authorization\")\n    if authHeader == \"\" {\n        http.Error(w, \"Authorization header not provided\", http.StatusUnauthorized)\n        return\n    }\n\n    token := strings.TrimPrefix(authHeader, \"Bearer \")\n    if token == \"\" {\n        http.Error(w, \"Token not provided\", http.StatusUnauthorized)\n        return\n    }\n\n    _, err := simplejwt.Validate(token)\n    if err != nil {\n        http.Error(w, \"Invalid or expired token\", http.StatusUnauthorized)\n        return\n    }\n\n    w.Header().Set(\"Content-Type\", \"application/json\")\n    w.Write([]byte(`{\"message\": \"Access granted to protected data.\"}`))\n}\n\nfunc rootHandler(w http.ResponseWriter, r *http.Request) {\n    html := `\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n\u003chead\u003e\n    \u003cmeta charset=\"UTF-8\"\u003e\n    \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"\u003e\n    \u003ctitle\u003eSimple JWT Example\u003c/title\u003e\n    \u003cstyle\u003e\n        body {\n            font-family: Arial, sans-serif;\n            max-width: 800px;\n            margin: 0 auto;\n            padding: 1rem;\n        }\n        pre {\n            background-color: #f5f5f5;\n            padding: 0.5rem;\n            overflow-x: scroll;\n        }\n    \u003c/style\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n    \u003ch1\u003eSimple JWT Example\u003c/h1\u003e\n    \u003cp\u003eUse the following curl commands to interact with the server:\u003c/p\u003e\n    \u003ch2\u003e1. Generate a JWT token\u003c/h2\u003e\n    \u003cp\u003eSend a POST request to \u003ccode\u003e/generate\u003c/code\u003e to generate a JWT token:\u003c/p\u003e\n    \u003cpre\u003ecurl -X POST http://localhost:4000/generate\u003c/pre\u003e\n    \u003ch2\u003e2. Access protected data\u003c/h2\u003e\n    \u003cp\u003eSend a GET request to \u003ccode\u003e/protected\u003c/code\u003e with the token in the Authorization header to access protected data:\u003c/p\u003e\n    \u003cpre\u003ecurl -H \"Authorization: Bearer \u0026lt;your_token_here\u0026gt;\" http://localhost:4000/protected\u003c/pre\u003e\n    \u003cp\u003eReplace \u003ccode\u003e\u0026lt;your_token_here\u0026gt;\u003c/code\u003e with the token you received from the previous command.\u003c/p\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n`\n    w.Header().Set(\"Content-Type\", \"text/html\")\n    w.Write([]byte(html))\n}\n\nfunc main() {\n    http.HandleFunc(\"/\", rootHandler)\n    http.HandleFunc(\"/generate\", generateHandler)\n    http.HandleFunc(\"/protected\", protectedHandler)\n    fmt.Println(\"Server running on :4000\")\n    http.ListenAndServe(\":4000\", nil)\n}\n```\n\nThis example is also available as `cmd/server/main.go`.\n\n### Kawaii Chat\n\nA simple chat application that uses Go for the backend and a vanilla JS SPA as the front end is available in `cmd/kawaiichat`.\n\nTo try it out, just enter the `cmd/kawaiichat` directory, run `go build -mod=vendor \u0026\u0026 ./kawaiichat` and then visit `http://localhost:8080` in a browser.\n\nScreenshot:\n\n![kawaii chat screenshot](img/kawaiichat.png)\n\n## General info\n\n* Version: 1.2.0\n* License: BSD-3\n* Author: Alexander F. Rødseth \u0026lt;xyproto@archlinux.org\u0026gt;\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxyproto%2Fsimplejwt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxyproto%2Fsimplejwt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxyproto%2Fsimplejwt/lists"}