def oauth_redirect():
    # https://www.patreon.com/platform/documentation/oauth -- Step 2
    # After authorizing this app to access their Patreon data, the user is redirected back here.
    if request.args.get('state') != config.oauth_csrf:
        abort(403)

    # https://www.patreon.com/platform/documentation/oauth -- Step 3
    # Use the code provided as a query parameter to get the user's access token and refresh token
    oauth_client = patreon.OAuth(config.patreon_client_id,
                                 config.patreon_client_secret)
    tokens = oauth_client.get_tokens(request.args.get('code'),
                                     'http://localhost:5000/oauth/redirect')

    # https://www.patreon.com/platform/documentation/oauth -- Step 4
    # Save off the user's tokens and fetch their Patreon data.
    user = patreon_user_mgr.update_user_for_tokens(
        patreon_refresh_token=tokens['refresh_token'],
        patreon_access_token=tokens['access_token'])

    # https://www.patreon.com/platform/documentation/oauth -- Step 5
    # If the user signed in successfully, take them to their profile page.
    if user:
        return redirect('/users/{user_id}'.format(user_id=user.user_id))
    else:
        abort(403)
Exemple #2
0
    def __init__(self, config_path: str):
        self.config_path = config_path
        with open(config_path, mode='r') as fp:
            data = json.load(fp)
            self.config = PatreonConfig(**data)

        self._client = patreon.OAuth(client_id=self.config.client_id,
                                     client_secret=self.config.client_secret)
Exemple #3
0
 def authenticate(self):
     """Authenticate with Patreon API"""
     oauth_client = patreon.OAuth(self.client_id, self.client_secret)
     tokens = oauth_client.get_tokens(request.args.get('code'),
                                      '/oauth/redirect')
     access_token = tokens['access_token']
     api_client = patreon.API(access_token)
     return api_client
Exemple #4
0
def patreon_callback():
    patreon_oauth_client = patreon.OAuth(config["patreon-client-id"], config["patreon-client-secret"])
    tokens = patreon_oauth_client.get_tokens(request.args.get("code"), url_for("user.patreon_callback", _external=True))
    if "error" in tokens:
        if "patreon" in session:
            del session["patreon"]
        return redirect(url_for("user.patreon_landing"))
    session["patreon"] = tokens
    return redirect(url_for("user.patreon_sync_get"))
Exemple #5
0
    def getApiClient(self, request):
        """ Called after callback was triggered to fetch acccess_token and
        API instance.
        Returns (token, api)
        """
        oauth_client = patreon.OAuth(self.client_id, self.client_secret)
        tokens = oauth_client.get_tokens(request.query.code, self.callback)
        access_token = tokens['access_token']

        return access_token, patreon.API(access_token)
Exemple #6
0
def page_patreon_oauth():

    try:
        code = request.args.get('code')
        ckey = request.args.get('state')

        if code != None and ckey != None:
            oauth_client = patreon.OAuth(
                cfg.PRIVATE["patreon"]["client_id"],
                cfg.PRIVATE["patreon"]["client_secret"])

            tokens = oauth_client.get_tokens(
                code, 'https://beestation13.com/patreonauth')

            access_token = tokens['access_token']

            api_client = patreon.API(access_token)

            user_identity = api_client.get_identity().data()

            user_id = user_identity.id()

            player = db.Player.from_ckey(ckey)

            if not player:
                return redirect("/linkpatreon?error=invalidckey")

            db.site_db.link_patreon(ckey, user_id)

            return redirect("/linkpatreon?success=true")

        else:
            return redirect("/linkpatreon?error=unknown")

    except Exception as E:
        return str(E)

    return redirect("/linkpatreon?error=unknown")