-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
✨ Implement OAuth2ClientCredentials class #11560
Open
rhysrevans3
wants to merge
6
commits into
fastapi:master
Choose a base branch
from
rhysrevans3:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a493995
Adding OAuth2ClientCredentials class.
rhysrevans3 e300655
Running formatter.
rhysrevans3 cd54625
Updating release note.
rhysrevans3 55693c9
Removing refreshUrl from client credentials flow.
rhysrevans3 277ce11
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 6da2919
Merge branch 'master' into master
rhysrevans3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
tests/test_security_oauth2_authorization_client_credentials.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from typing import Optional | ||
|
||
from fastapi import FastAPI, Security | ||
from fastapi.security import OAuth2ClientCredentials | ||
from fastapi.testclient import TestClient | ||
|
||
app = FastAPI() | ||
|
||
oauth2_scheme = OAuth2ClientCredentials(tokenUrl="token", auto_error=True) | ||
|
||
|
||
@app.get("/items/") | ||
async def read_items(token: Optional[str] = Security(oauth2_scheme)): | ||
return {"token": token} | ||
|
||
|
||
client = TestClient(app) | ||
|
||
|
||
def test_no_token(): | ||
response = client.get("/items") | ||
assert response.status_code == 401, response.text | ||
assert response.json() == {"detail": "Not authenticated"} | ||
|
||
|
||
def test_incorrect_token(): | ||
response = client.get("/items", headers={"Authorization": "Non-existent testtoken"}) | ||
assert response.status_code == 401, response.text | ||
assert response.json() == {"detail": "Not authenticated"} | ||
|
||
|
||
def test_token(): | ||
response = client.get("/items", headers={"Authorization": "Bearer testtoken"}) | ||
assert response.status_code == 200, response.text | ||
assert response.json() == {"token": "testtoken"} | ||
|
||
|
||
def test_openapi_schema(): | ||
response = client.get("/openapi.json") | ||
assert response.status_code == 200, response.text | ||
assert response.json() == { | ||
"openapi": "3.1.0", | ||
"info": {"title": "FastAPI", "version": "0.1.0"}, | ||
"paths": { | ||
"/items/": { | ||
"get": { | ||
"responses": { | ||
"200": { | ||
"description": "Successful Response", | ||
"content": {"application/json": {"schema": {}}}, | ||
} | ||
}, | ||
"summary": "Read Items", | ||
"operationId": "read_items_items__get", | ||
"security": [{"OAuth2ClientCredentials": []}], | ||
} | ||
} | ||
}, | ||
"components": { | ||
"securitySchemes": { | ||
"OAuth2ClientCredentials": { | ||
"type": "oauth2", | ||
"flows": { | ||
"clientCredentials": { | ||
"tokenUrl": "token", | ||
"scopes": {}, | ||
} | ||
}, | ||
} | ||
} | ||
}, | ||
} |
77 changes: 77 additions & 0 deletions
77
tests/test_security_oauth2_authorization_client_credentials_description.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from typing import Optional | ||
|
||
from fastapi import FastAPI, Security | ||
from fastapi.security import OAuth2ClientCredentials | ||
from fastapi.testclient import TestClient | ||
|
||
app = FastAPI() | ||
|
||
oauth2_scheme = OAuth2ClientCredentials( | ||
tokenUrl="token", | ||
description="OAuth2 Client Credentials Flow", | ||
auto_error=True, | ||
) | ||
|
||
|
||
@app.get("/items/") | ||
async def read_items(token: Optional[str] = Security(oauth2_scheme)): | ||
return {"token": token} | ||
|
||
|
||
client = TestClient(app) | ||
|
||
|
||
def test_no_token(): | ||
response = client.get("/items") | ||
assert response.status_code == 401, response.text | ||
assert response.json() == {"detail": "Not authenticated"} | ||
|
||
|
||
def test_incorrect_token(): | ||
response = client.get("/items", headers={"Authorization": "Non-existent testtoken"}) | ||
assert response.status_code == 401, response.text | ||
assert response.json() == {"detail": "Not authenticated"} | ||
|
||
|
||
def test_token(): | ||
response = client.get("/items", headers={"Authorization": "Bearer testtoken"}) | ||
assert response.status_code == 200, response.text | ||
assert response.json() == {"token": "testtoken"} | ||
|
||
|
||
def test_openapi_schema(): | ||
response = client.get("/openapi.json") | ||
assert response.status_code == 200, response.text | ||
assert response.json() == { | ||
"openapi": "3.1.0", | ||
"info": {"title": "FastAPI", "version": "0.1.0"}, | ||
"paths": { | ||
"/items/": { | ||
"get": { | ||
"responses": { | ||
"200": { | ||
"description": "Successful Response", | ||
"content": {"application/json": {"schema": {}}}, | ||
} | ||
}, | ||
"summary": "Read Items", | ||
"operationId": "read_items_items__get", | ||
"security": [{"OAuth2ClientCredentials": []}], | ||
} | ||
} | ||
}, | ||
"components": { | ||
"securitySchemes": { | ||
"OAuth2ClientCredentials": { | ||
"type": "oauth2", | ||
"flows": { | ||
"clientCredentials": { | ||
"tokenUrl": "token", | ||
"scopes": {}, | ||
} | ||
}, | ||
"description": "OAuth2 Client Credentials Flow", | ||
} | ||
} | ||
}, | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we not setting the
testtoken
in a const file instead of duplicating it everywhere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I followed the existing structure of the other oauth2 tests. I agree
test_token
and possibly thetest_client
should probably be move into fixtures but I think that is beyond the scope of this pull request. I could create a new issue for "reducing code duplication in tests"?