File size: 13,230 Bytes
5246c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python3
"""GitHub OAuth Device Flow authentication for OpenCodeReview.

Usage
-----
    export GITHUB_OAUTH_CLIENT_ID="Iv23li..."
    opencodereview auth login
    opencodereview auth status
    opencodereview auth logout

The Device Flow is used instead of the Authorization Code flow because
there is no web server to handle a redirect URI.
"""

from __future__ import annotations

import logging
import os
import sys
import time
from typing import Optional

import click
import keyring
import requests

logger = logging.getLogger(__name__)

# ─── Constants ───────────────────────────────────────────────────────────────

SERVICE_NAME = "opencodereview"
GH_DEVICE_CODE_URL = "https://github.com/login/device/code"
GH_ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token"
GH_API_BASE = "https://api.github.com"
DEFAULT_SCOPE = "repo"  # Full control of private repositories β€” needed for
                        # reading PRs and posting review comments on
                        # private repos.
POLL_SAFETY_MARGIN = 1  # Extra seconds to wait beyond the server's interval


# ─── Exceptions ─────────────────────────────────────────────────────────────


class AuthError(Exception):
    """Base exception for auth operations."""


class DeviceFlowExpired(AuthError):
    """The device code expired before the user approved."""


class DeviceFlowDenied(AuthError):
    """The user denied the authorization request."""


# ─── Auth Service ────────────────────────────────────────────────────────────


def _resolve_client_id(client_id: Optional[str] = None) -> str:
    """Return the GitHub OAuth client ID from the argument or environment."""
    cid = client_id or os.environ.get("GITHUB_OAUTH_CLIENT_ID")
    if not cid:
        raise AuthError(
            "GitHub OAuth client ID is required.\n\n"
            "  1. Register a new OAuth App at:\n"
            "     https://github.com/settings/developers\n\n"
            "  2. Enable Device Flow in the app settings.\n\n"
            "  3. Set the client ID:\n"
            f"       export GITHUB_OAUTH_CLIENT_ID=\"<your_client_id>\"\n"
            "     Or pass --client-id to the login command.",
        )
    return cid


class AuthService:
    """Manage GitHub OAuth tokens via the Device Flow."""

    def __init__(self, client_id: Optional[str] = None) -> None:
        self._client_id = client_id or os.environ.get("GITHUB_OAUTH_CLIENT_ID")
        self._session = requests.Session()
        self._session.headers.update({
            "Accept": "application/json",
            "User-Agent": "opencodereview/1.0",
        })

    # ── Device Flow Login ──────────────────────────────────────────────────

    def login(self) -> str:
        """Run the GitHub Device Flow and return the access token.

        Steps
        -----
        1. POST to ``/login/device/code`` to get a device + user code.
        2. Print the user code + verification URL for the user.
        3. Poll ``/login/oauth/access_token`` until the user approves.
        4. Validate that the granted scopes include what was requested.
        5. Save the token to the system keyring.
        6. Return the access token.
        """
        # Validate client_id before making any API calls
        self._client_id = _resolve_client_id(self._client_id)

        device_code, user_code, verification_uri, interval, expires_in = (
            self._request_device_code()
        )

        self._print_login_instructions(user_code, verification_uri, expires_in)

        access_token, granted_scope = self._poll_for_token(device_code, interval)

        # Validate that the granted scopes include what we asked for
        self._validate_scopes(granted_scope)

        # Save to keyring
        keyring.set_password(SERVICE_NAME, "github_token", access_token)
        logger.info("Token saved to system credential store (keyring).")

        # Fetch and display user info
        user = self._get_user(access_token)
        click.echo(f"  Logged in as: {user['login']}")

        return access_token

    def _request_device_code(
        self,
    ) -> tuple[str, str, str, int, int]:
        """POST to the device-code endpoint and return the response fields."""
        resp = self._session.post(
            GH_DEVICE_CODE_URL,
            data={
                "client_id": self._client_id,
                "scope": DEFAULT_SCOPE,
            },
        )
        resp.raise_for_status()
        data = resp.json()

        if "error" in data:
            raise AuthError(
                f"Device code request failed: {data.get('error_description', data['error'])}"
            )

        return (
            data["device_code"],
            data["user_code"],
            data["verification_uri"],
            data.get("interval", 5),
            data.get("expires_in", 900),
        )

    @staticmethod
    def _print_login_instructions(
        user_code: str, verification_uri: str, expires_in: int,
    ) -> None:
        """Print the user code and instructions to the terminal."""
        click.echo()
        click.echo("=" * 60)
        click.echo("  GitHub Device Authentication")
        click.echo("=" * 60)
        click.echo()
        click.echo(f"  1. Open this URL in your browser:")
        click.echo(f"     {verification_uri}")
        click.echo()
        click.echo(f"  2. Enter the following code:")
        click.echo(f"     {user_code}")
        click.echo()
        click.echo(f"  3. Authorize the OpenCodeReview application.")
        click.echo()
        click.echo(f"  This code expires in {expires_in // 60} minute(s).")
        click.echo(f"  Waiting for you to approve...")
        click.echo()

    def _poll_for_token(self, device_code: str, interval: int) -> tuple[str, str]:
        """Poll the access-token endpoint until the user approves or the code expires.

        Returns ``(access_token, granted_scope)`` where ``granted_scope`` is the
        space-separated scope string returned by GitHub (may be empty if the
        response omitted it).
        """
        poll_interval = interval
        start_time = time.monotonic()

        while True:
            time.sleep(poll_interval + POLL_SAFETY_MARGIN)

            resp = self._session.post(
                GH_ACCESS_TOKEN_URL,
                data={
                    "client_id": self._client_id,
                    "device_code": device_code,
                    "grant_type": "urn:ietf:params:oauth:grant-type:device_code",
                },
            )
            resp.raise_for_status()
            data = resp.json()

            if "access_token" in data:
                return data["access_token"], data.get("scope", "")

            error = data.get("error", "")

            if error == "authorization_pending":
                # User hasn't acted yet β€” keep polling
                continue
            elif error == "slow_down":
                # Polling too fast β€” increase interval by 5 seconds
                poll_interval += 5
                logger.debug("Polling rate limited β€” increasing interval to %ds", poll_interval)
                continue
            elif error == "expired_token":
                raise DeviceFlowExpired(
                    "The device code has expired. Run 'opencodereview auth login' again."
                )
            elif error == "access_denied":
                raise DeviceFlowDenied("The authorization request was denied.")
            else:
                raise AuthError(
                    f"Unexpected polling response: {data.get('error_description', error)}"
                )

    # ── Status ──────────────────────────────────────────────────────────────

    def status(self) -> Optional[dict]:
        """Check whether a valid token is stored and return the authenticated user.

        Returns ``None`` if no token is stored.
        Raises ``AuthError`` if the token is invalid or expired.
        """
        token = keyring.get_password(SERVICE_NAME, "github_token")
        if not token:
            return None

        user = self._get_user(token)
        return user

    # ── Logout ──────────────────────────────────────────────────────────────

    def logout(self) -> bool:
        """Delete the stored token from the keyring.

        Returns ``True`` if a token was deleted, ``False`` if none existed.
        """
        existing = keyring.get_password(SERVICE_NAME, "github_token")
        if existing:
            try:
                keyring.delete_password(SERVICE_NAME, "github_token")
            except keyring.errors.PasswordDeleteError:
                logger.warning("Failed to delete token from keyring (back-end error).")
                return False
            return True
        return False

    # ── Helpers ─────────────────────────────────────────────────────────────

    def _validate_scopes(self, granted_scope_str: str) -> None:
        """Compare the scopes GitHub actually granted against what was requested.

        The ``scope`` field in the access-token response is a space-separated
        string.  If the user denied some scopes, we warn them on stderr so
        they know certain features may not work.
        """
        if not granted_scope_str.strip():
            # Older GitHub Enterprise or unusual config β€” skip validation
            return

        requested = set(DEFAULT_SCOPE.split(","))
        granted = set(granted_scope_str.strip().split())
        missing = requested - granted

        if missing:
            click.echo(
                "\n"
                "Warning: Some requested permissions were not granted.\n"
                f"  Missing scopes: {', '.join(sorted(missing))}\n"
                f"  Granted scopes: {', '.join(sorted(granted))}\n"
                "\n"
                "  Features that need these scopes may not work correctly.\n"
                "  Re-run 'opencodereview auth login' and ensure you\n"
                "  authorize all requested permissions.\n",
                err=True,
            )

    def _get_user(self, token: str) -> dict:
        """Fetch the authenticated GitHub user via the API."""
        resp = self._session.get(
            f"{GH_API_BASE}/user",
            headers={"Authorization": f"token {token}"},
        )
        if resp.status_code == 401:
            raise AuthError(
                "The stored token is invalid or has been revoked.\n"
                "Run 'opencodereview auth login' to re-authenticate."
            )
        resp.raise_for_status()
        return resp.json()


# ─── CLI ─────────────────────────────────────────────────────────────────────


@click.group()
def auth() -> None:
    """Authenticate with GitHub via the OAuth Device Flow."""


@auth.command()
@click.option(
    "--client-id",
    envvar="GITHUB_OAUTH_CLIENT_ID",
    default=None,
    help="GitHub OAuth App client ID (or set GITHUB_OAUTH_CLIENT_ID).",
)
def login(client_id: Optional[str]) -> None:
    """Authenticate with GitHub and store the token securely."""
    try:
        svc = AuthService(client_id=client_id)
        svc.login()
    except (AuthError, requests.RequestException) as exc:
        click.echo(f"Error: {exc}", err=True)
        sys.exit(1)


@auth.command()
def logout() -> None:
    """Remove the stored GitHub token."""
    svc = AuthService()
    if svc.logout():
        click.echo("Token removed from credential store.")
    else:
        click.echo("No token found. Nothing to remove.")


@auth.command()
def status() -> None:
    """Show whether a valid token is stored and which user it belongs to."""
    svc = AuthService()
    try:
        user = svc.status()
    except AuthError as exc:
        click.echo(f"Token status: invalid ({exc})", err=True)
        sys.exit(1)
    except requests.RequestException as exc:
        click.echo(f"Error checking token: {exc}", err=True)
        sys.exit(1)

    if user is None:
        click.echo("No token stored. Run 'opencodereview auth login'.", err=True)
        sys.exit(1)
    else:
        click.echo(f"Authenticated as: {user['login']}")
        click.echo(f"Token: valid")
        if user.get("name"):
            click.echo(f"Name:  {user['name']}")