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

refactor: define provider based on auth backends #4539

Conversation

frascuchon
Copy link
Member

@frascuchon frascuchon commented Jan 26, 2024

Description

This PR is a refactor of the authentication provider. A new authentication provider is created based on the Starlette Authentication Backends. This change simplifies the auth. workflow and prepare incoming oauth integration.

Since the authentication backends should be used by the auth middleware component, the implementation done in this PR does not use it in that way since there are some problems exposing the db session at the middleware level (using global dependencies does not work).

Type of change

(Please delete options that are not relevant. Remember to title the PR according to the type of change)

  • New feature (non-breaking change which adds functionality)
  • Refactor (change restructuring the codebase without changing functionality)
  • Improvement (change adding some improvement to an existing functionality)

How Has This Been Tested

(Please describe the tests that you ran to verify your changes. And ideally, reference tests)

Running locally

  • Test A
  • Test B

Checklist

  • I added relevant documentation
  • I followed the style guidelines of this project
  • I did a self-review of my code
  • I made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • I filled out the contributor form (see text above)
  • I have added relevant notes to the CHANGELOG.md file (See https://keepachangelog.com/)

@frascuchon frascuchon self-assigned this Jan 26, 2024
@frascuchon frascuchon changed the title Refactor/define provider based on auth backends refactor: define provider based on auth backends Jan 26, 2024
@frascuchon frascuchon marked this pull request as ready for review January 26, 2024 14:26
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. area: server Indicates that an issue or pull request is related to the server language: python Pull requests or issues that update Python code team: backend Indicates that the issue or pull request is owned by the backend team type: refactor Indicates internal refactoring of the code-base labels Jan 26, 2024
@frascuchon frascuchon force-pushed the refactor/define-provider-based-on-auth-backends branch from 3d428ae to 6170fd0 Compare January 26, 2024 14:28
Copy link

The URL of the deployed environment for this PR is https://argilla-quickstart-pr-4539-ki24f765kq-no.a.run.app

Encryption algorithm for token data

token_expiration_in_minutes:
The session token expiration in minutes. Default=30000
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
The session token expiration in minutes. Default=30000
The session token expiration in minutes. Default=15

Copy link
Member Author

Choose a reason for hiding this comment

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

This is an old comment. I will update it.

Comment on lines 33 to 36
api_key: str = await self.scheme(request)

if not api_key:
return None
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
api_key: str = await self.scheme(request)
if not api_key:
return None
api_key: str = await self.scheme(request)
if not api_key:
return None

Comment on lines 38 to 42
db = request.state.db
user = await accounts.get_user_by_api_key(db, api_key=api_key)

if not user:
return None
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
db = request.state.db
user = await accounts.get_user_by_api_key(db, api_key=api_key)
if not user:
return None
user = await accounts.get_user_by_api_key(request.state.db, api_key=api_key)
if not user:
return None

return None

return AuthCredentials(["authenticated"]), UserInfo(
username=user.username, name=user.full_name, role=user.role, identity=str(user.id)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
username=user.username, name=user.full_name, role=user.role, identity=str(user.id)
identity=str(user.id), username=user.username, name=user.full_name, role=user.role

Comment on lines 33 to 36
credentials = await self.scheme(request)

if not credentials:
return None
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
credentials = await self.scheme(request)
if not credentials:
return None
credentials = await self.scheme(request)
if not credentials:
return None

src/argilla/server/security/authentication/provider.py Outdated Show resolved Hide resolved
Comment on lines 47 to 49
if v is not None:
return v
return values["token_expiration_in_minutes"] * 60
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if v is not None:
return v
return values["token_expiration_in_minutes"] * 60
if v is not None:
return v
return values["token_expiration_in_minutes"] * 60

secret_key: str = uuid4().hex
algorithm: str = "HS256"

token_expiration_in_minutes: int = 15
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 a really small default expiration. Do we have any good reason to set such a small expiration time?

algorithm: str = "HS256"

token_expiration_in_minutes: int = 15
token_expiration: Optional[int]
Copy link
Member

Choose a reason for hiding this comment

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

I think it's a little bit confusing to have a token_expiration_in_minutes and at the same time token_expiration. I believe we should go for only token_expiration (using seconds) to avoid unnecessary complex scenarios (for example specifying values to the two settings at the same time).

@frascuchon frascuchon force-pushed the refactor/define-provider-based-on-auth-backends branch from ea37b2a to ee4953c Compare January 26, 2024 22:28
@frascuchon frascuchon force-pushed the refactor/define-provider-based-on-auth-backends branch from 3930b23 to 816ef60 Compare January 29, 2024 10:55
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Jan 29, 2024
Copy link
Member

@jfcalvo jfcalvo left a comment

Choose a reason for hiding this comment

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

Looks fine to me (apart from the too small expiration time default value that we discussed).

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jan 29, 2024
@frascuchon frascuchon merged commit cf64861 into feature/oauth-integration Jan 29, 2024
8 of 10 checks passed
@frascuchon frascuchon deleted the refactor/define-provider-based-on-auth-backends branch January 29, 2024 12:15
davidberenstein1957 added a commit that referenced this pull request Mar 11, 2024
…4639)

# Description

I'm proposing to add a mention to the var `ARGILLA_AUTH_SECRET_KEY`
introduced in #4539 to the
`docker-compose`-related documentation in
docs/_source/getting_started/installation/deployments/docker_compose.md

As far as I am aware, that env var needs to be set in order to run the
service in docker-compose.

It's a small edit, I didn't create (yet) an issue for this.

- [x] Documentation update

**How Has This Been Tested**

- [ ] `sphinx-autobuild` 

I didn't run sphinx! I had troubles w/ cloning, will update this as soon
as I can

**Checklist**

- [x] I added relevant documentation
- [ ] I followed the style guidelines of this project
- [ ] I did a self-review of my code
- [ ] I made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I filled out [the contributor form](https://tally.so/r/n9XrxK)
(see text above)
- [ ] I have added relevant notes to the `CHANGELOG.md` file (See
https://keepachangelog.com/)

---------

Co-authored-by: David Berenstein <david.m.berenstein@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: server Indicates that an issue or pull request is related to the server language: python Pull requests or issues that update Python code lgtm This PR has been approved by a maintainer size:XL This PR changes 500-999 lines, ignoring generated files. team: backend Indicates that the issue or pull request is owned by the backend team type: refactor Indicates internal refactoring of the code-base
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants