Skip to content

Commit

Permalink
Use oauthlib to create code challenge and verifier for PKCE
Browse files Browse the repository at this point in the history
  • Loading branch information
Harmon758 committed Feb 7, 2022
1 parent fc0d967 commit eb22416
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions tweepy/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
# Copyright 2009-2022 Joshua Roesslein
# See LICENSE for details.

from base64 import urlsafe_b64encode
from hashlib import sha256
import logging
import secrets
import warnings

import requests
Expand Down Expand Up @@ -205,13 +202,11 @@ def __init__(self, *, client_id, redirect_uri, scope, client_secret=None):

def get_authorization_url(self):
"""Get the authorization URL to redirect the user to"""
self.code_verifier = secrets.token_urlsafe(128)[:128]
code_challenge = urlsafe_b64encode(
sha256(self.code_verifier.encode("ASCII")).digest()
).rstrip(b'=')
authorization_url, state = self.authorization_url(
"https://twitter.com/i/oauth2/authorize",
code_challenge=code_challenge, code_challenge_method="S256"
code_challenge=self._client.create_code_challenge(
self._client.create_code_verifier(128), "S256"
), code_challenge_method="S256"
)
return authorization_url

Expand All @@ -224,5 +219,5 @@ def fetch_token(self, authorization_response):
authorization_response=authorization_response,
auth=self.auth,
include_client_id=True,
code_verifier=self.code_verifier
code_verifier=self._client.code_verifier
)

0 comments on commit eb22416

Please sign in to comment.