{"id":21338993,"url":"https://github.com/supanadit/jwt-go","last_synced_at":"2025-07-12T14:31:32.355Z","repository":{"id":134530735,"uuid":"254530562","full_name":"supanadit/jwt-go","owner":"supanadit","description":"The easiest JWT library to GO","archived":false,"fork":false,"pushed_at":"2020-07-11T00:58:42.000Z","size":69,"stargazers_count":14,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-21T14:37:51.840Z","etag":null,"topics":["authentication","authorization","bcrypt","encryption","go","go-jwt","go-libray","golang","golang-library","hashes","jwt","jwt-go"],"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/supanadit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2020-04-10T02:58:44.000Z","updated_at":"2022-09-23T19:00:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"a249c0db-ff46-45ce-9415-cb33a14f04c4","html_url":"https://github.com/supanadit/jwt-go","commit_stats":null,"previous_names":["supanadit/easy-jwt-go"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/supanadit%2Fjwt-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/supanadit%2Fjwt-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/supanadit%2Fjwt-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/supanadit%2Fjwt-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/supanadit","download_url":"https://codeload.github.com/supanadit/jwt-go/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225825236,"owners_count":17529905,"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","authorization","bcrypt","encryption","go","go-jwt","go-libray","golang","golang-library","hashes","jwt","jwt-go"],"created_at":"2024-11-22T00:41:40.771Z","updated_at":"2024-11-22T00:41:41.433Z","avatar_url":"https://github.com/supanadit.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JWT Go\n\n[![Build Status](https://travis-ci.org/supanadit/jwt-go.svg?branch=master)](https://travis-ci.org/supanadit/jwt-go)\n[![Go Report Card](https://goreportcard.com/badge/github.com/supanadit/jwt-go)](https://goreportcard.com/report/github.com/supanadit/jwt-go)\n[![GoDoc](https://godoc.org/github.com/supanadit/jwt-go?status.svg)](https://godoc.org/github.com/supanadit/jwt-go)\n\nThe easiest JWT Library that could be a starting point for your project.\n\n## Installation\n\n`go get github.com/supanadit/jwt-go`\n\n## Quick Start\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/supanadit/jwt-go\"\n\t\"log\"\n)\n\nfunc main() {\n\t// Set Your JWT Secret Code, its optional but important, because default secret code is not secure\n\tjwt.SetJWTSecretCode(\"Your Secret Code\")\n\n\t// Create default authorization\n\tauth := jwt.Authorization{\n\t\tUsername: \"admin\",\n\t\tPassword: \"admin\",\n\t}\n\n\t// Generate JWT Token from default authorization model\n\ttoken, err := auth.GenerateJWT()\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tfmt.Println(\"JWT Token : \" + token)\n\n\t// Verify the token\n\tvalid, err := auth.VerifyJWT(token)\n\tif err != nil {\n\t\tfmt.Println(err)\n\t}\n\n\tfmt.Print(\"Status : \")\n\n\tif valid {\n\t\tfmt.Println(\"Valid\")\n\t} else {\n\t\tfmt.Println(\"Invalid\")\n\t}\n}\n```\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eCustom Authorization\u003c/b\u003e\u003c/summary\u003e\n\n\u003cp\u003e\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/supanadit/jwt-go\"\n\t\"log\"\n)\n\ntype Login struct {\n\tEmail    string\n\tPassword string\n\tName     string\n}\n\nfunc main() {\n\t// Set Your JWT Secret Code, its optional but important, because default secret code is not secure\n\tjwt.SetJWTSecretCode(\"Your Secret Code\")\n\n\t// Create default authorization\n\tauth := Login{\n\t\tEmail:    \"asd@asd.com\",\n\t\tPassword: \"asd\",\n\t\tName:     \"asd\",\n\t}\n\n\t// Generate JWT Token from default authorization model\n\ttoken, err := jwt.GenerateJWT(auth)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tfmt.Println(\"JWT Token : \" + token)\n\n\t// Variable for decoded JWT token\n\tvar dataAuth Login\n\t// Verify the token\n\tvalid, err := jwt.VerifyAndBindingJWT(\u0026dataAuth, token)\n\tif err != nil {\n\t\tfmt.Println(err)\n\t}\n\n\t// or simply you can do this, if you don't need to decode the JWT\n\t// valid, err := jwt.VerifyJWT(token)\n\t// if err != nil {\n\t//\t fmt.Println(err)\n\t// }\n\n\tfmt.Print(\"Status : \")\n\n\tif valid {\n\t\tfmt.Println(\"Valid\")\n\t} else {\n\t\tfmt.Println(\"Invalid\")\n\t}\n}\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eEncrypt \u0026 Verify Password\u003c/b\u003e\u003c/summary\u003e\n\n\u003cp\u003e\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/supanadit/jwt-go\"\n\t\"log\"\n)\n\ntype Login struct {\n\tEmail    string\n\tPassword string\n}\n\nfunc main() {\n\t// Set Your JWT Secret Code, its optional but important, because default secret code is not secure\n\tjwt.SetJWTSecretCode(\"Your Secret Code\")\n\n\t// Create authorization from your own struct\n\tauth := Login{\n\t\tEmail:    \"example@email.com\",\n\t\tPassword: \"123\",\n\t}\n\n\t// Encrypt password, which you can save to database\n\tep, err := jwt.EncryptPassword(auth.Password)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tfmt.Println(\"Encrypted Password \" + string(ep))\n\n\t// Verify Encrypted Password\n\tvalid, err := jwt.VerifyPassword(string(ep), auth.Password)\n\tif err != nil {\n\t\tfmt.Println(err)\n\t}\n\n\tfmt.Print(\"Status : \")\n\n\tif valid {\n\t\tfmt.Println(\"Valid\")\n\t} else {\n\t\tfmt.Println(\"Invalid\")\n\t}\n}\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eDecrypt Password\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\nNo you can't, as the thread at [Stack Exchange](https://security.stackexchange.com/questions/193943/is-it-possible-to-decrypt-bcrypt-encryption)\n\n\u003e bcrypt is not an encryption function, it's a password hashing function, relying on Blowfish's key scheduling, not its encryption. Hashing are mathematical one-way functions, meaning there is no way to reverse the output string to get the input string.\n  \u003cbr/\u003e of course only Siths deal in absolutes and there are a few attacks against hashes. But none of them are \"reversing\" the hashing, AFAIK.\n\nso that enough to secure the password\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSet Expired Time\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/supanadit/jwt-go\"\n\t\"log\"\n)\n\nfunc main() {\n\t// Set Your JWT Secret Code, its optional but important, because default secret code is not secure\n\tjwt.SetJWTSecretCode(\"Your Secret Code\")\n \n    // You can simply do this, jwt.setExpiredTime(Hour,Minute,Second)\n\tjwt.SetExpiredTime(0, 0, 1)\n}\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSupport Gin Web Framework\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n```go\npackage main\n\nimport (\n\t\"github.com/gin-gonic/gin\"\n\t\"github.com/supanadit/jwt-go\"\n\t\"net/http\"\n)\n\nfunc main() {\n\t// Set Your JWT Secret Code, its optional but important, because default secret code is not secure\n\tjwt.SetJWTSecretCode(\"Your Secret Code\")\n\n\t// Create authorization\n\tauth := jwt.Authorization{\n\t\tUsername: \"admin\",\n\t\tPassword: \"123\",\n\t}\n\n\trouter := gin.Default()\n\n\t// Login / Authorization for create JWT Token\n\trouter.POST(\"/auth\", func(c *gin.Context) {\n\t\tvar a jwt.Authorization\n\t\terr := c.Bind(\u0026a)\n\t\tif err != nil {\n\t\t\tc.JSON(http.StatusBadRequest, gin.H{\n\t\t\t\t\"status\": \"Invalid body request\",\n\t\t\t\t\"token\":  nil,\n\t\t\t})\n\t\t} else {\n\t\t\tvalid, err := auth.VerifyPassword(a.Password)\n\t\t\tif err != nil {\n\t\t\t\tc.JSON(http.StatusBadRequest, gin.H{\n\t\t\t\t\t\"status\": \"Wrong username or password\",\n\t\t\t\t\t\"token\":  nil,\n\t\t\t\t})\n\t\t\t} else {\n\t\t\t\tif valid {\n\t\t\t\t\ttoken, err := a.GenerateJWT()\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\tc.JSON(http.StatusInternalServerError, gin.H{\n\t\t\t\t\t\t\t\"status\": \"Can't generate JWT token\",\n\t\t\t\t\t\t\t\"token\":  nil,\n\t\t\t\t\t\t})\n\t\t\t\t\t} else {\n\t\t\t\t\t\tc.JSON(http.StatusOK, gin.H{\n\t\t\t\t\t\t\t\"status\": \"Success\",\n\t\t\t\t\t\t\t\"token\":  token,\n\t\t\t\t\t\t})\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tc.JSON(http.StatusBadRequest, gin.H{\n\t\t\t\t\t\t\"status\": \"Wrong username or password\",\n\t\t\t\t\t\t\"token\":  nil,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t})\n\n\t// Test Authorization\n\trouter.GET(\"/test\", func(c *gin.Context) {\n\t\t// Variable for binding if you need decoded JWT\n\t\tvar dataAuth jwt.Authorization\n\t\t// Verify and binding JWT\n\t\ttoken, valid, err := jwt.VerifyAndBindingGinHeader(\u0026dataAuth, c)\n\n\t\t// in case if you don't want to decode the JWT, simply use this code\n\t\t// token, valid, err := jwt.VerifyGinHeader(c)\n\n\t\tif err != nil {\n\t\t\tc.JSON(http.StatusOK, gin.H{\n\t\t\t\t\"status\": err.Error(),\n\t\t\t})\n\t\t} else {\n\t\t\tif valid {\n\t\t\t\tc.JSON(http.StatusOK, gin.H{\n\t\t\t\t\t\"status\": token + \" is valid\",\n\t\t\t\t})\n\t\t\t} else {\n\t\t\t\tc.JSON(http.StatusBadRequest, gin.H{\n\t\t\t\t\t\"status\": \"Invalid\",\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\t})\n\n\t_ = router.Run(\":8080\")\n}\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSupport Echo Web Framework\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n```go\npackage main\n\nimport (\n\t\"github.com/labstack/echo/v4\"\n\t\"github.com/supanadit/jwt-go\"\n\t\"net/http\"\n)\n\nfunc main() {\n\t// Set Your JWT Secret Code, its optional but important, because default secret code is not secure\n\tjwt.SetJWTSecretCode(\"Your Secret Code\")\n\n\t// Create authorization\n\tauth := jwt.Authorization{\n\t\tUsername: \"admin\",\n\t\tPassword: \"123\",\n\t}\n\n\te := echo.New()\n\n\t// Login / Authorization for create JWT Token\n\te.POST(\"/auth\", func(c echo.Context) error {\n\t\ta := new(jwt.Authorization)\n\t\t// Create struct for response, or you can create globally by your self\n\t\tvar r struct {\n\t\t\tStatus string\n\t\t\tToken  string\n\t\t}\n\t\terr := c.Bind(a)\n\t\tif err != nil {\n\t\t\tr.Status = \"Invalid body request\"\n\t\t\treturn c.JSON(http.StatusBadRequest, \u0026r)\n\t\t} else {\n\t\t\tvalid, err := auth.VerifyPassword(a.Password)\n\t\t\tif err != nil {\n\t\t\t\tr.Status = \"Wrong username or password\"\n\t\t\t\treturn c.JSON(http.StatusBadRequest, \u0026r)\n\t\t\t} else {\n\t\t\t\tif valid {\n\t\t\t\t\ttoken, err := a.GenerateJWT()\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\tr.Status = \"Can't generate JWT Token\"\n\t\t\t\t\t\treturn c.JSON(http.StatusInternalServerError, \u0026r)\n\t\t\t\t\t} else {\n\t\t\t\t\t\tr.Status = \"Success\"\n\t\t\t\t\t\tr.Token = token\n\t\t\t\t\t\treturn c.JSON(http.StatusOK, \u0026r)\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tr.Status = \"Wrong username or password\"\n\t\t\t\t\treturn c.JSON(http.StatusBadRequest, \u0026r)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t})\n\n\t// Test Authorization\n\te.GET(\"/test\", func(c echo.Context) error {\n\t\t// Create struct for response\n\t\tvar r struct {\n\t\t\tStatus string\n\t\t}\n\t\t// Variable for binding if you need decoded JWT\n\t\tdataAuth := new(jwt.Authorization)\n\t\t// Verify and binding JWT\n\t\ttoken, valid, err := jwt.VerifyAndBindingEchoHeader(\u0026dataAuth, c)\n\n\t\t// in case if you don't want to decode the JWT, simply use this code\n\t\t// Token, valid, err := jwt.VerifyEchoHeader(c)\n\n\t\tif err != nil {\n\t\t\tr.Status = err.Error()\n\t\t\treturn c.JSON(http.StatusBadRequest, \u0026r)\n\t\t} else {\n\t\t\tif valid {\n\t\t\t\tr.Status = token + \" is valid\"\n\t\t\t\treturn c.JSON(http.StatusOK, \u0026r)\n\t\t\t} else {\n\t\t\t\tr.Status = \"Invalid\"\n\t\t\t\treturn c.JSON(http.StatusBadRequest, \u0026r)\n\t\t\t}\n\t\t}\n\t})\n\n\t// Start server\n\te.Logger.Fatal(e.Start(\":1323\"))\n}\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eDisable \u0026 Enable Authorization\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n```go\npackage main\n\nimport (\n\t\"github.com/supanadit/jwt-go\"\n)\n\nfunc main() {\n\t// Set Your JWT Secret Code, its optional but important, because default secret code is not secure\n\tjwt.SetJWTSecretCode(\"Your Secret Code\")\n\n    // Disable authorization, meaning when verify jwt token it will return true even if the token was expired or invalid\n\tjwt.DisableAuthorization()\n\n\t// or\n\n    // Enable authorization\n\tjwt.EnableAuthorization()\n}\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eSet HMAC Signing Method\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n```go\npackage main\n\nimport \"github.com/supanadit/jwt-go\"\n\nfunc main() {\n\t// Set HMAC signing method\n\tjwt.SetHMACSigningMethod(jwt.HS256()) // or jwt.HS384(), jwt.HS512()\n}\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n## Thanks to\n- [jwt-go](https://github.com/dgrijalva/jwt-go)\n- [bcrypt](golang.org/x/crypto/bcrypt)\n- [mapstructure](github.com/mitchellh/mapstructure)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsupanadit%2Fjwt-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsupanadit%2Fjwt-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsupanadit%2Fjwt-go/lists"}