Skip to content

User and password authentication #2

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

Open
wants to merge 2 commits into
base: master
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
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,27 @@ from nocodb.infra.requests_client import NocoDBRequestsClient

# Usage with API Token
client = NocoDBRequestsClient(
# Your nocodb root path
"http://localhost:8080",
# Your API Token retrieved from NocoDB conf
APIToken("YOUR-API-TOKEN"),
# Your nocodb root path
"http://localhost:8080"
)

# Usage with JWT Token
client = NocoDBRequestsClient(
# Your nocodb root path
"http://localhost:8080",
# Your API Token retrieved from NocoDB conf
JWTAuthToken("your.jwt.token"),
)

# Usage with email and password
client = NocoDBRequestsClient(
# Your nocodb root path
"http://localhost:8080"
"http://localhost:8080",
# Your profile credentials
email="[email protected]",
password="123qwe123!"
)
```

Expand Down
11 changes: 11 additions & 0 deletions nocodb/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@

class NocoDBAPIUris(Enum):
V1_DB_DATA_PREFIX = "api/v1/db/data"
V1_AUTH_USER_PREFIX = "api/v1/auth/user"


class NocoDBAPI:
def __init__(self, base_uri: str):
self.__base_data_uri = (
f"{base_uri}/{NocoDBAPIUris.V1_DB_DATA_PREFIX.value}"
)
self.__base_auth_uri = (
f"{base_uri}/{NocoDBAPIUris.V1_AUTH_USER_PREFIX.value}"
)
def get_auth_uri(self):
return "/".join(
(
self.__base_auth_uri,
"signin"
)
)

def get_table_uri(self, project: NocoDBProject, table: str) -> str:
return "/".join(
Expand Down
29 changes: 25 additions & 4 deletions nocodb/infra/requests_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Optional
from ..nocodb import (
JWTAuthToken,
NocoDBClient,
NocoDBProject,
AuthToken,
Expand All @@ -12,13 +13,33 @@


class NocoDBRequestsClient(NocoDBClient):
def __init__(self, auth_token: AuthToken, base_uri: str):
def __init__(
self,
base_uri: str,
auth_token: AuthToken = None,
email: str = None,
password: str = None,
):
self.__session = requests.Session()
self.__api_info = NocoDBAPI(base_uri)

if not auth_token and not (email and password):
raise ValueError("Either сredentials or token must be provided")

if not auth_token and (email and password):
auth_token = JWTAuthToken(self.get_auth_token(email, password))
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not sure about having a HTTP request in the NocoDBRequestsClient initialization. I suggest creating a class with the purpose of retrieving the AuthToken.

classDiagram
class NocoDBAuthClient {
  << interface >>
   +get_auth_token() AuthToken
}


class NocoDBAuthRequestsClient {
   +get_auth_token() AuthToken
}

NocoDBAuthClient <|-- NocoDBAuthRequestsClient

Loading

Usage example:

auth_client = NocoDBAuthRequestsClient("username", "password")
auth_token = auth_client.get_auth_token()
client = NocoDBRequestsClient(base_uri, auth_token)


self.__session.headers.update(
auth_token.get_header(),
)
auth_token.get_header(),
Copy link
Owner

Choose a reason for hiding this comment

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

This change has nothing to do with the commit.

Suggested change
auth_token.get_header(),
auth_token.get_header(),

)
self.__session.headers.update({"Content-Type": "application/json"})
self.__api_info = NocoDBAPI(base_uri)

def get_auth_token(self, email: str, password: str) -> str:
auth_token = self.__session.post(
self.__api_info.get_auth_uri(),
json=dict(email=email, password=password)
).json()['token']
return auth_token

def table_row_list(
self,
Expand Down