javascript - 如何使用 Prisma API 生成 JWT token ?

标签 javascript reactjs graphql prisma prisma-graphql

我正在使用prisma使用 GraphQL。我知道有一种方法可以使用 key 来保护 graphql 服务器。例如,将 key 指定为:

secret: my-secret-42

prisma.yml 中,然后运行 ​​prisma deploy 将保护 graphql 服务器,所有后续查询都需要 JWT token 才能访问它。

我能够使用命令生成 JWT token

prisma token

这给了我 token ,并将其传递到 header 中,我就可以访问它。但是是否有一个 API 可以用来生成 token ,并且我不必在 CLI 上手动运行 prisma token 命令。

我希望 javascript 通过查询直接访问 GraphQL。为此,我需要进行某种形式的身份验证。由于会有多个用户使用该应用程序,因此我希望为不同的用户提供不同的 token 。因此,我正在寻找一种可以使用 API 生成 token (如果 Prisma 可用)的方法。

最佳答案

服务 token 是一个简单的 JWT token ,可以使用服务和阶段名称以及 key 轻松创建。您可以自己创建 token 并附加它。看一下 prisma CLI 使用的实际代码:

  getToken(serviceName: string, stageName: string): string | undefined {
    if (this.secrets) {
      const data = {
        data: {
          service: `${serviceName}@${stageName}`,
          roles: ['admin'],
        },
      }
      return jwt.sign(data, this.secrets[0], {
        expiresIn: '7d',
      })
    }

    return undefined
  }

来源:https://github.com/prisma/prisma/blob/master/cli/packages/prisma-yml/src/PrismaDefinition.ts

有关结构的更多信息:

服务 token 遵循 JSON Web token (JWT) 规范 (RFC 7519):

"JSON Web Token (JWT) is a compact, URL-safe means of representing claims to be transferred between two parties. The claims in a JWT are encoded as a JSON object that is used as the payload of a JSON Web Signature (JWS) structure or as the plaintext of a JSON Web Encryption (JWE) structure, enabling the claims to be digitally signed or integrity protected with a Message Authentication Code (MAC) and/or encrypted."

JWT 具有以下三个组件:

Header: The header typically consists of two parts: the type of the token, which is JWT, and the hashing algorithm being used (which is HS256 in the case of Prisma service tokens).

{   "alg": "HS256",   "typ": "JWT" } 

Payload: The payload contains the claims. Claims are statements about an entity (typically, the user) and additional data. Here is what it looks like for a service called demo deployed to the dev stage:

{   
   "data": {
      "service": "demo@dev",
      "roles": ["admin"]   
   },   
   "iat": 1532530208,   
   "exp": 1533135008 
} 

Signature: The signature is used to verify the message wasn't changed along the way. To create the signature part you have to take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign that. For example if you want to use the HMAC SHA256 algorithm, the signature will be created in the following way:

HMACSHA256(base64UrlEncode(header) + '.' + base64UrlEncode(payload), secret) 

因此,JWT 通常如下所示:xxxxx.yyyyy.zzzzz

来源:https://www.prisma.io/docs/prisma-server/authentication-and-security-kke4/#service-token

关于javascript - 如何使用 Prisma API 生成 JWT token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59979514/

相关文章:

javascript - 如何在javascript中循环数据

javascript - 如何在 jQuery 中使用变量 id 作为选择器

reactjs - 如何使用 React 根据路由更改单独标题组件上的页面标题?

reactjs - CSS :empty pseudo-selector combined with :after pseudo-element

amazon-web-services - 如何将默认GraphQL参数传递给AWS AppSync解析器

graphql - 将 Config.skip 与 React-Apollo 查询一起使用

javascript - 纯粹通过 JavaScript SDK 为所有用户获取 Facebook 页面事件,包括那些未登录 Facebook 的用户

arrays - Array.prototype.filter() 期望在箭头函数末尾返回一个值,React

javascript - GraphQL/中继架构无法查询类型 "store"上的字段 "CreateLinkPayload"

javascript - jQuery 日期选择器 UI 工具提示