Repository: internetarchive/openlibrary

Issue:
Allow admin account look-up by patron's archive.org identifier

<!-- What problem are we solving? What does the experience look like today? What are the symptoms? -->
When a patron clicks "Update email address" from their open library settings, they are taken to their archive.org settings. The update on archive.org is not applied to openlibrary.org.

### Proposed Solution

See https://github.com/internetarchive/openlibrary/issues/8151#issuecomment-1720704376

### Evidence / Screenshot (if possible)
<img width="462" alt="Screenshot 2023-08-01 at 12 36 56" src="https://github.com/internetarchive/openlibrary/assets/17739465/e3422a21-05b8-478c-9abd-5cad1de9f7da">

### Relevant url?
<!-- `https://openlibrary.org/...` -->
https://openlibrary.org/account
### Steps to Reproduce
<!-- What steps caused you to find the bug? -->
1. Go to ...https://openlibrary.org/account
2. Do ...click "update email address".

<!-- What actually happened after these steps? What did you expect to happen? -->
* Actual: Navigates to archive.org where email may be updated, but does not take effect on openlibrary.org.
* Expected: Email address should sync on both sites.

### Details

- **Logged in (Y/N)?**
- **Browser type/version?**
- **Operating system?**
- **Environment (prod/dev/local)?** prod
<!-- If not sure, put prod -->

### Proposal & Constraints
<!-- What is the proposed solution / implementation? Is there a precedent of this approach succeeding elsewhere? -->

### Related files
<!-- Files related to this issue; this is super useful for new contributors who might want to help! If you're not sure, leave this blank; a maintainer will add them. -->

### Stakeholders
<!-- @ tag stakeholders of this bug -->
@mekarpeles @JeffKaplan @jimchamp 


Discussion:
I am interested in solving this issue. I have forked the repository, cloned it on my machine, and have completed the setup. I am able to open the ```development version``` of the website but when I try to login I get the following error.
```c++
{"error": "internal_error", "message": "'NoneType' object has no attribute 'get'"}
```

Here is the screenshot from the logs

![Screenshot 2023-08-02 at 7 31 19 AM](https://github.com/internetarchive/openlibrary/assets/96362727/c251b405-29e4-47a9-acaf-b31e3a418d3d)

What should I do in order to resolve this issue ?

Does this help at all? Note, this will delete your Open Library database container, which shouldn't matter as Open Library isn't working at all, and if it works, the container will get recreated right away.
```
docker compose down
docker container rm openlibrary-db-1
docker compose up
```

If that doesn't work, you can try blowing away _all_ Docker containers, volumes, etc., but note, this deletes _all_ Docker containers and volumes, not just Open Library's. I do this fairly regularly to save space, but if you have some custom change in a container that isn't reflected in a `Dockerfile`, this might cause you a headache.
```
docker system prune -a
```

Having said all that, _this_ particular issue needs some more analysis first I think before anyone starts on it. Do any of [these](https://github.com/internetarchive/openlibrary/issues?q=is%3Aopen+is%3Aissue+label%3A%22Needs%3A+Help%22+label%3A%22Good+First+Issue%22) (that don't have linked pull requests) look interesting?
I have been able to run it properly with the above help provided. Thanks @scottbarnes !

Now I opened the development version of OpenLibrary and tried to ```Update the Email Address```, I was redirected to ```archive.org/accounts``` login as mentioned by OP. To solve this issue how do we want to go forward with it ?

The idea that came to me is to create a new page to update email-address in ```plugins/templates/account```, instead of redirecting to ```archive.org```. Am I thinking in the right direction ? 

![Screenshot 2023-08-05 at 9 54 18 AM](https://github.com/internetarchive/openlibrary/assets/96362727/54af8952-4e1b-476e-811a-af39579e9823)
The above image is of ```openlibrary/templates/account.html``` and I can see there are a few more links which redirects to ```archive.org```, so are those also issues or untouched links ?
I can see the logic in that approach, but in this particular case I think we do need to sync email addresses between archive.org and openlibrary.org, and for that, it's possible we may need to work on code outside of Open Library.

Alas, @gobbledy-gook, I think we are in a bit of a holding pattern at least until Monday when this is likely to get some more attention.
As far as I remember, emails and passwords will always be changed from Archive.org (based on how the Open Library service is configured on production). The best bet for fixing this is at time-of-login when we perform an xauthn request from Open Library to Archive.org. If we notice the connected Archive.org account has a different email from Open Library, it is probably safe to update the corresponding Open Library email with the Archive.org one.

This logic occurs in https://github.com/internetarchive/openlibrary/blob/master/openlibrary/accounts/model.py#L760-L904 and https://github.com/internetarchive/openlibrary/blob/master/openlibrary/plugins/upstream/account.py#L377-L414
I believe (after talking to @cdrini) the challenge @seabelis is having is looking up an Open Library account with an Archive.org email after it's been updated.

The workaround is that we _can_ query Open Library `store` by Archive.org itemname and maybe it's better for us to solve that problem by updating the admin page to have something like /admin/people/ which allows us to lookup by archive.org itemname. 

Lets **extend** the existing search area for @seabelis to allows lookup by either an email address or archive.org itemname. If there's an `@` symbol and it's not in 0th position, it's an email.

<img width="669" alt="Screenshot 2023-08-07 at 12 18 40 PM" src="https://github.com/internetarchive/openlibrary/assets/978325/2ab85a9b-a66b-4b12-9bd4-204cc28e2043">
I wonder if we want archive.org to hit an Open Library callback when an email is updated from archive.org. Alternatively, we can update Open Library to use a modified xauthn (which we may need to ask @jimbonator about) to see if we can update the patron's email using xauthn from OL and then also update the OL email from OL.
For this issue, I think we should simply add a 2nd input box that also allows the admin to lookup an account by archive.org identifier in addition to email. This should solve the problem and be relatively easy.

Something like...
<img width="678" alt="Screenshot 2023-09-14 at 10 31 54 PM" src="https://github.com/internetarchive/openlibrary/assets/978325/97f885dc-2e1e-4e92-a287-4dc2b8755e14">

@gobbledy-gook what do you think? We'd basically add a new row where the form has an `ia_identifier` instead of `email` -- e.g.`<input type="text" name="ia_identifier" id="ia_identifier" value="">` to the existing form. On submit, if `ia_identifier` is present, then do a `web.ctx` fetch for the identifier:

```
from openlibrary.account.model import OpenLibraryAccount
OpenLibraryAccount.get_by_link(ia_identifier)
```
I would like to take up this issue.
@Eds-Dbug are you still working on this?  Do you have any questions about anything?
Hi Jim, I am not currently working on this issue, I will unsubscribe for
now.

On Mon, Nov 20, 2023 at 4:44 PM jimchamp ***@***.***> wrote:

> @Eds-Dbug <https://github.com/Eds-Dbug> are you still working on this? Do
> you have any questions about anything?
>
> —
> Reply to this email directly, view it on GitHub
> <https://github.com/internetarchive/openlibrary/issues/8151#issuecomment-1819852231>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AT4EQGCZRJ3FAHSQPFPULJ3YFPFMZAVCNFSM6AAAAAA27R24CKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMJZHA2TEMRTGE>
> .
> You are receiving this because you were mentioned.Message ID:
> ***@***.***>
>


Resolving patch:
diff --git a/openlibrary/plugins/admin/code.py b/openlibrary/plugins/admin/code.py
--- a/openlibrary/plugins/admin/code.py
+++ b/openlibrary/plugins/admin/code.py
@@ -25,7 +25,7 @@
 import openlibrary
 
 from openlibrary import accounts
-
+from openlibrary.accounts.model import OpenLibraryAccount
 from openlibrary.core import admin as admin_stats, helpers as h, imports, cache
 from openlibrary.core.waitinglist import Stats as WLStats
 from openlibrary.core.sponsorships import summary, sync_completed_sponsored_books
@@ -174,13 +174,16 @@ def GET(self):
 
 class people:
     def GET(self):
-        i = web.input(email=None)
+        i = web.input(email=None, ia_id=None)
 
+        account = None
         if i.email:
             account = accounts.find(email=i.email)
-            if account:
-                raise web.seeother("/admin/people/" + account.username)
-        return render_template("admin/people/index", email=i.email)
+        if i.ia_id:
+            account = OpenLibraryAccount.get_by_link(i.ia_id)
+        if account:
+            raise web.seeother(f"/admin/people/{account.username}")
+        return render_template("admin/people/index", email=i.email, ia_id=i.ia_id)
 
 
 class add_work_to_staff_picks:


Test patch: