dserver-token-generator-plugin-oauth2 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dserver_token_generator_plugin_oauth2-0.1.0/PKG-INFO +351 -0
- dserver_token_generator_plugin_oauth2-0.1.0/README.md +328 -0
- dserver_token_generator_plugin_oauth2-0.1.0/dserver_token_generator_plugin_oauth2/__init__.py +36 -0
- dserver_token_generator_plugin_oauth2-0.1.0/dserver_token_generator_plugin_oauth2/blueprint.py +493 -0
- dserver_token_generator_plugin_oauth2-0.1.0/dserver_token_generator_plugin_oauth2/cli.py +228 -0
- dserver_token_generator_plugin_oauth2-0.1.0/dserver_token_generator_plugin_oauth2/config.py +274 -0
- dserver_token_generator_plugin_oauth2-0.1.0/dserver_token_generator_plugin_oauth2/device_flow.py +279 -0
- dserver_token_generator_plugin_oauth2-0.1.0/dserver_token_generator_plugin_oauth2/jwt_utils.py +189 -0
- dserver_token_generator_plugin_oauth2-0.1.0/dserver_token_generator_plugin_oauth2/plugin.py +131 -0
- dserver_token_generator_plugin_oauth2-0.1.0/dserver_token_generator_plugin_oauth2/sql_models.py +37 -0
- dserver_token_generator_plugin_oauth2-0.1.0/dserver_token_generator_plugin_oauth2/user_provisioning.py +97 -0
- dserver_token_generator_plugin_oauth2-0.1.0/dserver_token_generator_plugin_oauth2/version.py +24 -0
- dserver_token_generator_plugin_oauth2-0.1.0/pyproject.toml +54 -0
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dserver-token-generator-plugin-oauth2
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: OAuth 2.0 token generator plugin for dserver
|
|
5
|
+
Author-email: Lars Pastewka <lars.pastewka@imtek.uni-freiburg.de>, "Johannes L. Hörmann" <johannes.laurin@gmail.com>
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
Requires-Dist: flask>=2.0.0
|
|
9
|
+
Requires-Dist: flask-smorest>=0.40.0
|
|
10
|
+
Requires-Dist: authlib>=1.2.0
|
|
11
|
+
Requires-Dist: httpx>=0.24.0
|
|
12
|
+
Requires-Dist: PyJWT>=2.0.0
|
|
13
|
+
Requires-Dist: cryptography>=3.4.0
|
|
14
|
+
Requires-Dist: dservercore>=0.23.0
|
|
15
|
+
Requires-Dist: dservercore ; extra == "test"
|
|
16
|
+
Requires-Dist: pytest ; extra == "test"
|
|
17
|
+
Requires-Dist: pytest-cov ; extra == "test"
|
|
18
|
+
Requires-Dist: responses ; extra == "test"
|
|
19
|
+
Project-URL: Changelog, https://github.com/jic-dtool/dserver-token-generator-plugin-oauth2/blob/main/CHANGELOG.rst
|
|
20
|
+
Project-URL: Repository, https://github.com/jic-dtool/dserver-token-generator-plugin-oauth2
|
|
21
|
+
Provides-Extra: test
|
|
22
|
+
|
|
23
|
+
# dserver-token-generator-plugin-oauth2
|
|
24
|
+
|
|
25
|
+
A generic OAuth 2.0 / OpenID Connect token generator plugin for dserver that provides Single Sign-On (SSO) authentication with any OAuth2/OIDC compliant Identity Provider.
|
|
26
|
+
|
|
27
|
+
## Features
|
|
28
|
+
|
|
29
|
+
- **OAuth 2.0 Authorization Code flow** with PKCE support
|
|
30
|
+
- **OpenID Connect** support for providers that offer it
|
|
31
|
+
- **JWT token generation** after successful authentication
|
|
32
|
+
- **User lookup** in dserver: users must be registered in dserver beforehand; their display name is filled in from the provider
|
|
33
|
+
- **Device authorization flow** ([RFC 8628](https://www.rfc-editor.org/rfc/rfc8628)) for command-line clients such as `dtool dserver login`
|
|
34
|
+
- **Bounded sessions**: tokens can be refreshed, but at most `JWT_MAX_SESSION_HOURS` after the original login
|
|
35
|
+
- **Flexible attribute mapping** to adapt to any provider's response format
|
|
36
|
+
- **Built-in presets** for popular providers
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install dserver-token-generator-plugin-oauth2
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Or for development:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip install -e .
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Quick Start
|
|
51
|
+
|
|
52
|
+
1. Choose your OAuth2 provider and register an application to get client credentials
|
|
53
|
+
2. Configure the environment variables (see Configuration below)
|
|
54
|
+
3. Run the database migrations (`flask db migrate` / `flask db upgrade`) for the device-login table
|
|
55
|
+
4. Register users in dserver (see [Users](#users)) and restart dserver
|
|
56
|
+
5. Users can authenticate in the browser at `/auth/oauth2/login`, and on the command line with `dtool dserver login <server>` (from [dtool-dserver](https://github.com/jic-dtool/dtool-dserver))
|
|
57
|
+
|
|
58
|
+
## Configuration
|
|
59
|
+
|
|
60
|
+
### Required Environment Variables
|
|
61
|
+
|
|
62
|
+
| Variable | Description |
|
|
63
|
+
|----------|-------------|
|
|
64
|
+
| `OAUTH2_AUTHORIZATION_URL` | Provider's authorization endpoint |
|
|
65
|
+
| `OAUTH2_TOKEN_URL` | Provider's token endpoint |
|
|
66
|
+
| `OAUTH2_CLIENT_ID` | Your application's client ID |
|
|
67
|
+
| `OAUTH2_CLIENT_SECRET` | Your application's client secret |
|
|
68
|
+
|
|
69
|
+
**Security Note**: For credentials (`OAUTH2_CLIENT_ID` and `OAUTH2_CLIENT_SECRET`), use a `.env` file instead of hardcoding them in `docker-compose.yml`:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
# Copy the template
|
|
73
|
+
cp .env.template .env
|
|
74
|
+
|
|
75
|
+
# Edit with your credentials
|
|
76
|
+
nano .env
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
The `docker-compose.yml` uses variable substitution to read these values:
|
|
80
|
+
```yaml
|
|
81
|
+
OAUTH2_CLIENT_ID: "${OAUTH2_CLIENT_ID:-}"
|
|
82
|
+
OAUTH2_CLIENT_SECRET: "${OAUTH2_CLIENT_SECRET:-}"
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Optional Environment Variables
|
|
86
|
+
|
|
87
|
+
#### Provider Settings
|
|
88
|
+
|
|
89
|
+
| Variable | Description | Default |
|
|
90
|
+
|----------|-------------|---------|
|
|
91
|
+
| `OAUTH2_PROVIDER_NAME` | Identifier for the provider | `oauth2` |
|
|
92
|
+
| `OAUTH2_BASE_URL` | Base URL of your dserver instance | `http://localhost:5000` |
|
|
93
|
+
| `OAUTH2_USERINFO_URL` | OIDC userinfo endpoint | (none) |
|
|
94
|
+
| `OAUTH2_SCOPE` | OAuth2 scopes to request | `openid` |
|
|
95
|
+
| `OAUTH2_REDIRECT_URI` | Callback URL | `{OAUTH2_BASE_URL}/auth/oauth2/callback` |
|
|
96
|
+
|
|
97
|
+
#### Attribute Mapping
|
|
98
|
+
|
|
99
|
+
The plugin needs to know how to extract user information from the OAuth2 provider's response. Different providers return user data in different formats.
|
|
100
|
+
|
|
101
|
+
| Variable | Description | Default |
|
|
102
|
+
|----------|-------------|---------|
|
|
103
|
+
| `OAUTH2_ATTRIBUTE_MAP` | Maps provider fields to internal fields | `sub:user_id,email:email,name:display_name` |
|
|
104
|
+
| `OAUTH2_USERNAME_FIELD` | Which internal field to use as username | `user_id` |
|
|
105
|
+
|
|
106
|
+
**Format**: `provider_field:internal_field,provider_field2:internal_field2`
|
|
107
|
+
|
|
108
|
+
**Internal fields**:
|
|
109
|
+
- `user_id` - Unique identifier from the provider
|
|
110
|
+
- `username` - Username (if different from user_id)
|
|
111
|
+
- `email` - Email address
|
|
112
|
+
- `display_name` - Full display name
|
|
113
|
+
- `given_name` - First name
|
|
114
|
+
- `surname` - Last name
|
|
115
|
+
|
|
116
|
+
#### JWT Settings
|
|
117
|
+
|
|
118
|
+
| Variable | Description | Default |
|
|
119
|
+
|----------|-------------|---------|
|
|
120
|
+
| `JWT_PRIVATE_KEY_FILE` | Path to RSA private key | `/app/jwt/jwt_key` |
|
|
121
|
+
| `JWT_PUBLIC_KEY_FILE` | Path to RSA public key | `/app/jwt/jwt_key.pub` |
|
|
122
|
+
| `JWT_ALGORITHM` | Signing algorithm | `RS256` |
|
|
123
|
+
| `JWT_ISSUER` | Token issuer claim; must match dserver's `JWT_DECODE_ISSUER` | dserver's `JWT_DECODE_ISSUER`, else `dserver` |
|
|
124
|
+
| `JWT_AUDIENCE` | Token audience claim; must match dserver's `JWT_DECODE_AUDIENCE` | dserver's `JWT_DECODE_AUDIENCE`, else `dserver` |
|
|
125
|
+
| `JWT_TOKEN_EXPIRY_HOURS` | Token validity period | `24` |
|
|
126
|
+
| `JWT_MAX_SESSION_HOURS` | Maximum session age: tokens can be refreshed until this long after the original login (recorded in the `auth_time` claim), then the user must log in again | `168` |
|
|
127
|
+
|
|
128
|
+
#### Users
|
|
129
|
+
|
|
130
|
+
Users are **not** created on login: register them in dserver beforehand, with
|
|
131
|
+
the username the provider reports (e.g. the ORCID iD), and grant permissions:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
flask user add 0000-0002-1825-0097
|
|
135
|
+
flask user search_permission 0000-0002-1825-0097 s3://my-bucket
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
A login of an unregistered user yields a token that dserver rejects.
|
|
139
|
+
(`OAUTH2_AUTO_PROVISION_USERS` and `OAUTH2_DEFAULT_USER_PERMISSIONS` are read
|
|
140
|
+
but have no effect.)
|
|
141
|
+
|
|
142
|
+
#### Device Authorization (command-line login)
|
|
143
|
+
|
|
144
|
+
Command-line clients such as `dtool dserver login` use the device authorization
|
|
145
|
+
flow: the client shows a short code, the user approves it in the webapp, and
|
|
146
|
+
the client receives a token. Pending requests are stored in dserver's SQL
|
|
147
|
+
database (table `oauth2_device_authorization`), so run the database migrations
|
|
148
|
+
(`flask db migrate` / `flask db upgrade`) after installing or upgrading the
|
|
149
|
+
plugin.
|
|
150
|
+
|
|
151
|
+
| Variable | Description | Default |
|
|
152
|
+
|----------|-------------|---------|
|
|
153
|
+
| `OAUTH2_DEVICE_VERIFICATION_URL` | Page at which users approve device logins (the webapp, which opens its approval dialog for `?user_code=...`) | `OAUTH2_FRONTEND_URL` if absolute, otherwise `OAUTH2_BASE_URL` |
|
|
154
|
+
| `OAUTH2_DEVICE_CODE_LIFETIME_SECONDS` | Time to approve a device login | `600` |
|
|
155
|
+
| `OAUTH2_DEVICE_POLL_INTERVAL` | Minimum polling interval of clients in seconds | `5` |
|
|
156
|
+
|
|
157
|
+
#### Redirect URLs
|
|
158
|
+
|
|
159
|
+
| Variable | Description | Default |
|
|
160
|
+
|----------|-------------|---------|
|
|
161
|
+
| `OAUTH2_FRONTEND_URL` | Frontend base URL | `/` |
|
|
162
|
+
| `OAUTH2_LOGIN_SUCCESS_REDIRECT` | Redirect after successful login | `/` |
|
|
163
|
+
| `OAUTH2_LOGIN_ERROR_REDIRECT` | Redirect after failed login | `/login?error=auth_failed` |
|
|
164
|
+
| `OAUTH2_DELIVER_TOKEN_IN_FRAGMENT` | Deliver the JWT in the redirect URL fragment (`#token=...`). Set to `false` for session-only delivery: the token never appears in any URL and the (same-origin) webapp fetches it from `GET /auth/oauth2/token` using the session cookie | `true` |
|
|
165
|
+
|
|
166
|
+
## API Endpoints
|
|
167
|
+
|
|
168
|
+
All endpoints are under the `/auth/oauth2` prefix.
|
|
169
|
+
|
|
170
|
+
### Authentication Flow
|
|
171
|
+
|
|
172
|
+
| Endpoint | Method | Description |
|
|
173
|
+
|----------|--------|-------------|
|
|
174
|
+
| `/auth/oauth2/login` | GET | Initiates OAuth2 flow, redirects to provider |
|
|
175
|
+
| `/auth/oauth2/callback` | GET | Receives authorization code, exchanges for token |
|
|
176
|
+
| `/auth/oauth2/logout` | GET/POST | Clears the server-side session; redirects browsers to the frontend, returns JSON to clients that request it |
|
|
177
|
+
|
|
178
|
+
### Token Management
|
|
179
|
+
|
|
180
|
+
| Endpoint | Method | Description |
|
|
181
|
+
|----------|--------|-------------|
|
|
182
|
+
| `/auth/oauth2/token` | GET | Returns current user's JWT token |
|
|
183
|
+
| `/auth/oauth2/refresh` | POST | Refresh an existing JWT token, at most until `JWT_MAX_SESSION_HOURS` after the original login |
|
|
184
|
+
| `/auth/oauth2/verify` | POST | Verify a token and return its claims |
|
|
185
|
+
|
|
186
|
+
### Device Authorization (RFC 8628)
|
|
187
|
+
|
|
188
|
+
| Endpoint | Method | Description |
|
|
189
|
+
|----------|--------|-------------|
|
|
190
|
+
| `/auth/oauth2/device` | POST | Start a device login; returns `device_code`, `user_code`, `verification_uri(_complete)`, `expires_in`, `interval`. Optional body: `{"client_name": "..."}` |
|
|
191
|
+
| `/auth/oauth2/device/token` | POST | Poll with `grant_type=urn:ietf:params:oauth:grant-type:device_code` and `device_code` (JSON or form). Returns `authorization_pending`, `slow_down`, `access_denied` or `expired_token` errors until approved, then the token (once) |
|
|
192
|
+
| `/auth/oauth2/device/<user_code>` | GET | Describe a pending request (requires `Authorization: Bearer <token>`) |
|
|
193
|
+
| `/auth/oauth2/device/<user_code>/approve` | POST | Approve a request (requires `Authorization: Bearer <token>`) |
|
|
194
|
+
| `/auth/oauth2/device/<user_code>/deny` | POST | Deny a request (requires `Authorization: Bearer <token>`) |
|
|
195
|
+
|
|
196
|
+
The token issued to the device inherits the login time (`auth_time`) of the
|
|
197
|
+
approving session, so a device session ends when the approving browser
|
|
198
|
+
session would, and approving cannot extend an expired session.
|
|
199
|
+
|
|
200
|
+
### Information
|
|
201
|
+
|
|
202
|
+
| Endpoint | Method | Description |
|
|
203
|
+
|----------|--------|-------------|
|
|
204
|
+
| `/auth/oauth2/info` | GET | Returns configured provider information |
|
|
205
|
+
|
|
206
|
+
### Response Examples
|
|
207
|
+
|
|
208
|
+
**GET /auth/oauth2/token** (authenticated user):
|
|
209
|
+
```json
|
|
210
|
+
{
|
|
211
|
+
"token": "eyJhbGciOiJSUzI1NiIs...",
|
|
212
|
+
"username": "user@example.com",
|
|
213
|
+
"provider": "oauth2",
|
|
214
|
+
"token_type": "Bearer"
|
|
215
|
+
}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
**GET /auth/oauth2/info**:
|
|
219
|
+
```json
|
|
220
|
+
{
|
|
221
|
+
"provider": "orcid",
|
|
222
|
+
"login_url": "http://localhost:5000/auth/oauth2/login",
|
|
223
|
+
"configured": true
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## CLI Commands
|
|
228
|
+
|
|
229
|
+
The plugin provides Flask CLI commands for management and debugging:
|
|
230
|
+
|
|
231
|
+
```bash
|
|
232
|
+
# Display current configuration
|
|
233
|
+
flask oauth2 show-config
|
|
234
|
+
|
|
235
|
+
# List available provider presets
|
|
236
|
+
flask oauth2 list-presets
|
|
237
|
+
|
|
238
|
+
# Show environment variables for a specific preset
|
|
239
|
+
flask oauth2 show-preset <provider>
|
|
240
|
+
|
|
241
|
+
# Validate the current configuration
|
|
242
|
+
flask oauth2 validate-config
|
|
243
|
+
|
|
244
|
+
# Test connectivity to the OAuth2 provider
|
|
245
|
+
flask oauth2 test-connection
|
|
246
|
+
|
|
247
|
+
# Generate a test JWT token
|
|
248
|
+
flask oauth2 test-jwt --username test@example.com
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
## How It Works
|
|
252
|
+
|
|
253
|
+
The plugin implements the standard OAuth 2.0 Authorization Code flow:
|
|
254
|
+
|
|
255
|
+
```
|
|
256
|
+
┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐
|
|
257
|
+
│ User │ │ dserver │ │ OAuth2 │ │ Frontend │
|
|
258
|
+
│ Browser │ │ Plugin │ │ Provider │ │ App │
|
|
259
|
+
└────┬─────┘ └────┬─────┘ └────┬─────┘ └────┬─────┘
|
|
260
|
+
│ │ │ │
|
|
261
|
+
│ 1. Click Login │ │ │
|
|
262
|
+
│───────────────>│ │ │
|
|
263
|
+
│ │ │ │
|
|
264
|
+
│ 2. Redirect to Provider │ │
|
|
265
|
+
│<───────────────│ │ │
|
|
266
|
+
│ │ │ │
|
|
267
|
+
│ 3. Authenticate│ │ │
|
|
268
|
+
│────────────────────────────────>│ │
|
|
269
|
+
│ │ │ │
|
|
270
|
+
│ 4. Authorization Code │ │
|
|
271
|
+
│<────────────────────────────────│ │
|
|
272
|
+
│ │ │ │
|
|
273
|
+
│ 5. Code to Plugin │ │
|
|
274
|
+
│───────────────>│ │ │
|
|
275
|
+
│ │ │ │
|
|
276
|
+
│ │ 6. Exchange Code for Token │
|
|
277
|
+
│ │───────────────>│ │
|
|
278
|
+
│ │ │ │
|
|
279
|
+
│ │ 7. Access Token + User Info │
|
|
280
|
+
│ │<───────────────│ │
|
|
281
|
+
│ │ │ │
|
|
282
|
+
│ │ 8. Generate JWT│ │
|
|
283
|
+
│ │────────┐ │ │
|
|
284
|
+
│ │ │ │ │
|
|
285
|
+
│ │<───────┘ │ │
|
|
286
|
+
│ │ │ │
|
|
287
|
+
│ 9. Redirect with JWT Cookie │ │
|
|
288
|
+
│<───────────────│ │ │
|
|
289
|
+
│ │ │ │
|
|
290
|
+
│ 10. Access Frontend │ │
|
|
291
|
+
│─────────────────────────────────────────────────>│
|
|
292
|
+
│ │ │ │
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
## Provider-Specific Documentation
|
|
296
|
+
|
|
297
|
+
See the `provider-docs/` directory for detailed setup guides:
|
|
298
|
+
|
|
299
|
+
- **[ORCID](provider-docs/orcid.md)** - Researcher identification (recommended for academic use)
|
|
300
|
+
|
|
301
|
+
## Security Considerations
|
|
302
|
+
|
|
303
|
+
1. **Always use HTTPS in production** - OAuth2 tokens are transmitted in URLs and headers
|
|
304
|
+
2. **Keep client secrets secure** - Never commit secrets to version control
|
|
305
|
+
3. **Validate redirect URIs** - Ensure they match exactly in provider configuration
|
|
306
|
+
4. **Use state parameter** - The plugin automatically handles CSRF protection via OAuth2 state
|
|
307
|
+
5. **Protect JWT keys** - Restrict file permissions on private key files
|
|
308
|
+
6. **Bounded sessions** - Tokens record the login time (`auth_time`); refresh and device approval never extend a session beyond `JWT_MAX_SESSION_HOURS` after the login. dserver cannot revoke tokens, so keep `JWT_TOKEN_EXPIRY_HOURS` and `JWT_MAX_SESSION_HOURS` as short as practical
|
|
309
|
+
7. **Audience and issuer** - Tokens carry `aud` and `iss`, which dserver verifies by default. They follow dserver's `JWT_DECODE_AUDIENCE` / `JWT_DECODE_ISSUER` (default `dserver`); set `JWT_AUDIENCE` / `JWT_ISSUER` only to issue other values
|
|
310
|
+
|
|
311
|
+
## Development
|
|
312
|
+
|
|
313
|
+
### Running Tests
|
|
314
|
+
|
|
315
|
+
```bash
|
|
316
|
+
# Install development dependencies
|
|
317
|
+
pip install -e ".[dev]"
|
|
318
|
+
|
|
319
|
+
# Run tests
|
|
320
|
+
pytest
|
|
321
|
+
|
|
322
|
+
# Run with coverage
|
|
323
|
+
pytest --cov=dserver_token_generator_plugin_oauth2
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
### Adding a New Provider Preset
|
|
327
|
+
|
|
328
|
+
Edit `config.py` and add to the `PROVIDER_PRESETS` dictionary:
|
|
329
|
+
|
|
330
|
+
```python
|
|
331
|
+
PROVIDER_PRESETS = {
|
|
332
|
+
"your_provider": {
|
|
333
|
+
"name": "your_provider",
|
|
334
|
+
"authorization_url": "https://provider.com/oauth/authorize",
|
|
335
|
+
"token_url": "https://provider.com/oauth/token",
|
|
336
|
+
"userinfo_url": "https://provider.com/userinfo", # if applicable
|
|
337
|
+
"scope": "openid email profile",
|
|
338
|
+
"attribute_map": {
|
|
339
|
+
"sub": "user_id",
|
|
340
|
+
"email": "email",
|
|
341
|
+
"name": "display_name",
|
|
342
|
+
},
|
|
343
|
+
"username_field": "email",
|
|
344
|
+
},
|
|
345
|
+
}
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
## License
|
|
349
|
+
|
|
350
|
+
MIT License
|
|
351
|
+
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
# dserver-token-generator-plugin-oauth2
|
|
2
|
+
|
|
3
|
+
A generic OAuth 2.0 / OpenID Connect token generator plugin for dserver that provides Single Sign-On (SSO) authentication with any OAuth2/OIDC compliant Identity Provider.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **OAuth 2.0 Authorization Code flow** with PKCE support
|
|
8
|
+
- **OpenID Connect** support for providers that offer it
|
|
9
|
+
- **JWT token generation** after successful authentication
|
|
10
|
+
- **User lookup** in dserver: users must be registered in dserver beforehand; their display name is filled in from the provider
|
|
11
|
+
- **Device authorization flow** ([RFC 8628](https://www.rfc-editor.org/rfc/rfc8628)) for command-line clients such as `dtool dserver login`
|
|
12
|
+
- **Bounded sessions**: tokens can be refreshed, but at most `JWT_MAX_SESSION_HOURS` after the original login
|
|
13
|
+
- **Flexible attribute mapping** to adapt to any provider's response format
|
|
14
|
+
- **Built-in presets** for popular providers
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pip install dserver-token-generator-plugin-oauth2
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Or for development:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install -e .
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick Start
|
|
29
|
+
|
|
30
|
+
1. Choose your OAuth2 provider and register an application to get client credentials
|
|
31
|
+
2. Configure the environment variables (see Configuration below)
|
|
32
|
+
3. Run the database migrations (`flask db migrate` / `flask db upgrade`) for the device-login table
|
|
33
|
+
4. Register users in dserver (see [Users](#users)) and restart dserver
|
|
34
|
+
5. Users can authenticate in the browser at `/auth/oauth2/login`, and on the command line with `dtool dserver login <server>` (from [dtool-dserver](https://github.com/jic-dtool/dtool-dserver))
|
|
35
|
+
|
|
36
|
+
## Configuration
|
|
37
|
+
|
|
38
|
+
### Required Environment Variables
|
|
39
|
+
|
|
40
|
+
| Variable | Description |
|
|
41
|
+
|----------|-------------|
|
|
42
|
+
| `OAUTH2_AUTHORIZATION_URL` | Provider's authorization endpoint |
|
|
43
|
+
| `OAUTH2_TOKEN_URL` | Provider's token endpoint |
|
|
44
|
+
| `OAUTH2_CLIENT_ID` | Your application's client ID |
|
|
45
|
+
| `OAUTH2_CLIENT_SECRET` | Your application's client secret |
|
|
46
|
+
|
|
47
|
+
**Security Note**: For credentials (`OAUTH2_CLIENT_ID` and `OAUTH2_CLIENT_SECRET`), use a `.env` file instead of hardcoding them in `docker-compose.yml`:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
# Copy the template
|
|
51
|
+
cp .env.template .env
|
|
52
|
+
|
|
53
|
+
# Edit with your credentials
|
|
54
|
+
nano .env
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The `docker-compose.yml` uses variable substitution to read these values:
|
|
58
|
+
```yaml
|
|
59
|
+
OAUTH2_CLIENT_ID: "${OAUTH2_CLIENT_ID:-}"
|
|
60
|
+
OAUTH2_CLIENT_SECRET: "${OAUTH2_CLIENT_SECRET:-}"
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Optional Environment Variables
|
|
64
|
+
|
|
65
|
+
#### Provider Settings
|
|
66
|
+
|
|
67
|
+
| Variable | Description | Default |
|
|
68
|
+
|----------|-------------|---------|
|
|
69
|
+
| `OAUTH2_PROVIDER_NAME` | Identifier for the provider | `oauth2` |
|
|
70
|
+
| `OAUTH2_BASE_URL` | Base URL of your dserver instance | `http://localhost:5000` |
|
|
71
|
+
| `OAUTH2_USERINFO_URL` | OIDC userinfo endpoint | (none) |
|
|
72
|
+
| `OAUTH2_SCOPE` | OAuth2 scopes to request | `openid` |
|
|
73
|
+
| `OAUTH2_REDIRECT_URI` | Callback URL | `{OAUTH2_BASE_URL}/auth/oauth2/callback` |
|
|
74
|
+
|
|
75
|
+
#### Attribute Mapping
|
|
76
|
+
|
|
77
|
+
The plugin needs to know how to extract user information from the OAuth2 provider's response. Different providers return user data in different formats.
|
|
78
|
+
|
|
79
|
+
| Variable | Description | Default |
|
|
80
|
+
|----------|-------------|---------|
|
|
81
|
+
| `OAUTH2_ATTRIBUTE_MAP` | Maps provider fields to internal fields | `sub:user_id,email:email,name:display_name` |
|
|
82
|
+
| `OAUTH2_USERNAME_FIELD` | Which internal field to use as username | `user_id` |
|
|
83
|
+
|
|
84
|
+
**Format**: `provider_field:internal_field,provider_field2:internal_field2`
|
|
85
|
+
|
|
86
|
+
**Internal fields**:
|
|
87
|
+
- `user_id` - Unique identifier from the provider
|
|
88
|
+
- `username` - Username (if different from user_id)
|
|
89
|
+
- `email` - Email address
|
|
90
|
+
- `display_name` - Full display name
|
|
91
|
+
- `given_name` - First name
|
|
92
|
+
- `surname` - Last name
|
|
93
|
+
|
|
94
|
+
#### JWT Settings
|
|
95
|
+
|
|
96
|
+
| Variable | Description | Default |
|
|
97
|
+
|----------|-------------|---------|
|
|
98
|
+
| `JWT_PRIVATE_KEY_FILE` | Path to RSA private key | `/app/jwt/jwt_key` |
|
|
99
|
+
| `JWT_PUBLIC_KEY_FILE` | Path to RSA public key | `/app/jwt/jwt_key.pub` |
|
|
100
|
+
| `JWT_ALGORITHM` | Signing algorithm | `RS256` |
|
|
101
|
+
| `JWT_ISSUER` | Token issuer claim; must match dserver's `JWT_DECODE_ISSUER` | dserver's `JWT_DECODE_ISSUER`, else `dserver` |
|
|
102
|
+
| `JWT_AUDIENCE` | Token audience claim; must match dserver's `JWT_DECODE_AUDIENCE` | dserver's `JWT_DECODE_AUDIENCE`, else `dserver` |
|
|
103
|
+
| `JWT_TOKEN_EXPIRY_HOURS` | Token validity period | `24` |
|
|
104
|
+
| `JWT_MAX_SESSION_HOURS` | Maximum session age: tokens can be refreshed until this long after the original login (recorded in the `auth_time` claim), then the user must log in again | `168` |
|
|
105
|
+
|
|
106
|
+
#### Users
|
|
107
|
+
|
|
108
|
+
Users are **not** created on login: register them in dserver beforehand, with
|
|
109
|
+
the username the provider reports (e.g. the ORCID iD), and grant permissions:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
flask user add 0000-0002-1825-0097
|
|
113
|
+
flask user search_permission 0000-0002-1825-0097 s3://my-bucket
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
A login of an unregistered user yields a token that dserver rejects.
|
|
117
|
+
(`OAUTH2_AUTO_PROVISION_USERS` and `OAUTH2_DEFAULT_USER_PERMISSIONS` are read
|
|
118
|
+
but have no effect.)
|
|
119
|
+
|
|
120
|
+
#### Device Authorization (command-line login)
|
|
121
|
+
|
|
122
|
+
Command-line clients such as `dtool dserver login` use the device authorization
|
|
123
|
+
flow: the client shows a short code, the user approves it in the webapp, and
|
|
124
|
+
the client receives a token. Pending requests are stored in dserver's SQL
|
|
125
|
+
database (table `oauth2_device_authorization`), so run the database migrations
|
|
126
|
+
(`flask db migrate` / `flask db upgrade`) after installing or upgrading the
|
|
127
|
+
plugin.
|
|
128
|
+
|
|
129
|
+
| Variable | Description | Default |
|
|
130
|
+
|----------|-------------|---------|
|
|
131
|
+
| `OAUTH2_DEVICE_VERIFICATION_URL` | Page at which users approve device logins (the webapp, which opens its approval dialog for `?user_code=...`) | `OAUTH2_FRONTEND_URL` if absolute, otherwise `OAUTH2_BASE_URL` |
|
|
132
|
+
| `OAUTH2_DEVICE_CODE_LIFETIME_SECONDS` | Time to approve a device login | `600` |
|
|
133
|
+
| `OAUTH2_DEVICE_POLL_INTERVAL` | Minimum polling interval of clients in seconds | `5` |
|
|
134
|
+
|
|
135
|
+
#### Redirect URLs
|
|
136
|
+
|
|
137
|
+
| Variable | Description | Default |
|
|
138
|
+
|----------|-------------|---------|
|
|
139
|
+
| `OAUTH2_FRONTEND_URL` | Frontend base URL | `/` |
|
|
140
|
+
| `OAUTH2_LOGIN_SUCCESS_REDIRECT` | Redirect after successful login | `/` |
|
|
141
|
+
| `OAUTH2_LOGIN_ERROR_REDIRECT` | Redirect after failed login | `/login?error=auth_failed` |
|
|
142
|
+
| `OAUTH2_DELIVER_TOKEN_IN_FRAGMENT` | Deliver the JWT in the redirect URL fragment (`#token=...`). Set to `false` for session-only delivery: the token never appears in any URL and the (same-origin) webapp fetches it from `GET /auth/oauth2/token` using the session cookie | `true` |
|
|
143
|
+
|
|
144
|
+
## API Endpoints
|
|
145
|
+
|
|
146
|
+
All endpoints are under the `/auth/oauth2` prefix.
|
|
147
|
+
|
|
148
|
+
### Authentication Flow
|
|
149
|
+
|
|
150
|
+
| Endpoint | Method | Description |
|
|
151
|
+
|----------|--------|-------------|
|
|
152
|
+
| `/auth/oauth2/login` | GET | Initiates OAuth2 flow, redirects to provider |
|
|
153
|
+
| `/auth/oauth2/callback` | GET | Receives authorization code, exchanges for token |
|
|
154
|
+
| `/auth/oauth2/logout` | GET/POST | Clears the server-side session; redirects browsers to the frontend, returns JSON to clients that request it |
|
|
155
|
+
|
|
156
|
+
### Token Management
|
|
157
|
+
|
|
158
|
+
| Endpoint | Method | Description |
|
|
159
|
+
|----------|--------|-------------|
|
|
160
|
+
| `/auth/oauth2/token` | GET | Returns current user's JWT token |
|
|
161
|
+
| `/auth/oauth2/refresh` | POST | Refresh an existing JWT token, at most until `JWT_MAX_SESSION_HOURS` after the original login |
|
|
162
|
+
| `/auth/oauth2/verify` | POST | Verify a token and return its claims |
|
|
163
|
+
|
|
164
|
+
### Device Authorization (RFC 8628)
|
|
165
|
+
|
|
166
|
+
| Endpoint | Method | Description |
|
|
167
|
+
|----------|--------|-------------|
|
|
168
|
+
| `/auth/oauth2/device` | POST | Start a device login; returns `device_code`, `user_code`, `verification_uri(_complete)`, `expires_in`, `interval`. Optional body: `{"client_name": "..."}` |
|
|
169
|
+
| `/auth/oauth2/device/token` | POST | Poll with `grant_type=urn:ietf:params:oauth:grant-type:device_code` and `device_code` (JSON or form). Returns `authorization_pending`, `slow_down`, `access_denied` or `expired_token` errors until approved, then the token (once) |
|
|
170
|
+
| `/auth/oauth2/device/<user_code>` | GET | Describe a pending request (requires `Authorization: Bearer <token>`) |
|
|
171
|
+
| `/auth/oauth2/device/<user_code>/approve` | POST | Approve a request (requires `Authorization: Bearer <token>`) |
|
|
172
|
+
| `/auth/oauth2/device/<user_code>/deny` | POST | Deny a request (requires `Authorization: Bearer <token>`) |
|
|
173
|
+
|
|
174
|
+
The token issued to the device inherits the login time (`auth_time`) of the
|
|
175
|
+
approving session, so a device session ends when the approving browser
|
|
176
|
+
session would, and approving cannot extend an expired session.
|
|
177
|
+
|
|
178
|
+
### Information
|
|
179
|
+
|
|
180
|
+
| Endpoint | Method | Description |
|
|
181
|
+
|----------|--------|-------------|
|
|
182
|
+
| `/auth/oauth2/info` | GET | Returns configured provider information |
|
|
183
|
+
|
|
184
|
+
### Response Examples
|
|
185
|
+
|
|
186
|
+
**GET /auth/oauth2/token** (authenticated user):
|
|
187
|
+
```json
|
|
188
|
+
{
|
|
189
|
+
"token": "eyJhbGciOiJSUzI1NiIs...",
|
|
190
|
+
"username": "user@example.com",
|
|
191
|
+
"provider": "oauth2",
|
|
192
|
+
"token_type": "Bearer"
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
**GET /auth/oauth2/info**:
|
|
197
|
+
```json
|
|
198
|
+
{
|
|
199
|
+
"provider": "orcid",
|
|
200
|
+
"login_url": "http://localhost:5000/auth/oauth2/login",
|
|
201
|
+
"configured": true
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## CLI Commands
|
|
206
|
+
|
|
207
|
+
The plugin provides Flask CLI commands for management and debugging:
|
|
208
|
+
|
|
209
|
+
```bash
|
|
210
|
+
# Display current configuration
|
|
211
|
+
flask oauth2 show-config
|
|
212
|
+
|
|
213
|
+
# List available provider presets
|
|
214
|
+
flask oauth2 list-presets
|
|
215
|
+
|
|
216
|
+
# Show environment variables for a specific preset
|
|
217
|
+
flask oauth2 show-preset <provider>
|
|
218
|
+
|
|
219
|
+
# Validate the current configuration
|
|
220
|
+
flask oauth2 validate-config
|
|
221
|
+
|
|
222
|
+
# Test connectivity to the OAuth2 provider
|
|
223
|
+
flask oauth2 test-connection
|
|
224
|
+
|
|
225
|
+
# Generate a test JWT token
|
|
226
|
+
flask oauth2 test-jwt --username test@example.com
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## How It Works
|
|
230
|
+
|
|
231
|
+
The plugin implements the standard OAuth 2.0 Authorization Code flow:
|
|
232
|
+
|
|
233
|
+
```
|
|
234
|
+
┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐
|
|
235
|
+
│ User │ │ dserver │ │ OAuth2 │ │ Frontend │
|
|
236
|
+
│ Browser │ │ Plugin │ │ Provider │ │ App │
|
|
237
|
+
└────┬─────┘ └────┬─────┘ └────┬─────┘ └────┬─────┘
|
|
238
|
+
│ │ │ │
|
|
239
|
+
│ 1. Click Login │ │ │
|
|
240
|
+
│───────────────>│ │ │
|
|
241
|
+
│ │ │ │
|
|
242
|
+
│ 2. Redirect to Provider │ │
|
|
243
|
+
│<───────────────│ │ │
|
|
244
|
+
│ │ │ │
|
|
245
|
+
│ 3. Authenticate│ │ │
|
|
246
|
+
│────────────────────────────────>│ │
|
|
247
|
+
│ │ │ │
|
|
248
|
+
│ 4. Authorization Code │ │
|
|
249
|
+
│<────────────────────────────────│ │
|
|
250
|
+
│ │ │ │
|
|
251
|
+
│ 5. Code to Plugin │ │
|
|
252
|
+
│───────────────>│ │ │
|
|
253
|
+
│ │ │ │
|
|
254
|
+
│ │ 6. Exchange Code for Token │
|
|
255
|
+
│ │───────────────>│ │
|
|
256
|
+
│ │ │ │
|
|
257
|
+
│ │ 7. Access Token + User Info │
|
|
258
|
+
│ │<───────────────│ │
|
|
259
|
+
│ │ │ │
|
|
260
|
+
│ │ 8. Generate JWT│ │
|
|
261
|
+
│ │────────┐ │ │
|
|
262
|
+
│ │ │ │ │
|
|
263
|
+
│ │<───────┘ │ │
|
|
264
|
+
│ │ │ │
|
|
265
|
+
│ 9. Redirect with JWT Cookie │ │
|
|
266
|
+
│<───────────────│ │ │
|
|
267
|
+
│ │ │ │
|
|
268
|
+
│ 10. Access Frontend │ │
|
|
269
|
+
│─────────────────────────────────────────────────>│
|
|
270
|
+
│ │ │ │
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
## Provider-Specific Documentation
|
|
274
|
+
|
|
275
|
+
See the `provider-docs/` directory for detailed setup guides:
|
|
276
|
+
|
|
277
|
+
- **[ORCID](provider-docs/orcid.md)** - Researcher identification (recommended for academic use)
|
|
278
|
+
|
|
279
|
+
## Security Considerations
|
|
280
|
+
|
|
281
|
+
1. **Always use HTTPS in production** - OAuth2 tokens are transmitted in URLs and headers
|
|
282
|
+
2. **Keep client secrets secure** - Never commit secrets to version control
|
|
283
|
+
3. **Validate redirect URIs** - Ensure they match exactly in provider configuration
|
|
284
|
+
4. **Use state parameter** - The plugin automatically handles CSRF protection via OAuth2 state
|
|
285
|
+
5. **Protect JWT keys** - Restrict file permissions on private key files
|
|
286
|
+
6. **Bounded sessions** - Tokens record the login time (`auth_time`); refresh and device approval never extend a session beyond `JWT_MAX_SESSION_HOURS` after the login. dserver cannot revoke tokens, so keep `JWT_TOKEN_EXPIRY_HOURS` and `JWT_MAX_SESSION_HOURS` as short as practical
|
|
287
|
+
7. **Audience and issuer** - Tokens carry `aud` and `iss`, which dserver verifies by default. They follow dserver's `JWT_DECODE_AUDIENCE` / `JWT_DECODE_ISSUER` (default `dserver`); set `JWT_AUDIENCE` / `JWT_ISSUER` only to issue other values
|
|
288
|
+
|
|
289
|
+
## Development
|
|
290
|
+
|
|
291
|
+
### Running Tests
|
|
292
|
+
|
|
293
|
+
```bash
|
|
294
|
+
# Install development dependencies
|
|
295
|
+
pip install -e ".[dev]"
|
|
296
|
+
|
|
297
|
+
# Run tests
|
|
298
|
+
pytest
|
|
299
|
+
|
|
300
|
+
# Run with coverage
|
|
301
|
+
pytest --cov=dserver_token_generator_plugin_oauth2
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### Adding a New Provider Preset
|
|
305
|
+
|
|
306
|
+
Edit `config.py` and add to the `PROVIDER_PRESETS` dictionary:
|
|
307
|
+
|
|
308
|
+
```python
|
|
309
|
+
PROVIDER_PRESETS = {
|
|
310
|
+
"your_provider": {
|
|
311
|
+
"name": "your_provider",
|
|
312
|
+
"authorization_url": "https://provider.com/oauth/authorize",
|
|
313
|
+
"token_url": "https://provider.com/oauth/token",
|
|
314
|
+
"userinfo_url": "https://provider.com/userinfo", # if applicable
|
|
315
|
+
"scope": "openid email profile",
|
|
316
|
+
"attribute_map": {
|
|
317
|
+
"sub": "user_id",
|
|
318
|
+
"email": "email",
|
|
319
|
+
"name": "display_name",
|
|
320
|
+
},
|
|
321
|
+
"username_field": "email",
|
|
322
|
+
},
|
|
323
|
+
}
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
## License
|
|
327
|
+
|
|
328
|
+
MIT License
|