|
| 1 | +import re |
| 2 | +import json |
| 3 | +from urllib import request |
| 4 | +from .nethelper import urlopen_nt |
| 5 | +try: |
| 6 | + from PySide2.QtWidgets import QDialog, QVBoxLayout, QLabel, QSizePolicy, QPushButton, QMessageBox |
| 7 | + from PySide2.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage |
| 8 | + from PySide2.QtCore import Slot, Qt, QUrl |
| 9 | +except ImportError: |
| 10 | + raise NotImplementedError('web auth implemented only for QT5. Sorry, people who still use houdini 16.5. You will have to create access token manually. contact me to ask how.') |
| 11 | + |
| 12 | +import time |
| 13 | + |
| 14 | + |
| 15 | +class QGithubDeviceAuthDialog(QDialog): |
| 16 | + def __init__(self, client_id='42e8e8e9d844e45c2d05', hint_username=None, parent=None): |
| 17 | + super(QGithubDeviceAuthDialog, self).__init__(parent=parent) |
| 18 | + self.setWindowTitle('Log into your GitHub account and enter this code') |
| 19 | + |
| 20 | + self.__webview = QWebEngineView(parent=self) |
| 21 | + self.__webview.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) |
| 22 | + self.__webview.urlChanged.connect(self.on_url_changed) |
| 23 | + |
| 24 | + self.__infolaabel = QLabel('<p style="font-size:14px">' |
| 25 | + '<br>You need to allow hpaste to modify your gists on github. for that you need to log in to your account and authorize hpaste.\n' |
| 26 | + '<br>You can do it in <b>any</b> browser, not only in this window. Just go to <a href="https://github.com/login/device">https://github.com/login/device</a> and enter the code below.\n' |
| 27 | + '<br>close this window when you are done' |
| 28 | + '</p>', parent=self) |
| 29 | + self.__infolaabel.setTextFormat(Qt.RichText) |
| 30 | + self.__infolaabel.setTextInteractionFlags(Qt.TextBrowserInteraction) |
| 31 | + self.__infolaabel.setOpenExternalLinks(True) |
| 32 | + self.__devidlabel = QLabel(parent=self) |
| 33 | + self.__devidlabel.setTextInteractionFlags(Qt.TextBrowserInteraction) |
| 34 | + ff = self.__devidlabel.font() |
| 35 | + ff.setPointSize(64) |
| 36 | + self.__devidlabel.setFont(ff) |
| 37 | + |
| 38 | + self.__reload_button = QPushButton('log in to a different account', parent=self) |
| 39 | + self.__reload_button.clicked.connect(self.reload_button_clicked) |
| 40 | + |
| 41 | + self.__layout = QVBoxLayout() |
| 42 | + self.setLayout(self.__layout) |
| 43 | + self.__layout.addWidget(self.__infolaabel) |
| 44 | + self.__layout.addWidget(self.__devidlabel) |
| 45 | + self.__layout.addWidget(self.__reload_button) |
| 46 | + self.__layout.addWidget(self.__webview) |
| 47 | + self.__result = None |
| 48 | + |
| 49 | + # self.setGeometry(512, 256, 1024, 768) |
| 50 | + self.resize(1024, 860) |
| 51 | + |
| 52 | + # init auth process |
| 53 | + self.__client_id = client_id |
| 54 | + self.__headers = {'User-Agent': 'HPaste', 'Accept': 'application/json', 'charset': 'utf-8', 'Content-Type': 'application/json'} |
| 55 | + self.__hint_username = hint_username |
| 56 | + |
| 57 | + self.__webprofile = None |
| 58 | + self.__webpage = None |
| 59 | + |
| 60 | + self.__device_code = None |
| 61 | + self.__interval = None |
| 62 | + self.__await_login_redirect = False |
| 63 | + self.reinit_code() |
| 64 | + |
| 65 | + def reinit_code(self): |
| 66 | + reqdata = {'client_id': self.__client_id, |
| 67 | + 'scope': 'gist'} |
| 68 | + req = request.Request('https://github.com/login/device/code', data=json.dumps(reqdata).encode('UTF-8'), headers=self.__headers) |
| 69 | + req.get_method = lambda: 'POST' |
| 70 | + code, ret = urlopen_nt(req) |
| 71 | + if code != 200: |
| 72 | + raise RuntimeError('code %d when trying to register device' % code) |
| 73 | + init_data = json.loads(ret.read().decode('UTF-8')) |
| 74 | + print(init_data) |
| 75 | + self.__device_code = init_data['device_code'] |
| 76 | + self.__interval = init_data.get('interval', 5) |
| 77 | + url = init_data['verification_uri'] |
| 78 | + |
| 79 | + self.__await_login_redirect = True |
| 80 | + self.__webprofile = QWebEngineProfile(parent=self.__webview) |
| 81 | + self.__webpage = QWebEnginePage(self.__webprofile, parent=self.__webview) # just to be sure they are deleted in proper order |
| 82 | + self.__webview.setPage(self.__webpage) |
| 83 | + self.__webview.load(QUrl(url)) |
| 84 | + self.__devidlabel.setText('code: %s' % (init_data['user_code'],)) |
| 85 | + |
| 86 | + def get_result(self): |
| 87 | + return self.__result |
| 88 | + |
| 89 | + def closeEvent(self, event): |
| 90 | + # here we assume user has done his part, so lets get checking |
| 91 | + for attempt in range(5): |
| 92 | + reqdata = {'client_id': self.__client_id, |
| 93 | + 'device_code': self.__device_code, |
| 94 | + 'grant_type': 'urn:ietf:params:oauth:grant-type:device_code'} |
| 95 | + req = request.Request('https://github.com/login/oauth/access_token', data=json.dumps(reqdata).encode('UTF-8'), headers=self.__headers) |
| 96 | + req.get_method = lambda: 'POST' |
| 97 | + code, ret = urlopen_nt(req) |
| 98 | + if code == 200: |
| 99 | + rep = json.loads(ret.read().decode('UTF-8')) |
| 100 | + print(rep) |
| 101 | + if 'error' not in rep: # a SUCC |
| 102 | + headers = {'User-Agent': 'HPaste', |
| 103 | + 'Authorization': 'Token %s' % rep['access_token'], |
| 104 | + 'Accept': 'application/vnd.github.v3+json'} |
| 105 | + req = request.Request(r'https://api.github.com/user', headers=headers) |
| 106 | + usercode, userrep = urlopen_nt(req) |
| 107 | + if usercode != 200: |
| 108 | + raise RuntimeError('could not probe! %d' % (usercode,)) |
| 109 | + userdata = json.loads(userrep.read().decode('UTF-8')) |
| 110 | + print(userdata) |
| 111 | + |
| 112 | + self.__result = {'token': rep['access_token'], |
| 113 | + 'user': userdata['login']} |
| 114 | + break |
| 115 | + |
| 116 | + # NO SUCC |
| 117 | + errcode = rep['error'] |
| 118 | + if errcode == 'authorization_pending': |
| 119 | + # note that this error will happen if user just closes down the window |
| 120 | + break |
| 121 | + elif errcode == 'slow_down': |
| 122 | + self.__interval = rep.get('interval', self.__interval + 5) |
| 123 | + time.sleep(self.__interval) |
| 124 | + continue |
| 125 | + elif errcode == 'expired_token': |
| 126 | + if QMessageBox.warning(self, 'device code expired', 'it took you too long to enter the code, now it\'s expired.\nWant to retry?', buttons=QMessageBox.Yes | QMessageBox.No) == QMessageBox.Yes: |
| 127 | + event.reject() |
| 128 | + self.reinit_code() |
| 129 | + return |
| 130 | + break |
| 131 | + elif errcode == 'unsupported_grant_type': |
| 132 | + raise RuntimeError('unsupported grant type. probably github changed API. need to update the plugin') |
| 133 | + elif errcode == 'incorrect_client_credentials': |
| 134 | + raise RuntimeError('incorect client id. probably pedohorse changed hpaste id for some reason. update the plugin') |
| 135 | + elif errcode == 'incorrect_device_code': |
| 136 | + if QMessageBox.warning(self, 'bad device code', 'server reported wrong device code\nWant to retry?', buttons=QMessageBox.Yes | QMessageBox.No) == QMessageBox.Yes: |
| 137 | + event.reject() |
| 138 | + self.reinit_code() |
| 139 | + return |
| 140 | + break |
| 141 | + elif errcode == 'access_denied': |
| 142 | + # means user denied the request |
| 143 | + break |
| 144 | + else: |
| 145 | + raise RuntimeError('unexpected error: %s' % (json.dumps(rep),)) |
| 146 | + else: |
| 147 | + raise RuntimeError('bad return code. server reported with bad return code %d' % code) |
| 148 | + else: |
| 149 | + if QMessageBox.warning(self, 'unknown error', 'could not manage to check authorization in reasonable amount of attempts\nWant to retry?', buttons=QMessageBox.Yes | QMessageBox.No) == QMessageBox.Yes: |
| 150 | + event.reject() |
| 151 | + self.reinit_code() |
| 152 | + return |
| 153 | + |
| 154 | + return super(QGithubDeviceAuthDialog, self).closeEvent(event) |
| 155 | + |
| 156 | + @Slot(object) |
| 157 | + def on_url_changed(self, qurl): |
| 158 | + url = qurl.toString() |
| 159 | + print(url) |
| 160 | + if not self.__await_login_redirect: |
| 161 | + return |
| 162 | + if qurl.path() != '/login': |
| 163 | + return |
| 164 | + self.__await_login_redirect = False |
| 165 | + if self.__hint_username is not None: |
| 166 | + if qurl.hasQuery(): |
| 167 | + qurl.setQuery('&'.join((qurl.query(), 'login=%s' % self.__hint_username))) |
| 168 | + else: |
| 169 | + qurl.setQuery('login=%s' % self.__hint_username) |
| 170 | + print('redir to %s' % qurl.toString) |
| 171 | + self.__webview.load(qurl) |
| 172 | + |
| 173 | + @Slot() |
| 174 | + def reload_button_clicked(self): |
| 175 | + self.reinit_code() |
| 176 | + |
| 177 | + |
| 178 | +if __name__ == '__main__': # testing |
| 179 | + import sys |
| 180 | + import string |
| 181 | + import random |
| 182 | + from PySide2.QtWidgets import QApplication |
| 183 | + qapp = QApplication(sys.argv) |
| 184 | + # w = QWebAuthDialog('https://www.google.com', r'https://www.google.com/search\?(.*)') |
| 185 | + webauthstate = ''.join(random.choice(string.ascii_letters) for _ in range(32)) |
| 186 | + webauthparms = {'client_id': '42e8e8e9d844e45c2d05', |
| 187 | + 'redirect_uri': 'https://github.com/login/oauth/success', |
| 188 | + 'scope': 'gist', |
| 189 | + 'state': webauthstate} |
| 190 | + w = QGithubDeviceAuthDialog(client_id='42e8e8e9d844e45c2d05', hint_username='ololovich', parent=None) |
| 191 | + res = w.exec_() |
| 192 | + print(res == QGithubDeviceAuthDialog.Accepted) |
| 193 | + print(w.get_result()) |
| 194 | + |
0 commit comments