Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ cur.execute('SELECT * FROM system.runtime.nodes')
rows = cur.fetchall()
```

# JWT Authentication
The `JWTAuthentication` can be used to connect to a JWT token authentication enabled Trino cluster:
```python
import trino
JWT_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiSm9obiBEb2UifQ.DjwRE2jZhren2Wt37t5hlVru6Myq4AhpGLiiefF69u8'
conn = trino.dbapi.connect(
host='coordinator-url',
port=8443,
user='the-user',
catalog='the-catalog',
schema='the-schema',
http_scheme='https',
auth=trino.auth.JWTAuthentication(JWT_TOKEN),
)
cur = conn.cursor()
cur.execute('SELECT * FROM system.runtime.nodes')
rows = cur.fetchall()
```

# Transactions
The client runs by default in *autocommit* mode. To enable transactions, set
*isolation_level* to a value different than `IsolationLevel.AUTOCOMMIT`:
Expand Down
36 changes: 36 additions & 0 deletions trino/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from six import with_metaclass
from typing import Any, Optional, Text # NOQA
from requests.auth import AuthBase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's out of scope of this PR, but we should define an ALL here, so that from trino.auth import * doesn't pull things like AuthBase... or we say we don't care.



class Authentication(with_metaclass(abc.ABCMeta)): # type: ignore
Expand Down Expand Up @@ -131,3 +132,38 @@ def get_exceptions(self):

def handle_error(self, handle_error):
pass


class _BearerAuth(AuthBase):
"""
Custom implementation of Authentication class for bearer token
"""
def __init__(self, token):
self.token = token

def __call__(self, r):
r.headers["Authorization"] = "Bearer " + self.token
return r


class JWTAuthentication(Authentication):

def __init__(self, token):
self.token = token

def set_client_session(self, client_session):
pass

def set_http_session(self, http_session):
http_session.auth = _BearerAuth(self.token)
return http_session

def setup(self, trino_client):
self.set_client_session(trino_client.client_session)
self.set_http_session(trino_client.http_session)

def get_exceptions(self):
return ()

def handle_error(self, handle_error):
pass
Comment on lines +165 to +169
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is default behavior of Authentication, so seems can be omitted here