Exemple #1
0
    def GET(self):
        i = web.input(username='', email='', key='')
        if i.key != lending.config_internal_tests_api_key:
            return delegate.RawText(simplejson.dumps({
                'error': 'Authentication failed for private API'
            }), content_type="application/json")
        try:
            if i.username:
                ol_account = OpenLibraryAccount.get(username=i.username)
            elif i.email:
                ol_account = OpenLibraryAccount.get(email=i.email)
        except Exception as e:
            return delegate.RawText(simplejson.dumps({
                'error': 'bad-account'
            }), content_type="application/json")
        if ol_account:
            ol_account.enc_password = '******'
            if ol_account.itemname:
                return delegate.RawText(simplejson.dumps({
                    'status': 'link-exists',
                    'username': ol_account.username,
                    'itemname': ol_account.itemname,
                    'email': ol_account.email.lower()
                }), content_type="application/json")
            if not ol_account.itemname:
                ia_account = InternetArchiveAccount.get(email=ol_account.email.lower())
                if ia_account:
                    ol_account.link(ia_account.itemname)
                    return delegate.RawText(simplejson.dumps({
                        'username': ol_account.username,
                        'status': 'link-found',
                        'itemname': ia_account.itemname,
                        'ol-itemname': ol_account.itemname,
                        'email': ol_account.email.lower(),
                        'ia': ia_account
                    }), content_type="application/json")

                password = OpenLibraryAccount.generate_random_password(16)
                ia_account = InternetArchiveAccount.create(
                    ol_account.username or ol_account.displayname,
                    ol_account.email, password, verified=True, retries=USERNAME_RETRIES)
                return delegate.RawText(simplejson.dumps({
                    'username': ol_account.username,
                    'email': ol_account.email,
                    'itemname': ia_account.itemname,
                    'password': password,
                    'status': 'link-created'
                }), content_type="application/json")
Exemple #2
0
    def GET(self):
        """Internal API endpoint used for authorized test cases and
        administrators to unlink linked OL and IA accounts.
        """
        i = web.input(email='', username='', itemname='', key='', unlink='',
                      new_itemname='')
        if i.key != lending.config_internal_tests_api_key:
            result = {'error': 'Authentication failed for private API'}
        else:
            try:
                result = OpenLibraryAccount.get(email=i.email, link=i.itemname,
                                                username=i.username)
                if result is None:
                    raise ValueError('Invalid Open Library account email ' \
                                     'or itemname')
                result.enc_password = '******'
                if i.new_itemname:
                    result.link(i.new_itemname)
                if i.unlink:
                    result.unlink()
            except ValueError as e:
                result = {'error': str(e)}

        return delegate.RawText(simplejson.dumps(result),
                                content_type="application/json")
Exemple #3
0
    def POST_resend_verification_email(self, i):
        try:
            ol_login = OpenLibraryAccount.authenticate(i.email, i.password)
        except ClientException as e:
            code = e.get_data().get("code")
            if code != "account_not_verified":
                return self.error("account_incorrect_password", i)

        account = OpenLibraryAccount.get(email=i.email)
        account.send_verification_email()

        title = _("Hi, %(user)s", user=account.displayname)
        message = _("We've sent the verification email to %(email)s. You'll need to read that and click on the verification link to verify your email.", email=account.email)
        return render.message(title, message)
Exemple #4
0
    def POST(self):
        i = web.input(username='', password='')
        err = ""
        act = OpenLibraryAccount.get(username=i.username)

        if act:
            if OpenLibraryAccount.authenticate(act.email, i.password) == "ok":
                return render_template('account/email/forgot', email=act.email)
            else:
                err = "Incorrect password"

        elif valid_email(i.username):
            err = "Please enter a username, not an email"

        else:
            err="Sorry, this user does not exist"

        return render_template('account/email/forgot', err=err)
Exemple #5
0
    def POST(self):
        i = web.input(username='', password='')
        err = ""
        act = OpenLibraryAccount.get(username=i.username)

        if act:
            if OpenLibraryAccount.authenticate(act.email, i.password) == "ok":
                return render_template('account/email/forgot', email=act.email)
            else:
                err = "Incorrect password"

        elif valid_email(i.username):
            err = "Please enter a username, not an email"

        else:
            err = "Sorry, this user does not exist"

        return render_template('account/email/forgot', err=err)
Exemple #6
0
    def POST(self):
        i = web.input(email='', password='')
        err = ""

        if valid_email(i.email):
            act = OpenLibraryAccount.get(email=i.email)
            if act:
                if OpenLibraryAccount.authenticate(i.email, i.password) == "ok":
                    ia_act = act.get_linked_ia_account()
                    if ia_act:
                        return render_template('account/email/forgot-ia', email=ia_act.email)
                    else:
                        err = "Open Library Account not linked. Login with your Open Library credentials to connect or create an Archive.org account"
                else:
                    err = "Incorrect password"
            else:
                err = "Sorry, this Open Library account does not exist"
        else:
            err = "Please enter a valid Open Library email"
        return render_template('account/email/forgot-ia', err=err)
Exemple #7
0
    def POST(self):
        i = web.input(email='', password='')
        err = ""

        if valid_email(i.email):
            act = OpenLibraryAccount.get(email=i.email)
            if act:
                if OpenLibraryAccount.authenticate(i.email, i.password) == "ok":
                    ia_act = act.get_linked_ia_account()
                    if ia_act:
                        return render_template('account/email/forgot-ia', email=ia_act.email)
                    else:
                        err = "Open Library Account not linked. Login with your Open Library credentials to connect or create an Archive.org account"
                else:
                    err = "Incorrect password"
            else:
                err = "Sorry, this Open Library account does not exist"
        else:
            err = "Please enter a valid Open Library email"
        return render_template('account/email/forgot-ia', err=err)
Exemple #8
0
    def GET(self):
        """Internal API endpoint used for authorized test cases and
        administrators to unlink linked OL and IA accounts.
        """
        i = web.input(email='', itemname='', key='', readonly='')
        if i.key != lending.config_internal_tests_api_key:
            result = {'error': 'Authentication failed for private API'}
        else:
            try:
                result = OpenLibraryAccount.get(email=i.email, link=i.itemname)
                if result is None:
                    raise ValueError('Invalid Open Library account email ' \
                                     'or itemname')
                    result.enc_password = '******'
                if not readonly:
                    result.unlink()
            except ValueError as e:
                result = {'error': str(e)}

        return delegate.RawText(simplejson.dumps(result),
                                content_type="application/json")
Exemple #9
0
    def GET(self):
        i = web.input(username='', email='', key='')
        if i.key != lending.config_internal_tests_api_key:
            return delegate.RawText(simplejson.dumps(
                {'error': 'Authentication failed for private API'}),
                                    content_type="application/json")
        try:
            if i.username:
                ol_account = OpenLibraryAccount.get(username=i.username)
            elif i.email:
                ol_account = OpenLibraryAccount.get(email=i.email)
        except Exception as e:
            return delegate.RawText(simplejson.dumps({'error': 'bad-account'}),
                                    content_type="application/json")
        if ol_account:
            ol_account.enc_password = '******'
            if ol_account.itemname:
                return delegate.RawText(simplejson.dumps({
                    'status':
                    'link-exists',
                    'username':
                    ol_account.username,
                    'itemname':
                    ol_account.itemname,
                    'email':
                    ol_account.email.lower()
                }),
                                        content_type="application/json")
            if not ol_account.itemname:
                ia_account = InternetArchiveAccount.get(
                    email=ol_account.email.lower())
                if ia_account:
                    ol_account.link(ia_account.itemname)
                    return delegate.RawText(simplejson.dumps({
                        'username':
                        ol_account.username,
                        'status':
                        'link-found',
                        'itemname':
                        ia_account.itemname,
                        'ol-itemname':
                        ol_account.itemname,
                        'email':
                        ol_account.email.lower(),
                        'ia':
                        ia_account
                    }),
                                            content_type="application/json")

                password = OpenLibraryAccount.generate_random_password(16)
                ia_account = InternetArchiveAccount.create(
                    ol_account.username or ol_account.displayname,
                    ol_account.email,
                    password,
                    verified=True,
                    retries=USERNAME_RETRIES)
                return delegate.RawText(simplejson.dumps({
                    'username':
                    ol_account.username,
                    'email':
                    ol_account.email,
                    'itemname':
                    ia_account.itemname,
                    'password':
                    password,
                    'status':
                    'link-created'
                }),
                                        content_type="application/json")
Exemple #10
0
class account_login(delegate.page):
    """Account login.

    Login can fail because of the following reasons:

    * account_not_found: Error message is displayed.
    * account_bad_password: Error message is displayed with a link to reset password.
    * account_not_verified: Error page is dispalyed with button to "resend verification email".
    """
    path = "/account/login"

    def render_error(self, error_key, i):
        f = forms.Login()
        f.fill(i)
        f.note = LOGIN_ERRORS[error_key]
        return render.login(f)

    def GET(self):
        referer = web.ctx.env.get('HTTP_REFERER', '/')
        i = web.input(redirect=referer)
        f = forms.Login()
        f['redirect'].value = i.redirect
        page = render.login(f)
        page.v2 = True
        return page

    def POST(self):
        i = web.input(username="",
                      connect=None,
                      password="",
                      remember=False,
                      redirect='/',
                      test=False,
                      access=None,
                      secret=None)
        email = i.username  # XXX username is now email
        audit = audit_accounts(email,
                               i.password,
                               require_link=True,
                               s3_access_key=i.access,
                               s3_secret_key=i.secret,
                               test=i.test)
        error = audit.get('error')
        if error:
            return self.render_error(error, i)

        expires = (i.remember and 3600 * 24 * 7) or ""
        web.setcookie(config.login_cookie_name,
                      web.ctx.conn.get_auth_token(),
                      expires=expires)
        blacklist = [
            "/account/login", "/account/password", "/account/email",
            "/account/create"
        ]
        if i.redirect == "" or any([path in i.redirect for path in blacklist]):
            i.redirect = "/"
        raise web.seeother(i.redirect)

    def POST_resend_verification_email(self, i):
        try:
            ol_login = OpenLibraryAccount.authenticate(i.email, i.password)
        except ClientException, e:
            code = e.get_data().get("code")
            if code != "account_not_verified":
                return self.error("account_incorrect_password", i)

        account = OpenLibraryAccount.get(email=i.email)
        account.send_verification_email()

        title = _("Hi, %(user)s", user=account.displayname)
        message = _(
            "We've sent the verification email to %(email)s. You'll need to read that and click on the verification link to verify your email.",
            email=account.email)
        return render.message(title, message)