Exemplo n.º 1
0
def get_account_id(username, token_manager=None, app_url=defaults.APP_URL):
    """
    get the account id for the username specified

    """
    headers = token_manager.get_access_token_headers()
    auth_url = environment.get_auth_url(app_url=app_url)
    url = "%s/api/v1/accounts?username=%s" % (auth_url, username)

    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        return response.json()["id"]

    else:
        raise JutException("Error %s; %s" % (response.status_code, response.text))
Exemplo n.º 2
0
def get_logged_in_account(token_manager=None, app_url=defaults.APP_URL):
    """
    get the account details for credentials provided

    """
    headers = token_manager.get_access_token_headers()
    auth_url = environment.get_auth_url(app_url=app_url)
    url = "%s/api/v1/account" % auth_url

    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        return response.json()

    else:
        raise JutException("Error %s; %s" % (response.status_code, response.text))
Exemplo n.º 3
0
def user_exists(username, token_manager=None, app_url=defaults.APP_URL):
    """
    check if the user exists with the specified username

    """
    headers = token_manager.get_access_token_headers()
    auth_url = environment.get_auth_url(app_url=app_url)
    url = "%s/api/v1/accounts?username=%s" % (auth_url, username)
    response = requests.get(url, headers=headers)

    if response.status_code == 404:
        return False
    elif response.status_code == 200:
        return True
    else:
        raise JutException("Error %s: %s" % (response.status_code, response.text))
Exemplo n.º 4
0
def get_accounts(account_ids, token_manager=None, app_url=defaults.APP_URL):
    """
    get the account details for each of the account ids in the account_ids list

    """
    headers = token_manager.get_access_token_headers()
    auth_url = environment.get_auth_url(app_url=app_url)

    url = "%s/api/v1/accounts/%s" % (auth_url, ",".join(account_ids))

    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        return response.json()

    else:
        raise JutException("Error %s; %s" % (response.status_code, response.text))
Exemplo n.º 5
0
def get_authorization(token_manager,
                      app_url=defaults.APP_URL):
    """
    returns the authorization grant

    """
    auth_url = environment.get_auth_url(app_url=app_url)
    url = '%s/api/v1/authorizations' % auth_url

    headers = token_manager.get_access_token_headers()
    response = requests.post(url,
                             headers=headers)

    if response.status_code == 201:
        return response.json()

    else:
        raise JutException('Error Auth %s: %s' % (response.status_code, response.text))
Exemplo n.º 6
0
def delete_user(username, token_manager=None, app_url=defaults.APP_URL):
    """
    delete a user by its account_id and with the access_token from that same
    user as only a user may delete him/her-self

    """
    account_id = get_account_id(username, token_manager=token_manager, app_url=app_url)

    headers = token_manager.get_access_token_headers()
    auth_url = environment.get_auth_url(app_url=app_url)
    url = "%s/api/v1/accounts/%s" % (auth_url, account_id)
    response = requests.delete(url, headers=headers)

    if response.status_code == 204:
        return response.text

    else:
        raise JutException("Error %s; %s" % (response.status_code, response.text))
Exemplo n.º 7
0
def create_user(name, username, email, password, token_manager=None, app_url=defaults.APP_URL):
    """
    create a new user with the specified name, username email and password

    """
    headers = token_manager.get_access_token_headers()
    auth_url = environment.get_auth_url(app_url=app_url)
    url = "%s/api/v1/accounts" % auth_url

    payload = {"name": name, "username": username, "email": email, "password": password}

    response = requests.post(url, data=json.dumps(payload), headers=headers)

    if response.status_code == 201:
        return response.json()

    else:
        raise JutException("Error %s: %s" % (response.status_code, response.text))
Exemplo n.º 8
0
def get_access_token(username=None,
                     password=None,
                     client_id=None,
                     client_secret=None,
                     app_url=defaults.APP_URL):
    """
    get the access token (http://docs.jut.io/api-guide/#unique_171637733),
    by either using the direct username, password combination or better yet by
    using the authorization grant you generated on jut with the client_id,
    client_secret combination

    auth_url: auth url for the jut application (defaults to production)

    Use either two of the following to authenticate:

    username: username used to login to Jut
    password: password used to login to Jut
    client_id: client_id generated through the Jut application or using the
               authorizations API
    client_secret: client_secret generated through the Jut appliation or using
                   the authorizations API
    """
    sess = requests.Session()
    auth_url = environment.get_auth_url(app_url=app_url)

    if client_id != None:
        headers = {
            'content-type': 'application/json'
        }
        response = sess.post(auth_url + "/token",
                             headers=headers,
                             data=json.dumps({
                                 'grant_type': 'client_credentials',
                                 'client_id': client_id,
                                 'client_secret': client_secret
                             }))
    else:
        form = {
            'username': username,
            'password': password
        }

        response = sess.post('%s/local' % auth_url, data=form)

        if response.status_code != 200:
            raise JutException('Failed /local check %s: %s' %
                               (response.status_code, response.text))

        response = sess.get(auth_url + '/status')
        if response.status_code != 200:
            raise JutException('Failed /status check %s: %s' %
                               (response.status_code, response.text))

        if not response.json()['authorized']:
            raise JutException('Failed authentication with (%s,%s), got %s' %
                               (username, password, response.text))

        response = sess.post(auth_url + '/token',
                             data={'grant_type': 'client_credentials'})

    if response.status_code != 200:
        raise JutException('Unable to get auth token %s: %s' %
                           (response.status_code, response.text))

    return response.json()