Beispiel #1
0
def get_auth_config(username, password):
    payload = dict(
        username=username,
        password=password,
        remember='CHECKED',
        action='login',
    )

    with requests.Session() as s:
        # Attache logged-in-* cookies to Session.
        u = 'https://archive.org/account/login.php'
        r = s.post(u, data=payload, cookies={'test-cookie': '1'})
        if 'logged-in-sig' not in s.cookies:
            raise AuthenticationError(
                'Authentication failed. '
                'Please check your credentials and try again.')

        # Get S3 keys.
        u = 'https://archive.org/account/s3.php'
        p = dict(output_json=1)
        r = s.get(u, params=p)
        j = r.json()
        access_key = j['key']['s3accesskey']
        secret_key = j['key']['s3secretkey']
        if not j or not j.get('key'):
            raise AuthenticationError(
                'Authentication failed. '
                'Please check your credentials and try again.')

        # Get user info (screenname).
        u = 'https://s3.us.archive.org'
        p = dict(check_auth=1)
        r = requests.get(u, params=p, auth=auth.S3Auth(access_key, secret_key))
        r.raise_for_status()
        j = r.json()
        if j.get('error'):
            raise AuthenticationError(j.get('error'))
        user_info = j['screenname']

        auth_config = {
            's3': {
                'access': access_key,
                'secret': secret_key,
            },
            'cookies': {
                'logged-in-user': s.cookies['logged-in-user'],
                'logged-in-sig': s.cookies['logged-in-sig'],
            },
            'general': {
                'screenname': user_info,
            }
        }

    return auth_config
Beispiel #2
0
def get_auth_config(email, password):
    u = 'https://archive.org/services/xauthn/'
    p = dict(op='login')
    d = dict(email=email, password=password)
    r = requests.post(u, params=p, data=d)
    j = r.json()
    if not j.get('success'):
        try:
            msg = j['values']['reason']
        except KeyError:
            msg = j['error']
        if msg == 'account_not_found':
            msg = 'Account not found, check your email and try again.'
        elif msg == 'account_bad_password':
            msg = 'Incorrect password, try again.'
        else:
            msg = 'Authentication failed: {}'.format(msg)
        raise AuthenticationError(msg)
    auth_config = {
        's3': {
            'access': j['values']['s3']['access'],
            'secret': j['values']['s3']['secret'],
        },
        'cookies': {
            'logged-in-user': j['values']['cookies']['logged-in-user'],
            'logged-in-sig': j['values']['cookies']['logged-in-sig'],
        },
        'general': {
            'screenname': j['values']['screenname'],
        }
    }
    return auth_config
Beispiel #3
0
def get_auth_config(email: str,
                    password: str,
                    host: str = 'archive.org') -> dict:
    u = f'https://{host}/services/xauthn/'
    p = {'op': 'login'}
    d = {'email': email, 'password': password}
    r = requests.post(u, params=p, data=d)
    j = r.json()
    if not j.get('success'):
        try:
            msg = j['values']['reason']
        except KeyError:
            msg = j['error']
        if msg == 'account_not_found':
            msg = 'Account not found, check your email and try again.'
        elif msg == 'account_bad_password':
            msg = 'Incorrect password, try again.'
        else:
            msg = f'Authentication failed: {msg}'
        raise AuthenticationError(msg)
    auth_config = {
        's3': {
            'access': j['values']['s3']['access'],
            'secret': j['values']['s3']['secret'],
        },
        'cookies': {
            'logged-in-user': j['values']['cookies']['logged-in-user'],
            'logged-in-sig': j['values']['cookies']['logged-in-sig'],
        },
        'general': {
            'screenname': j['values']['screenname'],
        }
    }
    return auth_config
Beispiel #4
0
    def __call__(self, r):
        if not self.access_key:
            if self.secret_key:
                raise AuthenticationError('No access_key set!'
                                          ' Have you run `ia configure`?')
        if not self.secret_key:
            if self.access_key:
                raise AuthenticationError('No secret_key set!'
                                          ' Have you run `ia configure`?')
            else:
                raise AuthenticationError('No access_key or secret_key set!'
                                          ' Have you run `ia configure`?')

        auth_str = f'LOW {self.access_key}:{self.secret_key}'
        r.headers['Authorization'] = auth_str
        return r
Beispiel #5
0
def get_user_info(access_key, secret_key):
    """Returns details about an Archive.org user given an IA-S3 key pair.

    :type access_key: str
    :param access_key: IA-S3 access_key to use when making the given request.

    :type secret_key: str
    :param secret_key: IA-S3 secret_key to use when making the given request.
    """
    u = 'https://s3.us.archive.org'
    p = dict(check_auth=1)
    r = requests.get(u, params=p, auth=auth.S3Auth(access_key, secret_key))
    r.raise_for_status()
    j = r.json()
    if j.get('error'):
        raise AuthenticationError(j.get('error'))
    else:
        return j
Beispiel #6
0
def get_user_info(access_key: str, secret_key: str) -> dict[str, str]:
    """Returns details about an Archive.org user given an IA-S3 key pair.

    :param access_key: IA-S3 access_key to use when making the given request.

    :param secret_key: IA-S3 secret_key to use when making the given request.

    :returns: Archive.org use info.
    """
    u = "https://s3.us.archive.org"
    p = {"check_auth": 1}
    r = requests.get(u, params=p, auth=auth.S3Auth(access_key, secret_key))
    r.raise_for_status()
    j = r.json()
    if j.get("error"):
        raise AuthenticationError(j.get("error"))
    else:
        return j
Beispiel #7
0
def get_auth_config(username, password):
    payload = dict(
        username=username,
        password=password,
        remember='CHECKED',
        action='login',
    )

    with requests.Session() as s:
        # Attache logged-in-* cookies to Session.
        u = 'https://archive.org/account/login.php'
        r = s.post(u, data=payload, cookies={'test-cookie': '1'})

        if 'logged-in-sig' not in s.cookies:
            raise AuthenticationError(
                'Authentication failed. '
                'Please check your credentials and try again.')

        # Get S3 keys.
        u = 'https://archive.org/account/s3.php'
        p = dict(output_json=1)
        r = s.get(u, params=p)
        j = r.json()

        if not j or not j.get('key'):
            raise requests.exceptions.HTTPError(
                'Authorization failed. Please check your credentials and try again.'
            )

        auth_config = {
            's3': {
                'access': j['key']['s3accesskey'],
                'secret': j['key']['s3secretkey'],
            },
            'cookies': {
                'logged-in-user': s.cookies['logged-in-user'],
                'logged-in-sig': s.cookies['logged-in-sig'],
            }
        }

    return auth_config