aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/requests-oauthlib/requests_oauthlib/compliance_fixes/facebook.py
blob: f44558a83902a76022c38f2310a7eddb39cee150 (plain) (blame)
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
from json import dumps
from urllib.parse import parse_qsl


def facebook_compliance_fix(session):
    def _compliance_fix(r):
        # if Facebook claims to be sending us json, let's trust them.
        if "application/json" in r.headers.get("content-type", {}):
            return r

        # Facebook returns a content-type of text/plain when sending their
        # x-www-form-urlencoded responses, along with a 200. If not, let's
        # assume we're getting JSON and bail on the fix.
        if "text/plain" in r.headers.get("content-type", {}) and r.status_code == 200:
            token = dict(parse_qsl(r.text, keep_blank_values=True))
        else:
            return r

        expires = token.get("expires")
        if expires is not None:
            token["expires_in"] = expires
        token["token_type"] = "Bearer"
        r._content = dumps(token).encode()
        return r

    session.register_compliance_hook("access_token_response", _compliance_fix)
    return session