ntfy icon indicating copy to clipboard operation
ntfy copied to clipboard

Web: User account management

Open binwiederhier opened this issue 3 years ago • 2 comments

binwiederhier avatar Nov 29 '22 14:11 binwiederhier

I have been working on this. It's very very very rough. Code here: https://github.com/binwiederhier/ntfy/pull/526 -- It's like 20% done and very bad code.

Here are some raw notes.



ntfy data model
--

user
- user_id
- plan_id
- username
- password (hash)
- role (user, admin)

user_access
- user_id
- topic
- read
- write

user_subscription
- user_id
- base_url
- topic

user_setting

user_token
 - user_id
 - token
 - expires

plan
- plan_id
- name
- 

 

Flows:
- Login
  GET v1/user/token -> token
  Redirect
 

Endpoints:

GET v1/user/token
  {
    "token": "abcdef..."
  }

GET v1/user/account
  {
     "username": "phil",
     "role": "user",
     "plan": {
     	"id": 1,
     	"name": "ntfy Free"     	
     },
     "notification": {
       "sound": "ding",
       "min_priority": 1,
       "delete_after": 1234
     },
     "language": "de_DE",
     "users: [
       {
         "base_url": "https://ntfy.sh",
         "user": "phil",
         "pass": "*"
       }
     ],
     "subscriptions": [
      	{
      	  "base_url": "https://ntfy.sh",
      	  "topic": "mytopic"
      	}
     ],
     "access": [
        {
          "user": "phil",
          "topic": "mytopic",
          "read": true,
          "write": false
        }
     ],
     "limits": {
       "messages": 1000,
       "messages_available": 877,
       "emails": 16,
       "emails_available": 15,
       ...
     }     
  }
     

GET v1/user/account   // anonymous user
  {
     "username": "anonymous",
     "limits": {
       "messages": 1000,
       "messages_available": 877,
       "emails": 16,
       "emails_available": 15,
       ...
     }
  }
          
     
PUT v1/user/access
  {
    "user": "phil",
    "topic": "mytopic",
    "access": "private"
  }

binwiederhier avatar Dec 02 '22 20:12 binwiederhier

Implemented this:

curl -u ben:ben localhost:2586/user/auth
{"token":"1JntEz3EDrXYYj539wDiEgqRrukGQsVD"}

curl -H "Authorization: Bearer 1JntEz3EDrXYYj539wDiEgqRrukGQsVD" localhost:2586/user/account
{"username":"ben","role":"admin","language":"de-DE", "notification":{"sound":"dadum","min_priority":"","delete_after":0}}

This can now be used in the UI to log in and sync the config and subscriptions. I have it working to sync the language already. But it's really rough.

Server-side it's a few more tables, though I am contemplating collapsing all the tables into just one more column in the user table called settings and just storing a JSON blob in that.

binwiederhier avatar Dec 03 '22 20:12 binwiederhier

Current progress:

https://user-images.githubusercontent.com/664597/206604453-ec679164-4fde-409c-bfa8-93a80459e039.mp4

Current ideas for API endpoints:


Login:
	GET /user/token
	{
	  "token": "abcdef..."
	}

Logout:
	DELETE /user/auth
	Authorization: Bearer abcdef...

Get user account (if not logged in):
	GET /user/account
	  {
	     "username": "anonymous",
	     "limits": {
	       "messages": 1000,
	       "messages_available": 877,
	       "emails": 16,
	       "emails_available": 15,
	       ...
	     }
	  }

Get account settings:
	GET /user/account
	Authorization: Bearer abcdef...	
	{
	     "username": "phil",
	     "role": "user",
	     "plan": {
	     	"id": 1,
	     	"name": "ntfy Free"     	
	     },
	     "notification": {
	       "sound": "ding",
	       "min_priority": 1,
	       "delete_after": 1234
	     },
	     "language": "de_DE",
	     "users: [
	       {
		 "id": "ewrwr-rwer-ewrwr-rwerwrwerwerrw",
		 "base_url": "https://ntfy.sh",
		 "user": "phil",
		 "pass": "*"
	       }
	     ],
	     "subscriptions": [
	      	{
	      	  "id": "adefsdf-dasd-dedsd-dasdasdadasd",
	      	  "base_url": "https://ntfy.sh",
	      	  "topic": "mytopic"
	      	}
	     ],
	     "access": [
		{
		  "user": "phil",
		  "topic": "mytopic",
		  "read": true,
		  "write": false
		}
	     ]
	     "limits": {
	       "messages": 1000,
	       "messages_available": 877,
	       "emails": 16,
	       "emails_available": 15,
	       ...
	     }     
	  }

Update simple settings:
	PUT /user/account
	Authorization: Bearer abcdef...
		
	{
	  "language": "de-DE",
	  "notification": {
	    "sound": "ding"
	  }
	}


Add subscription:
	PUT /user/subscription
	Authorization: Bearer abcdef...
		
	{
	  "base_url": "https://ntfy.sh",
	  "topic": "mytopic"
	}
	
	Response:
		{
		  "id": "adefsdf-dasd-dedsd-dasdasdadasd",
		  "base_url": "https://ntfy.sh",
		  "topic": "mytopic"
		}
	
	
Delete subscription:
	DELETE /user/subscription/adefsdf-dasd-dedsd-dasdasdadasd
	Authorization: Bearer abcdef...

binwiederhier avatar Dec 09 '22 01:12 binwiederhier

I'm curious- does this imply that a single user has the same subscriptions on all clients they log in with? (Personally, I would prefer not to sync in all cases- I can see having subscriptions to different topics on different devices- this could be handled by using different accounts for different devices, but I'm not sure that's sustainable?)

ngerstle avatar Feb 05 '23 01:02 ngerstle

@ngerstle, all that would be necessary would be different device profiles per account.

RokeJulianLockhart avatar Feb 06 '23 22:02 RokeJulianLockhart