@auth/dgraph-adapter 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/README.md +28 -0
- package/index.d.ts +255 -0
- package/index.d.ts.map +1 -0
- package/index.js +499 -0
- package/lib/client.d.ts +31 -0
- package/lib/client.d.ts.map +1 -0
- package/lib/client.js +40 -0
- package/lib/graphql/fragments.d.ts +5 -0
- package/lib/graphql/fragments.d.ts.map +1 -0
- package/lib/graphql/fragments.js +38 -0
- package/lib/utils.d.ts +4 -0
- package/lib/utils.d.ts.map +1 -0
- package/lib/utils.js +22 -0
- package/package.json +60 -0
- package/src/index.ts +570 -0
- package/src/lib/client.ts +79 -0
- package/src/lib/graphql/fragments.ts +40 -0
- package/src/lib/graphql/schema.gql +82 -0
- package/src/lib/utils.ts +24 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-2023, Balázs Orbán
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<br/>
|
|
3
|
+
<a href="https://authjs.dev" target="_blank">
|
|
4
|
+
<img height="64px" src="https://authjs.dev/img/logo/logo-sm.png" />
|
|
5
|
+
</a>
|
|
6
|
+
<a href="https://dgraph.io" target="_blank">
|
|
7
|
+
<img height="64px" src="https://authjs.dev/img/adapters/dgraph.svg"/>
|
|
8
|
+
</a>
|
|
9
|
+
<h3 align="center"><b>Dgraph Adapter</b> - NextAuth.js / Auth.js</a></h3>
|
|
10
|
+
<p align="center" style="align: center;">
|
|
11
|
+
<a href="https://npm.im/@auth/dgraph-adapter">
|
|
12
|
+
<img src="https://img.shields.io/badge/TypeScript-blue?style=flat-square" alt="TypeScript" />
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://npm.im/@auth/dgraph-adapter">
|
|
15
|
+
<img alt="npm" src="https://img.shields.io/npm/v/@auth/dgraph-adapter?color=green&label=@auth/dgraph-adapter&style=flat-square">
|
|
16
|
+
</a>
|
|
17
|
+
<a href="https://www.npmtrends.com/@auth/dgraph-adapter">
|
|
18
|
+
<img src="https://img.shields.io/npm/dm/@auth/dgraph-adapter?label=%20downloads&style=flat-square" alt="Downloads" />
|
|
19
|
+
</a>
|
|
20
|
+
<a href="https://github.com/nextauthjs/next-auth/stargazers">
|
|
21
|
+
<img src="https://img.shields.io/github/stars/nextauthjs/next-auth?style=flat-square" alt="Github Stars" />
|
|
22
|
+
</a>
|
|
23
|
+
</p>
|
|
24
|
+
</p>
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
Check out the documentation at [authjs.dev](https://authjs.dev/reference/adapter/dgraph).
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { format } from "./lib/utils";
|
|
2
|
+
import type { Adapter } from "@auth/core/adapters";
|
|
3
|
+
import type { DgraphClientParams } from "./lib/client";
|
|
4
|
+
export type { DgraphClientParams, DgraphClientError } from "./lib/client";
|
|
5
|
+
/** This is the interface of the Dgraph adapter options. */
|
|
6
|
+
export interface DgraphAdapterOptions {
|
|
7
|
+
/**
|
|
8
|
+
* The GraphQL {@link https://dgraph.io/docs/query-language/fragments/ Fragments} you can supply to the adapter
|
|
9
|
+
* to define how the shapes of the `user`, `account`, `session`, `verificationToken` entities look.
|
|
10
|
+
*
|
|
11
|
+
* By default the adapter will uses the [default defined fragments](https://github.com/nextauthjs/next-auth/blob/main/packages/adapter-dgraph/src/lib/graphql/fragments.ts)
|
|
12
|
+
* , this config option allows to extend them.
|
|
13
|
+
*/
|
|
14
|
+
fragments?: {
|
|
15
|
+
User?: string;
|
|
16
|
+
Account?: string;
|
|
17
|
+
Session?: string;
|
|
18
|
+
VerificationToken?: string;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export { format };
|
|
22
|
+
/**
|
|
23
|
+
* ## Setup
|
|
24
|
+
*
|
|
25
|
+
* Add this adapter to your `pages/api/[...nextauth].js` next-auth configuration object:
|
|
26
|
+
*
|
|
27
|
+
* ```ts title="pages/api/auth/[...nextauth].js"
|
|
28
|
+
* import NextAuth from "next-auth"
|
|
29
|
+
* import { DgraphAdapter } from "@auth/dgraph-adapter"
|
|
30
|
+
*
|
|
31
|
+
* export default NextAuth({
|
|
32
|
+
* providers: [],
|
|
33
|
+
* adapter: DgraphAdapter({
|
|
34
|
+
* endpoint: process.env.DGRAPH_GRAPHQL_ENDPOINT,
|
|
35
|
+
* authToken: process.env.DGRAPH_GRAPHQL_KEY,
|
|
36
|
+
* // you can omit the following properties if you are running an unsecure schema
|
|
37
|
+
* authHeader: process.env.AUTH_HEADER, // default: "Authorization",
|
|
38
|
+
* jwtSecret: process.env.SECRET,
|
|
39
|
+
* }),
|
|
40
|
+
* })
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* ### Unsecure schema
|
|
44
|
+
*
|
|
45
|
+
* The quickest way to use Dgraph is by applying the unsecure schema to your [local](https://dgraph.io/docs/graphql/admin/#modifying-a-schema) Dgraph instance or if using Dgraph [cloud](https://dgraph.io/docs/cloud/cloud-quick-start/#the-schema) you can paste the schema in the codebox to update.
|
|
46
|
+
*
|
|
47
|
+
* :::warning
|
|
48
|
+
* This approach is not secure or for production use, and does not require a `jwtSecret`.
|
|
49
|
+
* :::
|
|
50
|
+
*
|
|
51
|
+
* > This schema is adapted for use in Dgraph and based upon our main [schema](https://authjs.dev/reference/adapters#models)
|
|
52
|
+
*
|
|
53
|
+
* #### Example
|
|
54
|
+
*
|
|
55
|
+
*```graphql
|
|
56
|
+
* type Account {
|
|
57
|
+
* id: ID
|
|
58
|
+
* type: String
|
|
59
|
+
* provider: String @search(by: [hash])
|
|
60
|
+
* providerAccountId: String @search(by: [hash])
|
|
61
|
+
* refreshToken: String
|
|
62
|
+
* expires_at: Int64
|
|
63
|
+
* accessToken: String
|
|
64
|
+
* token_type: String
|
|
65
|
+
* refresh_token: String
|
|
66
|
+
* access_token: String
|
|
67
|
+
* scope: String
|
|
68
|
+
* id_token: String
|
|
69
|
+
* session_state: String
|
|
70
|
+
* user: User @hasInverse(field: "accounts")
|
|
71
|
+
* }
|
|
72
|
+
* type Session {
|
|
73
|
+
* id: ID
|
|
74
|
+
* expires: DateTime
|
|
75
|
+
* sessionToken: String @search(by: [hash])
|
|
76
|
+
* user: User @hasInverse(field: "sessions")
|
|
77
|
+
* }
|
|
78
|
+
* type User {
|
|
79
|
+
* id: ID
|
|
80
|
+
* name: String
|
|
81
|
+
* email: String @search(by: [hash])
|
|
82
|
+
* emailVerified: DateTime
|
|
83
|
+
* image: String
|
|
84
|
+
* accounts: [Account] @hasInverse(field: "user")
|
|
85
|
+
* sessions: [Session] @hasInverse(field: "user")
|
|
86
|
+
* }
|
|
87
|
+
*
|
|
88
|
+
* type VerificationToken {
|
|
89
|
+
* id: ID
|
|
90
|
+
* identifier: String @search(by: [hash])
|
|
91
|
+
* token: String @search(by: [hash])
|
|
92
|
+
* expires: DateTime
|
|
93
|
+
* }
|
|
94
|
+
*```
|
|
95
|
+
*
|
|
96
|
+
* ### Secure schema
|
|
97
|
+
*
|
|
98
|
+
* For production deployments you will want to restrict the access to the types used
|
|
99
|
+
* by next-auth. The main form of access control used in Dgraph is via `@auth` directive alongside types in the schema.
|
|
100
|
+
* #### Example
|
|
101
|
+
*
|
|
102
|
+
* ```graphql
|
|
103
|
+
* type Account
|
|
104
|
+
* @auth(
|
|
105
|
+
* delete: { rule: "{$nextAuth: { eq: true } }" }
|
|
106
|
+
* add: { rule: "{$nextAuth: { eq: true } }" }
|
|
107
|
+
* query: { rule: "{$nextAuth: { eq: true } }" }
|
|
108
|
+
* update: { rule: "{$nextAuth: { eq: true } }" }
|
|
109
|
+
* ) {
|
|
110
|
+
* id: ID
|
|
111
|
+
* type: String
|
|
112
|
+
* provider: String @search(by: [hash])
|
|
113
|
+
* providerAccountId: String @search(by: [hash])
|
|
114
|
+
* refreshToken: String
|
|
115
|
+
* expires_at: Int64
|
|
116
|
+
* accessToken: String
|
|
117
|
+
* token_type: String
|
|
118
|
+
* refresh_token: String
|
|
119
|
+
* access_token: String
|
|
120
|
+
* scope: String
|
|
121
|
+
* id_token: String
|
|
122
|
+
* session_state: String
|
|
123
|
+
* user: User @hasInverse(field: "accounts")
|
|
124
|
+
* }
|
|
125
|
+
* type Session
|
|
126
|
+
* @auth(
|
|
127
|
+
* delete: { rule: "{$nextAuth: { eq: true } }" }
|
|
128
|
+
* add: { rule: "{$nextAuth: { eq: true } }" }
|
|
129
|
+
* query: { rule: "{$nextAuth: { eq: true } }" }
|
|
130
|
+
* update: { rule: "{$nextAuth: { eq: true } }" }
|
|
131
|
+
* ) {
|
|
132
|
+
* id: ID
|
|
133
|
+
* expires: DateTime
|
|
134
|
+
* sessionToken: String @search(by: [hash])
|
|
135
|
+
* user: User @hasInverse(field: "sessions")
|
|
136
|
+
* }
|
|
137
|
+
* type User
|
|
138
|
+
* @auth(
|
|
139
|
+
* query: {
|
|
140
|
+
* or: [
|
|
141
|
+
* {
|
|
142
|
+
* rule: """
|
|
143
|
+
* query ($userId: String!) {queryUser(filter: { id: { eq: $userId } } ) {id}}
|
|
144
|
+
* """
|
|
145
|
+
* }
|
|
146
|
+
* { rule: "{$nextAuth: { eq: true } }" }
|
|
147
|
+
* ]
|
|
148
|
+
* }
|
|
149
|
+
* delete: { rule: "{$nextAuth: { eq: true } }" }
|
|
150
|
+
* add: { rule: "{$nextAuth: { eq: true } }" }
|
|
151
|
+
* update: {
|
|
152
|
+
* or: [
|
|
153
|
+
* {
|
|
154
|
+
* rule: """
|
|
155
|
+
* query ($userId: String!) {queryUser(filter: { id: { eq: $userId } } ) {id}}
|
|
156
|
+
* """
|
|
157
|
+
* }
|
|
158
|
+
* { rule: "{$nextAuth: { eq: true } }" }
|
|
159
|
+
* ]
|
|
160
|
+
* }
|
|
161
|
+
* ) {
|
|
162
|
+
* id: ID
|
|
163
|
+
* name: String
|
|
164
|
+
* email: String @search(by: [hash])
|
|
165
|
+
* emailVerified: DateTime
|
|
166
|
+
* image: String
|
|
167
|
+
* accounts: [Account] @hasInverse(field: "user")
|
|
168
|
+
* sessions: [Session] @hasInverse(field: "user")
|
|
169
|
+
* }
|
|
170
|
+
*
|
|
171
|
+
* type VerificationToken
|
|
172
|
+
* @auth(
|
|
173
|
+
* delete: { rule: "{$nextAuth: { eq: true } }" }
|
|
174
|
+
* add: { rule: "{$nextAuth: { eq: true } }" }
|
|
175
|
+
* query: { rule: "{$nextAuth: { eq: true } }" }
|
|
176
|
+
* update: { rule: "{$nextAuth: { eq: true } }" }
|
|
177
|
+
* ) {
|
|
178
|
+
* id: ID
|
|
179
|
+
* identifier: String @search(by: [hash])
|
|
180
|
+
* token: String @search(by: [hash])
|
|
181
|
+
* expires: DateTime
|
|
182
|
+
* }
|
|
183
|
+
*
|
|
184
|
+
* # Dgraph.Authorization {"VerificationKey":"<YOUR JWT SECRET HERE>","Header":"<YOUR AUTH HEADER HERE>","Namespace":"<YOUR CUSTOM NAMESPACE HERE>","Algo":"HS256"}
|
|
185
|
+
* ```
|
|
186
|
+
*
|
|
187
|
+
* ### Dgraph.Authorization
|
|
188
|
+
*
|
|
189
|
+
* In order to secure your graphql backend define the `Dgraph.Authorization` object at the
|
|
190
|
+
* bottom of your schema and provide `authHeader` and `jwtSecret` values to the DgraphClient.
|
|
191
|
+
*
|
|
192
|
+
* ```js
|
|
193
|
+
* # Dgraph.Authorization {"VerificationKey":"<YOUR JWT SECRET HERE>","Header":"<YOUR AUTH HEADER HERE>","Namespace":"YOUR CUSTOM NAMESPACE HERE","Algo":"HS256"}
|
|
194
|
+
* ```
|
|
195
|
+
*
|
|
196
|
+
* ### VerificationKey and jwtSecret
|
|
197
|
+
*
|
|
198
|
+
* This is the key used to sign the JWT. Ex. `process.env.SECRET` or `process.env.APP_SECRET`.
|
|
199
|
+
*
|
|
200
|
+
* ### Header and authHeader
|
|
201
|
+
*
|
|
202
|
+
* The `Header` tells Dgraph where to lookup a JWT within the headers of the incoming requests made to the dgraph server.
|
|
203
|
+
* You have to configure it at the bottom of your schema file. This header is the same as the `authHeader` property you
|
|
204
|
+
* provide when you instantiate the `DgraphClient`.
|
|
205
|
+
*
|
|
206
|
+
* ### The nextAuth secret
|
|
207
|
+
*
|
|
208
|
+
* The `$nextAuth` secret is securely generated using the `jwtSecret` and injected by the DgraphAdapter in order to allow interacting with the JWT DgraphClient for anonymous user requests made within the system `ie. login, register`. This allows
|
|
209
|
+
* secure interactions to be made with all the auth types required by next-auth. You have to specify it for each auth rule of
|
|
210
|
+
* each type defined in your secure schema.
|
|
211
|
+
*
|
|
212
|
+
* ```js
|
|
213
|
+
* type VerificationRequest
|
|
214
|
+
* @auth(
|
|
215
|
+
* delete: { rule: "{$nextAuth: { eq: true } }" },
|
|
216
|
+
* add: { rule: "{$nextAuth: { eq: true } }" },
|
|
217
|
+
* query: { rule: "{$nextAuth: { eq: true } }" },
|
|
218
|
+
* update: { rule: "{$nextAuth: { eq: true } }" }
|
|
219
|
+
* ) {
|
|
220
|
+
* ...
|
|
221
|
+
* }
|
|
222
|
+
* ```
|
|
223
|
+
*
|
|
224
|
+
* ### JWT session and `@auth` directive
|
|
225
|
+
*
|
|
226
|
+
* Dgraph only works with HS256 or RS256 algorithms. If you want to use session jwt to securely interact with your dgraph
|
|
227
|
+
* database you must customize next-auth `encode` and `decode` functions, as the default algorithm is HS512. You can
|
|
228
|
+
* further customize the jwt with roles if you want to implement [`RBAC logic`](https://dgraph.io/docs/graphql/authorization/directive/#role-based-access-control).
|
|
229
|
+
*
|
|
230
|
+
* ```js
|
|
231
|
+
* import * as jwt from "jsonwebtoken"
|
|
232
|
+
* export default NextAuth({
|
|
233
|
+
* session: {
|
|
234
|
+
* strategy: "jwt",
|
|
235
|
+
* },
|
|
236
|
+
* jwt: {
|
|
237
|
+
* secret: process.env.SECRET,
|
|
238
|
+
* encode: async ({ secret, token }) => {
|
|
239
|
+
* return jwt.sign({ ...token, userId: token.id }, secret, {
|
|
240
|
+
* algorithm: "HS256",
|
|
241
|
+
* expiresIn: 30 * 24 * 60 * 60, // 30 days
|
|
242
|
+
* })
|
|
243
|
+
* },
|
|
244
|
+
* decode: async ({ secret, token }) => {
|
|
245
|
+
* return jwt.verify(token, secret, { algorithms: ["HS256"] })
|
|
246
|
+
* },
|
|
247
|
+
* },
|
|
248
|
+
* })
|
|
249
|
+
* ```
|
|
250
|
+
*
|
|
251
|
+
* Once your `Dgraph.Authorization` is defined in your schema and the JWT settings are set, this will allow you to define
|
|
252
|
+
* [`@auth rules`](https://dgraph.io/docs/graphql/authorization/authorization-overview/) for every part of your schema.
|
|
253
|
+
**/
|
|
254
|
+
export declare function DgraphAdapter(client: DgraphClientParams, options?: DgraphAdapterOptions): Adapter;
|
|
255
|
+
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAClD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAGtD,YAAY,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAEzE,2DAA2D;AAC3D,MAAM,WAAW,oBAAoB;IACnC;;;;;;OAMG;IACH,SAAS,CAAC,EAAE;QACV,IAAI,CAAC,EAAE,MAAM,CAAA;QACb,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,iBAAiB,CAAC,EAAE,MAAM,CAAA;KAC3B,CAAA;CACF;AAED,OAAO,EAAE,MAAM,EAAE,CAAA;AAEjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAuOI;AACJ,wBAAgB,aAAa,CAC3B,MAAM,EAAE,kBAAkB,EAC1B,OAAO,CAAC,EAAE,oBAAoB,GAC7B,OAAO,CAmST"}
|