@powerhousedao/reactor-api 4.1.0-staging.1 → 5.0.0-staging.2

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.
@@ -0,0 +1,154 @@
1
+ import { verifyAuthBearerToken } from "@renown/sdk";
2
+ export class AuthService {
3
+ config;
4
+ constructor(config) {
5
+ this.config = config;
6
+ }
7
+ /**
8
+ * Middleware function to authenticate requests
9
+ */
10
+ async authenticate(req, res, next) {
11
+ if (!this.config.enabled ||
12
+ req.method === "OPTIONS" ||
13
+ req.method === "GET") {
14
+ next();
15
+ return;
16
+ }
17
+ // Set auth lists on request
18
+ req.admins = this.config.admins;
19
+ req.users = this.config.users;
20
+ req.guests = this.config.guests;
21
+ req.auth_enabled = this.config.enabled;
22
+ const token = req.headers.authorization?.split(" ")[1];
23
+ if (!token) {
24
+ res.status(400).json({ error: "Missing authorization token" });
25
+ return;
26
+ }
27
+ try {
28
+ const verified = (await this.verifyToken(token));
29
+ if (!verified) {
30
+ res.status(401).json({ error: "Verification failed" });
31
+ return;
32
+ }
33
+ const user = this.extractUserFromVerification(verified);
34
+ if (!user) {
35
+ res.status(401).json({ error: "Missing credentials" });
36
+ return;
37
+ }
38
+ // Verify that the credentials still exist on the Renown API
39
+ const credentialExists = await this.verifyCredentialExists(user.address, user.chainId, verified.issuer);
40
+ if (!credentialExists) {
41
+ res.status(401).json({ error: "Credentials no longer valid" });
42
+ return;
43
+ }
44
+ req.user = user;
45
+ // Check if user is in allowed lists
46
+ if (!this.isUserAllowed(user.address)) {
47
+ res.status(403).json({ error: "Forbidden" });
48
+ return;
49
+ }
50
+ next();
51
+ }
52
+ catch (error) {
53
+ res.status(401).json({ error: "Authentication failed" });
54
+ }
55
+ }
56
+ /**
57
+ * Verify the auth bearer token
58
+ */
59
+ async verifyToken(token) {
60
+ return await verifyAuthBearerToken(token);
61
+ }
62
+ /**
63
+ * Extract user information from verification result
64
+ */
65
+ extractUserFromVerification(verified) {
66
+ if (!verified)
67
+ return null;
68
+ try {
69
+ const { address, chainId, networkId } = verified.verifiableCredential?.credentialSubject || {};
70
+ if (!address || !chainId || !networkId) {
71
+ return null;
72
+ }
73
+ return {
74
+ address,
75
+ chainId,
76
+ networkId,
77
+ };
78
+ }
79
+ catch (error) {
80
+ return null;
81
+ }
82
+ }
83
+ /**
84
+ * Check if user address is in allowed lists
85
+ */
86
+ isUserAllowed(address) {
87
+ const all = [
88
+ ...this.config.admins,
89
+ ...this.config.users,
90
+ ...this.config.guests,
91
+ ];
92
+ return all.includes(address.toLocaleLowerCase());
93
+ }
94
+ /**
95
+ * Get additional context fields for GraphQL
96
+ */
97
+ getAdditionalContextFields() {
98
+ if (!this.config.enabled) {
99
+ return {
100
+ isGuest: (address) => true,
101
+ isUser: (address) => true,
102
+ isAdmin: (address) => true,
103
+ };
104
+ }
105
+ return {
106
+ isGuest: (address) => this.config.enabled &&
107
+ this.config.guests?.includes(address.toLowerCase()),
108
+ isUser: (address) => this.config.enabled &&
109
+ this.config.users?.includes(address.toLowerCase()),
110
+ isAdmin: (address) => this.config.enabled &&
111
+ this.config.admins?.includes(address.toLowerCase()),
112
+ };
113
+ }
114
+ /**
115
+ * Get user context for GraphQL
116
+ */
117
+ getUserContext(user) {
118
+ if (!user)
119
+ return {};
120
+ return {
121
+ user: {
122
+ address: user.address.toLowerCase(),
123
+ chainId: user.chainId,
124
+ networkId: user.networkId,
125
+ },
126
+ };
127
+ }
128
+ /**
129
+ * Verify that the credential still exists on the Renown API
130
+ */
131
+ async verifyCredentialExists(address, chainId, connectId) {
132
+ const url = `https://auth.renown.id/api/auth/credential?address=${address}&chainId=${chainId}&connectId=${connectId}`;
133
+ try {
134
+ const response = await fetch(url, {
135
+ method: "GET",
136
+ });
137
+ const body = (await response.json());
138
+ const credential = body.credential;
139
+ const connectIdVerfied = credential.credentialSubject.id.id;
140
+ const addressVerfied = credential.issuer.id.split(":")[4];
141
+ const chainIdVerfied = credential.issuer.id.split(":")[3];
142
+ if (response.status !== 200) {
143
+ return false;
144
+ }
145
+ return (connectIdVerfied === connectId &&
146
+ addressVerfied.toLocaleLowerCase() === address.toLocaleLowerCase() &&
147
+ chainIdVerfied === chainId.toString());
148
+ }
149
+ catch (error) {
150
+ return false;
151
+ }
152
+ }
153
+ }
154
+ //# sourceMappingURL=auth.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.service.js","sourceRoot":"","sources":["../../../src/services/auth.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AA6BpD,MAAM,OAAO,WAAW;IACL,MAAM,CAAa;IAEpC,YAAY,MAAkB;QAC5B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,YAAY,CAChB,GAAyB,EACzB,GAAa,EACb,IAAkB;QAElB,IACE,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO;YACpB,GAAG,CAAC,MAAM,KAAK,SAAS;YACxB,GAAG,CAAC,MAAM,KAAK,KAAK,EACpB,CAAC;YACD,IAAI,EAAE,CAAC;YACP,OAAO;QACT,CAAC;QAED,4BAA4B;QAC5B,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;QAChC,GAAG,CAAC,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;QAC9B,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;QAChC,GAAG,CAAC,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC;QAEvC,MAAM,KAAK,GAAG,GAAG,CAAC,OAAO,CAAC,aAAa,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QACvD,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,6BAA6B,EAAE,C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