Skip to content

Commit f44e672

Browse files
authored
Fix auth token smart account (#1140)
* add chain id for create auth token * pass chain id on invaldiate
1 parent 6e95b4f commit f44e672

File tree

7 files changed

+55
-30
lines changed

7 files changed

+55
-30
lines changed

src/@types/commands.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,6 @@ export interface CreateAuthTokenCommand extends Command {
292292
validUntil?: number | null
293293
}
294294

295-
export interface InvalidateAuthTokenCommand extends Command {
296-
address: string
297-
signature: string
298-
token: string
299-
}
300-
301295
export interface GetJobsCommand extends Command {
302296
environments?: string[]
303297
fromTimestamp?: string

src/components/Auth/index.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface AuthValidation {
1111
nonce?: string
1212
signature?: string
1313
message?: string
14+
chainId?: string | null
1415
}
1516

1617
export class Auth {
@@ -42,9 +43,16 @@ export class Auth {
4243
address: string,
4344
jwtToken: string,
4445
validUntil: number,
45-
createdAt: number
46+
createdAt: number,
47+
chainId?: string | null
4648
): Promise<void> {
47-
await this.authTokenDatabase.createToken(jwtToken, address, validUntil, createdAt)
49+
await this.authTokenDatabase.createToken(
50+
jwtToken,
51+
address,
52+
validUntil,
53+
createdAt,
54+
chainId
55+
)
4856
}
4957

5058
async invalidateToken(jwtToken: string): Promise<void> {
@@ -71,7 +79,7 @@ export class Auth {
7179
async validateAuthenticationOrToken(
7280
authValidation: AuthValidation
7381
): Promise<CommonValidation> {
74-
const { token, address, nonce, signature, message } = authValidation
82+
const { token, address, nonce, signature, message, chainId } = authValidation
7583
try {
7684
if (signature && address && nonce) {
7785
const oceanNode = OceanNode.getInstance()
@@ -80,7 +88,8 @@ export class Auth {
8088
address,
8189
parseInt(nonce),
8290
signature,
83-
message
91+
message,
92+
chainId
8493
)
8594

8695
if (!nonceCheckResult.valid) {

src/components/core/handler/authHandler.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ export interface AuthMessage {
1717

1818
export interface CreateAuthTokenCommand extends AuthMessage, Command {
1919
validUntil?: number | null
20+
chainId?: string | null
2021
}
2122

2223
export interface InvalidateAuthTokenCommand extends AuthMessage, Command {
2324
token: string
25+
chainId?: string | null
2426
}
2527

2628
export class CreateAuthTokenHandler extends CommandHandler {
@@ -42,7 +44,8 @@ export class CreateAuthTokenHandler extends CommandHandler {
4244
address,
4345
parseInt(nonce),
4446
signature,
45-
String(address + nonce)
47+
String(address + nonce),
48+
task.chainId
4649
)
4750

4851
if (!nonceCheckResult.valid) {
@@ -59,7 +62,7 @@ export class CreateAuthTokenHandler extends CommandHandler {
5962

6063
await this.getOceanNode()
6164
.getAuth()
62-
.insertToken(task.address, jwtToken, task.validUntil, createdAt)
65+
.insertToken(task.address, jwtToken, task.validUntil, createdAt, task.chainId)
6366

6467
return {
6568
stream: Readable.from(JSON.stringify({ token: jwtToken })),
@@ -93,7 +96,8 @@ export class InvalidateAuthTokenHandler extends CommandHandler {
9396
address,
9497
parseInt(nonce),
9598
signature,
96-
String(address + nonce)
99+
String(address + nonce),
100+
task.chainId
97101
)
98102
if (!isValid) {
99103
return {

src/components/core/utils/nonceHandler.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ export async function checkNonce(
120120
consumer: string,
121121
nonce: number,
122122
signature: string,
123-
message: string
123+
message: string,
124+
chainId?: string | null
124125
): Promise<NonceResponse> {
125126
try {
126127
// get nonce from db
@@ -135,7 +136,8 @@ export async function checkNonce(
135136
previousNonce, // will return 0 if none exists
136137
consumer,
137138
signature,
138-
message // String(ddoId + nonce)
139+
message, // String(ddoId + nonce)
140+
chainId
139141
)
140142
if (validate.valid) {
141143
const updateStatus = await updateNonce(db, consumer, nonce)
@@ -182,7 +184,8 @@ async function validateNonceAndSignature(
182184
existingNonce: number,
183185
consumer: string,
184186
signature: string,
185-
message: string = null
187+
message: string = null,
188+
chainId?: string | null
186189
): Promise<NonceResponse> {
187190
if (nonce <= existingNonce) {
188191
return {
@@ -217,10 +220,10 @@ async function validateNonceAndSignature(
217220
// Try ERC-1271 (smart account) validation
218221
try {
219222
const config = await getConfiguration()
220-
const firstChainId = Object.keys(config?.supportedNetworks || {})[0]
221-
if (firstChainId) {
223+
const targetChainId = chainId || Object.keys(config?.supportedNetworks || {})[0]
224+
if (targetChainId && config?.supportedNetworks?.[targetChainId]) {
222225
const provider = new ethers.JsonRpcProvider(
223-
config.supportedNetworks[firstChainId].rpc
226+
config.supportedNetworks[targetChainId].rpc
224227
)
225228

226229
// Try custom hash format (for backward compatibility)

src/components/database/AuthTokenDatabase.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface AuthToken {
1111
created: Date
1212
validUntil: Date | null
1313
isValid: boolean
14+
chainId?: string | null
1415
}
1516

1617
export class AuthTokenDatabase extends AbstractDatabase {
@@ -36,9 +37,10 @@ export class AuthTokenDatabase extends AbstractDatabase {
3637
token: string,
3738
address: string,
3839
validUntil: number | null = null,
39-
createdAt: number
40+
createdAt: number,
41+
chainId?: string | null
4042
): Promise<string> {
41-
await this.provider.createToken(token, address, createdAt, validUntil)
43+
await this.provider.createToken(token, address, createdAt, validUntil, chainId)
4244
return token
4345
}
4446

src/components/database/sqliteAuthToken.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ interface AuthTokenDatabaseProvider {
77
token: string,
88
address: string,
99
createdAt: number,
10-
validUntil: number | null
10+
validUntil: number | null,
11+
chainId?: string | null
1112
): Promise<void>
1213
validateTokenEntry(token: string): Promise<AuthToken | null>
1314
invalidateTokenEntry(token: string): Promise<void>
@@ -27,22 +28,32 @@ export class SQLiteAuthToken implements AuthTokenDatabaseProvider {
2728
address TEXT NOT NULL,
2829
createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
2930
validUntil DATETIME,
30-
isValid BOOLEAN DEFAULT TRUE
31+
isValid BOOLEAN DEFAULT TRUE,
32+
chainId TEXT
3133
)
3234
`)
35+
36+
// Migration: Add chainId column if it doesn't exist
37+
return new Promise<void>((resolve) => {
38+
this.db.run(`ALTER TABLE authTokens ADD COLUMN chainId TEXT`, (_err) => {
39+
// Ignore error if column already exists
40+
resolve()
41+
})
42+
})
3343
}
3444

3545
createToken(
3646
token: string,
3747
address: string,
3848
createdAt: number,
39-
validUntil: number | null = null
49+
validUntil: number | null = null,
50+
chainId?: string | null
4051
): Promise<void> {
4152
const insertSQL = `
42-
INSERT INTO authTokens (token, address, createdAt, validUntil) VALUES (?, ?, ?, ?)
53+
INSERT INTO authTokens (token, address, createdAt, validUntil, chainId) VALUES (?, ?, ?, ?, ?)
4354
`
4455
return new Promise<void>((resolve, reject) => {
45-
this.db.run(insertSQL, [token, address, createdAt, validUntil], (err) => {
56+
this.db.run(insertSQL, [token, address, createdAt, validUntil, chainId], (err) => {
4657
if (err) {
4758
DATABASE_LOGGER.error(`Error creating auth token: ${err}`)
4859
reject(err)

src/components/httpRoutes/auth.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ authRoutes.post(
1515
express.json(),
1616
async (req, res) => {
1717
try {
18-
const { signature, address, nonce, validUntil } = req.body
18+
const { signature, address, nonce, validUntil, chainId } = req.body
1919

2020
if (!signature || !address) {
2121
return res.status(400).json({ error: 'Missing required parameters' })
@@ -26,7 +26,8 @@ authRoutes.post(
2626
signature,
2727
address,
2828
nonce,
29-
validUntil
29+
validUntil,
30+
chainId
3031
})
3132

3233
if (response.status.error) {
@@ -49,7 +50,7 @@ authRoutes.post(
4950
express.json(),
5051
async (req, res) => {
5152
try {
52-
const { signature, address, nonce, token } = req.body
53+
const { signature, address, nonce, token, chainId } = req.body
5354

5455
if (!signature || !address || !token) {
5556
return res.status(400).json({ error: 'Missing required parameters' })
@@ -60,7 +61,8 @@ authRoutes.post(
6061
signature,
6162
address,
6263
nonce,
63-
token
64+
token,
65+
chainId
6466
})
6567

6668
if (response.status.error) {

0 commit comments

Comments
 (0)