validateToken method
- String token
override
Checks if the given token
is valid or not.
Returns a Future that resolves to true
if the token is valid, false
otherwise.
Implementation
@override
Future<bool> validateToken(String token) async {
log.info("Verifying token [redacted]....");
var response = await networkService.post(
"${config.kMoodleServerAdress}/webservice/rest/server.php",
body: {
"wstoken": token,
"moodlewsrestformat": "json",
});
if (response.isNotOk) {
log.info(
"Got response ${response.statusCode} from token validation. Invaildating token by default.");
return false;
}
final body = response.body as JSON;
// if errorcode is 'accessexception' or 'invalidtoken' the token is invalid
var errorCode = body["errorcode"];
if (errorCode == "accessexception") {
log.info("Token expired");
return false;
}
if (errorCode == "invalidtoken") {
log.info("Token invalid");
return false;
}
log.info("Token is valid");
return true;
}