{"id":14976106,"url":"https://github.com/drakota/gql-auth-directives","last_synced_at":"2025-09-11T16:07:54.372Z","repository":{"id":43986903,"uuid":"241367279","full_name":"Drakota/gql-auth-directives","owner":"Drakota","description":"🛡 Guard your GraphQL API using roles \u0026 permissions schema directives.","archived":false,"fork":false,"pushed_at":"2023-01-05T08:20:14.000Z","size":621,"stargazers_count":8,"open_issues_count":22,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-27T10:22:28.891Z","etag":null,"topics":["authentication","authorization","graphql","graphql-schema","nodejs","schema-directives","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/Drakota.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":"2020-02-18T13:26:22.000Z","updated_at":"2025-04-13T03:48:05.000Z","dependencies_parsed_at":"2023-02-03T20:16:36.502Z","dependency_job_id":null,"html_url":"https://github.com/Drakota/gql-auth-directives","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/Drakota/gql-auth-directives","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drakota%2Fgql-auth-directives","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drakota%2Fgql-auth-directives/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drakota%2Fgql-auth-directives/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drakota%2Fgql-auth-directives/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Drakota","download_url":"https://codeload.github.com/Drakota/gql-auth-directives/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Drakota%2Fgql-auth-directives/sbom","scorecard":{"id":42328,"data":{"date":"2025-08-11","repo":{"name":"github.com/Drakota/gql-auth-directives","commit":"be5cd17449122ae6e5689b7a5474b442f348f194"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":2.7,"checks":[{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Code-Review","score":0,"reason":"Found 0/6 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Pinned-Dependencies","score":3,"reason":"dependency not pinned by hash detected -- score normalized to 3","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/CI.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/Drakota/gql-auth-directives/CI.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/CI.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/Drakota/gql-auth-directives/CI.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/publish.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/Drakota/gql-auth-directives/publish.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/publish.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/Drakota/gql-auth-directives/publish.yml/master?enable=pin","Info:   0 out of   4 GitHub-owned GitHubAction dependencies pinned","Info:   2 out of   2 npmCommand dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/CI.yml:1","Warn: no topLevel permission defined: .github/workflows/publish.yml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 24 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":0,"reason":"54 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-968p-4wvh-cqc8","Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92","Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw","Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw","Warn: Project is vulnerable to: GHSA-j5g3-5c8r-7qfx","Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7","Warn: Project is vulnerable to: GHSA-v6h2-p8h4-qcjw","Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg","Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x","Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275","Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c","Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq","Warn: Project is vulnerable to: GHSA-wm7h-9275-46v2","Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc","Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx","Warn: Project is vulnerable to: GHSA-fjxv-7rqg-78g4","Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6","Warn: Project is vulnerable to: GHSA-pfrx-2q88-qq97","Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj","Warn: Project is vulnerable to: GHSA-rc47-6667-2j5j","Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37","Warn: Project is vulnerable to: GHSA-896r-f27r-55mw","Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h","Warn: Project is vulnerable to: GHSA-8cf7-32gw-wr33","Warn: Project is vulnerable to: GHSA-hjrf-2m68-5959","Warn: Project is vulnerable to: GHSA-qwph-4952-7xr6","Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9","Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm","Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv","Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3","Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m","Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h","Warn: Project is vulnerable to: GHSA-w7rc-rwvf-8q5r","Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g","Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p","Warn: Project is vulnerable to: GHSA-px4h-xg32-q955","Warn: Project is vulnerable to: GHSA-cwx2-736x-mf6w","Warn: Project is vulnerable to: GHSA-v39p-96qg-c8rf","Warn: Project is vulnerable to: GHSA-8v63-cqqc-6r2c","Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9","Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j","Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w","Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp","Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6","Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw","Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg","Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p","Warn: Project is vulnerable to: GHSA-52f5-9888-hmc6","Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v","Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3","Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7","Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693","Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q","Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-14T21:51:29.164Z","repository_id":43986903,"created_at":"2025-08-14T21:51:29.164Z","updated_at":"2025-08-14T21:51:29.164Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274665267,"owners_count":25327159,"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-09-11T02:00:13.660Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","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","authorization","graphql","graphql-schema","nodejs","schema-directives","typescript"],"created_at":"2024-09-24T13:53:18.802Z","updated_at":"2025-09-11T16:07:54.333Z","avatar_url":"https://github.com/Drakota.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\" style=\"background: #112233; border-radius: 10px; box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);\"\u003e\n    \u003cimg src=\"../assets/logo.png\" /\u003e\n    \u003cbr/\u003e\u003cbr/\u003e\n    \u003ca href=\"https://github.com/Drakota/gql-auth-directives/actions\"\u003e\n      \u003cimg src=\"https://img.shields.io/github/workflow/status/drakota/gql-auth-directives/CI\" /\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://github.com/Drakota/gql-auth-directives/blob/master/LICENSE\"\u003e\n        \u003cimg src=\"https://img.shields.io/github/license/drakota/gql-auth-directives?color=blue\" /\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://www.npmjs.com/package/gql-auth-directives\"\u003e\n        \u003cimg src=\"https://img.shields.io/npm/v/gql-auth-directives\" /\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://www.npmjs.com/package/gql-auth-directives?activeTab=dependencies\"\u003e\n        \u003cimg src=\"https://img.shields.io/david/drakota/gql-auth-directives\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n---\n\n# Summary\n\n`gql-auth-directives` is a set of schema directives that allows a granular approach to role-based access control with GraphQL.\n\n## Features\n\n- Directives for roles, permissions and authentication\n- Allows access control for queries, mutations\n- Allows access control for input types\n- Provided with default handlers using JWTs\n\n# Getting Started\n\n## Prerequisites\n\nThis package requires the user to use a server that supports [Apollo schema directives](https://www.apollographql.com/docs/graphql-tools/schema-directives/).\n\n## Installing\n\nUse npm\n\n```\n$ npm install --save gql-auth-directives\n```\n\nor yarn\n\n```\n$ yarn add gql-auth-directives\n```\n\nto install the package.\n\n# Available directives\n\n- `@hasRole(roles: [\"ADMIN\", \"USER\"])`\n  \u003e A user with either an admin or user role will be authorized\n- `@hasPermission(permissions: [\"CREATE_USERS\"])`\n- `@isAuthenticated`\n\n# Setup\n\n## Default handlers\n\nTo use the default handlers, the environment variable `JWT_SECRET` must be set. The variable will be used for verifying authorization headers and grab `roles`/`permissions` arrays in the token's payload.\n\nWe need to generate the auth directives and add it to the server configuration like so:\n\n```javascript\nimport { createAuthDirectives } from \"gql-auth-directives\";\n\nconst authDirectives = createAuthDirectives();\n\nconst server = new ApolloServer({\n  typeDefs,\n  resolvers,\n  schemaDirectives: { ...authDirectives },\n  context(ctx) {\n    return { ...ctx }; // ⚠ Make sure to always pass the ctx from the server's context function\n  },\n});\n```\n\n## Override handlers\n\nThe library also allows using custom functions for the provided directives.\n\nYou could override the `hasPermission` handler like so:\n\n```javascript\nimport { createAuthDirectives } from \"gql-auth-directives\";\n\nconst authDirectives = createAuthDirectives({\n  hasPermissionHandler: (ctx, permissions) =\u003e {\n    const user = getUser(ctx);\n    // Do something with the user and throw an error if the user's permissions doesn't match the permissions passed\n  },\n});\n\nconst server = new ApolloServer({\n  typeDefs,\n  resolvers,\n  schemaDirectives: { ...authDirectives },\n  context(ctx) {\n    return { ...ctx };\n  },\n});\n```\n\n## Schema declarations\n\nWe also need to add the directives declarations at the top of our GraphQL schema like so:\n\n```graphql\ndirective @isAuthenticated on FIELD | FIELD_DEFINITION | INPUT_FIELD_DEFINITION\ndirective @hasPermission(permissions: [String!]!) on FIELD | FIELD_DEFINITION | INPUT_FIELD_DEFINITION\ndirective @hasRole(roles: [String!]!) on FIELD | FIELD_DEFINITION | INPUT_FIELD_DEFINITION\n\n[...]\n```\n\nIf you use something like `makeExecutableSchema` from [graphql-tools](https://github.com/apollographql/graphql-tools) that allows schema stitching you can also add the directives declarations like so:\n\n```javascript\nimport { createAuthDirectives, authTypeDefs } from \"gql-auth-directives\";\nimport { makeExecutableSchema } from \"graphql-tools\";\n\nconst authDirectives = createAuthDirectives();\n\nconst schema = makeExecutableSchema({\n  resolvers,\n  typeDefs: [authTypeDefs, typeDefs],\n  schemaDirectives: { ...authDirectives },\n});\n\nconst server = new ApolloServer({\n  schema,\n  context(ctx) {\n    return { ...ctx };\n  },\n});\n```\n\n# Usage\n\nWe can now use our directives inside our GraphQL schema:\n\n```graphql\ninput CreatePostInput {\n  name: String!\n  description: String!\n  published: Boolean @hasPermission(permissions: [\"PUBLISH_POST\"])\n}\n\ntype Mutation {\n  createPost(data: CreatePostInput!): Post! @hasRole(roles: [\"AUTHOR\"])\n}\n\ntype Query {\n  me: User! @isAuthenticated\n}\n```\n\n\u003e ℹ Notice that you can also use the directives for inputs. If an unauthorized user tries to fill the published field it will throw an error and never reach the resolver.\n\n# License\n\n[MIT](https://github.com/Drakota/gql-auth-directives/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrakota%2Fgql-auth-directives","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrakota%2Fgql-auth-directives","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrakota%2Fgql-auth-directives/lists"}