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

feat: add more authentication information to swagger #35674

Merged

Conversation

deborahgu
Copy link
Member

@deborahgu deborahgu commented Oct 18, 2024

Description

  • updates the docs-settings to make the generated swagger securityDefinitions include both JWT and CSRF methods, as well as basic. A few linter fixes happened as a side effect.
  • Put in wordier descriptions for all three, since we don't have great shared documentation about authn/authz.
  • Added CSRF to login_session, which also serves as a proof of concept for other endpoints
  • Also regenerated the swagger doc, which picked up some extra changes.

Generated swagger now has help and allows extra auth methods so some preveiously unusable endpoints can be hit.

Testing instructions

  • verify that make swagger works
  • Plug the generated lms-openapi.yml into any OpenAPI validator or preview tool. Online tools are available at https://swagger.io/, and most IDEs have validating plugins.
  • Make sure that you can see the authentication methods and their description
  • Make sure that you can see the authentication method CSRF on login_session

Images

general authentication modal

the first page of the swagger authorizations modal, showing basic auth and JWT
The 2nd page showing CSRF

login_session authentication modal

Modal for the login session endpoint, showing only CSRF

FIXES: APER-3554

this is work in progress

FIXES: APER-3554
* updates the `docs-settings` to make the generated swagger
  `securityDefinitions` include both JWT and CSRF methods, as well as
  basic. A few linter fixes happened as a side effect.
* Put in wordier descriptions for all three, since we don't have great
  shared documentation about authn/authz.
* Added CSRF to `login_session`, which also serves as a proof of concept
  for other endpoits
* Also regenerated the swagger doc, which picked up some extra changes.

Generated swagger now has help and allows extra auth methods so some
preveiously unusable endpoints can be hit.

FIXES: APER-3554
@deborahgu deborahgu requested a review from a team as a code owner October 18, 2024 17:28
Fixed capitalization error, and ran the linter  which reformatted a lot
of the file, because github actions really didn't like some lint errors.
@@ -62,17 +65,17 @@
log = logging.getLogger("edx.student")
AUDIT_LOG = logging.getLogger("audit")
USER_MODEL = get_user_model()
PASSWORD_RESET_INITIATED = 'edx.user.passwordreset.initiated'
Copy link
Member Author

@deborahgu deborahgu Oct 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I apologize for mixing reformatter auto fixes with actual changes in a single PR. Assume everything in this file except the blocks that I am flagging with particular comments are just automatic fixes to make the linter happier.

Comment on lines +26 to +33
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from edx_django_utils.monitoring import set_custom_attribute
from eventtracking import tracker
from openedx_events.learning.data import UserData, UserPersonalData
from openedx_events.learning.signals import SESSION_LOGIN_COMPLETED
from openedx_filters.learning.filters import StudentLoginRequested
from rest_framework import status
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these lines are a real change, not just formatting.

Comment on lines +752 to +764
@swagger_auto_schema(
request_body=login_user_schema,
responses=login_user_responses,
security=[
{"csrf": []},
],
)
@method_decorator(csrf_protect)
def post(self, request, api_version):
"""Log in a user.

See `login_user` for details.

Example Usage:

POST /api/user/v1/login_session
with POST params `email`, `password`.

200 {'success': true}
"""
POST /user/{api_version}/account/login_session/

Returns 200 on success, and a detailed error message otherwise.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these changes are real, not just formatter.

Comment on lines +715 to +730
login_user_schema = openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
"email": openapi.Schema(type=openapi.TYPE_STRING),
"password": openapi.Schema(type=openapi.TYPE_STRING),
},
)

login_user_return_schema = openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
"success": openapi.Schema(type=openapi.TYPE_BOOLEAN),
"value": openapi.Schema(type=openapi.TYPE_STRING),
"error_code": openapi.Schema(type=openapi.TYPE_STRING),
},
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these changes are real, not just formatter.

Comment on lines +740 to 746
login_user_responses = {
status.HTTP_200_OK: login_user_return_schema,
status.HTTP_400_BAD_REQUEST: login_user_return_schema,
status.HTTP_403_FORBIDDEN: login_user_return_schema,
}

@method_decorator(ensure_csrf_cookie)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these changes are real, not just formatter.

@@ -6788,6 +6826,59 @@ paths:
in: path
required: true
type: string
/mobile/{api_version}/notifications/create-token/:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are unrelated changes, clearly the documents haven't been regenerated since these endpoints were created.

'token_type': 'jwt'
```

Your JWT will be returned in the response as `access_token`. Prefix with `JWT ` in your header.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the space in JWT intentional? My assumption by reading this would be to add this to my header:

JWT {access token string}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, that's correct.

@jsnwesson
Copy link
Contributor

Everything looks good to me!

the automatic reformatting broke some of the lint amnesty, so I undid
the automatic reformatting on those lines in particular.
@deborahgu deborahgu merged commit 97449ef into master Oct 28, 2024
49 checks passed
@deborahgu deborahgu deleted the dkaplan1/APER-3554_add-sample-payloads-to-user_api-api-docs branch October 28, 2024 20:34
@edx-pipeline-bot
Copy link
Contributor

2U Release Notice: This PR has been deployed to the edX staging environment in preparation for a release to production.

@edx-pipeline-bot
Copy link
Contributor

2U Release Notice: This PR has been deployed to the edX production environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants