~sircmpwn/meta.sr.ht

ref: 72548bd7545f78670878667674cc7645835a17bd meta.sr.ht/metasrht/blueprints/oauth_exchange.py -rw-r--r-- 10.9 KiB
72548bd7Drew DeVault API: Updates per core-go auth changes 1 year, 1 month ago
                                                                                
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
import hashlib
import json
import os
import urllib
from datetime import datetime, timedelta
from flask import Blueprint, render_template, request, redirect
from metasrht.audit import audit_log
from metasrht.oauth import OAuthScope
from metasrht.types import OAuthClient, OAuthToken, User, RevocationUrl
from srht.database import db
from srht.flask import csrf_bypass
from srht.oauth import current_user, loginrequired
from srht.redis import redis
from srht.validation import Validation

oauth_exchange = Blueprint('oauth_exchange', __name__)

def _oauth_redirect(redirect_uri, **params):
    parts = list(urllib.parse.urlparse(redirect_uri))
    parsed = urllib.parse.parse_qs(parts[4])
    parsed.update(params)
    parts[4] = urllib.parse.urlencode(parsed)
    return redirect(urllib.parse.urlunparse(parts))

def _oauth_exchange(client, scopes, state, redirect_uri):
    token = hashlib.sha512(os.urandom(8)).hexdigest()[:12]
    scopes = ','.join([str(s) for s in scopes])
    stash = {
        "user_id": current_user.id,
        "client_id": client.id,
        "scopes": scopes
    }
    redis.set(token, json.dumps(stash), ex=(15 * 60))

    redirect_params = {
        "exchange": token,
        "scopes": scopes,
    }
    if state:
        redirect_params["state"] = state

    if redirect_uri == "urn:ietf:wg:oauth:2.0:oob":
        return render_template("oauth-oob.html",
            exchange_token=token), 200

    return _oauth_redirect(redirect_uri, **redirect_params)

@oauth_exchange.route("/oauth/authorize")
@loginrequired
def oauth_authorize_GET():
    client_id = request.args.get('client_id')
    scopes = request.args.get('scopes')
    redirect_uri = request.args.get('redirect_uri')
    state = request.args.get('state')
    client = OAuthClient.query.filter(OAuthClient.client_id == client_id).first()

    # workaround to enable rfc6749 interoperability
    if not scopes:
        scopes = request.args.get('scope')

    if not client_id or not client:
        return render_template("oauth-error.html",
                details="Unknown client ID"), 400

    if not redirect_uri:
        redirect_uri = client.redirect_uri

    if not client.redirect_uri.startswith(redirect_uri):
        return _oauth_redirect(redirect_uri, error='invalid_redirect',
                details='The URI provided must use be a subpath of your '
                    'configured redirect URI')

    try:
        scopes = set(OAuthScope(s) for s in scopes.split(','))
        if not OAuthScope('profile:read') in scopes:
            scopes.update([OAuthScope('profile:read')])
    except Exception as ex:
        return _oauth_redirect(redirect_uri,
                error='invalid_scope', details=ex.args[0])

    if redirect_uri != "urn:ietf:wg:oauth:2.0:oob":
        previous = (OAuthToken.query
            .filter(OAuthToken.user_id == current_user.id)
            .filter(OAuthToken.client_id == client.id)
            .filter(OAuthToken.expires > datetime.utcnow())
        ).first()

        if previous:
            if set(previous.scopes) == scopes:
                return _oauth_exchange(client, scopes, state, redirect_uri)
        if client.preauthorized:
            return _oauth_exchange(client, scopes, state, redirect_uri)

    return render_template("oauth-authorize.html",
            client=client, scopes=scopes,
            redirect_uri=redirect_uri, state=state)

@oauth_exchange.route("/oauth/authorize", methods=["POST"])
@loginrequired
def oauth_authorize_POST():
    client_id = request.form.get('client_id')
    redirect_uri = request.form.get('redirect_uri')
    state = request.form.get('state')
    client = (OAuthClient.query
        .filter(OAuthClient.client_id == client_id)
    ).one_or_none()

    if not client_id or not client:
        return render_template("oauth-error.html",
                details="Unknown client ID"), 400

    if not redirect_uri:
        redirect_uri = client.redirect_uri

    if not client.redirect_uri.startswith(redirect_uri):
        return _oauth_redirect(redirect_uri,
                error='invalid_redirect',
                details='The URI provided must use be a subpath of your '
                    'configured redirect URI')

    if not "accept" in request.form:
        return _oauth_redirect(redirect_uri, error='user_declined',
                details='User declined to grant access to your application')

    scopes = set()

    for key in request.form:
        if key in [
                "client_id",
                "state",
                "redirect_uri",
                "accept",
                "_csrf_token"
            ]:
            continue
        value = request.form.get(key)
        if not value:
            continue
        try:
            scope = OAuthScope(key)
            scopes.update([str(scope)])
        except Exception as ex:
            return render_template("oauth-error.html", details=str(ex)), 400

    if not OAuthScope('profile:read') in scopes:
        scopes.update([OAuthScope('profile:read')])

    return _oauth_exchange(client, scopes, state, redirect_uri)

@csrf_bypass
@oauth_exchange.route("/oauth/exchange", methods=["POST"])
def oauth_exchange_POST():
    valid = Validation(request)
    client_id = valid.require('client_id')
    client_secret = valid.require('client_secret')
    exchange = valid.require('exchange')
    if not valid.ok:
        return valid.response

    client = (OAuthClient.query
        .filter(OAuthClient.client_id == client_id)).one_or_none()
    valid.expect(client, 'Unknown client ID')
    if not valid.ok:
        return valid.response

    client_secret_hash = hashlib.sha512(client_secret.encode()).hexdigest()
    valid.expect(client_secret_hash == client.client_secret_hash,
            'Invalid client secret')
    if not valid.ok:
        return valid.response

    stash = redis.get(exchange)
    valid.expect(stash, 'Exchange token expired')
    if not valid.ok:
        return valid.response

    stash = json.loads(stash.decode())
    redis.delete(exchange)

    user = stash.get('user_id')
    scopes = stash.get('scopes')
    user = User.query.filter(User.id == user).first()
    valid.expect(user, "Unknown user ID stored for "
        "this exchange token (this isn't supposed to happen")
    if not valid.ok:
        return valid.response

    previous = (OAuthToken.query
        .filter(OAuthToken.user_id == user.id)
        .filter(OAuthToken.client_id == client.id)
    ).one_or_none()
    if not previous:
        oauth_token = OAuthToken(user, client)
    else:
        oauth_token = previous
        previous.expires = datetime.utcnow() + timedelta(days=365)

    oauth_token.scopes = [OAuthScope(s) for s in scopes.split(",")]
    token = oauth_token.gen_token()
    if not client.preauthorized:
        audit_log("oauth token issued",
                "issued oauth token {} to client {}".format(
                    oauth_token.token_partial, client.client_id), user=user)
    if not previous:
        db.session.add(oauth_token)
    db.session.commit()

    return {
        "token": token,
        "expires": oauth_token.expires
    }

@csrf_bypass
@oauth_exchange.route("/oauth/token/<token>", methods=["POST"])
def oauth_token_legacy_POST(token):
    # TODO: Remove this once deployments are complete
    valid = Validation(request)
    client_id = valid.require("client_id")
    client_secret = valid.require("client_secret")
    revocation_url = valid.require("revocation_url")
    if not valid.ok:
        return valid.response

    client = (OAuthClient.query
        .filter(OAuthClient.client_id == client_id)
    ).one_or_none()
    if not client:
        return { "errors": [ { "reason": "404 not found" } ] }, 404

    client_secret_hash = hashlib.sha512(client_secret.encode()).hexdigest()
    valid.expect(client_secret_hash == client.client_secret_hash,
            "Invalid client secret")
    if not valid.ok:
        return valid.response

    h = hashlib.sha512(token.encode()).hexdigest()
    oauth_token = (OAuthToken.query
        .filter(OAuthToken.token_hash == h)
    ).one_or_none()
    valid.expect(oauth_token is not None
            and oauth_token.expires > datetime.utcnow(),
            "Invalid or expired OAuth token")
    if not valid.ok:
        return valid.response

    rev = (RevocationUrl.query
            .filter(RevocationUrl.token_id == oauth_token.id)
            .filter(RevocationUrl.client_id == client.id)).first()
    if not rev:
        rev = RevocationUrl(client, oauth_token, revocation_url)
        db.session.add(rev)
    else:
        rev.url = revocation_url
    db.session.commit()

    if oauth_token._scopes == "*":
        return { "expires": oauth_token.expires, "scopes": "*" }

    scopes = [
        str(s) for s in oauth_token.scopes
        if (s.client_id and s.client_id == client.client_id)
            or s == OAuthScope("profile:read")
    ]
    valid.expect(any(scopes), "Invalid or expired OAuth token")
    if not valid.ok:
        return valid.response

    scopes = ",".join(scopes)

    return {
        "expires": oauth_token.expires,
        "scopes": ",".join(str(s) for s in oauth_token.scopes)
    }

@csrf_bypass
@oauth_exchange.route("/oauth/token/verify", methods=["POST"])
def oauth_token_POST():
    valid = Validation(request)
    token = valid.require("oauth_token")
    client_id = valid.require("client_id")
    client_secret = valid.require("client_secret")
    revocation_url = valid.require("revocation_url")
    if not valid.ok:
        return valid.response

    client = (OAuthClient.query
        .filter(OAuthClient.client_id == client_id)
    ).one_or_none()
    if not client:
        return { "errors": [ { "reason": "404 not found" } ] }, 404

    client_secret_hash = hashlib.sha512(client_secret.encode()).hexdigest()
    valid.expect(client_secret_hash == client.client_secret_hash,
            "Invalid client secret")
    if not valid.ok:
        return valid.response

    h = hashlib.sha512(token.encode()).hexdigest()
    oauth_token = (OAuthToken.query
        .filter(OAuthToken.token_hash == h)
    ).one_or_none()
    valid.expect(oauth_token is not None
            and oauth_token.expires > datetime.utcnow(),
            "Invalid or expired OAuth token")
    if not valid.ok:
        return valid.response

    rev = (RevocationUrl.query
            .filter(RevocationUrl.token_id == oauth_token.id)
            .filter(RevocationUrl.client_id == client.id)).first()
    if not rev:
        rev = RevocationUrl(client, oauth_token, revocation_url)
        db.session.add(rev)
    else:
        rev.url = revocation_url
    db.session.commit()

    if oauth_token._scopes == "*":
        return { "expires": oauth_token.expires, "scopes": "*" }

    scopes = [
        str(s) for s in oauth_token.scopes
        if (s.client_id and s.client_id == client.client_id)
            or s == OAuthScope("profile:read")
    ]
    valid.expect(any(scopes), "Invalid or expired OAuth token")
    if not valid.ok:
        return valid.response

    scopes = ",".join(scopes)

    return {
        "expires": oauth_token.expires,
        "scopes": ",".join(str(s) for s in oauth_token.scopes)
    }