Extend session token from web app
This commit is contained in:
		
							parent
							
								
									276301dc87
								
							
						
					
					
						commit
						7ae8049438
					
				
					 3 changed files with 42 additions and 14 deletions
				
			
		|  | @ -36,8 +36,6 @@ import ( | ||||||
| 
 | 
 | ||||||
| /* | /* | ||||||
| 	TODO | 	TODO | ||||||
| 		expire tokens |  | ||||||
| 		auto-extend tokens from UI |  | ||||||
| 		use token auth in "SubscribeDialog" | 		use token auth in "SubscribeDialog" | ||||||
| 		upload files based on user limit | 		upload files based on user limit | ||||||
| 		database migration | 		database migration | ||||||
|  |  | ||||||
|  | @ -16,11 +16,19 @@ import { | ||||||
| } from "./utils"; | } from "./utils"; | ||||||
| import userManager from "./UserManager"; | import userManager from "./UserManager"; | ||||||
| import session from "./Session"; | import session from "./Session"; | ||||||
|  | import subscriptionManager from "./SubscriptionManager"; | ||||||
|  | 
 | ||||||
|  | const delayMillis = 45000; // 45 seconds
 | ||||||
|  | const intervalMillis = 900000; // 15 minutes
 | ||||||
| 
 | 
 | ||||||
| class AccountApi { | class AccountApi { | ||||||
|  |     constructor() { | ||||||
|  |         this.timer = null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     async login(user) { |     async login(user) { | ||||||
|         const url = accountTokenUrl(config.baseUrl); |         const url = accountTokenUrl(config.baseUrl); | ||||||
|         console.log(`[Api] Checking auth for ${url}`); |         console.log(`[AccountApi] Checking auth for ${url}`); | ||||||
|         const response = await fetch(url, { |         const response = await fetch(url, { | ||||||
|             method: "POST", |             method: "POST", | ||||||
|             headers: maybeWithBasicAuth({}, user) |             headers: maybeWithBasicAuth({}, user) | ||||||
|  | @ -39,7 +47,7 @@ class AccountApi { | ||||||
| 
 | 
 | ||||||
|     async logout(token) { |     async logout(token) { | ||||||
|         const url = accountTokenUrl(config.baseUrl); |         const url = accountTokenUrl(config.baseUrl); | ||||||
|         console.log(`[Api] Logging out from ${url} using token ${token}`); |         console.log(`[AccountApi] Logging out from ${url} using token ${token}`); | ||||||
|         const response = await fetch(url, { |         const response = await fetch(url, { | ||||||
|             method: "DELETE", |             method: "DELETE", | ||||||
|             headers: maybeWithBearerAuth({}, token) |             headers: maybeWithBearerAuth({}, token) | ||||||
|  | @ -57,7 +65,7 @@ class AccountApi { | ||||||
|             username: username, |             username: username, | ||||||
|             password: password |             password: password | ||||||
|         }); |         }); | ||||||
|         console.log(`[Api] Creating user account ${url}`); |         console.log(`[AccountApi] Creating user account ${url}`); | ||||||
|         const response = await fetch(url, { |         const response = await fetch(url, { | ||||||
|             method: "POST", |             method: "POST", | ||||||
|             body: body |             body: body | ||||||
|  | @ -73,7 +81,7 @@ class AccountApi { | ||||||
| 
 | 
 | ||||||
|     async get() { |     async get() { | ||||||
|         const url = accountUrl(config.baseUrl); |         const url = accountUrl(config.baseUrl); | ||||||
|         console.log(`[Api] Fetching user account ${url}`); |         console.log(`[AccountApi] Fetching user account ${url}`); | ||||||
|         const response = await fetch(url, { |         const response = await fetch(url, { | ||||||
|             headers: maybeWithBearerAuth({}, session.token()) |             headers: maybeWithBearerAuth({}, session.token()) | ||||||
|         }); |         }); | ||||||
|  | @ -83,13 +91,13 @@ class AccountApi { | ||||||
|             throw new Error(`Unexpected server response ${response.status}`); |             throw new Error(`Unexpected server response ${response.status}`); | ||||||
|         } |         } | ||||||
|         const account = await response.json(); |         const account = await response.json(); | ||||||
|         console.log(`[Api] Account`, account); |         console.log(`[AccountApi] Account`, account); | ||||||
|         return account; |         return account; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     async delete() { |     async delete() { | ||||||
|         const url = accountUrl(config.baseUrl); |         const url = accountUrl(config.baseUrl); | ||||||
|         console.log(`[Api] Deleting user account ${url}`); |         console.log(`[AccountApi] Deleting user account ${url}`); | ||||||
|         const response = await fetch(url, { |         const response = await fetch(url, { | ||||||
|             method: "DELETE", |             method: "DELETE", | ||||||
|             headers: maybeWithBearerAuth({}, session.token()) |             headers: maybeWithBearerAuth({}, session.token()) | ||||||
|  | @ -103,7 +111,7 @@ class AccountApi { | ||||||
| 
 | 
 | ||||||
|     async changePassword(newPassword) { |     async changePassword(newPassword) { | ||||||
|         const url = accountPasswordUrl(config.baseUrl); |         const url = accountPasswordUrl(config.baseUrl); | ||||||
|         console.log(`[Api] Changing account password ${url}`); |         console.log(`[AccountApi] Changing account password ${url}`); | ||||||
|         const response = await fetch(url, { |         const response = await fetch(url, { | ||||||
|             method: "POST", |             method: "POST", | ||||||
|             headers: maybeWithBearerAuth({}, session.token()), |             headers: maybeWithBearerAuth({}, session.token()), | ||||||
|  | @ -120,7 +128,7 @@ class AccountApi { | ||||||
| 
 | 
 | ||||||
|     async extendToken() { |     async extendToken() { | ||||||
|         const url = accountTokenUrl(config.baseUrl); |         const url = accountTokenUrl(config.baseUrl); | ||||||
|         console.log(`[Api] Extending user access token ${url}`); |         console.log(`[AccountApi] Extending user access token ${url}`); | ||||||
|         const response = await fetch(url, { |         const response = await fetch(url, { | ||||||
|             method: "PATCH", |             method: "PATCH", | ||||||
|             headers: maybeWithBearerAuth({}, session.token()) |             headers: maybeWithBearerAuth({}, session.token()) | ||||||
|  | @ -135,7 +143,7 @@ class AccountApi { | ||||||
|     async updateSettings(payload) { |     async updateSettings(payload) { | ||||||
|         const url = accountSettingsUrl(config.baseUrl); |         const url = accountSettingsUrl(config.baseUrl); | ||||||
|         const body = JSON.stringify(payload); |         const body = JSON.stringify(payload); | ||||||
|         console.log(`[Api] Updating user account ${url}: ${body}`); |         console.log(`[AccountApi] Updating user account ${url}: ${body}`); | ||||||
|         const response = await fetch(url, { |         const response = await fetch(url, { | ||||||
|             method: "PATCH", |             method: "PATCH", | ||||||
|             headers: maybeWithBearerAuth({}, session.token()), |             headers: maybeWithBearerAuth({}, session.token()), | ||||||
|  | @ -151,7 +159,7 @@ class AccountApi { | ||||||
|     async addSubscription(payload) { |     async addSubscription(payload) { | ||||||
|         const url = accountSubscriptionUrl(config.baseUrl); |         const url = accountSubscriptionUrl(config.baseUrl); | ||||||
|         const body = JSON.stringify(payload); |         const body = JSON.stringify(payload); | ||||||
|         console.log(`[Api] Adding user subscription ${url}: ${body}`); |         console.log(`[AccountApi] Adding user subscription ${url}: ${body}`); | ||||||
|         const response = await fetch(url, { |         const response = await fetch(url, { | ||||||
|             method: "POST", |             method: "POST", | ||||||
|             headers: maybeWithBearerAuth({}, session.token()), |             headers: maybeWithBearerAuth({}, session.token()), | ||||||
|  | @ -163,13 +171,13 @@ class AccountApi { | ||||||
|             throw new Error(`Unexpected server response ${response.status}`); |             throw new Error(`Unexpected server response ${response.status}`); | ||||||
|         } |         } | ||||||
|         const subscription = await response.json(); |         const subscription = await response.json(); | ||||||
|         console.log(`[Api] Subscription`, subscription); |         console.log(`[AccountApi] Subscription`, subscription); | ||||||
|         return subscription; |         return subscription; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     async deleteSubscription(remoteId) { |     async deleteSubscription(remoteId) { | ||||||
|         const url = accountSubscriptionSingleUrl(config.baseUrl, remoteId); |         const url = accountSubscriptionSingleUrl(config.baseUrl, remoteId); | ||||||
|         console.log(`[Api] Removing user subscription ${url}`); |         console.log(`[AccountApi] Removing user subscription ${url}`); | ||||||
|         const response = await fetch(url, { |         const response = await fetch(url, { | ||||||
|             method: "DELETE", |             method: "DELETE", | ||||||
|             headers: maybeWithBearerAuth({}, session.token()) |             headers: maybeWithBearerAuth({}, session.token()) | ||||||
|  | @ -180,6 +188,27 @@ class AccountApi { | ||||||
|             throw new Error(`Unexpected server response ${response.status}`); |             throw new Error(`Unexpected server response ${response.status}`); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     startWorker() { | ||||||
|  |         if (this.timer !== null) { | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |         console.log(`[AccountApi] Starting worker`); | ||||||
|  |         this.timer = setInterval(() => this.runWorker(), intervalMillis); | ||||||
|  |         setTimeout(() => this.runWorker(), delayMillis); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     async runWorker() { | ||||||
|  |         if (!session.token()) { | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |         console.log(`[AccountApi] Extending user access token`); | ||||||
|  |         try { | ||||||
|  |             await this.extendToken(); | ||||||
|  |         } catch (e) { | ||||||
|  |             console.log(`[AccountApi] Error extending user access token`, e); | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export class UsernameTakenError extends Error { | export class UsernameTakenError extends Error { | ||||||
|  |  | ||||||
|  | @ -94,5 +94,6 @@ export const useBackgroundProcesses = () => { | ||||||
|     useEffect(() => { |     useEffect(() => { | ||||||
|         poller.startWorker(); |         poller.startWorker(); | ||||||
|         pruner.startWorker(); |         pruner.startWorker(); | ||||||
|  |         accountApi.startWorker(); | ||||||
|     }, []); |     }, []); | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue