Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Apply suggestions from code review
  • Loading branch information
PythonCoderAS committed May 7, 2020
commit 50742ff9f84080b96542a28a30a3aa14a1199fda
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Unreleased

* When calling :meth:`.Session.request`, we add the key-value pair
``"api_type": "json"`` to the ``json`` parameter, if it is a ``dict``.
* :class:`.ScriptAuthorizer` has a new parameter ``otp_function`` that supplies
* :class:`.ScriptAuthorizer` has a new parameter ``two_factor_callback `` that supplies
OTPs (One-Time Passcodes) every hour.

1.3.0 (2020-04-23)
Expand Down
16 changes: 9 additions & 7 deletions prawcore/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,29 +331,31 @@ class ScriptAuthorizer(Authorizer):

AUTHENTICATOR_CLASS = TrustedAuthenticator

def __init__(self, authenticator, username, password, otp_function=None):
def __init__(
self, authenticator, username, password, two_factor_callback=None
):
"""Represent a single personal-use authorization to Reddit's API.

:param authenticator: An instance of :class:`TrustedAuthenticator`.
:param username: The Reddit username of one of the application's
developers.
:param password: The password associated with ``username``.
:param otp_function: A function that returns OTPs (One-Time Passcodes),
also known as 2FA auth codes. If this function is provided,
prawcore will call it when authenticating, each time it needs
an OTP code.
:param two_factor_callback: A function that returns OTPs (One-Time
Passcodes), also known as 2FA auth codes. If this function is
provided, prawcore will call it when authenticating, each time it
needs an OTP code.

"""
super(ScriptAuthorizer, self).__init__(authenticator)
self._username = username
self._password = password
self._otp_function = otp_function
self._two_factor_callback = two_factor_callback

def refresh(self):
"""Obtain a new personal-use script type access token."""
self._request_token(
grant_type="password",
username=self._username,
password=self._password,
otp=self._otp_function and self._otp_function(),
otp=self._two_factor_callback and self._two_factor_callback(),
)