@powerhousedao/reactor-api 1.29.26-dev.5 → 1.29.26-dev.7
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/dist/src/graphql/graphql-manager.d.ts +1 -2
- package/dist/src/graphql/graphql-manager.d.ts.map +1 -1
- package/dist/src/graphql/graphql-manager.js +4 -2
- package/dist/src/graphql/graphql-manager.js.map +1 -1
- package/dist/src/graphql/index.d.ts +1 -2
- package/dist/src/graphql/index.d.ts.map +1 -1
- package/dist/src/graphql/index.js +1 -2
- package/dist/src/graphql/index.js.map +1 -1
- package/dist/src/graphql/system/index.d.ts.map +1 -1
- package/dist/src/graphql/system/index.js +2 -12
- package/dist/src/graphql/system/index.js.map +1 -1
- package/dist/src/graphql/system/types.d.ts +3 -3
- package/dist/src/graphql/system/types.d.ts.map +1 -1
- package/dist/src/server.d.ts +6 -0
- package/dist/src/server.d.ts.map +1 -1
- package/dist/src/server.js +43 -0
- package/dist/src/server.js.map +1 -1
- package/dist/src/utils/auth.d.ts +3 -0
- package/dist/src/utils/auth.d.ts.map +1 -0
- package/dist/src/utils/auth.js +19 -0
- package/dist/src/utils/auth.js.map +1 -0
- package/dist/src/utils/create-schema.js +1 -1
- package/dist/src/utils/create-schema.js.map +1 -1
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/package.json +9 -7
- package/dist/src/graphql/auth/env/getters.d.ts +0 -3
- package/dist/src/graphql/auth/env/getters.d.ts.map +0 -1
- package/dist/src/graphql/auth/env/getters.js +0 -27
- package/dist/src/graphql/auth/env/getters.js.map +0 -1
- package/dist/src/graphql/auth/env/index.d.ts +0 -8
- package/dist/src/graphql/auth/env/index.d.ts.map +0 -1
- package/dist/src/graphql/auth/env/index.js +0 -14
- package/dist/src/graphql/auth/env/index.js.map +0 -1
- package/dist/src/graphql/auth/index.d.ts +0 -11
- package/dist/src/graphql/auth/index.d.ts.map +0 -1
- package/dist/src/graphql/auth/index.js +0 -260
- package/dist/src/graphql/auth/index.js.map +0 -1
- package/dist/src/graphql/auth/types.d.ts +0 -35
- package/dist/src/graphql/auth/types.d.ts.map +0 -1
- package/dist/src/graphql/auth/types.js +0 -2
- package/dist/src/graphql/auth/types.js.map +0 -1
- package/dist/src/graphql/auth/utils/helpers.d.ts +0 -12
- package/dist/src/graphql/auth/utils/helpers.d.ts.map +0 -1
- package/dist/src/graphql/auth/utils/helpers.js +0 -101
- package/dist/src/graphql/auth/utils/helpers.js.map +0 -1
- package/dist/src/graphql/auth/utils/session.d.ts +0 -22
- package/dist/src/graphql/auth/utils/session.d.ts.map +0 -1
- package/dist/src/graphql/auth/utils/session.js +0 -101
- package/dist/src/graphql/auth/utils/session.js.map +0 -1
- package/dist/src/graphql/auth/utils/user.d.ts +0 -12
- package/dist/src/graphql/auth/utils/user.d.ts.map +0 -1
- package/dist/src/graphql/auth/utils/user.js +0 -27
- package/dist/src/graphql/auth/utils/user.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powerhousedao/reactor-api",
|
|
3
|
-
"version": "1.29.26-dev.
|
|
3
|
+
"version": "1.29.26-dev.7",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"esbuild": "^0.24.0",
|
|
32
32
|
"graphql-tag": "^2.12.6",
|
|
33
33
|
"tinybench": "^3.1.1",
|
|
34
|
-
"
|
|
34
|
+
"vitest": "^3.1.2"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@apollo/gateway": "^2.10.0",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"@powerhousedao/analytics-engine-core": "^0.4.0",
|
|
42
42
|
"@powerhousedao/analytics-engine-graphql": "^0.3.0",
|
|
43
43
|
"@powerhousedao/analytics-engine-knex": "^0.5.1",
|
|
44
|
+
"@powerhousedao/document-engineering": "^1.0.2",
|
|
44
45
|
"body-parser": "^1.20.3",
|
|
45
46
|
"change-case": "^5.4.4",
|
|
46
47
|
"cors": "^2.8.5",
|
|
@@ -50,7 +51,7 @@
|
|
|
50
51
|
"drizzle-orm": "^0.34.1",
|
|
51
52
|
"ethers": "^6.0.8",
|
|
52
53
|
"express": "^4.21.1",
|
|
53
|
-
"graphql": "^16.
|
|
54
|
+
"graphql": "^16.11.0",
|
|
54
55
|
"graphql-request": "^6.1.0",
|
|
55
56
|
"graphql-type-json": "^0.3.2",
|
|
56
57
|
"jsonwebtoken": "^9.0.2",
|
|
@@ -62,10 +63,11 @@
|
|
|
62
63
|
"siwe": "^2.3.2",
|
|
63
64
|
"uuid": "^9.0.1",
|
|
64
65
|
"wildcard-match": "^5.1.3",
|
|
65
|
-
"zod": "^3.24.
|
|
66
|
-
"@powerhousedao/config": "1.27.0-dev.
|
|
67
|
-
"
|
|
68
|
-
"document-
|
|
66
|
+
"zod": "^3.24.3",
|
|
67
|
+
"@powerhousedao/config": "1.27.0-dev.14",
|
|
68
|
+
"@renown/sdk": "0.15.4-dev.2",
|
|
69
|
+
"document-drive": "1.29.12-dev.8",
|
|
70
|
+
"document-model": "2.28.1-dev.15"
|
|
69
71
|
},
|
|
70
72
|
"scripts": {
|
|
71
73
|
"build": "tsc --build",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getters.d.ts","sourceRoot":"","sources":["../../../../../src/graphql/auth/env/getters.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,YAAY,QAAO,MAO/B,CAAC;AAEF,eAAO,MAAM,sBAAsB,QAAO,MAkBzC,CAAC"}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import ms from "ms";
|
|
2
|
-
export const getJwtSecret = () => {
|
|
3
|
-
if (!process.env.JWT_SECRET) {
|
|
4
|
-
if (process.env.NODE_ENV === "production") {
|
|
5
|
-
throw new Error("JWT_SECRET is not defined");
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
return process.env.JWT_SECRET || "dev";
|
|
9
|
-
};
|
|
10
|
-
export const getJwtExpirationPeriod = () => {
|
|
11
|
-
if (!process.env.JWT_EXPIRATION_PERIOD) {
|
|
12
|
-
return "7d";
|
|
13
|
-
}
|
|
14
|
-
// check if number of seconds is provided
|
|
15
|
-
const expirationSeconds = Number(process.env.JWT_EXPIRATION_PERIOD);
|
|
16
|
-
if (!Number.isNaN(expirationSeconds)) {
|
|
17
|
-
// https://www.npmjs.com/package/jsonwebtoken for `expiresIn` format
|
|
18
|
-
return ms(expirationSeconds * 1000);
|
|
19
|
-
}
|
|
20
|
-
// check if a valid time string is provided
|
|
21
|
-
const expirationMs = ms(process.env.JWT_EXPIRATION_PERIOD);
|
|
22
|
-
if (!expirationMs) {
|
|
23
|
-
throw new Error("JWT_EXPIRATION_PERIOD must be a number of seconds or ms string");
|
|
24
|
-
}
|
|
25
|
-
return process.env.JWT_EXPIRATION_PERIOD;
|
|
26
|
-
};
|
|
27
|
-
//# sourceMappingURL=getters.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getters.js","sourceRoot":"","sources":["../../../../../src/graphql/auth/env/getters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AAEpB,MAAM,CAAC,MAAM,YAAY,GAAG,GAAW,EAAE;IACvC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC;QAC5B,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,EAAE,CAAC;YAC1C,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;QAC/C,CAAC;IACH,CAAC;IACD,OAAO,OAAO,CAAC,GAAG,CAAC,UAAU,IAAI,KAAK,CAAC;AACzC,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,sBAAsB,GAAG,GAAW,EAAE;IACjD,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,qBAAqB,EAAE,CAAC;QACvC,OAAO,IAAI,CAAC;IACd,CAAC;IACD,yCAAyC;IACzC,MAAM,iBAAiB,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAC;IACpE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,iBAAiB,CAAC,EAAE,CAAC;QACrC,oEAAoE;QACpE,OAAO,EAAE,CAAC,iBAAiB,GAAG,IAAI,CAAC,CAAC;IACtC,CAAC;IACD,2CAA2C;IAC3C,MAAM,YAAY,GAAG,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAC;IAC3D,IAAI,CAAC,YAAY,EAAE,CAAC;QAClB,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAC;IACJ,CAAC;IACD,OAAO,OAAO,CAAC,GAAG,CAAC,qBAAqB,CAAC;AAC3C,CAAC,CAAC"}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export declare const JWT_SECRET: string;
|
|
2
|
-
export declare const PORT: string;
|
|
3
|
-
export declare const isDevelopment: boolean;
|
|
4
|
-
export declare const AUTH_SIGNUP_ENABLED: boolean;
|
|
5
|
-
export declare const JWT_EXPIRATION_PERIOD: string;
|
|
6
|
-
export declare const API_ORIGIN: string;
|
|
7
|
-
export declare const CORS_ORIGINS: string[];
|
|
8
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/graphql/auth/env/index.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,UAAU,QAAiB,CAAC;AACzC,eAAO,MAAM,IAAI,QAA6B,CAAC;AAC/C,eAAO,MAAM,aAAa,SAAyC,CAAC;AACpE,eAAO,MAAM,mBAAmB,SAA2C,CAAC;AAC5E,eAAO,MAAM,qBAAqB,EAAE,MAAiC,CAAC;AACtE,eAAO,MAAM,UAAU,QAAuD,CAAC;AAC/E,eAAO,MAAM,YAAY,UAGxB,CAAC"}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import dotenv from "dotenv";
|
|
2
|
-
import { getJwtExpirationPeriod, getJwtSecret } from "./getters.js";
|
|
3
|
-
dotenv.config();
|
|
4
|
-
export const JWT_SECRET = getJwtSecret();
|
|
5
|
-
export const PORT = process.env.PORT ?? "3000";
|
|
6
|
-
export const isDevelopment = process.env.NODE_ENV === "development";
|
|
7
|
-
export const AUTH_SIGNUP_ENABLED = Boolean(process.env.AUTH_SIGNUP_ENABLED);
|
|
8
|
-
export const JWT_EXPIRATION_PERIOD = getJwtExpirationPeriod();
|
|
9
|
-
export const API_ORIGIN = process.env.API_ORIGIN || `http://localhost:${PORT}`;
|
|
10
|
-
export const CORS_ORIGINS = process.env.ORIGINS?.split(",") ?? [
|
|
11
|
-
"https://studio.apollographql.com",
|
|
12
|
-
"https://ph-switchboard-nginx-prod-c84ebf8c6e3b.herokuapp.com",
|
|
13
|
-
];
|
|
14
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/graphql/auth/env/index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,sBAAsB,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEpE,MAAM,CAAC,MAAM,EAAE,CAAC;AAEhB,MAAM,CAAC,MAAM,UAAU,GAAG,YAAY,EAAE,CAAC;AACzC,MAAM,CAAC,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,MAAM,CAAC;AAC/C,MAAM,CAAC,MAAM,aAAa,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,aAAa,CAAC;AACpE,MAAM,CAAC,MAAM,mBAAmB,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAC;AAC5E,MAAM,CAAC,MAAM,qBAAqB,GAAW,sBAAsB,EAAE,CAAC;AACtE,MAAM,CAAC,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,UAAU,IAAI,oBAAoB,IAAI,EAAE,CAAC;AAC/E,MAAM,CAAC,MAAM,YAAY,GAAG,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,KAAK,CAAC,GAAG,CAAC,IAAI;IAC7D,kCAAkC;IAClC,8DAA8D;CAC/D,CAAC"}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Subgraph } from "#graphql/index.js";
|
|
2
|
-
import { type GraphQLResolverMap } from "@apollo/subgraph/dist/schema-helper/resolverMap.js";
|
|
3
|
-
import { type AuthContext } from "./types.js";
|
|
4
|
-
export declare class AuthSubgraph extends Subgraph {
|
|
5
|
-
#private;
|
|
6
|
-
name: string;
|
|
7
|
-
typeDefs: import("graphql").DocumentNode;
|
|
8
|
-
resolvers: GraphQLResolverMap<AuthContext>;
|
|
9
|
-
onSetup(): Promise<void>;
|
|
10
|
-
}
|
|
11
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/graphql/auth/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE7C,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,oDAAoD,CAAC;AAM7F,OAAO,EACL,KAAK,WAAW,EAIjB,MAAM,YAAY,CAAC;AASpB,qBAAa,YAAa,SAAQ,QAAQ;;IACxC,IAAI,SAAU;IACd,QAAQ,iCAkDN;IAEF,SAAS,EAAE,kBAAkB,CAAC,WAAW,CAAC,CA8KxC;IAEI,OAAO;CAyEd"}
|
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
import { Subgraph } from "#graphql/index.js";
|
|
2
|
-
import { generateId } from "document-model";
|
|
3
|
-
import { GraphQLError } from "graphql";
|
|
4
|
-
import { gql } from "graphql-tag";
|
|
5
|
-
import { SiweMessage } from "siwe";
|
|
6
|
-
import { generateTokenAndSession } from "./utils/helpers.js";
|
|
7
|
-
import { authenticate, createAuthenticationSession, verifySignature, } from "./utils/session.js";
|
|
8
|
-
import { getUser, upsertUser } from "./utils/user.js";
|
|
9
|
-
export class AuthSubgraph extends Subgraph {
|
|
10
|
-
name = "auth";
|
|
11
|
-
typeDefs = gql `
|
|
12
|
-
type Query {
|
|
13
|
-
me: User
|
|
14
|
-
sessions: [Session!]!
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
type Mutation {
|
|
18
|
-
createChallenge(address: String!): Challenge
|
|
19
|
-
solveChallenge(nonce: String!, signature: String!): SessionOutput
|
|
20
|
-
createSession(session: SessionInput!): SessionOutput
|
|
21
|
-
revokeSession(sessionId: String!): SessionOutput
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
type User {
|
|
25
|
-
address: String!
|
|
26
|
-
createdAt: DateTime!
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
type Challenge {
|
|
30
|
-
nonce: String!
|
|
31
|
-
message: String!
|
|
32
|
-
hex: String!
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
type SessionOutput {
|
|
36
|
-
id: ID!
|
|
37
|
-
token: String
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
type Session {
|
|
41
|
-
id: ID!
|
|
42
|
-
userId: String!
|
|
43
|
-
address: String!
|
|
44
|
-
expiresAt: DateTime!
|
|
45
|
-
createdAt: DateTime!
|
|
46
|
-
updatedAt: DateTime!
|
|
47
|
-
referenceTokenId: String!
|
|
48
|
-
createdBy: String!
|
|
49
|
-
referenceExpiryDate: DateTime
|
|
50
|
-
isUserCreated: Boolean!
|
|
51
|
-
name: String
|
|
52
|
-
allowedOrigins: String
|
|
53
|
-
revokedAt: DateTime
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
input SessionInput {
|
|
57
|
-
expiryDurationSeconds: Int
|
|
58
|
-
name: String!
|
|
59
|
-
allowedOrigins: String!
|
|
60
|
-
}
|
|
61
|
-
`;
|
|
62
|
-
resolvers = {
|
|
63
|
-
Query: {
|
|
64
|
-
me: async (_, __, ctx) => {
|
|
65
|
-
const db = ctx.db;
|
|
66
|
-
const session = await authenticate(ctx);
|
|
67
|
-
const user = await getUser(db, session.createdBy);
|
|
68
|
-
return user;
|
|
69
|
-
},
|
|
70
|
-
sessions: async (_, __, ctx) => {
|
|
71
|
-
const session = await authenticate(ctx);
|
|
72
|
-
const db = ctx.db;
|
|
73
|
-
const sessions = await db("Session")
|
|
74
|
-
.select()
|
|
75
|
-
.where("createdBy", session.createdBy)
|
|
76
|
-
.orderBy("createdAt", "desc");
|
|
77
|
-
return sessions;
|
|
78
|
-
},
|
|
79
|
-
},
|
|
80
|
-
Mutation: {
|
|
81
|
-
createChallenge: async (_, { address }, ctx) => {
|
|
82
|
-
const db = ctx.db;
|
|
83
|
-
const { API_ORIGIN } = process.env;
|
|
84
|
-
const origin = API_ORIGIN ?? "http://localhost:3000";
|
|
85
|
-
const domain = new URL(origin).hostname;
|
|
86
|
-
if (!domain) {
|
|
87
|
-
throw new GraphQLError("Invalid origin");
|
|
88
|
-
}
|
|
89
|
-
const nonce = generateId().replace(/-/g, "");
|
|
90
|
-
const message = new SiweMessage({
|
|
91
|
-
address,
|
|
92
|
-
nonce,
|
|
93
|
-
uri: origin,
|
|
94
|
-
domain,
|
|
95
|
-
version: "1",
|
|
96
|
-
chainId: 1,
|
|
97
|
-
}).prepareMessage();
|
|
98
|
-
const textToHex = (textMessage) => `0x${Buffer.from(textMessage, "utf8").toString("hex")}`;
|
|
99
|
-
if (!message || typeof message !== "string") {
|
|
100
|
-
throw new GraphQLError("Failed to create challenge");
|
|
101
|
-
}
|
|
102
|
-
const hexMessage = textToHex(message);
|
|
103
|
-
await db("Challenge").insert({
|
|
104
|
-
nonce,
|
|
105
|
-
message,
|
|
106
|
-
updatedAt: new Date().toISOString(),
|
|
107
|
-
});
|
|
108
|
-
return {
|
|
109
|
-
nonce,
|
|
110
|
-
message,
|
|
111
|
-
hex: hexMessage,
|
|
112
|
-
};
|
|
113
|
-
},
|
|
114
|
-
solveChallenge: async (_, { nonce, signature }, ctx) => {
|
|
115
|
-
const db = ctx.db;
|
|
116
|
-
const data = await db.transaction(async (tx) => {
|
|
117
|
-
const [challenge] = await tx("Challenge")
|
|
118
|
-
.select()
|
|
119
|
-
.where("nonce", nonce);
|
|
120
|
-
// check that challenge with this nonce exists
|
|
121
|
-
if (!challenge) {
|
|
122
|
-
throw new GraphQLError("The nonce is not known");
|
|
123
|
-
}
|
|
124
|
-
// check that challenge was not used
|
|
125
|
-
if (challenge.signature) {
|
|
126
|
-
throw new GraphQLError("The signature was already used");
|
|
127
|
-
}
|
|
128
|
-
// verify signature
|
|
129
|
-
const parsedMessage = new SiweMessage(challenge.message);
|
|
130
|
-
try {
|
|
131
|
-
await verifySignature(parsedMessage, signature);
|
|
132
|
-
}
|
|
133
|
-
catch (error) {
|
|
134
|
-
throw new GraphQLError("Signature validation has failed");
|
|
135
|
-
}
|
|
136
|
-
// mark challenge as used
|
|
137
|
-
await tx("Challenge")
|
|
138
|
-
.update({
|
|
139
|
-
signature,
|
|
140
|
-
})
|
|
141
|
-
.where("nonce", nonce);
|
|
142
|
-
// create user and session
|
|
143
|
-
const user = await upsertUser(db, {
|
|
144
|
-
address: parsedMessage.address,
|
|
145
|
-
networkId: "1",
|
|
146
|
-
chainId: 1,
|
|
147
|
-
});
|
|
148
|
-
if (!user) {
|
|
149
|
-
throw new GraphQLError("User not found");
|
|
150
|
-
}
|
|
151
|
-
const tokenAndSession = await createAuthenticationSession(db, user.address);
|
|
152
|
-
return tokenAndSession;
|
|
153
|
-
});
|
|
154
|
-
return data;
|
|
155
|
-
},
|
|
156
|
-
createSession: async (_, { session }, ctx) => {
|
|
157
|
-
const db = ctx.db;
|
|
158
|
-
const sessionAuth = await authenticate(ctx);
|
|
159
|
-
const tokenAndSession = await generateTokenAndSession(db, session, sessionAuth.createdBy, sessionAuth.isUserCreated);
|
|
160
|
-
if (!tokenAndSession) {
|
|
161
|
-
throw new GraphQLError("Failed to create session");
|
|
162
|
-
}
|
|
163
|
-
return tokenAndSession;
|
|
164
|
-
},
|
|
165
|
-
revokeSession: async (_, { sessionId }, ctx) => {
|
|
166
|
-
const user = await authenticate(ctx);
|
|
167
|
-
const db = ctx.db;
|
|
168
|
-
const [session] = await db("Session").select().where({
|
|
169
|
-
id: sessionId,
|
|
170
|
-
createdBy: user.createdBy,
|
|
171
|
-
});
|
|
172
|
-
if (!session) {
|
|
173
|
-
throw new GraphQLError("Session not found", {
|
|
174
|
-
extensions: { code: "SESSION_NOT_FOUND" },
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
if (session.revokedAt !== null) {
|
|
178
|
-
throw new GraphQLError("Session already revoked", {
|
|
179
|
-
extensions: { code: "SESSION_ALREADY_REVOKED" },
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
await db("Session")
|
|
183
|
-
.update({
|
|
184
|
-
revokedAt: new Date().toISOString(),
|
|
185
|
-
})
|
|
186
|
-
.where({
|
|
187
|
-
id: sessionId,
|
|
188
|
-
createdBy: user.createdBy,
|
|
189
|
-
});
|
|
190
|
-
return { id: session.id };
|
|
191
|
-
},
|
|
192
|
-
},
|
|
193
|
-
};
|
|
194
|
-
async onSetup() {
|
|
195
|
-
await super.onSetup();
|
|
196
|
-
await this.#createTables();
|
|
197
|
-
this.graphqlManager.setAdditionalContextFields({
|
|
198
|
-
session: async (ctx) => {
|
|
199
|
-
const bearerToken = ctx.headers.authorization?.split(" ")[1];
|
|
200
|
-
if (!bearerToken) {
|
|
201
|
-
return null;
|
|
202
|
-
}
|
|
203
|
-
// @todo: optimize and cache this
|
|
204
|
-
const db = ctx.db;
|
|
205
|
-
const [session] = await db("Session")
|
|
206
|
-
.select()
|
|
207
|
-
.where({
|
|
208
|
-
referenceTokenId: bearerToken,
|
|
209
|
-
})
|
|
210
|
-
.limit(1);
|
|
211
|
-
return session;
|
|
212
|
-
},
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
async #createTables() {
|
|
216
|
-
if (!(await this.operationalStore.schema.hasTable("User"))) {
|
|
217
|
-
await this.operationalStore.schema.createTable("User", (table) => {
|
|
218
|
-
table.string("address").primary().notNullable();
|
|
219
|
-
table.timestamp("createdAt").notNullable().defaultTo(`now()`);
|
|
220
|
-
table.timestamp("updatedAt").notNullable().defaultTo(`now()`);
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
if (!(await this.operationalStore.schema.hasTable("Session"))) {
|
|
224
|
-
await this.operationalStore.schema.createTable("Session", (table) => {
|
|
225
|
-
table.string("id").primary().notNullable();
|
|
226
|
-
table.timestamp("createdAt").notNullable().defaultTo(`now()`);
|
|
227
|
-
table.string("createdBy").notNullable();
|
|
228
|
-
table.string("referenceExpiryDate");
|
|
229
|
-
table.string("name");
|
|
230
|
-
table.string("revokedAt");
|
|
231
|
-
table.string("referenceTokenId").notNullable();
|
|
232
|
-
table.boolean("isUserCreated").notNullable().defaultTo(false);
|
|
233
|
-
table.string("allowedOrigins").notNullable();
|
|
234
|
-
table.index(["createdBy", "id"], "Session_createdBy_id_key", {
|
|
235
|
-
indexType: "UNIQUE",
|
|
236
|
-
storageEngineIndexType: "btree",
|
|
237
|
-
});
|
|
238
|
-
table
|
|
239
|
-
.foreign("createdBy")
|
|
240
|
-
.references("User.address")
|
|
241
|
-
.onDelete("cascade")
|
|
242
|
-
.onUpdate("cascade");
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
if (!(await this.operationalStore.schema.hasTable("Challenge"))) {
|
|
246
|
-
await this.operationalStore.schema.createTable("Challenge", (table) => {
|
|
247
|
-
table.string("nonce").primary().notNullable();
|
|
248
|
-
table.string("message").notNullable();
|
|
249
|
-
table.string("signature");
|
|
250
|
-
table.timestamp("createdAt").notNullable().defaultTo(`now()`);
|
|
251
|
-
table.timestamp("updatedAt").notNullable();
|
|
252
|
-
table.index("nonce", "Challenge_message_key", {
|
|
253
|
-
indexType: "UNIQUE",
|
|
254
|
-
storageEngineIndexType: "btree",
|
|
255
|
-
});
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/graphql/auth/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAG7C,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,EAAE,WAAW,EAAE,MAAM,MAAM,CAAC;AAQnC,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAC7D,OAAO,EACL,YAAY,EACZ,2BAA2B,EAC3B,eAAe,GAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAEtD,MAAM,OAAO,YAAa,SAAQ,QAAQ;IACxC,IAAI,GAAG,MAAM,CAAC;IACd,QAAQ,GAAG,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDb,CAAC;IAEF,SAAS,GAAoC;QAC3C,KAAK,EAAE;YACL,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE;gBACvB,MAAM,EAAE,GAAG,GAAG,CAAC,EAAQ,CAAC;gBACxB,MAAM,OAAO,GAAG,MAAM,YAAY,CAAC,GAAG,CAAC,CAAC;gBACxC,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,EAAE,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC;gBAClD,OAAO,IAAI,CAA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AC,OAAO,CAAC,CAAC,OAAO,EAAE,CAAC,WAAW,EAAE,CAAC;gBAC9C,KAAK,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,WAAW,EAAE,CAAC;gBACtC,KAAK,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;gBAC1B,KAAK,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,WAAW,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;gBAC9D,KAAK,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,WAAW,EAAE,CAAC;gBAE3C,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE,uBAAuB,EAAE;oBAC5C,SAAS,EAAE,QAAQ;oBACnB,sBAAsB,EAAE,OAAO;iBAChC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC;CACF"}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { Context } from "#graphql/types.js";
|
|
2
|
-
export interface SessionInput {
|
|
3
|
-
name: string;
|
|
4
|
-
allowedOrigins: string[];
|
|
5
|
-
expiresAt?: string;
|
|
6
|
-
}
|
|
7
|
-
export interface SessionOutput {
|
|
8
|
-
session: Session;
|
|
9
|
-
token: string;
|
|
10
|
-
}
|
|
11
|
-
export interface Session {
|
|
12
|
-
id: string;
|
|
13
|
-
userId: string;
|
|
14
|
-
address: string;
|
|
15
|
-
name?: string;
|
|
16
|
-
expiresAt: string;
|
|
17
|
-
createdAt: string;
|
|
18
|
-
updatedAt: string;
|
|
19
|
-
revokedAt: string | null;
|
|
20
|
-
allowedOrigins: string;
|
|
21
|
-
referenceExpiryDate: string;
|
|
22
|
-
referenceTokenId: string;
|
|
23
|
-
isUserCreated: boolean;
|
|
24
|
-
createdBy: string;
|
|
25
|
-
}
|
|
26
|
-
export interface Challenge {
|
|
27
|
-
id: string;
|
|
28
|
-
nonce: string;
|
|
29
|
-
signature: string;
|
|
30
|
-
message: string;
|
|
31
|
-
}
|
|
32
|
-
export type AuthContext = Context & {
|
|
33
|
-
session: Session;
|
|
34
|
-
};
|
|
35
|
-
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/graphql/auth/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAE5C,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,cAAc,EAAE,MAAM,CAAC;IACvB,mBAAmB,EAAE,MAAM,CAAC;IAC5B,gBAAgB,EAAE,MAAM,CAAC;IACzB,aAAa,EAAE,OAAO,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,SAAS;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,MAAM,WAAW,GAAG,OAAO,GAAG;IAClC,OAAO,EAAE,OAAO,CAAC;CAClB,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../src/graphql/auth/types.ts"],"names":[],"mappings":""}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { SessionInput } from "#graphql/auth/types.js";
|
|
2
|
-
import { Db } from "#utils/db.js";
|
|
3
|
-
export declare const formatToken: (token: string) => string;
|
|
4
|
-
export declare const verifyToken: (token: string) => {
|
|
5
|
-
sessionId: string;
|
|
6
|
-
} | undefined;
|
|
7
|
-
export declare function validateOriginAgainstAllowed(allowedOrigins: string, originReceived?: string): void;
|
|
8
|
-
export declare const generateTokenAndSession: (db: Db, session: SessionInput, userId: string, isUserCreated: boolean) => Promise<{
|
|
9
|
-
token: string;
|
|
10
|
-
session: number[];
|
|
11
|
-
}>;
|
|
12
|
-
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../../../src/graphql/auth/utils/helpers.ts"],"names":[],"mappings":"AACA,OAAO,EAAW,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,EAAE,EAAE,MAAM,cAAc,CAAC;AAYlC,eAAO,MAAM,WAAW,UAAW,MAAM,WACI,CAAC;AA4B9C,eAAO,MAAM,WAAW,UACf,MAAM,KACZ;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,GAAG,SAiB1B,CAAC;AAkBF,wBAAgB,4BAA4B,CAC1C,cAAc,EAAE,MAAM,EACtB,cAAc,CAAC,EAAE,MAAM,QAmBxB;AAED,eAAO,MAAM,uBAAuB,OAC9B,EAAE,WACG,YAAY,UACb,MAAM,iBACC,OAAO;;;EAwBvB,CAAC"}
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import { JWT_EXPIRATION_PERIOD, JWT_SECRET } from "#graphql/auth/env/index.js";
|
|
2
|
-
import { randomUUID } from "crypto";
|
|
3
|
-
import { GraphQLError } from "graphql";
|
|
4
|
-
import jwt from "jsonwebtoken";
|
|
5
|
-
import ms from "ms";
|
|
6
|
-
import wildcard from "wildcard-match";
|
|
7
|
-
import z from "zod";
|
|
8
|
-
const jwtSchema = z.object({
|
|
9
|
-
sessionId: z.string(),
|
|
10
|
-
exp: z.optional(z.number()),
|
|
11
|
-
});
|
|
12
|
-
export const formatToken = (token) => `${token.slice(0, 4)}...${token.slice(-4)}`;
|
|
13
|
-
/** Generate a JWT token
|
|
14
|
-
* - If expiryDurationSeconds is null, the token will never expire
|
|
15
|
-
* - If expiryDurationSeconds is undefined, the token will expire after the default expiry period
|
|
16
|
-
*/
|
|
17
|
-
const generateToken = (sessionId, expiryDurationSeconds) => {
|
|
18
|
-
if (expiryDurationSeconds === null) {
|
|
19
|
-
return jwt.sign({ sessionId }, JWT_SECRET);
|
|
20
|
-
}
|
|
21
|
-
const expiresIn = expiryDurationSeconds
|
|
22
|
-
? ms(expiryDurationSeconds * 1000)
|
|
23
|
-
: (JWT_EXPIRATION_PERIOD ?? 3600);
|
|
24
|
-
return jwt.sign({ sessionId }, JWT_SECRET, { expiresIn });
|
|
25
|
-
};
|
|
26
|
-
const getExpiryDateFromToken = (token) => {
|
|
27
|
-
const { exp } = jwtSchema.parse(jwt.verify(token, JWT_SECRET));
|
|
28
|
-
if (!exp) {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
return new Date(exp * 1000);
|
|
32
|
-
};
|
|
33
|
-
export const verifyToken = (token) => {
|
|
34
|
-
const verified = jwt.verify(token, JWT_SECRET, (err, decoded) => {
|
|
35
|
-
if (err) {
|
|
36
|
-
throw new GraphQLError(err.name === "TokenExpiredError"
|
|
37
|
-
? "Token expired"
|
|
38
|
-
: "Invalid authentication token", { extensions: { code: "AUTHENTICATION_TOKEN_ERROR" } });
|
|
39
|
-
}
|
|
40
|
-
return decoded;
|
|
41
|
-
});
|
|
42
|
-
if (!verified) {
|
|
43
|
-
return undefined;
|
|
44
|
-
}
|
|
45
|
-
const validated = jwtSchema.parse(verified);
|
|
46
|
-
return validated;
|
|
47
|
-
};
|
|
48
|
-
function parseOriginMarkup(originParam) {
|
|
49
|
-
if (originParam === "*") {
|
|
50
|
-
return "*";
|
|
51
|
-
}
|
|
52
|
-
const trimmedOriginParam = originParam.trim();
|
|
53
|
-
const origins = trimmedOriginParam.split(",").map((origin) => origin.trim());
|
|
54
|
-
origins.forEach((origin) => {
|
|
55
|
-
if (!origin.startsWith("http://") && !origin.startsWith("https://")) {
|
|
56
|
-
throw new GraphQLError("Origin must start with 'http://' or 'https://'", {
|
|
57
|
-
extensions: { code: "INVALID_ORIGIN_PROTOCOL" },
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
return origins.join(",");
|
|
62
|
-
}
|
|
63
|
-
export function validateOriginAgainstAllowed(allowedOrigins, originReceived) {
|
|
64
|
-
if (allowedOrigins === "*") {
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
if (!originReceived) {
|
|
68
|
-
throw new GraphQLError("Origin not provided", {
|
|
69
|
-
extensions: { code: "ORIGIN_HEADER_MISSING" },
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
const allowedOriginsSplit = allowedOrigins.split(",");
|
|
73
|
-
if (!wildcard(allowedOriginsSplit)(originReceived)) {
|
|
74
|
-
throw new GraphQLError(`Access denied due to origin restriction: ${allowedOrigins}, ${originReceived}`, {
|
|
75
|
-
extensions: { code: "ORIGIN_FORBIDDEN" },
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
export const generateTokenAndSession = async (db, session, userId, isUserCreated) => {
|
|
80
|
-
const sessionId = randomUUID();
|
|
81
|
-
const generatedToken = generateToken(sessionId, Number(session.expiresAt));
|
|
82
|
-
const referenceExpiryDate = getExpiryDateFromToken(generatedToken);
|
|
83
|
-
const referenceTokenId = formatToken(generatedToken);
|
|
84
|
-
const allowedOrigins = parseOriginMarkup(Array.isArray(session.allowedOrigins)
|
|
85
|
-
? session.allowedOrigins.join(",")
|
|
86
|
-
: session.allowedOrigins);
|
|
87
|
-
const createdSession = await db("Session").insert({
|
|
88
|
-
id: sessionId,
|
|
89
|
-
name: session.name,
|
|
90
|
-
allowedOrigins,
|
|
91
|
-
referenceExpiryDate: referenceExpiryDate?.toISOString(),
|
|
92
|
-
referenceTokenId,
|
|
93
|
-
isUserCreated: isUserCreated,
|
|
94
|
-
createdBy: userId,
|
|
95
|
-
});
|
|
96
|
-
return {
|
|
97
|
-
token: generatedToken,
|
|
98
|
-
session: createdSession,
|
|
99
|
-
};
|
|
100
|
-
};
|
|
101
|
-
//# sourceMappingURL=helpers.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../../../src/graphql/auth/utils/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AAG/E,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,GAAG,MAAM,cAAc,CAAC;AAC/B,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AACtC,OAAO,CAAC,MAAM,KAAK,CAAC;AACpB,MAAM,SAAS,GAAG,CAAC,CAAC,MAAM,CAAC;IACzB,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;IACrB,GAAG,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;CAC5B,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAa,EAAE,EAAE,CAC3C,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AAE9C;;;GAGG;AACH,MAAM,aAAa,GAAG,CACpB,SAAiB,EACjB,qBAAqC,EAC7B,EAAE;IACV,IAAI,qBAAqB,KAAK,IAAI,EAAE,CAAC;QACn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AA4C,cAAc,KAAK,cAAc,EAAE,EAC/E;YACE,UAAU,EAAE,EAAE,IAAI,EAAE,kBAAkB,EAAE;SACzC,CACF,CAAC;IACJ,CAAC;AACH,CAAC;AAED,MAAM,CAAC,MAAM,uBAAuB,GAAG,KAAK,EAC1C,EAAM,EACN,OAAqB,EACrB,MAAc,EACd,aAAsB,EACtB,EAAE;IACF,MAAM,SAAS,GAAG,UAAU,EAAE,CAAC;IAC/B,MAAM,cAAc,GAAG,aAAa,CAAC,SAAS,EAAE,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC;IAC3E,MAAM,mBAAmB,GAAG,sBAAsB,CAAC,cAAc,CAAC,CAAC;IACnE,MAAM,gBAAgB,GAAG,WAAW,CAAC,cAAc,CAAC,CAAC;IACrD,MAAM,cAAc,GAAG,iBAAiB,CACtC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC;QACnC,CAAC,CAAC,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,GAAG,CAAC;QAClC,CAAC,CAAC,OAAO,CAAC,cAAc,CAC3B,CAAC;IACF,MAAM,cAAc,GAAG,MAAM,EAAE,CAAU,SAAS,CAAC,CAAC,MAAM,CAAC;QACzD,EAAE,EAAE,SAAS;QACb,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,cAAc;QACd,mBAAmB,EAAE,mBAAmB,EAAE,WAAW,EAAE;QACvD,gBAAgB;QAChB,aAAa,EAAE,aAAa;QAC5B,SAAS,EAAE,MAAM;KAClB,CAAC,CAAC;IACH,OAAO;QACL,KAAK,EAAE,cAAc;QACrB,OAAO,EAAE,cAAc;KACxB,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { type Context } from "#graphql/types.js";
|
|
2
|
-
import { type Db } from "#types.js";
|
|
3
|
-
import { type SiweMessage } from "siwe";
|
|
4
|
-
import { type Session } from "../types.js";
|
|
5
|
-
export declare const createAuthenticationSession: (db: Db, userId: string, allowedOrigins?: string[]) => Promise<{
|
|
6
|
-
token: string;
|
|
7
|
-
session: number[];
|
|
8
|
-
}>;
|
|
9
|
-
export declare const createCustomSession: (db: Db, userId: string, session: {
|
|
10
|
-
expiryDurationSeconds?: number | null;
|
|
11
|
-
name: string;
|
|
12
|
-
allowedOrigins: string[];
|
|
13
|
-
}, isUserCreated?: boolean) => Promise<{
|
|
14
|
-
token: string;
|
|
15
|
-
session: number[];
|
|
16
|
-
}>;
|
|
17
|
-
export declare const listSessions: (db: Db, userId: string) => Promise<Session[]>;
|
|
18
|
-
export declare const revoke: (db: Db, sessionId: string, userId: string) => Promise<void>;
|
|
19
|
-
export declare const authenticate: (context: Context) => Promise<Session>;
|
|
20
|
-
export declare const getSessionByToken: (db: Db, origin?: string, token?: string) => Promise<Session>;
|
|
21
|
-
export declare const verifySignature: (parsedMessage: SiweMessage, signature: string) => Promise<import("siwe").SiweResponse>;
|
|
22
|
-
//# sourceMappingURL=session.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../../../../src/graphql/auth/utils/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,KAAK,EAAE,EAAE,MAAM,WAAW,CAAC;AAGpC,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,MAAM,CAAC;AAExC,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,aAAa,CAAC;AAO3C,eAAO,MAAM,2BAA2B,OAClC,EAAE,UACE,MAAM;;;EAef,CAAC;AAEF,eAAO,MAAM,mBAAmB,OAC1B,EAAE,UACE,MAAM,WACL;IACP,qBAAqB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtC,IAAI,EAAE,MAAM,CAAC;IACb,cAAc,EAAE,MAAM,EAAE,CAAC;CAC1B;;;EAIF,CAAC;AAEF,eAAO,MAAM,YAAY,OAAc,EAAE,UAAU,MAAM,uBAExD,CAAC;AAEF,eAAO,MAAM,MAAM,OAAc,EAAE,aAAa,MAAM,UAAU,MAAM,kBAwBrE,CAAC;AAEF,eAAO,MAAM,YAAY,YAAmB,OAAO,qBAYlD,CAAC;AAEF,eAAO,MAAM,iBAAiB,OACxB,EAAE,WACG,MAAM,UACP,MAAM,qBAoCf,CAAC;AAEF,eAAO,MAAM,eAAe,kBACX,WAAW,aACf,MAAM,yCAWlB,CAAC"}
|