OAuth Support for refreshing tokens given an access and refresh token #321
Replies: 8 comments 1 reply
-
Hi, thanks for raising this issue and for offering to help solve for this use case. As you mention, it is a bit niche and difficult to generalize, so I propose the following conditions before committing to including this:
If you think you can meet these conditions, then I would love for you to implement this, and, knowing the downloader code base, have a few suggestions for how to approach it:
Let me know what you think. |
Beta Was this translation helpful? Give feedback.
-
I have the exact same use-case and was gonna create a ticket for this :) Wondering if this would be easier by providing some kind of token resolver on the Dart side, not sure if the plugin could launch a Dart VM and execute the resolver even if the app is suspended. CC @lyio |
Beta Was this translation helpful? Give feedback.
-
I believe it's possible to launch a Dart VM from the background, but it is complicated and resource intense, so I believe this should be handled on the native side so that a background task can remain fully in the background. |
Beta Was this translation helpful? Give feedback.
-
I think the approach outlined looks solid, let me have a review of some other OAuth libraries, to get a feel for the different approaches for refreshing tokens, and how those requests are formatted, query param, post body etc. I'll add some notes here, once I'm clear on a generic approach for refreshing tokens |
Beta Was this translation helpful? Give feedback.
-
I'm going to convert this to a discussion (instead of an issue) so it doesn't get labeled as stale by the bot. |
Beta Was this translation helpful? Give feedback.
-
On branch From the (draft) readme: OnTaskStart and OnTaskFinished "native" callbacksFor more complex situations you can use OnTaskStart and OnTaskFinished callbacks. This is only required if you need to - for example - refresh an expired auth token just before an enqueued task starts, or conditionally call your server to confirm an upload has finished successfully, which requires the callback to be called even when the main application has been suspended by the OS. final task = DownloadTask(url: 'https://google.com',
options: TaskOptions(onTaskStart: myStartCallback)); where For most situations, using the event listeners or registered "regular" callbacks is recommended, as they run in the normal application context on the main isolate. Native callbacks are called directly from native code (iOS, Android or Desktop) and therefore behave differently:
You should assume that the callback runs in an isolate, and has no access to application state or to plugins. Native callbacks are really only meant to perform simple "local" functions, operating only on the parameter passed into the callback function. OnTaskStartCallback with signature OnTaskFinishedCallback with signature |
Beta Was this translation helpful? Give feedback.
-
Added an Auth mechanism using the same callback concept, on the AuthorizationThe
The downloader uses the
A typical way to construct a task with authorization and default final auth = Auth(
accessToken: 'initialAccessToken',
accessHeaders: {'Authorization': 'Bearer {accessToken}'},
refreshToken: 'initialRefreshToken',
refreshUrl: 'https://your.server/refresh_endpoint',
accessTokenExpiryTime: DateTime.now()
.add(const Duration(minutes: 10)), // typically extracted from token
onAuth: defaultOnAuth // to use typical default callback
);
final task = DownloadTask(
url: 'https://your.server/download_endpoint',
urlQueryParameters: {'param1': 'value1'},
headers: {'Header1': 'value2'},
filename: 'my_file.txt',
options: TaskOptions(auth: auth)); |
Beta Was this translation helpful? Give feedback.
-
Published in V8.9.0 |
Beta Was this translation helpful? Give feedback.
-
We have an application that using background downloader to upload images in the background, as part of completing a large process based submission. The service is using OAuth with JWTs to manage the Authentication and Authorization of the upload requests. We are seeing instances of the access token expiring between when we create the task (and set the Auth Header) and when the task happens.
Would love to be able to provide, access token, refresh token and token refresh endpoint to allow the token to be refreshed if the current access token expires.
Have attempted to update the access token, just before creating the task, and although this reduces the error, it doesn't eliminate it.
I'm willing to put together a PR across both platforms, although mindful that I don't want to complicate your API too much, and question whether we can make it generic enough to update the token for a wide enough audience to make it worth while.
Beta Was this translation helpful? Give feedback.
All reactions