Skip to content

Commit 9dbfafd

Browse files
committed
added jwt auth
1 parent be32f72 commit 9dbfafd

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ export class BasicAuth implements Auth {
3535
constructor(readonly username: string, readonly password?: string) {}
3636
}
3737

38+
export class JwtAuth implements Auth {
39+
readonly type: AuthType = 'bearer';
40+
constructor(readonly jwt: string) {}
41+
}
42+
43+
3844
export type Session = {[key: string]: string};
3945

4046
export type ExtraCredential = {[key: string]: string};
@@ -170,9 +176,9 @@ const cleanHeaders = (headers: RawAxiosRequestHeaders) => {
170176
};
171177

172178
/* It's a wrapper around the Axios library that adds some Trino specific headers to the requests */
173-
class Client {
179+
export class Client {
174180
private constructor(
175-
private readonly clientConfig: AxiosRequestConfig,
181+
readonly clientConfig: AxiosRequestConfig,
176182
private readonly options: ConnectionOptions
177183
) {}
178184

@@ -205,6 +211,10 @@ class Client {
205211

206212
headers[TRINO_USER_HEADER] = basic.username;
207213
}
214+
else if (options.auth && options.auth.type === 'bearer') {
215+
const jwt: JwtAuth = <JwtAuth>options.auth;
216+
headers.Authorization = `Bearer ${jwt.jwt}`;
217+
}
208218

209219
clientConfig.headers = cleanHeaders(headers);
210220

tests/it/client.spec.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {BasicAuth, QueryData, Trino} from '../../src';
1+
import {BasicAuth, JwtAuth, QueryData, Trino, Client} from '../../src';
22

33
const allCustomerQuery = 'select * from customer';
44
const limit = 1;
@@ -175,4 +175,23 @@ describe('trino', () => {
175175
]);
176176
expect(sales).toHaveLength(limit);
177177
});
178+
179+
test.concurrent('Check JWT authentication', async () => {
180+
const trino = Trino.create({
181+
catalog: 'tpcds',
182+
schema: 'sf100000',
183+
auth: new JwtAuth('test-jwt-token'),
184+
});
185+
const query = await trino.query("select 1");
186+
});
187+
188+
test.concurrent('Check Client has correct auth for JWT', async () => {
189+
const client = Client.create({
190+
catalog: 'tpcds',
191+
schema: 'sf100000',
192+
auth: new JwtAuth('test-jwt-token'),
193+
});
194+
const actualAuth = client.clientConfig.headers?.Authorization
195+
expect(actualAuth).toBe('Bearer test-jwt-token');
196+
});
178197
});

0 commit comments

Comments
 (0)