Line data Source code
1 : library msc_2835_uia_login; 2 : 3 : import 'dart:convert'; 4 : 5 : import 'package:http/http.dart' hide Client; 6 : 7 : import 'package:matrix/matrix.dart'; 8 : 9 : extension UiaLogin on Client { 10 : /// Implementation of MSC2835: 11 : /// https://github.com/Sorunome/matrix-doc/blob/soru/uia-on-login/proposals/2835-uia-on-login.md 12 : /// Set `pathVersion` to `r0` if you need to use the previous 13 : /// version of the login endpoint. 14 0 : Future<LoginResponse> uiaLogin( 15 : LoginType type, { 16 : String? address, 17 : String? deviceId, 18 : AuthenticationIdentifier? identifier, 19 : String? initialDeviceDisplayName, 20 : String? medium, 21 : String? password, 22 : String? token, 23 : String? user, 24 : AuthenticationData? auth, 25 : String pathVersion = 'v3', 26 : bool? refreshToken, 27 : }) async { 28 0 : final requestUri = Uri(path: '_matrix/client/$pathVersion/login'); 29 0 : final request = Request('POST', baseUri!.resolveUri(requestUri)); 30 0 : request.headers['content-type'] = 'application/json'; 31 0 : request.bodyBytes = utf8.encode(jsonEncode({ 32 0 : if (address != null) 'address': address, 33 0 : if (deviceId != null) 'device_id': deviceId, 34 0 : if (identifier != null) 'identifier': identifier.toJson(), 35 : if (initialDeviceDisplayName != null) 36 0 : 'initial_device_display_name': initialDeviceDisplayName, 37 0 : if (medium != null) 'medium': medium, 38 0 : if (password != null) 'password': password, 39 0 : if (token != null) 'token': token, 40 0 : 'type': { 41 : LoginType.mLoginPassword: 'm.login.password', 42 : LoginType.mLoginToken: 'm.login.token' 43 0 : }[type]!, 44 0 : if (user != null) 'user': user, 45 0 : if (auth != null) 'auth': auth.toJson(), 46 0 : if (refreshToken != null) 'refresh_token': refreshToken, 47 : })); 48 0 : final response = await httpClient.send(request); 49 0 : final responseBody = await response.stream.toBytes(); 50 0 : if (response.statusCode != 200) unexpectedResponse(response, responseBody); 51 0 : final responseString = utf8.decode(responseBody); 52 0 : final json = jsonDecode(responseString); 53 0 : return LoginResponse.fromJson(json); 54 : } 55 : }