From e28b0fa1b3df01b596154da26a523439961c8d5d Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Tue, 9 Mar 2021 07:40:16 +0100 Subject: [PATCH] refactor: login method AuthenticationIdentifier This is a breaking change for the login method to use the correct format. It makes it possible to login with email or phone. Also this does some housekeeping stuff while upgrading to pedantic 1.11.0 which doesnt allow curly braces in Strings where not needed anymore. --- CHANGELOG.md | 10 ++++++++++ lib/src/matrix_api.dart | 23 ++++++++++++----------- pubspec.yaml | 6 +++--- test/matrix_api_test.dart | 5 +++-- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c71fdc5f..808c2a9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +## 0.2.0 +- refactor: login method AuthenticationIdentifier + +This is a breaking change for the login method to use the correct format. +It makes it possible to login with email or phone. +Also this does some housekeeping stuff while +upgrading to pedantic 1.11.0 which doesnt +allow curly braces in Strings where not needed +anymore. + ## 0.1.9 - feat: Add support for fallback keys diff --git a/lib/src/matrix_api.dart b/lib/src/matrix_api.dart index 037847dc..b227f375 100644 --- a/lib/src/matrix_api.dart +++ b/lib/src/matrix_api.dart @@ -28,6 +28,7 @@ import 'dart:typed_data'; import 'package:http/http.dart' as http; import 'package:mime/mime.dart'; +import '../matrix_api_lite.dart'; import 'model/auth/authentication_data.dart'; import 'model/auth/authentication_types.dart'; import 'model/device.dart'; @@ -152,7 +153,7 @@ class MatrixApi { ?.where((x) => x.value != null) ?.map((x) => [x.key, x.value].map(Uri.encodeQueryComponent).join('=')) ?.join('&'); - final url = ['${homeserver.toString()}/_matrix${action}', queryPart] + final url = ['${homeserver.toString()}/_matrix$action', queryPart] .where((x) => x != null && x != '') .join('?'); @@ -161,7 +162,7 @@ class MatrixApi { headers['Content-Type'] = contentType; } if (accessToken != null) { - headers['Authorization'] = 'Bearer ${accessToken}'; + headers['Authorization'] = 'Bearer $accessToken'; } http.Response resp; @@ -253,25 +254,25 @@ class MatrixApi { /// Authenticates the user, and issues an access token they can use to authorize themself in subsequent requests. /// https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-login + /// To just login with the username 'alice' you set [identifier] to: + /// `AuthenticationUserIdentifier(user: 'alice')` + /// Maybe you want to set [user] to the same String to stay compatible with + /// older server versions. Future login({ String type = AuthenticationTypes.password, - String userIdentifierType, - String user, - String medium, - String address, + AuthenticationIdentifier identifier, String password, String token, String deviceId, String initialDeviceDisplayName, AuthenticationData auth, + @Deprecated('Deprecated in favour of identifier.') String user, + @Deprecated('Deprecated in favour of identifier.') String medium, + @Deprecated('Deprecated in favour of identifier.') String address, }) async { final response = await request(RequestType.POST, '/client/r0/login', data: { 'type': type, - if (userIdentifierType != null) - 'identifier': { - 'type': userIdentifierType, - if (user != null) 'user': user, - }, + if (identifier != null) 'identifier': identifier.toJson(), if (user != null) 'user': user, if (medium != null) 'medium': medium, if (address != null) 'address': address, diff --git a/pubspec.yaml b/pubspec.yaml index 48825ac5..ec883d32 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,10 +1,10 @@ name: matrix_api_lite description: Dead simple data model for the matrix.org client-server API. -version: 0.1.9 +version: 0.2.0 homepage: https://famedly.com environment: - sdk: '>=2.10.0 <3.0.0' + sdk: ">=2.10.0 <3.0.0" dependencies: http: ^0.12.2 @@ -12,5 +12,5 @@ dependencies: mime: ^0.9.7 dev_dependencies: - pedantic: ^1.9.0 + pedantic: ^1.11.0 test: ^1.14.4 diff --git a/test/matrix_api_test.dart b/test/matrix_api_test.dart index c52b0778..c25f8430 100644 --- a/test/matrix_api_test.dart +++ b/test/matrix_api_test.dart @@ -155,8 +155,9 @@ void main() { }); test('login', () async { matrixApi.homeserver = Uri.parse('https://fakeserver.notexisting'); - final loginResponse = - await matrixApi.login(userIdentifierType: 'username'); + final loginResponse = await matrixApi.login( + identifier: AuthenticationUserIdentifier(user: 'username'), + ); expect(FakeMatrixApi.api['POST']['/client/r0/login']({}), loginResponse.toJson()); matrixApi.homeserver = null;