diff --git a/package.json b/package.json index 2630d5b1..3e6f12f7 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.2.7", + "version": "0.2.8", "name": "trino-client", "description": "Trino client library", "author": { diff --git a/src/index.ts b/src/index.ts index 1928aaf3..8f25c4af 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,6 +35,12 @@ export class BasicAuth implements Auth { constructor(readonly username: string, readonly password?: string) {} } +export class JwtAuth implements Auth { + readonly type: AuthType = 'bearer'; + constructor(readonly jwt: string) {} +} + + export type Session = {[key: string]: string}; export type ExtraCredential = {[key: string]: string}; @@ -170,9 +176,9 @@ const cleanHeaders = (headers: RawAxiosRequestHeaders) => { }; /* It's a wrapper around the Axios library that adds some Trino specific headers to the requests */ -class Client { +export class Client { private constructor( - private readonly clientConfig: AxiosRequestConfig, + readonly clientConfig: AxiosRequestConfig, private readonly options: ConnectionOptions ) {} @@ -205,6 +211,10 @@ class Client { headers[TRINO_USER_HEADER] = basic.username; } + else if (options.auth && options.auth.type === 'bearer') { + const jwt: JwtAuth = options.auth; + headers.Authorization = `Bearer ${jwt.jwt}`; + } clientConfig.headers = cleanHeaders(headers); diff --git a/tests/it/client.spec.ts b/tests/it/client.spec.ts index 1510d032..010adcc5 100644 --- a/tests/it/client.spec.ts +++ b/tests/it/client.spec.ts @@ -1,4 +1,4 @@ -import {BasicAuth, QueryData, Trino} from '../../src'; +import {BasicAuth, JwtAuth, QueryData, Trino, Client} from '../../src'; const allCustomerQuery = 'select * from customer'; const limit = 1; @@ -175,4 +175,23 @@ describe('trino', () => { ]); expect(sales).toHaveLength(limit); }); + + test.concurrent('Check JWT authentication', async () => { + const trino = Trino.create({ + catalog: 'tpcds', + schema: 'sf100000', + auth: new JwtAuth('test-jwt-token'), + }); + const query = await trino.query("select 1"); + }); + + test.concurrent('Check Client has correct auth for JWT', async () => { + const client = Client.create({ + catalog: 'tpcds', + schema: 'sf100000', + auth: new JwtAuth('test-jwt-token'), + }); + const actualAuth = client.clientConfig.headers?.Authorization + expect(actualAuth).toBe('Bearer test-jwt-token'); + }); });