authorize method

Future<Authorization> authorize (String user, String password)

Implementation

static Future<Authorization> authorize(String user, String password) async {
  String url = "https://authserver.mojang.com/authenticate";
  Map cont = {
    "agent": {
      "name": "Minecraft",
      "version": 1
    },
    "username": user,
    "password": password,
    "clientToken": _uuid.v1(),
    "requestUser": true
  };

  Map<String,String> headers = {
    "Content-Type": "application/json"
  };

  Map<String, dynamic> answer;

  try {
    answer = json.decode((await http.post(url,body: json.encode(cont), headers: headers)).body);
  } catch(e) {
    Chooser chooser = Chooser<String>(
      ["Y", "N"],
      message: "Could not connect to auth-server. Do you want to launch an offline session instead? (Y/n): ",
    );
    if(await chooser.choose() == "Y") {
      String name = await readLine();
      return Authorization.offline(name: name);
    }
    else {
      throw e;
    }
  }

  if(answer["selectedProfile"] == null) {
    throw("Wrong user / password!");
  }

  return new Authorization(
    access_token: answer["accessToken"],
    client_token: answer["clientToken"],
    uuid: answer["selectedProfile"]["id"],
    name: answer["selectedProfile"]["name"],
    selected_profile: answer["selectedProfile"],
    user_properties: json.encode(answer["user"]["properties"] ?? {}),
    answer: answer,
    userId: answer["user"]["id"],
    email: answer["user"]["email"],
  );
}