Skip to content

Commit

Permalink
pydantic + add model for login endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
eikichi18 committed May 18, 2024
1 parent 504bc94 commit cc1480f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def read(filename):
"PyJWT>=1.7.0",
"pytz",
"pyyaml",
"pyDantic",
],
extras_require={"test": TEST_REQUIRES},
entry_points="""
Expand Down
34 changes: 15 additions & 19 deletions src/plone/restapi/services/auth/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from zope import component
from zope.interface import alsoProvides

from .model import LoginData

import plone.protect.interfaces


Expand All @@ -23,13 +25,7 @@ def __restapi_doc__(cls):
"required": True,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"login": {"type": "string", "example": "admin"},
"password": {"type": "string", "example": "admin"},
},
}
"schema": LoginData.schema(),
}
},
},
Expand Down Expand Up @@ -79,28 +75,28 @@ def __restapi_doc__(cls):
}

def reply(self):
data = json_body(self.request)
if "login" not in data or "password" not in data:
self.request.response.setStatus(400)
return dict(
error=dict(
type="Missing credentials",
message="Login and password must be provided in body.",
)
)
data = LoginData(**json_body(self.request))
# if "login" not in data or "password" not in data:
# self.request.response.setStatus(400)
# return dict(
# error=dict(
# type="Missing credentials",
# message="Login and password must be provided in body.",
# )
# )

# Disable CSRF protection
if "IDisableCSRFProtection" in dir(plone.protect.interfaces):
alsoProvides(self.request, plone.protect.interfaces.IDisableCSRFProtection)

userid = data["login"]
password = data["password"]
userid = data.login
password = data.password
uf = self._find_userfolder(userid)

# Also put the password in __ac_password on the request.
# The post-login code in PlonePAS expects to find it there
# when it calls the PAS updateCredentials plugin.
self.request.form["__ac_password"] = data["password"]
self.request.form["__ac_password"] = password

if uf is not None:
plugins = uf._getOb("plugins")
Expand Down
14 changes: 14 additions & 0 deletions src/plone/restapi/services/auth/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from pydantic import BaseModel


class LoginData(BaseModel):
login: str
password: str


class TokenResponse(BaseModel):
token: str


class ErrorResponse(BaseModel):
error: dict

0 comments on commit cc1480f

Please sign in to comment.