authenticate method
- required String username,
- required String password,
- required Set<
Webservice> webservices,
override
Authenticates the user with the given username
and password
for the given webservices
.
Implementation
@override
Future<Set<Token>> authenticate({required String username, required String password, required Set<Webservice> webservices}) async {
log('Authenticating user $username for ${webservices.map((webService) => webService.name).join(', ')}');
const url = '$kMoodleServerAdress/login/token.php';
final tokens = <Token>{};
for (final webservice in webservices) {
final response = await _networkService.post(
url,
{
'username': username,
'password': password,
'service': webservice.name,
'moodlewsrestformat': 'json',
},
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
);
if (response.isNotOk) {
final e = AuthException(response, webservice);
log('Authentication failed', e);
throw e;
}
final json = response.body as JSON;
final token = json['token'] as String?;
if (token == null) {
final e = _parseError(json, webservice);
log('Authentication failed', e);
throw e;
}
tokens.add(Token(token: token, webservice: webservice));
log('Authenticated user $username for ${webservice.name}');
}
return tokens;
}