{"id":25731361,"url":"https://github.com/hieunc229/eveauth","last_synced_at":"2025-02-26T02:46:00.588Z","repository":{"id":90379266,"uuid":"379997535","full_name":"hieunc229/eveauth","owner":"hieunc229","description":"Auth middleware and wrapper in GO. Supports username/password, register, login, change password with role restriction","archived":false,"fork":false,"pushed_at":"2021-06-27T17:03:53.000Z","size":47,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-06-21T19:09:51.276Z","etag":null,"topics":["auth","authentication","go","middleware"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hieunc229.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":"2021-06-24T17:05:55.000Z","updated_at":"2022-08-28T12:52:48.000Z","dependencies_parsed_at":null,"dependency_job_id":"2f158f9e-a010-4c49-8b49-9cd4491ed60d","html_url":"https://github.com/hieunc229/eveauth","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hieunc229%2Feveauth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hieunc229%2Feveauth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hieunc229%2Feveauth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hieunc229%2Feveauth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hieunc229","download_url":"https://codeload.github.com/hieunc229/eveauth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240783104,"owners_count":19856776,"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","authentication","go","middleware"],"created_at":"2025-02-26T02:45:59.794Z","updated_at":"2025-02-26T02:46:00.541Z","avatar_url":"https://github.com/hieunc229.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EveAuth\n\nAuth middleware and request handler wrapper for GO. It uses [boltDB](https://github.com/boltdb/bolt) to store user data\n\nIncluded in this guide:\n\n1. [Usage](#1-getting-started)\n- [Install eveauth](#install)\n- [Auth wrapper and Middleware](#auth-handle-wrapper-and-middleware)\n- [Register handler](#register-handler)\n- [Login handler](#login-handler)\n- [Change password handler](#change-password-handler)\n- [How to use JWT token](#how-to-use-jwt-token)\n- [How to verify a request](#how-to-verify-a-request-contains-a-jwt-token)\n- [Setup enviroment variables](#setup-enviroment-variables)\n2. [Changelog](#2-changelog)\n3. [Feedback and Contribute](#3-feedback-and-contribute)\n4. [Licenses](#4-licenses)\n\n## 1. Getting started\n\n### Install\n\n```ssh\n$ go get github.com/hieunc229/eveauth\n```\n\n### Auth Handle Wrapper and Middleware\n\nUse `eveauth.AuthMiddleware` or `eveauth.AuthHandler` to authorize users.\n\n```go\nimport (\n    \"github.com/hieunc229/eveauth\"\n)\n\n// It can be used for any router with standard handler\n// ie. func (http.ResponseWriter, r *http.Request)\nrouter := mux.NewRouter()\n\n// auth options\nauthOptions := eveauth.AuthHandlerOptions{\n    // allow only member\n    // other values can be eveauth.RoleAdmin, eveauth.RoleAnonymous\n    Role: eveauth.RoleMember,\n\n\t// Set to true to forbid access from users with different RoleLevel.\n\t// Or set to false (or nil) to forbid only users with lower RoleLevel\n    RoleExact: true\n}\n\n// Use as middleware\nrouter.Use(\"/user_only\", eveauth.AuthMiddleware(\u0026authOptions))\n\n// Or wrap around a handler\nrouter.HandlerFunc(\"/user_only\", eveauth.AuthHandler(yourHandler, \u0026authOptions))\n```\n\nWhen there is an anonymous request to these paths, it return the following:\n\n```js\n{\n    \"ok\": false,\n    \"error\": \"invalid access\"\n}\n```\n\n### Register handler\n\nUse `eveauth.RegisterHandler` handler to handle create account. Registed users will have `eveauth.RoleMember` role\n\n```go\nrouter.HandlerFunc(\"/auth/register\", eveauth.RegisterHandler)\n```\n\nThe body json data must be:\n```js\n{\n    \"data\": {\n        \"username\": \"xxxxxx\",\n        \"password\": \"xxxxxx\"\n    }\n}\n```\n\nSuccess return:\n```js\n{\n    \"ok\": true\n}\n```\n\nError return:\n```js\n{\n    \"error\": \"error message\",\n    \"ok\": false\n}\n```\n\n### Login handler\n\nUse `eveauth.LoginHandler` handler to handle login\n\n```go\nrouter.HandlerFunc(\"/auth/login\", eveauth.LoginHandler)\n```\n\nThe body json data must be:\n```js\n{\n    \"data\": {\n        \"username\": \"xxxxxx\",\n        \"password\": \"xxxxxx\"\n    }\n}\n```\n\nSuccess return:\n```js\n{\n    \"data\": {\n        \"token\": \"jwt token str\" // use as bearer token\n    },\n    \"ok\": true\n}\n```\n\nError return:\n```js\n{\n    \"error\": \"error message\",\n    \"ok\": false\n}\n```\n\n\n### Change password handler\n\nUse `eveauth.ChangePasswordhandler` to handle change password request. Note that **the request must be authorized with Bearer token** mention above. _If you don't have a bearer token, login to get a bearer token first_\n\n```go\nrouter.HandlerFunc(\"/auth/change-password\", eveauth.ChangePasswordhandler)\n```\n\nThe body json payload must be:\n```js\n{\n    \"data\": {\n        \"password\": \"oldPassword\",\n        \"new_password\": \"xxxxxxx\",\n\n        //// set to `true` to replace the current token with a new one\n        \"change_token\": false, \n\n        // set to `true` to remove all existing tokens, then add a new one \n        // (i.e useful for logout all other devices feature)\n        \"clear_tokens\": false, \n    }\n}\n```\nSuccess response:\n```js\n{\n    \"data\" {\n        // If `change_token` or `clear_tokens` is true, you will need to use this new token\n        // Otherwise, this value will be an empty string (\"\")\n        \"new_token\": \"\" \n    },\n    \"ok\": true\n}\n```\n\nError response:\n```js\n{\n    \"error\": \"error message\",\n    \"ok\": false\n}\n```\n\nHere is a change password example using fetch in JavaScript\n```js\nfetch(\"/user_only/items/goodItemId\", {\n    method: \"POST\",\n    headers: {\n        'Authorization': 'Bearer \u003ctoken\u003e'\n        // 'Content-Type': 'application/json'\n        // ...\n    }\n    body: JSON.stringify({\n        data: {\n            password: \"xxxxx\",\n            new_password: \"xxxxxxxx\"\n        }\n    })\n})\n```\n\n### How to use JWT token\n\nAfter send a login request and receive a sucess response, you'll be given a `token`. This token is meant to use as [Bearer](https://swagger.io/docs/specification/authentication/bearer-authentication/) token.\n\nWhenever you make a request and want it to be authorized, added `Authorization: Bearer \u003ctoken\u003e` to the request header\n\nHere is an example with [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) in JavaScript\n```js\nfetch(\"/user_only/items/goodItemId\", {\n    method: \"POST\",\n    headers: {\n        'Authorization': 'Bearer \u003ctoken\u003e'\n        // 'Content-Type': 'application/json'\n        // ...\n    }\n    // body: ...\n})\n```\n\n### How to verify a request contains a JWT token\n\nUse `eveauth.VerifyRequest(*http.Request, *eveauth.AuthHandlerOptions) (*JWTPayload, err)` to verify your http request. \n\nVerify a http.Request by (1) get bearer token, (2) verify if the token is a valid jwt token, (3) get userData then check if token is still active (4) then check if the user has the proper role if authOption != nil\n\nHere is an example:\n```go\nfunc yourHandler(w http.ResponseWriter, r *http.Request) {\n\n    payload, err := eveauth.VerifyRequest(r, \u0026eveauth.AuthHandlerOptions{})\n\n    if err != nil {\n        // bearer token is not valid or expired\n        return;\n    }\n\n    // no err, looking good\n\n    username = payload.Username\n}\n```\n\n### Setup enviroment variables\n\nThere are a few enviroment variables that you should update when using the product:\n\n- `EVEAUTH_JWT_SECRET` (default `eveauth`): a secret string to create jwt token\n- `EVEAUTH_PATH` (default `auth`): path to your auth database. You can use absolute or relative path. If you use relative path, the path root is where you run the command)\n\nThere are 2 ways to set these enviroment variables:\n\n1. Using flags in command (_only used after you build the application (aka binary file)_). For example:\n```sh\n$ ./coolapp -EVEAUTH_JWT_SECRET=randomstringnoonecanguess -EVEAUTH_PATH=/ect/safe-area/coolappAuth.db\n```\n\n2. Use [godotenv](https://github.com/joho/godotenv) (or any dotenv alternative). First, install `godotenv` (`go get https://github.com/joho/godotenv`), then create a `.env` at your root directory with the following content:\n\n```\n# .env\nEVEAUTH_JWT_SECRET=randomstringnoonecanguess\nEVEAUTH_PATH=/ect/safe-area/coolappAuth.db\n```\n\n3. Load the `.env` file. Read the manual from `dotenv` package you use. For example, for `godotenv`:\n\n```go\npackage main\n\nimport (\n    ...\n    \"github.com/joho/godotenv\"\n)\n\nfunc main() {\n\n    // Load .env file\n    err := godotenv.Load()\n    if err != nil {\n        log.Fatal(\"Error loading .env file\")\n    }\n\n}\n```\n\n## 2. Changelog\n\n- 25 Jun 2021: added roles\n- 24 Jun 2021: initiate project\n\n## 3. Feedback and Contribute\n\nAlways welcome. Please [open a new thread](https://github.com/hieunc229/eveauth/issues/new)\n\n## 4. Licenses\n\n- eveauth MIT\n- BoltDB MIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhieunc229%2Feveauth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhieunc229%2Feveauth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhieunc229%2Feveauth/lists"}