{"id":27923336,"url":"https://github.com/andiveloper/ts-rbac","last_synced_at":"2025-05-06T22:32:31.881Z","repository":{"id":57381068,"uuid":"353260028","full_name":"andiveloper/ts-rbac","owner":"andiveloper","description":"Use ts-rbac if you need role-based access control in your TypeScript application, independent from your architecture.","archived":false,"fork":false,"pushed_at":"2023-01-13T07:34:18.000Z","size":204,"stargazers_count":5,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-22T10:52:14.754Z","etag":null,"topics":["jwt","security","ts","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/andiveloper.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":"2021-03-31T07:05:52.000Z","updated_at":"2025-02-03T14:14:40.000Z","dependencies_parsed_at":"2023-02-09T15:01:57.039Z","dependency_job_id":null,"html_url":"https://github.com/andiveloper/ts-rbac","commit_stats":null,"previous_names":["andiveloper/ts-rbac"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andiveloper%2Fts-rbac","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andiveloper%2Fts-rbac/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andiveloper%2Fts-rbac/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andiveloper%2Fts-rbac/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andiveloper","download_url":"https://codeload.github.com/andiveloper/ts-rbac/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252779381,"owners_count":21802935,"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":["jwt","security","ts","typescript"],"created_at":"2025-05-06T22:32:12.673Z","updated_at":"2025-05-06T22:32:31.823Z","avatar_url":"https://github.com/andiveloper.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ts-rbac\n\nUse `ts-rbac` if you need role-based access control in your TypeScript application, independent from your internal architecture: It works with REST endpoints in NodeJS, function calls triggered by Kafka or any other framework.\n\n# Installation\n\n```\nnpm install andiveloper/ts-rbac\n```\n\n# Concept\n\n`ts-rbac` assumes that a user belongs to different groups within an organization. \nFor authorization `roles` and `actions` are used. The following relations apply:\n\n- A `subject` (person or service) can have many `roles`\n- A `role` can inherit from other roles\n- A `role` consists of many `actions`\n- `Actions` are architecture-independent and can for example be named after use cases (e.g. \"UploadImage\") or after endpoints in a REST API (e.g.: \"PostImage\")\n- `Actions` contain a `scope` which defines what entities can be accessed\n- You can either define your own `scopes` or use a set of default `scopes`: \"self\", \"group\", \"org\" and \"all\":\n  - \"self\" means only entities created by the user himself can be accessed\n  - \"group\" means only entities that are owned by a group to which the user belongs can be accessed\n  - \"org\" means all entities of the organization can be accessed\n  - \"all\" means all entities of all organizations can be accessed (\"superadmin\")\n- `scopes` have weights which determine the rank of the `scope`, e.g. the default scope \"all\" has the highest weight which means if access to a lower ranked scope is requested, e.g. \"group\" it is also granted\n- When accessing an endpoint/use case:\n  - Your API authenticates the user, e.g. for example through a JWT token or a session cookie\n  - Your API retrieves the roles of the user from the JWT token, a database, ...\n  - `ts-rbac` is used to calculate the actions of all the roles and inherited roles the user owns and checks if a action exists for the specific action needed to perform the action\n  - On data access the \"scope\" must be checked by your action/use case or database query, for example: \n  ```\n  (action.scope.self AND entity.createdBy === user.id) \n  OR (action.scope.group AND entity.ownerGroup in user.groups) \n  OR (action.scope.org AND entity.ownerOrg === user.organization)\n  ```\n\n# Usage\nSee `test/samples/sample.ts` and `test/samples/sample.test.ts`\n\n# Future work\n- Add entity \"attribute\"-level actions:\n  - Actions could contain a field called \"attributes\" which is an array of attributes of the entity that are allowed or denied to be accessed\n  - For example: `{\"attributes\": [\"!id\", \"modifiedTime\", \"!comments\", \"metadata.location\"]}`, default is `[*]`\n  \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandiveloper%2Fts-rbac","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandiveloper%2Fts-rbac","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandiveloper%2Fts-rbac/lists"}