Skip to content

Commit 1a9134d

Browse files
committed
add auth device func
1 parent c5d878c commit 1a9134d

File tree

4 files changed

+144
-6
lines changed

4 files changed

+144
-6
lines changed

02_auth.ipynb

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
{
147147
"data": {
148148
"text/plain": [
149-
"('4a862fd5d474b58cfad0e2432f47c33a2976facc', '28F1-E3A6')"
149+
"('466da70dae0a30102cd6df6436b57a6a5c58701a', 'CA0C-C4AE')"
150150
]
151151
},
152152
"execution_count": null,
@@ -189,7 +189,7 @@
189189
"name": "stdout",
190190
"output_type": "stream",
191191
"text": [
192-
"First copy your one-time code: 28F1-E3A6\n",
192+
"First copy your one-time code: CA0C-C4AE\n",
193193
"Then visit https://github.com/login/device in your browser, and paste the code when prompted.\n"
194194
]
195195
}
@@ -302,6 +302,54 @@
302302
"# token = ghauth.wait(lambda: print('.', end=''))"
303303
]
304304
},
305+
{
306+
"cell_type": "code",
307+
"execution_count": null,
308+
"metadata": {},
309+
"outputs": [],
310+
"source": [
311+
"#export\n",
312+
"def github_auth_device(wb='', n_polls=9999):\n",
313+
" \"Authenticate with GitHub, polling up to `n_polls` times to wait for completion\"\n",
314+
" auth = GhDeviceAuth()\n",
315+
" print(f\"First copy your one-time code: \\x1b[33m{auth.user_code}\\x1b[m\")\n",
316+
" print(f\"Then visit {auth.verification_uri} in your browser, and paste the code when prompted.\")\n",
317+
" if not wb: wb = input(\"Shall we try to open the link for you? [y/n] \")\n",
318+
" if wb[0].lower()=='y': auth.open_browser()\n",
319+
"\n",
320+
" print(\"Waiting for authorization...\", end='')\n",
321+
" token = auth.wait(lambda: print('.', end=''), n_polls=n_polls)\n",
322+
" if not token: return print('Authentication not complete!')\n",
323+
" print(\"Authenticated with GitHub\")\n",
324+
" return token"
325+
]
326+
},
327+
{
328+
"cell_type": "markdown",
329+
"metadata": {},
330+
"source": [
331+
"When we run this we'll be shown a URL to visit and a code to enter in order to authenticate. Normally we'll be prompted to open a browser, and the function will wait for authentication to complete -- for demonstrating here we'll skip both of these steps:"
332+
]
333+
},
334+
{
335+
"cell_type": "code",
336+
"execution_count": null,
337+
"metadata": {},
338+
"outputs": [
339+
{
340+
"name": "stdout",
341+
"output_type": "stream",
342+
"text": [
343+
"First copy your one-time code: \u001b[33mDD47-0B67\u001b[m\n",
344+
"Then visit https://github.com/login/device in your browser, and paste the code when prompted.\n",
345+
"Waiting for authorization...Authentication not complete!\n"
346+
]
347+
}
348+
],
349+
"source": [
350+
"github_auth_device('n',n_polls=0)"
351+
]
352+
},
305353
{
306354
"cell_type": "markdown",
307355
"metadata": {},
@@ -327,6 +375,8 @@
327375
"Converted 50_fullapi.ipynb.\n",
328376
"Converted 80_tutorial_actions.ipynb.\n",
329377
"Converted 90_build_lib.ipynb.\n",
378+
"Converted Untitled.ipynb.\n",
379+
"Converted ghapi demo.ipynb.\n",
330380
"Converted index.ipynb.\n"
331381
]
332382
}

docs/auth.html

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ <h2 id="GhDeviceAuth" class="doc_header"><code>class</code> <code>GhDeviceAuth</
190190

191191

192192
<div class="output_text output_subarea output_execute_result">
193-
<pre>(&#39;4a862fd5d474b58cfad0e2432f47c33a2976facc&#39;, &#39;28F1-E3A6&#39;)</pre>
193+
<pre>(&#39;466da70dae0a30102cd6df6436b57a6a5c58701a&#39;, &#39;CA0C-C4AE&#39;)</pre>
194194
</div>
195195

196196
</div>
@@ -260,7 +260,7 @@ <h4 id="GhDeviceAuth.url_docs" class="doc_header"><code>GhDeviceAuth.url_docs</c
260260
<div class="output_area">
261261

262262
<div class="output_subarea output_stream output_stdout output_text">
263-
<pre>First copy your one-time code: 28F1-E3A6
263+
<pre>First copy your one-time code: CA0C-C4AE
264264
Then visit https://github.com/login/device in your browser, and paste the code when prompted.
265265
</pre>
266266
</div>
@@ -422,6 +422,78 @@ <h4 id="GhDeviceAuth.wait" class="doc_header"><code>GhDeviceAuth.wait</code><a h
422422
</div>
423423
</div>
424424
</div>
425+
{% raw %}
426+
427+
<div class="cell border-box-sizing code_cell rendered">
428+
429+
<div class="output_wrapper">
430+
<div class="output">
431+
432+
<div class="output_area">
433+
434+
435+
<div class="output_markdown rendered_html output_subarea ">
436+
<h4 id="github_auth_device" class="doc_header"><code>github_auth_device</code><a href="https://github.com/fastai/ghapi/tree/master/ghapi/auth.py#L82" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>github_auth_device</code>(<strong><code>wb</code></strong>=<em><code>''</code></em>, <strong><code>n_polls</code></strong>=<em><code>9999</code></em>)</p>
437+
</blockquote>
438+
<p>Authenticate with GitHub, polling up to <code>n_polls</code> times to wait for completion</p>
439+
440+
</div>
441+
442+
</div>
443+
444+
</div>
445+
</div>
446+
447+
</div>
448+
{% endraw %}
449+
450+
{% raw %}
451+
452+
<div class="cell border-box-sizing code_cell rendered">
453+
454+
</div>
455+
{% endraw %}
456+
457+
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
458+
<div class="text_cell_render border-box-sizing rendered_html">
459+
<p>When we run this we'll be shown a URL to visit and a code to enter in order to authenticate. Normally we'll be prompted to open a browser, and the function will wait for authentication to complete -- for demonstrating here we'll skip both of these steps:</p>
460+
461+
</div>
462+
</div>
463+
</div>
464+
{% raw %}
465+
466+
<div class="cell border-box-sizing code_cell rendered">
467+
<div class="input">
468+
469+
<div class="inner_cell">
470+
<div class="input_area">
471+
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">github_auth_device</span><span class="p">(</span><span class="s1">&#39;n&#39;</span><span class="p">,</span><span class="n">n_polls</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
472+
</pre></div>
473+
474+
</div>
475+
</div>
476+
</div>
477+
478+
<div class="output_wrapper">
479+
<div class="output">
480+
481+
<div class="output_area">
482+
483+
<div class="output_subarea output_stream output_stdout output_text">
484+
<pre>First copy your one-time code: <span class="ansi-yellow-fg">DD47-0B67</span>
485+
Then visit https://github.com/login/device in your browser, and paste the code when prompted.
486+
Waiting for authorization...Authentication not complete!
487+
</pre>
488+
</div>
489+
</div>
490+
491+
</div>
492+
</div>
493+
494+
</div>
495+
{% endraw %}
496+
425497
</div>
426498

427499

ghapi/_nbdev.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"GhDeviceAuth.open_browser": "02_auth.ipynb",
4848
"GhDeviceAuth.auth": "02_auth.ipynb",
4949
"GhDeviceAuth.wait": "02_auth.ipynb",
50+
"github_auth_device": "02_auth.ipynb",
5051
"paged": "03_page.ipynb",
5152
"parse_link_hdr": "03_page.ipynb",
5253
"GhApi.last_page": "03_page.ipynb",

ghapi/auth.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: 02_auth.ipynb (unless otherwise specified).
22

3-
__all__ = ['Scope', 'scope_str', 'GhDeviceAuth']
3+
__all__ = ['Scope', 'scope_str', 'GhDeviceAuth', 'github_auth_device']
44

55
# Cell
66
from fastcore.utils import *
@@ -76,4 +76,19 @@ def wait(self:GhDeviceAuth, cb:callable=None, n_polls=9999)->str:
7676
res = self.auth()
7777
if res: return res
7878
if cb: cb()
79-
time.sleep(interval)
79+
time.sleep(interval)
80+
81+
# Cell
82+
def github_auth_device(wb='', n_polls=9999):
83+
"Authenticate with GitHub, polling up to `n_polls` times to wait for completion"
84+
auth = GhDeviceAuth()
85+
print(f"First copy your one-time code: \x1b[33m{auth.user_code}\x1b[m")
86+
print(f"Then visit {auth.verification_uri} in your browser, and paste the code when prompted.")
87+
if not wb: wb = input("Shall we try to open the link for you? [y/n] ")
88+
if wb[0].lower()=='y': auth.open_browser()
89+
90+
print("Waiting for authorization...", end='')
91+
token = auth.wait(lambda: print('.', end=''), n_polls=n_polls)
92+
if not token: return print('Authentication not complete!')
93+
print("Authenticated with GitHub")
94+
return token

0 commit comments

Comments
 (0)