{"id":24518388,"url":"https://github.com/rest-go/auth","last_synced_at":"2025-07-23T09:33:51.249Z","repository":{"id":65423922,"uuid":"588538488","full_name":"rest-go/auth","owner":"rest-go","description":"RESTFul Authentication and Authorization package for Golang app","archived":false,"fork":false,"pushed_at":"2023-03-23T01:29:59.000Z","size":113,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-15T11:12:27.382Z","etag":null,"topics":["authentication","golang","jwt","rest-api"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rest-go.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-01-13T11:13:40.000Z","updated_at":"2023-01-27T01:46:37.000Z","dependencies_parsed_at":"2024-06-21T16:35:33.906Z","dependency_job_id":"7838a9ca-6e6d-49d7-b397-86ce883cf074","html_url":"https://github.com/rest-go/auth","commit_stats":{"total_commits":29,"total_committers":1,"mean_commits":29.0,"dds":0.0,"last_synced_commit":"1d6afb374aa4d142964d7edf0df5dd20174d9221"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/rest-go/auth","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rest-go%2Fauth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rest-go%2Fauth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rest-go%2Fauth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rest-go%2Fauth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rest-go","download_url":"https://codeload.github.com/rest-go/auth/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rest-go%2Fauth/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266655335,"owners_count":23963552,"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","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","golang","jwt","rest-api"],"created_at":"2025-01-22T01:42:11.744Z","updated_at":"2025-07-23T09:33:51.222Z","avatar_url":"https://github.com/rest-go.png","language":"Go","readme":"**Deprecated** This package has moved to [rest-go/rest repo](https://github.com/rest-go/rest/tree/main/pkg/auth)\n\n\n# Auth\n\n![ci](https://github.com/rest-go/auth/actions/workflows/ci.yml/badge.svg)\n[![codecov](https://codecov.io/gh/rest-go/auth/branch/main/graph/badge.svg?token=T38FWXMVY1)](https://codecov.io/gh/rest-go/auth)\n[![Go Reference](https://pkg.go.dev/badge/github.com/rest-go/auth.svg)](https://pkg.go.dev/github.com/rest-go/auth)\n\n\n\nAuth is a RESTFul Authentication and Authorization package for Golang HTTP apps.\n\nIt handles the common tasks of registration, logging in, logging out, JWT token generation, and JWT token verification.\n\n## Installation\n\n```bash\n$ go get github.com/rest-go/auth\n```\n\n## Usage\nimport `auth` to your app, create `auth.Handler` and `auth.Middleware` based on requirements.\n``` go\npackage main\n\nimport (\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/rest-go/auth\"\n)\n\nfunc handle(w http.ResponseWriter, req *http.Request) {\n\tuser := auth.GetUser(req)\n\tif user.IsAnonymous() {\n\t\tw.WriteHeader(http.StatusUnauthorized)\n\t} else {\n\t\tw.WriteHeader(http.StatusOK)\n\t}\n}\n\nfunc main() {\n\tdbURL := \"sqlite://my.db\"\n\tjwtSecret := \"my secret\"\n\tauthHandler, err := auth.NewHandler(dbURL, []byte(jwtSecret))\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\thttp.Handle(\"/auth/\", authHandler)\n\n\tmiddleware := auth.NewMiddleware([]byte(jwtSecret))\n\thttp.Handle(\"/\", middleware(http.HandlerFunc(handle)))\n\tlog.Fatal(http.ListenAndServe(\":8000\", nil)) //nolint:gosec\n}\n```\n\n## Setup database\n\nSend a `POST` request to `/auth/setup` to set up database tables for users. This\nwill also create an admin user account and return the username and password in\nthe response.\n\n```bash\n$ curl -XPOST \"localhost:8000/auth/setup\"\n```\n\n## Auth handler\n\nThe `Auth` struct implements the `http.Hanlder` interface and provides the below endpoints for user management.\n\n1. Register\n\n```bash\n$ curl  -XPOST \"localhost:8000/auth/register\" -d '{\"username\":\"hello\", \"password\": \"world\"}'\n```\n\n2. Login\n\n```bash\n$ curl  -XPOST \"localhost:8000/auth/login\" -d '{\"username\":\"hello\", \"password\": \"world\"}'\n```\n\n3. Logout\n\nCurrently, the authentication mechanism is based on JWT token only, logout is a no-op on the\nserver side, and the client should clear the token by itself.\n\n```bash\n$ curl  -XPOST \"localhost:8000/auth/logout\"\n```\n\n## Auth middleware and `GetUser`\n\nAuth middleware will parse JWT token in the HTTP header, and when successful,\nset the user in the request context, the `GetUser` method can be used to get the\nuser from the request.\n\n``` go\nuser := auth.GetUser(req)\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frest-go%2Fauth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frest-go%2Fauth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frest-go%2Fauth/lists"}