Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre-registration validation for IA email and username #9223

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions openlibrary/i18n/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5472,15 +5472,15 @@ msgid "Book Details"
msgstr ""

#: type/edition/view.html type/work/view.html
msgid "Published in"
msgid "First Sentence"
msgstr ""

#: type/edition/view.html type/work/view.html
msgid "First Sentence"
msgid "Edition Notes"
msgstr ""

#: type/edition/view.html type/work/view.html
msgid "Edition Notes"
msgid "Published in"
msgstr ""

#: type/edition/view.html type/work/view.html
Expand Down Expand Up @@ -6453,6 +6453,10 @@ msgstr ""
msgid "Email already registered"
msgstr ""

#: account.py
msgid "An Internet Archive account already exists with this email"
msgstr ""

#: account.py
msgid "Email address is already used."
msgstr ""
Expand Down
18 changes: 18 additions & 0 deletions openlibrary/plugins/upstream/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import logging
import re
import requests
from typing import Any, TYPE_CHECKING, Final
from collections.abc import Callable
from collections.abc import Iterable, Mapping
Expand Down Expand Up @@ -551,16 +552,29 @@ def GET(self):
class account_validation(delegate.page):
path = '/account/validate'

@staticmethod
def ia_username_exists(username):
url = "https://archive.org/metadata/@%s" % username
try:
return bool(requests.get(url).json())
except (OSError, ValueError):
return

@staticmethod
def validate_username(username):
if not 3 <= len(username) <= 20:
return _('Username must be between 3-20 characters')
if not re.match('^[A-Za-z0-9-_]{3,20}$', username):
return _('Username may only contain numbers and letters')

ol_account = OpenLibraryAccount.get(username=username)
if ol_account:
return _("Username unavailable")

ia_account = account_validation.ia_username_exists(username)
if ia_account:
return _("Username unavailable")

@staticmethod
def validate_email(email):
if not (email and re.match(r'.*@.*\..*', email)):
Expand All @@ -570,6 +584,10 @@ def validate_email(email):
if ol_account:
return _('Email already registered')

ia_account = InternetArchiveAccount.get(email=email)
if ia_account:
return _('An Internet Archive account already exists with this email')

def GET(self):
i = web.input()
errors = {'email': None, 'username': None}
Expand Down
Loading