farmOS / field-kit

A modular, offline-first companion app to farmOS.
https://farmOS.org
GNU General Public License v3.0
59 stars 38 forks source link

Detect farmOS server version (1 or 2). #440

Closed jgaehring closed 2 years ago

jgaehring commented 3 years ago

Since we'll have two versions of both Field Kit and farmOS living side by side for a while as folks migrate, we need to ensure that FK can detect which version of the farmOS server it's connecting to, and if it doesn't match, provide intelligible error message explaining why it's incompatible, and a link to the other version of FK. We should do so before any viable version of FK 2.x is out in the wild, so preferably before the beta release.

paul121 commented 3 years ago

The 2.x version of the aggregator supports both 1.x and 2.x servers which created a few spots where we need to determine the server version... a simple way I found was to check the length of the access token! (example)

In 2.x the access tokens are JWTs which will always be much longer than the access tokens used in 1.x (believe they are 50 char long). But just thinking.... this method might only work when checking FK instances that are already authorized/connected to a farmOS server though.

Detecting the server version when first connecting FK to a server might be a bit more difficult.. an initial request to /api will show the version for 2.x. Another complication is that the OAuth scopes used in 2.x will likely be different from 1.x. If the wrong scopes are used when connecting to either a 1.x or 2.x server, that authorization should fail. So perhaps some error catching will be necessary...

symbioquine commented 3 years ago

Detecting the server version when first connecting FK to a server might be a bit more difficult.. an initial request to /api will show the version for 2.x. Another complication is that the OAuth scopes used in 2.x will likely be different from 1.x. If the wrong scopes are used when connecting to either a 1.x or 2.x server, that authorization should fail. So perhaps some error catching will be necessary...

This sounds right. I was starting to write this in reply to https://farmos.discourse.group/t/migrating-from-farmos-app-to-v1-farmos-app but then I found this issue;

jgaehring commented 3 years ago

otherwise assume it's a 1.x server - until proven otherwise.

Yea, I think this is the key to any approach. I will probably concentrate most of my efforts on the initial authorization process, on the assumption that it shouldn't be possible for users with fresh installs to get past that step anyways, so I shouldn't need to guard against those kinds of requests.

Of course, where this may get really thorny is for users whose server just recently underwent a migration to 2.x, and then try to connect via v1.farmos.app. That will probably be point with the greatest likelihood of data loss, too.