# Path: ob2/util/security.py
# def generate_shitty_random_string(N=30):
#     return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))


import github3
import requests
import urllib
import urlparse
import ob2.config as config
from flask import session
from ob2.util.security import generate_shitty_random_string
        session["authentication_oauth_state"] = state

        return "%s?%s" % (authorize_url, urllib.urlencode({
            "client_id": config.github_oauth_consumer_key,
            "redirect_uri": redirect_uri,
            "state": state}))


def authentication_provider_get_token(code, state):
    if not state or session.get("authentication_oauth_state") != state:
        raise AuthenticationIntegrityError("OAuth state parameter does not match")
    response = requests.post(access_token_url,
                             data={"client_id": config.github_oauth_consumer_key,
                                   "client_secret": config.github_oauth_consumer_secret,
                                   "code": code})
    if response.status_code != 200:
        raise AuthenticationTemporaryError("Failed to get OAuth api token from GitHub")
    response_dict = dict(urlparse.parse_qsl(response.text))
    if "access_token" not in response_dict:
        raise AuthenticationTemporaryError("GitHub OAuth response did not contain access_token")

    return response_dict["access_token"]


def get_username_from_token(token):
    github = github3.login(token=token)
    github_user = github.user()
    return github_user.login



# Path: ob2/util/security.py
# def generate_shitty_random_string(N=30):
#     return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))


# Path: ob2/util/github_login.py
import github3
import requests
import urllib
import urlparse
import ob2.config as config
from flask import session
from ob2.util.security import generate_shitty_random_string

        session["authentication_oauth_state"] = state

        return "%s?%s" % (authorize_url, urllib.urlencode({
            "client_id": config.github_oauth_consumer_key,
            "redirect_uri": redirect_uri,
            "state": state}))


def authentication_provider_get_token(code, state):
    if not state or session.get("authentication_oauth_state") != state:
        raise AuthenticationIntegrityError("OAuth state parameter does not match")
    response = requests.post(access_token_url,
                             data={"client_id": config.github_oauth_consumer_key,
                                   "client_secret": config.github_oauth_consumer_secret,
                                   "code": code})
    if response.status_code != 200:
        raise AuthenticationTemporaryError("Failed to get OAuth api token from GitHub")
    response_dict = dict(urlparse.parse_qsl(response.text))
    if "access_token" not in response_dict:
        raise AuthenticationTemporaryError("GitHub OAuth response did not contain access_token")

    return response_dict["access_token"]


def get_username_from_token(token):
    github = github3.login(token=token)
    github_user = github.user()
    return github_user.login



def github_username():