Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support other AuthTypes in addition to BasicAuth #755

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ A [Trino](https://trino.io) client for [Node.js](https://nodejs.org/).
## Features

- Connections over HTTP or HTTPS
- Supports HTTP Basic Authentication
- Supports Basic and OAuth2 authentication types
- Per-query user information for access control

## Requirements
Expand Down Expand Up @@ -67,6 +67,17 @@ const data: QueryData[] = await iter
.fold<QueryData[]>([], (row, acc) => [...acc, ...row]);
```

### Using OAuth2 Authentication

```typescript
const trino: Trino = Trino.create({
server: 'http://localhost:8080',
catalog: 'tpcds',
schema: 'sf100000',
auth: new OAuth2Auth('token', 'clientId', 'clientSecret', 'refreshToken', 'tokenEndpoint'),
});
```

## Examples

More usage examples can be found in the
Expand Down
78 changes: 60 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ export class BasicAuth implements Auth {
constructor(readonly username: string, readonly password?: string) {}
}

export class OAuth2Auth implements Auth {
readonly type: AuthType = 'oauth2';
constructor(
readonly token: string,
readonly clientId?: string,
readonly clientSecret?: string,
readonly refreshToken?: string,
readonly tokenEndpoint?: string,
readonly scopes?: string[],
readonly tokenType?: string,
readonly expiresIn?: number,
readonly redirectUri?: string,
readonly grantType?: string
) {}
}

export type Session = {[key: string]: string};

export type ExtraCredential = {[key: string]: string};
Expand Down Expand Up @@ -143,16 +159,6 @@ export type QueryInfo = {
failureInfo?: QueryFailureInfo;
};

export type Query = {
query: string;
catalog?: string;
schema?: string;
user?: string;
session?: Session;
extraCredential?: ExtraCredential;
extraHeaders?: RequestHeaders;
};

/**
* It takes a Headers object and returns a new object with the same keys, but only the values that are
* truthy
Expand Down Expand Up @@ -196,14 +202,50 @@ class Client {
...(options.extraHeaders ?? {}),
};

if (options.auth && options.auth.type === 'basic') {
const basic: BasicAuth = <BasicAuth>options.auth;
clientConfig.auth = {
username: basic.username,
password: basic.password ?? '',
};

headers[TRINO_USER_HEADER] = basic.username;
if (options.auth) {
switch (options.auth.type) {
case 'basic':
const basic: BasicAuth = <BasicAuth>options.auth;
clientConfig.auth = {
username: basic.username,
password: basic.password ?? '',
};
headers[TRINO_USER_HEADER] = basic.username;
break;
case 'oauth2':
const oauth2: OAuth2Auth = <OAuth2Auth>options.auth;
headers['Authorization'] = `Bearer ${oauth2.token}`;
if (oauth2.clientId) {
headers['Client-Id'] = oauth2.clientId;
}
if (oauth2.clientSecret) {
headers['Client-Secret'] = oauth2.clientSecret;
}
if (oauth2.refreshToken) {
headers['Refresh-Token'] = oauth2.refreshToken;
}
if (oauth2.tokenEndpoint) {
headers['Token-Endpoint'] = oauth2.tokenEndpoint;
}
if (oauth2.scopes) {
headers['Scopes'] = oauth2.scopes.join(' ');
}
if (oauth2.tokenType) {
headers['Token-Type'] = oauth2.tokenType;
}
if (oauth2.expiresIn) {
headers['Expires-In'] = oauth2.expiresIn.toString();
}
if (oauth2.redirectUri) {
headers['Redirect-Uri'] = oauth2.redirectUri;
}
if (oauth2.grantType) {
headers['Grant-Type'] = oauth2.grantType;
}
break;
default:
throw new Error(`Unsupported auth type: ${options.auth.type}`);
}
}

clientConfig.headers = cleanHeaders(headers);
Expand Down
17 changes: 16 additions & 1 deletion tests/it/client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {BasicAuth, QueryData, Trino} from '../../src';
import {BasicAuth, OAuth2Auth, QueryData, Trino} from '../../src';

const allCustomerQuery = 'select * from customer';
const limit = 1;
Expand Down Expand Up @@ -175,4 +175,19 @@ describe('trino', () => {
]);
expect(sales).toHaveLength(limit);
});

test.concurrent('oauth2 auth', async () => {
const trino = Trino.create({
catalog: 'tpcds',
schema: 'sf100000',
auth: new OAuth2Auth('token'),
});

const iter = await trino.query(singleCustomerQuery);
const data = await iter
.map(r => r.data ?? [])
.fold<QueryData[]>([], (row, acc) => [...acc, ...row]);

expect(data).toHaveLength(limit);
});
});