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

14.0 login jwt user #656

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions auth_jwt_login/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
9 changes: 9 additions & 0 deletions auth_jwt_login/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "Auth JWT Login Base",
"summary": """
JWT bearer token authentication with user and password.""",
"version": "14.0.1.1.0",
"website": "https://github.com/OCA/server-auth",
"depends": ["auth_jwt"],
"external_dependencies": {"python": ["pyjwt", "cryptography"]},
}
1 change: 1 addition & 0 deletions auth_jwt_login/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import auth_jwt_validator
34 changes: 34 additions & 0 deletions auth_jwt_login/models/auth_jwt_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import time

import werkzeug

from odoo import _, fields, models
from odoo.exceptions import ValidationError


class AuthJwtValidator(models.Model):
_inherit = "auth.jwt.validator"

user_id_strategy = fields.Selection(
selection_add=[("login", "Login")],
ondelete={"login": "cascade"},
)

def _get_uid(self, payload):
if self.user_id_strategy == "login":
timestamp_expiration_date = payload["exp"]

Check warning on line 19 in auth_jwt_login/models/auth_jwt_validator.py

View check run for this annotation

Codecov / codecov/patch

auth_jwt_login/models/auth_jwt_validator.py#L19

Added line #L19 was not covered by tests
if timestamp_expiration_date:
timestamp_now = int(time.time())

Check warning on line 21 in auth_jwt_login/models/auth_jwt_validator.py

View check run for this annotation

Codecov / codecov/patch

auth_jwt_login/models/auth_jwt_validator.py#L21

Added line #L21 was not covered by tests
if (timestamp_expiration_date - timestamp_now) < 0:
raise werkzeug.exceptions.Forbidden(_("Token not valid."))

Check warning on line 23 in auth_jwt_login/models/auth_jwt_validator.py

View check run for this annotation

Codecov / codecov/patch

auth_jwt_login/models/auth_jwt_validator.py#L23

Added line #L23 was not covered by tests
if "username" in payload:
user = self.env["res.users"].search(

Check warning on line 25 in auth_jwt_login/models/auth_jwt_validator.py

View check run for this annotation

Codecov / codecov/patch

auth_jwt_login/models/auth_jwt_validator.py#L25

Added line #L25 was not covered by tests
[("login", "=", payload["username"])]
)
if not user:
raise ValidationError
return user.id

Check warning on line 30 in auth_jwt_login/models/auth_jwt_validator.py

View check run for this annotation

Codecov / codecov/patch

auth_jwt_login/models/auth_jwt_validator.py#L29-L30

Added lines #L29 - L30 were not covered by tests
else:
raise ValidationError

Check warning on line 32 in auth_jwt_login/models/auth_jwt_validator.py

View check run for this annotation

Codecov / codecov/patch

auth_jwt_login/models/auth_jwt_validator.py#L32

Added line #L32 was not covered by tests
else:
return super()._get_uid(payload)
1 change: 1 addition & 0 deletions setup/auth_jwt_login/odoo/addons/auth_jwt_login
6 changes: 6 additions & 0 deletions setup/auth_jwt_login/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
Loading