~sircmpwn/core.sr.ht

core.sr.ht/srht/oauth/__init__.py -rw-r--r-- 2.2 KiB
04703e6aDrew DeVault layout.html: remove workaround for chromium#332189 8 days 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
from flask import g, request, redirect, current_app
from functools import wraps
from srht.crypto import fernet
from srht.validation import Validation
from werkzeug.local import LocalProxy
import json

current_token = LocalProxy(lambda:
        g.current_oauth_token if "current_oauth_token" in g else None)
"""
Proxy for the currently authorized OAuth token. The type is implementation
defined, it's populated from the return value of
AbstractOAuthService.get_token.
"""

current_user = LocalProxy(lambda:
        g.current_user if "current_user" in g else None)
"""
Proxy for the currently authorzied user. The type is implementation defined,
it's populated from the return value of AbstractOAuthService.get_user.
"""

def login_user(user, set_cookie=False):
    g.current_user = user
    g.set_current_user = set_cookie

def logout_user():
    g.current_user = None
    g.set_current_user = True

def freshen_user():
    g.set_current_user = True

def loginrequired(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        from srht.oauth import UserType
        if not current_user:
            return redirect(current_app.oauth_service.oauth_url(request.url))
        elif current_user.user_type == UserType.suspended:
            return f"Your account has been suspended for the following reason: {current_user.suspension_notice}. Contact support.", 401
        else:
            return f(*args, **kwargs)
    return wrapper

class OAuthError(Exception):
    def __init__(self, err, *args, status=401, **kwargs):
        super().__init__(*args, **kwargs)
        if isinstance(err, dict):
            self.response = err, status
        else:
            valid = Validation(request)
            valid.error(err, status=status)
            self.response = valid.response
        self.status = status

from srht.oauth.client import OAuthClientMixin
from srht.oauth.token import OAuthTokenMixin, ExternalOAuthTokenMixin
from srht.oauth.user import UserMixin, UserType, ExternalUserMixin

from srht.oauth.blueprint import oauth_blueprint
from srht.oauth.decorator import oauth
from srht.oauth.scope import OAuthScope
from srht.oauth.interface import AbstractOAuthService, AbstractOAuthProvider
from srht.oauth.interface import DelegatedScope