Commit graph

28 commits

Author SHA1 Message Date
Mislav Marohnić
61609db9ef Cover HTTP 402 in oauth tests 2020-10-01 17:21:35 +02:00
Mislav Marohnić
93642529da Enforce correct content-type in Device Flow detection 2020-10-01 17:09:47 +02:00
Mislav Marohnić
5f8648159d Fix handling of HTTP 403 in Device Flow detection 2020-10-01 17:09:14 +02:00
Ricky Pike
f1d675f137 use status codes from http package 2020-09-18 19:13:32 -05:00
Mislav Marohnić
cb4cc72e50 Handle HTTP 422 response to OAuth Device flow detection
If HTTP 422 is encountered, assume that OAuth Device Flow is unavailable
and fall back to OAuth app authorization flow.
2020-08-31 22:26:04 +02:00
Mislav Marohnić
1c0f83ffd5 Handle "unauthorized_client" HTTP 400 error when doing Device Flow
The "unauthorized_client" error means that OAuth Device Flow is implemented, but either isn't enabled on this GHES instance, or that it isn't enabled for this particular OAuth app. In these cases we fall back to old OAuth app authorization flow.
2020-08-27 14:10:16 +02:00
Mislav Marohnić
a6776d0ba2 Allow more HTTP 40x codes to trigger fallback to old OAuth flow
I want to avoid falling back to the old OAuth flow for just any HTTP
4xx/5xx because other statuses should be allowed to surface a problem
with a request or the server.
2020-08-14 17:31:23 +02:00
Mislav Marohnić
60eebd2896 Implement OAuth Device Authorization flow
Before, we implemented the OAuth app authorization flow which requires a
callback URL. To provide such a URL, we had to spin up a local HTTP
server, which was brittle and did not cover cases where a person might
want to authenticate with a browser that runs on a different machine
than the GitHub CLI process.

This implements the OAuth Device Authorization flow where the user is
given a one-time code and asked to paste it in the browser flow. There
is no callback URL, so we can avoid spinning up a local server, and the
user may open a browser on any of their devices, as long as they provide
the correct one-time code.

If the Device Authorization flow is detected to be unavailable for the
OAuth app (right now, it's specifically enabled for GitHub CLI) or for
an older GitHub Enterprise instance, this falls back to the old app
authentication flow.
2020-08-13 19:21:08 +02:00
Mislav Marohnić
288d01318b Respect the hostname of current repository in queries 2020-07-23 22:31:08 +02:00
Mislav Marohnić
b48237aa59 Update headless authentication instructions 2020-06-03 15:16:55 +02:00
Mislav Marohnić
92e68af46f Fix referring to loopback interface in OAuth flow
Avoids the error when `localhost` was resolved to something else other
than `127.0.0.1`:

    listen tcp 192.168.1.*:0: bind: can't assign requested address
2020-05-27 15:10:11 +02:00
Mislav Marohnić
14ce1f99a7 Ask for an additional read:org OAuth scope
This is to facilitate:
- requesting teams for review on `pr create`
- allowing `repo create ORG/REPO --team TEAM`
2020-04-15 14:28:07 +02:00
Mislav Marohnić
734497a8d8
Code fixes informed by golangci-lint failures (#738) 2020-04-03 16:33:34 +02:00
Mislav Marohnić
71d3696667 Avoid redirecting to localhost during authorization flow
Web developers who have previously ran an application on
`http://localhost` that enabled HSTS (HTTP Strict Transport Security)
will find themselves unable to authenticate because their browser
(typically Safari, in practice) will keep redirecting them to
`https://localhost`, which isn't handled by our local server.

This switches the authorization callback to be to `127.0.0.1`, which
should be equivalent to `localhost`, but not subject to HSTS.
2020-03-13 13:40:04 +01:00
Micheal Looney
a0cb79af41 Print details of authentication workaround to terminal
Mitigates https://github.com/cli/cli/issues/297

This adds some more information to the output when authentication fails
due to `gh` being unable to open a browser. It communicates the details
of the workaround without the user having to search issues on the `gh`
repo to see how to get around it.
2020-02-26 14:58:37 +00:00
vertextau
95cbc56dec Improve randomString func 2020-02-13 22:42:58 +01:00
Mislav Marohnić
fe7cdd8ab7 Extract web browser launching to a package
This fixes opening URLs with `&` on Windows.
2020-01-29 11:49:38 +01:00
Mislav Marohnić
853fda13e9 Add more OAuth flow debugging to stderr with DEBUG=oauth 2020-01-13 20:31:31 +01:00
Mislav Marohnić
d6a437a93c Add a TODO to make scopes configurable 2020-01-13 20:17:20 +01:00
Mislav Marohnić
bbeb558fce Narrow the scope of the local server handler
Before, the local server handled any request regardless of path, which
could potentially include requests generated by the browser such as the
one for favicon. This could lead to race conditions around reading the
code to continue to OAuth flow with.

Now, have the OAuth flow redirect to `localhost:PORT/callback` and only
handle `/callback` requests specifically.
2020-01-13 20:14:00 +01:00
Mislav Marohnić
635d2963f6 Add more explicit error handling around the OAuth flow 2020-01-13 20:13:41 +01:00
Mislav Marohnić
295c5d122b Fix opening OAuth URL in browser 2019-11-27 14:08:42 +01:00
Mislav Marohnić
5aca575964 Wire up OAuth authentication flow to initialize config file
The config file is now `~/.config/gh`.
2019-10-18 19:08:11 +02:00
Mislav Marohnić
de85294c79 Extract OAuth logic into a struct 2019-10-18 15:44:46 +02:00
Mislav Marohnić
7bf306f022 Generate and verify random "state" value
This is for extra security during OAuth flow.
2019-10-18 15:44:46 +02:00
Mislav Marohnić
216ffb89e2 Use random available port number 2019-10-18 15:44:46 +02:00
Mislav Marohnić
db0084f623 One weird trick to prevent macOS firewall popup
Discovered by a stay-at-home developer!
2019-10-18 15:44:46 +02:00
Mislav Marohnić
d90552454d Preliminary OAuth flow 2019-10-18 15:44:44 +02:00