]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:bug: Fix utility OAuth2PasswordRequestForm to use forms
authorSebastián Ramírez <tiangolo@gmail.com>
Mon, 24 Dec 2018 05:34:28 +0000 (09:34 +0400)
committerSebastián Ramírez <tiangolo@gmail.com>
Mon, 24 Dec 2018 05:34:28 +0000 (09:34 +0400)
and be used as a dependency

fastapi/security/oauth2.py

index d915af184a6c5b5ccaefa2834c90150c25d6dd10..d30f6c760f2c28c4608a312f630fded6fb324238 100644 (file)
@@ -1,6 +1,7 @@
 from typing import List, Optional
 
 from fastapi.openapi.models import OAuth2 as OAuth2Model, OAuthFlows as OAuthFlowsModel
+from fastapi.params import Form
 from fastapi.security.base import SecurityBase
 from pydantic import BaseModel, Schema
 from starlette.exceptions import HTTPException
@@ -18,12 +19,12 @@ class OAuth2PasswordRequestData(BaseModel):
     client_secret: Optional[str] = None
 
 
-class OAuth2PasswordRequestForm(BaseModel):
+class OAuth2PasswordRequestForm:
     """
-    This is not a "Security" model. Use it as request Body. As in:
+    This is a dependency class, use it like:
 
         @app.post("/login")
-        def login(form_data: Oauth2PasswordRequestForm):
+        def login(form_data: Oauth2PasswordRequestForm = Depends()):
             data = form_data.parse()
             print(data.username)
             print(data.password)
@@ -54,13 +55,21 @@ class OAuth2PasswordRequestForm(BaseModel):
     It has the method parse() that returns a model with all the same data and the scopes extracted as a list of strings.
     """
 
-    grant_type: str = Schema(None, regex="password")
-    username: str
-    password: str
-    scope: str = ""
-    # Client ID and secret might come from headers
-    client_id: Optional[str] = None
-    client_secret: Optional[str] = None
+    def __init__(
+        self,
+        grant_type: str = Form(None, regex="password"),
+        username: str = Form(...),
+        password: str = Form(...),
+        scope: str = Form(""),
+        client_id: Optional[str] = Form(None),
+        client_secret: Optional[str] = Form(None),
+    ):
+        self.grant_type = grant_type
+        self.username = username
+        self.password = password
+        self.scope = scope
+        self.client_id = client_id
+        self.client_secret = client_secret
 
     def parse(self) -> OAuth2PasswordRequestData:
         return OAuth2PasswordRequestData(