question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Can OAuth login via Json data instead of Form?

See original GitHub issue

Description

I want to use the jwt auth, and learn the tutorial about it. There are sth confused me:

@router.post('/token')
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
    user = await authenticate_user(form_data.username, form_data.password)
    if not user:
        raise HTTPException(status_code=400, detail='incorrect username')
    access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
    access_token = await create_access_token(data={'sub': user.username}, expires_delta=access_token_expires)
    return {'access_token': access_token, 'token_type': 'bearer'}

If login success, the token will be returned , and next time you send other request need auth like

oauth2_scheme = OAuth2PasswordBearer(tokenUrl='/token')
async def get_current_user(token: str=Depends(oauth2_scheme)):
    pass
  • How does it works the next request was added a header with Authorization: Bear automically? Where does the token returned stored?
  • When I replace the Form with json, the following requests after login are failing to be with Auth Header
async def login(user_data: schemas.UserLogin):
    pass

I am sorry about my English and less experience about auth in Web, wish you could solve my confusion. Thanks in advance.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
dmig-alarstudioscommented, May 20, 2020

Look inside the OAuth2PasswordRequestForm – form fields are described there. You can replace this class with your own, accepting application/json instead of application/x-www-form-urlencoded.

0reactions
yashp280793commented, Dec 14, 2022

@dmig-alarstudios I have a requirement where frontend is posting content using content-type = “application/json”. Due to OAuth2PasswordRequestForm,I am getting error as “username field required”.

Is there a way to add support of application/json or any middleware to convert incoming json to www-form-urlencoded? I am in situation that either frontend works (with custom class) or swagger works with OAuth2PasswordRequestForm.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Return RESTful/json response instead of login form in Spring ...
When I do a get request using the browser or postman, I receive back the default spring HTML login form. security.basic.enabled=true. I'm using...
Read more >
Using OAuth 2.0 for Server to Server Applications | Authorization
This document describes how an application can complete the server-to-server OAuth 2.0 flow by using either a Google APIs client library ( ...
Read more >
[SOLVED] Receiving oAuth POST Response - Comes back as ...
BigCommerce Developers — Morgan Wowk (Partner) asked a question. ... [SOLVED] Receiving oAuth POST Response - Comes back as BC Login instead of...
Read more >
What the Heck is OAuth? - Okta Developer
The OAuth spec doesn't define what a token is. It can be in whatever format you want. Usually though, you want these tokens...
Read more >
JSON Web Token Tutorial using AngularJS & Laravel - Toptal
(You can use a JSON formatter tool to prettify the JSON object.) ... the username and password data from the sign-in and sign-up...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found