@@ -409,39 +409,25 @@ class Config:
409
409
#
410
410
# r = req.post('/repos/sociomantic-tsunami/test/labels/', name=name, color=color)
411
411
#
412
- # The basic auth has priority over oauthtoken for authentication. If you want
413
- # to use OAuth just leave `basic_auth` and set `oauthtoken`. To fill the
414
- # `basic_auth` member, the `set_basic_auth()` convenient method is provided).
415
- #
416
- # See https://developer.github.com/ for more details on the GitHub API
412
+ # See https://docs.github.com/en/free-pro-team@latest/rest for more details on
413
+ # the GitHub API
417
414
class RequestManager :
418
415
419
- basic_auth = None
420
416
oauthtoken = None
421
417
links_re = re .compile (r'<([^>]+)>;.*rel=[\'"]?([^"]+)[\'"]?' , re .M )
422
418
423
- def __init__ (self , base_url , oauthtoken = None ,
424
- username = None , password = None ):
419
+ def __init__ (self , base_url , oauthtoken = None ):
425
420
self .base_url = base_url
426
421
if oauthtoken is not None :
427
422
self .oauthtoken = oauthtoken
428
- elif username is not None :
429
- self .set_basic_auth (username , password )
430
-
431
- # Configure the class to use basic authentication instead of OAuth
432
- def set_basic_auth (self , username , password ):
433
- self .basic_auth = b"Basic " + base64 .urlsafe_b64encode (
434
- _encode ("%s:%s" % (username , password )))
435
423
436
424
# Open an URL in an authenticated manner using the specified HTTP
437
425
# method. It also add other convenience headers, like Content-Type,
438
426
# Accept (both to json) and Content-Length).
439
427
def auth_urlopen (self , url , method , data , media_type ):
440
428
req = urllib .request .Request (url , _encode (data ))
441
- if self .basic_auth :
442
- req .add_header ("Authorization" , self .basic_auth )
443
- elif self .oauthtoken :
444
- req .add_header ("Authorization" , "bearer " + self .oauthtoken )
429
+ if self .oauthtoken :
430
+ req .add_header ("Authorization" , "token " + self .oauthtoken )
445
431
req .add_header ("Content-Type" , "application/json" )
446
432
req .add_header ("Accept" , media_type )
447
433
req .add_header ("Content-Length" , str (len (data ) if data else 0 ))
@@ -695,8 +681,8 @@ class SetupCmd (object):
695
681
"will be searched and used instead, if found (for "
696
682
"this to work the e-mail must be part of the public "
697
683
"profile)" )
698
- parser .add_argument ('-p ' , '--password ' ,
699
- help = "GitHub's password (will not be stored) " )
684
+ parser .add_argument ('-o ' , '--oauthtoken' , metavar = 'TOKEN ' ,
685
+ help = "GitHub Personal Access Token (PAT) to use " )
700
686
parser .add_argument ('-b' , '--baseurl' , metavar = 'URL' ,
701
687
help = "GitHub's base URL to use to access the API "
702
688
"(Enterprise servers usually use https://host/api/v3)" )
@@ -726,62 +712,41 @@ class SetupCmd (object):
726
712
infof ("Using Git repository local configuration" )
727
713
728
714
username = args .username
729
- password = args .password
730
- if (username is None or password is None ) and \
731
- not sys .stdin .isatty ():
732
- die ("Can't perform an interactive setup outside a tty" )
733
715
if username is None :
734
- username = config .username or getpass . getuser ()
716
+ username = config .username
735
717
reply = user_input ('GitHub username [%s]: ' % username )
736
718
if reply :
737
719
username = reply
738
- if password is None :
720
+
721
+ oauthtoken = args .oauthtoken
722
+ if oauthtoken is None :
723
+ infof ("\n You need to use a GitHub Personal Access Token to do "
724
+ "almost anything useful. To create one you can go to: "
725
+ "https://github.com/settings/tokens/new.\n More help at: "
726
+ "https://docs.github.com/en/free-pro-team@latest/github/"
727
+ "authenticating-to-github/creating-a-personal-access-token.\n " )
739
728
try :
740
- password = getpass .getpass ('GitHub '
741
- 'password (will not be stored): ' )
742
- except EOFError :
729
+ oauthtoken = getpass .getpass ('GitHub token: ' )
730
+ except EOFError as e :
743
731
sys .stdout .write ('\n ' )
744
- password = ''
732
+ warnf ("Couldn't read Personal Access Token: {}" , e )
733
+ if oauthtoken == "" :
734
+ oauthtoken = None
735
+ warnf ("Token will not be used because it's empty." )
736
+
745
737
if '@' in username :
746
738
infof ("E-mail used to authenticate, trying to "
747
739
"retrieve the GitHub username..." )
748
740
username = cls .find_username (username )
749
741
infof ("Found: {}" , username )
750
742
751
- req .set_basic_auth (username , password )
752
-
753
- note = 'git-hub'
754
- if not is_global and config .forkrepo :
755
- proj = config .forkrepo .split ('/' , 1 )[1 ]
756
- note += ' (%s)' % proj
757
-
758
- while True :
759
- infof ("Looking for GitHub authorization token..." )
760
- auths = dict (
761
- [(a ['note' ], a ) for a in req .get ('/authorizations' )])
762
-
763
- if note not in auths :
764
- break
765
-
766
- errf ("The OAuth token with name '{}' already exists." , note )
767
- infof ("If you want to create a new one, enter a "
768
- "name for it. Otherwise you can go to "
769
- "https://github.com/settings/tokens to "
770
- "regenerate or delete the token '{}'" , note )
771
- note = user_input ("Enter a new token name (an empty "
772
- "name cancels the setup): " )
773
-
774
- if not note :
775
- sys .exit (0 )
776
-
777
- infof ("Creating auth token '{}'" , note )
778
- auth = req .post ('/authorizations' , note = note ,
779
- scopes = ['user' , 'repo' ])
780
-
781
- set_config = lambda k , v : git_config (k , value = v , opts = args .opts )
743
+ def set_config (k , v ):
744
+ git_config (k , value = v , opts = args .opts )
745
+ infof ("Saved git config {}{}" , GIT_CONFIG_PREFIX , k )
782
746
783
747
set_config ('username' , username )
784
- set_config ('oauthtoken' , auth ['token' ])
748
+ if oauthtoken is not None :
749
+ set_config ('oauthtoken' , oauthtoken )
785
750
if args .baseurl is not None :
786
751
set_config ('baseurl' , args .baseurl )
787
752
0 commit comments