Skip to content

Commit 8b5cb8e

Browse files
author
Daniele Briggi
committed
feat(token): support auth with access token
1 parent ada25bd commit 8b5cb8e

File tree

5 files changed

+33
-18
lines changed

5 files changed

+33
-18
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sqlitecloud/drivers",
3-
"version": "1.0.455",
3+
"version": "1.0.491",
44
"description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

src/drivers/utilities.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,46 +40,47 @@ export function anonimizeError(error: Error): Error {
4040
export function getInitializationCommands(config: SQLiteCloudConfig): string {
4141
// we check the credentials using non linearizable so we're quicker
4242
// then we bring back linearizability unless specified otherwise
43-
let commands = 'SET CLIENT KEY NONLINEARIZABLE TO 1; '
43+
let commands = 'SET CLIENT KEY NONLINEARIZABLE TO 1;'
4444

45-
// TODO: include authentication via token
4645
// first user authentication, then all other commands
4746
if (config.apikey) {
48-
commands += `AUTH APIKEY ${config.apikey}; `
47+
commands += `AUTH APIKEY ${config.apikey};`
48+
} else if (config.token) {
49+
commands += `AUTH TOKEN ${config.token};`
4950
} else {
50-
commands += `AUTH USER ${config.username || ''} ${config.password_hashed ? 'HASH' : 'PASSWORD'} ${config.password || ''}; `
51+
commands += `AUTH USER ${config.username || ''} ${config.password_hashed ? 'HASH' : 'PASSWORD'} ${config.password || ''};`
5152
}
5253

5354
if (config.compression) {
54-
commands += 'SET CLIENT KEY COMPRESSION TO 1; '
55+
commands += 'SET CLIENT KEY COMPRESSION TO 1;'
5556
}
5657
if (config.zerotext) {
57-
commands += 'SET CLIENT KEY ZEROTEXT TO 1; '
58+
commands += 'SET CLIENT KEY ZEROTEXT TO 1;'
5859
}
5960
if (config.noblob) {
60-
commands += 'SET CLIENT KEY NOBLOB TO 1; '
61+
commands += 'SET CLIENT KEY NOBLOB TO 1;'
6162
}
6263
if (config.maxdata) {
63-
commands += `SET CLIENT KEY MAXDATA TO ${config.maxdata}; `
64+
commands += `SET CLIENT KEY MAXDATA TO ${config.maxdata};`
6465
}
6566
if (config.maxrows) {
66-
commands += `SET CLIENT KEY MAXROWS TO ${config.maxrows}; `
67+
commands += `SET CLIENT KEY MAXROWS TO ${config.maxrows};`
6768
}
6869
if (config.maxrowset) {
69-
commands += `SET CLIENT KEY MAXROWSET TO ${config.maxrowset}; `
70+
commands += `SET CLIENT KEY MAXROWSET TO ${config.maxrowset};`
7071
}
7172

7273
// we ALWAYS set non linearizable to 1 when we start so we can be quicker on login
7374
// but then we need to put it back to its default value if "linearizable" unless set
7475
if (!config.non_linearizable) {
75-
commands += 'SET CLIENT KEY NONLINEARIZABLE TO 0; '
76+
commands += 'SET CLIENT KEY NONLINEARIZABLE TO 0;'
7677
}
7778

7879
if (config.database) {
7980
if (config.create && !config.memory) {
80-
commands += `CREATE DATABASE ${config.database} IF NOT EXISTS; `
81+
commands += `CREATE DATABASE ${config.database} IF NOT EXISTS;`
8182
}
82-
commands += `USE DATABASE ${config.database}; `
83+
commands += `USE DATABASE ${config.database};`
8384
}
8485

8586
return commands

test/connection-tls.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ it(
174174
expect(error).toBeDefined()
175175
expect(error).toBeInstanceOf(SQLiteCloudError)
176176
const sqliteCloudError = error as SQLiteCloudError
177-
expect(sqliteCloudError.message).toBe('The user, password and host arguments or the ?apikey= must be specified.')
177+
expect(sqliteCloudError.message).toBe('The user, password and host arguments, the ?apikey= or the ?token= must be specified.')
178178
expect(sqliteCloudError.errorCode).toBe('ERR_MISSING_ARGS')
179179
expect(sqliteCloudError.externalErrorCode).toBeUndefined()
180180
expect(sqliteCloudError.offsetCode).toBeUndefined()

test/utilities.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44

55
import { SQLiteCloudError } from '../src/index'
6-
import { parseconnectionstring, sanitizeSQLiteIdentifier } from '../src/drivers/utilities'
6+
import { getInitializationCommands, parseconnectionstring, sanitizeSQLiteIdentifier } from '../src/drivers/utilities'
77
import { getTestingDatabaseName } from './shared'
88

99
import { expect, describe, it } from '@jest/globals'
@@ -190,3 +190,17 @@ describe('sanitizeSQLiteIdentifier()', () => {
190190
expect(sanitized).toBe('"chinook.sql; DROP TABLE \"\"albums\"\""')
191191
})
192192
})
193+
194+
describe('getInitializationCommands()', () => {
195+
it('should return commands with auth token command', () => {
196+
const config = {
197+
token: 'mytoken',
198+
database: 'mydb',
199+
}
200+
201+
const result = getInitializationCommands(config)
202+
203+
expect(result).toContain('AUTH TOKEN mytoken;')
204+
expect(result).not.toContain('AUTH APIKEY')
205+
})
206+
})

0 commit comments

Comments
 (0)