|
| 1 | +# Configuration |
| 2 | + |
| 3 | +The extension reads OAuth credentials from the active Flask application's |
| 4 | +config. Configure the app before serving requests. |
| 5 | + |
| 6 | +## Flask configuration |
| 7 | + |
| 8 | +| Key | Required | Default | Purpose | |
| 9 | +| --- | --- | --- | --- | |
| 10 | +| `SECRET_KEY` | Yes | — | Flask session signing key. Keep it secret and stable. | |
| 11 | +| `GITHUB_CLIENT_ID` | Yes | — | Client ID from your GitHub OAuth App. | |
| 12 | +| `GITHUB_CLIENT_SECRET` | Yes | — | Client secret from your GitHub OAuth App. | |
| 13 | +| `GITHUB_REDIRECT_URI` | Yes | — | Absolute callback URL registered with the OAuth App. | |
| 14 | +| `GITHUB_SCOPE` | No | `read:user` | Space-separated OAuth scopes requested at login. | |
| 15 | +| `GITHUB_LOGIN_VIEW` | No | Decorated login view, otherwise `login` | Flask endpoint to use when a refresh token has expired. | |
| 16 | + |
| 17 | +The client ID, client secret, redirect URI, scope and `SECRET_KEY` must be |
| 18 | +non-empty strings. If any is missing or empty, the login route returns a |
| 19 | +JSON error with HTTP 500, the callback route returns the same error once its |
| 20 | +`state` check passes, and `github.client()` returns `None`. |
| 21 | + |
| 22 | +## Initialize the extension |
| 23 | + |
| 24 | +For a single app, pass the app to the extension: |
| 25 | + |
| 26 | +```python |
| 27 | +github = flask_githubkit.GitHub(app) |
| 28 | +``` |
| 29 | + |
| 30 | +For an application factory, create the extension without an app and call |
| 31 | +`init_app` when the app is created: |
| 32 | + |
| 33 | +```python |
| 34 | +github = flask_githubkit.GitHub() |
| 35 | + |
| 36 | + |
| 37 | +def create_app(): |
| 38 | + app = flask.Flask(__name__) |
| 39 | + app.config.from_mapping(...) |
| 40 | + github.init_app(app) |
| 41 | + return app |
| 42 | +``` |
| 43 | + |
| 44 | +See [application factory and Blueprint examples](examples.md). |
| 45 | + |
| 46 | +## Scope and login endpoint overrides |
| 47 | + |
| 48 | +The constructor accepts optional `scope` and `login_view` keyword |
| 49 | +arguments: |
| 50 | + |
| 51 | +```python |
| 52 | +github = flask_githubkit.GitHub( |
| 53 | + scope="read:user repo", |
| 54 | + login_view="auth.login", |
| 55 | +) |
| 56 | +``` |
| 57 | + |
| 58 | +For either option, the constructor value takes precedence over its Flask |
| 59 | +config key. If no scope is supplied, the default is `read:user`. If no |
| 60 | +login endpoint is supplied, the extension looks for a route decorated |
| 61 | +with `@github.login` and uses its Flask endpoint. This works with |
| 62 | +Blueprints (for example, `auth.login`). If no decorated route exists, it |
| 63 | +uses the endpoint named `login`. |
| 64 | + |
| 65 | +`login_view` is an endpoint name used by `flask.url_for`, not a URL path. |
| 66 | +Most apps do not need to set it when they decorate their login route with |
| 67 | +`@github.login`. |
| 68 | + |
| 69 | +Scopes determine what user access the OAuth App requests. Request only |
| 70 | +the access your app needs. GitHub's OAuth scope behavior is documented |
| 71 | +in the [GitHub OAuth scopes guide](https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/scopes-for-oauth-apps). |
| 72 | + |
| 73 | +## Session storage |
| 74 | + |
| 75 | +Tokens are stored in `flask.session`. The extension marks the session |
| 76 | +permanent when it saves tokens, but does not select or configure a |
| 77 | +session backend or its expiration policy. |
| 78 | + |
| 79 | +Flask's default session is a signed client-side cookie; it is **not |
| 80 | +encrypted**, so users can read its contents. For production, use a |
| 81 | +server-side session backend, such as Flask-Session with Redis, and |
| 82 | +configure the backend and session lifetime for your application. |
| 83 | +Sliding expiry and TTL behavior depend on that backend. |
| 84 | + |
| 85 | +## Token refresh |
| 86 | + |
| 87 | +Enable **Expire user authorization tokens** in the GitHub OAuth App if |
| 88 | +you want expiring access tokens and refresh tokens. GitHubKit refreshes |
| 89 | +an expired access token when an API request uses the client. |
| 90 | +flask-githubkit writes the updated token values back to the session after |
| 91 | +the request. |
| 92 | + |
| 93 | +GitHub refresh tokens are single-use. Concurrent requests using the same |
| 94 | +expired token can race; this extension does not provide a distributed |
| 95 | +lock. A deployment that needs coordination across workers must implement |
| 96 | +that policy in its own storage or infrastructure. |
| 97 | + |
| 98 | +## Limitations |
| 99 | + |
| 100 | +- Only synchronous Flask applications and GitHub OAuth Apps are supported. |
| 101 | + GitHub Apps and GitHub Enterprise endpoints are not configurable. |
| 102 | +- Real GitHub code exchange and token refresh, Redis, and Flask-Session TTL |
| 103 | + behavior are not verified by the automated tests. Test them against those |
| 104 | + services before relying on them in production. |
| 105 | +- Errors raised by your own API calls, such as network failures, are not |
| 106 | + caught by the extension. |
0 commit comments