feat: Allow init with access token
Makes it possible to just init with an access token and a homeserver uri and the init() method would just call /whoami to fetch the missing data. Makes it easier to login with Matrix native OIDC
This commit is contained in:
parent
5ef19c6d3b
commit
4170c0fca5
|
|
@ -1352,7 +1352,7 @@ class FakeMatrixApi extends BaseClient {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
'/client/v3/account/whoami': (var req) =>
|
'/client/v3/account/whoami': (var req) =>
|
||||||
{'user_id': 'alice@example.com'},
|
{'user_id': 'alice@example.com', 'device_id': 'ABCDEFGH'},
|
||||||
'/client/v3/capabilities': (var req) => {
|
'/client/v3/capabilities': (var req) => {
|
||||||
'capabilities': {
|
'capabilities': {
|
||||||
'm.change_password': {'enabled': false},
|
'm.change_password': {'enabled': false},
|
||||||
|
|
|
||||||
|
|
@ -1982,9 +1982,9 @@ class Client extends MatrixApi {
|
||||||
///
|
///
|
||||||
/// Sends [LoginState.loggedIn] to [onLoginStateChanged].
|
/// Sends [LoginState.loggedIn] to [onLoginStateChanged].
|
||||||
///
|
///
|
||||||
/// If one of [newToken], [newUserID], [newDeviceID], [newDeviceName] is set then
|
/// If one of [newToken] is set, but one of [newUserID], [newDeviceID] is
|
||||||
/// all of them must be set! If you don't set them, this method will try to
|
/// null, then this method calls `/whoami` to fetch user ID and device ID
|
||||||
/// get them from the database.
|
/// and rethrows if this request fails.
|
||||||
///
|
///
|
||||||
/// Set [waitForFirstSync] and [waitUntilLoadCompletedLoaded] to false to speed this
|
/// Set [waitForFirstSync] and [waitUntilLoadCompletedLoaded] to false to speed this
|
||||||
/// up. You can then wait for `roomsLoading`, `_accountDataLoading` and
|
/// up. You can then wait for `roomsLoading`, `_accountDataLoading` and
|
||||||
|
|
@ -2008,16 +2008,9 @@ class Client extends MatrixApi {
|
||||||
/// To track what actually happens you can set a callback here.
|
/// To track what actually happens you can set a callback here.
|
||||||
void Function(InitState)? onInitStateChanged,
|
void Function(InitState)? onInitStateChanged,
|
||||||
}) async {
|
}) async {
|
||||||
if ((newToken != null ||
|
if (newToken != null && homeserver == null && newHomeserver == null) {
|
||||||
newUserID != null ||
|
|
||||||
newDeviceID != null ||
|
|
||||||
newDeviceName != null) &&
|
|
||||||
(newToken == null ||
|
|
||||||
newUserID == null ||
|
|
||||||
newDeviceID == null ||
|
|
||||||
newDeviceName == null)) {
|
|
||||||
throw ClientInitPreconditionError(
|
throw ClientInitPreconditionError(
|
||||||
'If one of [newToken, newUserID, newDeviceID, newDeviceName] is set then all of them must be set!',
|
'init() can not be performed with an access token when no homeserver was specified.',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -2109,6 +2102,12 @@ class Client extends MatrixApi {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (accessToken != null && (userID == null || deviceID == null)) {
|
||||||
|
final userInfo = await getTokenOwner();
|
||||||
|
_userID = userID = userInfo.userId;
|
||||||
|
_deviceID = userInfo.deviceId;
|
||||||
|
}
|
||||||
|
|
||||||
if (accessToken == null || homeserver == null || userID == null) {
|
if (accessToken == null || homeserver == null || userID == null) {
|
||||||
if (legacyDatabaseBuilder != null) {
|
if (legacyDatabaseBuilder != null) {
|
||||||
await _migrateFromLegacyDatabase(
|
await _migrateFromLegacyDatabase(
|
||||||
|
|
|
||||||
|
|
@ -524,6 +524,22 @@ void main() {
|
||||||
FakeMatrixApi.currentApi?.api = oldapi!;
|
FakeMatrixApi.currentApi?.api = oldapi!;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('init() with access token', () async {
|
||||||
|
final client = Client(
|
||||||
|
'testclient',
|
||||||
|
httpClient: FakeMatrixApi(),
|
||||||
|
database: await getDatabase(),
|
||||||
|
);
|
||||||
|
await client.init(
|
||||||
|
newToken: 'abcd1234',
|
||||||
|
newHomeserver: Uri.parse('https://fakeserver.notexisting'),
|
||||||
|
);
|
||||||
|
expect(client.isLogged(), true);
|
||||||
|
expect(client.userID, 'alice@example.com');
|
||||||
|
expect(client.deviceID, 'ABCDEFGH');
|
||||||
|
await client.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
test('Login', () async {
|
test('Login', () async {
|
||||||
matrix = Client(
|
matrix = Client(
|
||||||
'testclient',
|
'testclient',
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue