Exemplo n.º 1
0
def github_login_verify():
    """
    Handles response after the redirect to Github. This response determines
    if the user has allowed this application access. If we were then we send
    a POST request for the access_key used to authenticate requests to Github.
    """
    url_to_redirect = flask.session.pop("github_auth_redirect",
                                        flask.url_for("snapcraft.homepage"))

    state = flask.request.args.get("state")

    # Avoid CSRF attacks
    if state != flask.session["csrf_token"]:
        flask.flash("Invalid request", "negative")
        return flask.redirect(url_to_redirect)

    data = {
        "code": flask.request.args.get("code"),
        "client_id": os.getenv("GITHUB_CLIENT_ID"),
        "client_secret": os.getenv("GITHUB_CLIENT_SECRET"),
    }

    session = Session()
    response = session.request(
        method="POST",
        url="https://github.com/login/oauth/access_token",
        json=data,
        headers={"Accept": "application/json"},
    )

    data = response.json()

    if "error" in data:
        raise BadRequest(data["error_description"], response=response)

    flask.session["github_auth_secret"] = data["access_token"]

    return flask.redirect(url_to_redirect)
Exemplo n.º 2
0
import json
import os

import flask
from canonicalwebteam.launchpad import Launchpad
from ruamel.yaml import YAML
from webapp.api.requests import PublisherSession, Session

_yaml = YAML(typ="rt")
_yaml_safe = YAML(typ="safe")
api_session = Session()
api_publisher_session = PublisherSession()

launchpad = Launchpad(
    username=os.getenv("LP_API_USERNAME"),
    token=os.getenv("LP_API_TOKEN"),
    secret=os.getenv("LP_API_TOKEN_SECRET"),
    session=api_publisher_session,
)


def get_yaml_loader(typ="safe"):
    if typ == "safe":
        return _yaml_safe
    return _yaml


def get_licenses():
    try:
        with open("webapp/licenses.json") as f:
            licenses = json.load(f)["licenses"]