Add QR code support

This commit is contained in:
Andrea Cavalli 2021-09-27 22:15:17 +02:00
parent c6a8af0c44
commit 1cba9f7e38
7 changed files with 71 additions and 16 deletions

View File

@ -1,9 +1,3 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
package it.tdlight.example; package it.tdlight.example;
import it.tdlight.client.APIToken; import it.tdlight.client.APIToken;

View File

@ -2,12 +2,18 @@ package it.tdlight.client;
public interface AuthenticationData { public interface AuthenticationData {
boolean isQrCode();
boolean isBot(); boolean isBot();
long getUserPhoneNumber(); long getUserPhoneNumber();
String getBotToken(); String getBotToken();
static AuthenticationData qrCode() {
return new AuthenticationDataQrCode();
}
static AuthenticationData user(long userPhoneNumber) { static AuthenticationData user(long userPhoneNumber) {
return new AuthenticationDataImpl(userPhoneNumber, null); return new AuthenticationDataImpl(userPhoneNumber, null);
} }

View File

@ -20,6 +20,11 @@ final class AuthenticationDataImpl implements AuthenticationData {
this.botToken = botToken; this.botToken = botToken;
} }
@Override
public boolean isQrCode() {
return false;
}
@Override @Override
public boolean isBot() { public boolean isBot() {
return botToken != null; return botToken != null;

View File

@ -0,0 +1,24 @@
package it.tdlight.client;
class AuthenticationDataQrCode implements AuthenticationData {
@Override
public boolean isQrCode() {
return true;
}
@Override
public boolean isBot() {
return false;
}
@Override
public long getUserPhoneNumber() {
throw new UnsupportedOperationException("This is not a user");
}
@Override
public String getBotToken() {
throw new UnsupportedOperationException("This is not a bot");
}
}

View File

@ -40,6 +40,13 @@ final class AuthorizationStateWaitAuthenticationDataHandler implements GenericUp
throw new TelegramError((Error) ok); throw new TelegramError((Error) ok);
} }
}, exceptionHandler); }, exceptionHandler);
} else if (authenticationData.isQrCode()) {
TdApi.RequestQrCodeAuthentication response = new TdApi.RequestQrCodeAuthentication();
client.send(response, ok -> {
if (ok.getConstructor() == Error.CONSTRUCTOR) {
throw new TelegramError((Error) ok);
}
}, exceptionHandler);
} else { } else {
PhoneNumberAuthenticationSettings phoneSettings = new PhoneNumberAuthenticationSettings(false, false, false); PhoneNumberAuthenticationSettings phoneSettings = new PhoneNumberAuthenticationSettings(false, false, false);

View File

@ -9,6 +9,7 @@ final class ConsoleInteractiveAuthenticationData implements AuthenticationData {
private static final Object LOCK = new Object(); private static final Object LOCK = new Object();
private boolean initialized = false; private boolean initialized = false;
private boolean isQr;
private boolean isBot; private boolean isBot;
private String botToken; private String botToken;
private long phoneNumber; private long phoneNumber;
@ -25,6 +26,12 @@ final class ConsoleInteractiveAuthenticationData implements AuthenticationData {
return initialized; return initialized;
} }
@Override
public boolean isQrCode() {
initializeIfNeeded();
return isQr;
}
@Override @Override
public boolean isBot() { public boolean isBot() {
initializeIfNeeded(); initializeIfNeeded();
@ -34,7 +41,7 @@ final class ConsoleInteractiveAuthenticationData implements AuthenticationData {
@Override @Override
public long getUserPhoneNumber() { public long getUserPhoneNumber() {
initializeIfNeeded(); initializeIfNeeded();
if (isBot) { if (isBot || isQr) {
throw new UnsupportedOperationException("This is not a user"); throw new UnsupportedOperationException("This is not a user");
} }
return phoneNumber; return phoneNumber;
@ -43,7 +50,7 @@ final class ConsoleInteractiveAuthenticationData implements AuthenticationData {
@Override @Override
public String getBotToken() { public String getBotToken() {
initializeIfNeeded(); initializeIfNeeded();
if (!isBot) { if (!isBot || isQr) {
throw new UnsupportedOperationException("This is not a bot"); throw new UnsupportedOperationException("This is not a bot");
} }
return botToken; return botToken;
@ -58,22 +65,24 @@ final class ConsoleInteractiveAuthenticationData implements AuthenticationData {
String choice; String choice;
// Choose login type // Choose login type
Boolean useBotToken; String mode;
do { do {
choice = ScannerUtils choice = ScannerUtils
.askParameter("login", "Do you want to login using a bot [token] or a [phone] number? [token/phone]") .askParameter("login", "Do you want to login using a bot [token], a [phone] number, or a [qr] code? [token/phone/qr]")
.trim() .trim()
.toLowerCase(Locale.ROOT); .toLowerCase(Locale.ROOT);
if ("phone".equals(choice)) { if ("phone".equals(choice)) {
useBotToken = false; mode = "PHONE";
} else if ("token".equals(choice)) { } else if ("token".equals(choice)) {
useBotToken = true; mode = "TOKEN";
} else if ("qr".equals(choice)) {
mode = "QR";
} else { } else {
useBotToken = null; mode = null;
} }
} while (useBotToken == null); } while (mode == null);
if (useBotToken) { if ("TOKEN".equals(mode)) {
String token; String token;
do { do {
token = ScannerUtils.askParameter("login", "Please type the bot token"); token = ScannerUtils.askParameter("login", "Please type the bot token");
@ -82,7 +91,8 @@ final class ConsoleInteractiveAuthenticationData implements AuthenticationData {
this.isBot = true; this.isBot = true;
this.phoneNumber = -1; this.phoneNumber = -1;
this.botToken = token; this.botToken = token;
} else { this.isQr = false;
} else if ("PHONE".equals(mode)) {
String phoneNumber; String phoneNumber;
do { do {
phoneNumber = ScannerUtils.askParameter("login", "Please type your phone number"); phoneNumber = ScannerUtils.askParameter("login", "Please type your phone number");
@ -97,6 +107,13 @@ final class ConsoleInteractiveAuthenticationData implements AuthenticationData {
this.isBot = false; this.isBot = false;
this.phoneNumber = phoneNumberLong; this.phoneNumber = phoneNumberLong;
this.botToken = null; this.botToken = null;
this.isQr = false;
} else {
this.isBot = false;
this.phoneNumber = -1;
this.botToken = null;
this.isQr = true;
} }
initialized = true; initialized = true;

View File

@ -23,6 +23,8 @@ final class ScannerClientInteraction implements ClientInteraction {
} }
if (!useRealWho) { if (!useRealWho) {
who = "login"; who = "login";
} else if (authenticationData.isQrCode()) {
who = "QR login";
} else if (authenticationData.isBot()) { } else if (authenticationData.isBot()) {
who = authenticationData.getBotToken().split(":", 2)[0]; who = authenticationData.getBotToken().split(":", 2)[0];
} else { } else {