{"id":17312198,"url":"https://github.com/oreqizer/go-jaywt","last_synced_at":"2025-03-27T01:14:45.508Z","repository":{"id":57502649,"uuid":"75559780","full_name":"oreqizer/go-jaywt","owner":"oreqizer","description":":unlock: A utility package for extracting and validating JWT tokens from requests.","archived":false,"fork":false,"pushed_at":"2017-07-17T11:01:14.000Z","size":14,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-01T06:27:42.407Z","etag":null,"topics":["auth","go","jwt","token"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oreqizer.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}},"created_at":"2016-12-04T19:40:27.000Z","updated_at":"2017-05-28T19:41:38.000Z","dependencies_parsed_at":"2022-09-13T08:20:31.400Z","dependency_job_id":null,"html_url":"https://github.com/oreqizer/go-jaywt","commit_stats":null,"previous_names":["oreqizer/go-jwt-parser"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oreqizer%2Fgo-jaywt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oreqizer%2Fgo-jaywt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oreqizer%2Fgo-jaywt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oreqizer%2Fgo-jaywt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oreqizer","download_url":"https://codeload.github.com/oreqizer/go-jaywt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245761301,"owners_count":20667895,"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","go","jwt","token"],"created_at":"2024-10-15T12:42:44.551Z","updated_at":"2025-03-27T01:14:45.482Z","avatar_url":"https://github.com/oreqizer.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JayWT\n\n[![godoc](https://img.shields.io/badge/godoc-reference-blue.svg)](https://godoc.org/github.com/oreqizer/go-jaywt)\n[![Build Status](https://travis-ci.org/oreqizer/go-jaywt.svg?branch=master)](https://travis-ci.org/oreqizer/go-jaywt)\n[![codecov](https://codecov.io/gh/oreqizer/go-jaywt/branch/master/graph/badge.svg)](https://codecov.io/gh/oreqizer/go-jaywt)\n\nA utility package that provides a DRY approach to extracting and validating JWT tokens.\n\n\u003e While it solves the exact problem [go-jwt-middleware](https://github.com/auth0/go-jwt-middleware) does, it doesn't have Gorilla context as a dependency and lets you use your own type of claims.\n\n## Usage\n\nThe API basically consists of three important functions and an `Options` struct:\n\n* Create a new instance with `jaywt.New(\u0026jaywt.Options{})`\n* Extract \u0026 verify a JWT using `jaywt.Get(request)`\n* Extract \u0026 verify a JWT with custom claims using `jaywt.GetWithClaims(request, \u0026MyClaims{})`\n\n### Dependencies\n\n* [jwt-go](https://github.com/dgrijalva/jwt-go)\n\n## Examples\n\nCreate an instance (all options are optional):\n\n```go\nj := jaywt.New(\u0026jaywt.Options{\n    // Defaults to 'nil'\n    Keyfunc: func(_ *jwt.Token) (interface{}, error) {\n        return []byte(\"secretAF\"), nil\n    },\n    // Defaults to 'Authorization' header being: Bearer \u003ctoken\u003e\n    Extractor: func(r *http.Request) (string, error) {\n        return r.Header.Get(\"X-Authorization\"), nil\n    },\n    // This is the default:\n    SigningMethod: jwt.SigningMethodHS256,\n})\n```\n\n### Get JWT\n\nCreate any middleware you like! All you need is a `http.Request`. An example using [gin](https://github.com/gin-gonic/gin):\n\n```go\n// usage: api.Use(AuthMiddleware(p))\nfunc AuthMiddleware(j *jaywt.Core) gin.HandlerFunc {\n\treturn func(c *gin.Context) {\n\t\ttoken, err := j.Get(c.Request)\n\t\tif err != nil {\n\t\t\tc.AbortWithError(http.StatusUnauthorized, err)\n\t\t\treturn\n\t\t}\n\n\t\tc.Set(\"userId\", token.Claims.Subject)\n\t\tc.Next()\n\t}\n}\n```\n\n### Get JWT with claims\n\nPass your claims struct as a second argument to `GetWithClaims`:\n\n```go\ntype MyClaims struct {\n\tDoe string `json:\"doe\"`\n\t// important to allow jwt-go built-in validation:\n\tjwt.StandardClaims\n}\n\nfunc AuthMiddleware(j *jaywt.Core) gin.HandlerFunc {\n\treturn func(c *gin.Context) {\n\t\ttoken, err := j.GetWithClaims(c.Request, \u0026MyClaims{})\n\t\tif err != nil {\n\t\t\tc.AbortWithError(http.StatusUnauthorized, err)\n\t\t\treturn\n\t\t}\n\n\t\tclaims, ok := token.Claims.(*MyClaims) \n\t\tif !ok {\n\t\t\tc.AbortWithStatus(http.StatusUnauthorized)\n\t\t\treturn\n\t\t}\n\n\t\tc.Set(\"userId\", claims.Subject)\n\t\tc.Set(\"doe\", claims.Doe)\n\t\tc.Next()\n\t}\n}\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foreqizer%2Fgo-jaywt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foreqizer%2Fgo-jaywt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foreqizer%2Fgo-jaywt/lists"}