Add QR code support
This commit is contained in:
parent
c6a8af0c44
commit
1cba9f7e38
@ -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;
|
||||
|
||||
import it.tdlight.client.APIToken;
|
||||
|
@ -2,12 +2,18 @@ package it.tdlight.client;
|
||||
|
||||
public interface AuthenticationData {
|
||||
|
||||
boolean isQrCode();
|
||||
|
||||
boolean isBot();
|
||||
|
||||
long getUserPhoneNumber();
|
||||
|
||||
String getBotToken();
|
||||
|
||||
static AuthenticationData qrCode() {
|
||||
return new AuthenticationDataQrCode();
|
||||
}
|
||||
|
||||
static AuthenticationData user(long userPhoneNumber) {
|
||||
return new AuthenticationDataImpl(userPhoneNumber, null);
|
||||
}
|
||||
|
@ -20,6 +20,11 @@ final class AuthenticationDataImpl implements AuthenticationData {
|
||||
this.botToken = botToken;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isQrCode() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBot() {
|
||||
return botToken != null;
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
@ -40,6 +40,13 @@ final class AuthorizationStateWaitAuthenticationDataHandler implements GenericUp
|
||||
throw new TelegramError((Error) ok);
|
||||
}
|
||||
}, 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 {
|
||||
PhoneNumberAuthenticationSettings phoneSettings = new PhoneNumberAuthenticationSettings(false, false, false);
|
||||
|
||||
|
@ -9,6 +9,7 @@ final class ConsoleInteractiveAuthenticationData implements AuthenticationData {
|
||||
private static final Object LOCK = new Object();
|
||||
|
||||
private boolean initialized = false;
|
||||
private boolean isQr;
|
||||
private boolean isBot;
|
||||
private String botToken;
|
||||
private long phoneNumber;
|
||||
@ -25,6 +26,12 @@ final class ConsoleInteractiveAuthenticationData implements AuthenticationData {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isQrCode() {
|
||||
initializeIfNeeded();
|
||||
return isQr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBot() {
|
||||
initializeIfNeeded();
|
||||
@ -34,7 +41,7 @@ final class ConsoleInteractiveAuthenticationData implements AuthenticationData {
|
||||
@Override
|
||||
public long getUserPhoneNumber() {
|
||||
initializeIfNeeded();
|
||||
if (isBot) {
|
||||
if (isBot || isQr) {
|
||||
throw new UnsupportedOperationException("This is not a user");
|
||||
}
|
||||
return phoneNumber;
|
||||
@ -43,7 +50,7 @@ final class ConsoleInteractiveAuthenticationData implements AuthenticationData {
|
||||
@Override
|
||||
public String getBotToken() {
|
||||
initializeIfNeeded();
|
||||
if (!isBot) {
|
||||
if (!isBot || isQr) {
|
||||
throw new UnsupportedOperationException("This is not a bot");
|
||||
}
|
||||
return botToken;
|
||||
@ -58,22 +65,24 @@ final class ConsoleInteractiveAuthenticationData implements AuthenticationData {
|
||||
String choice;
|
||||
|
||||
// Choose login type
|
||||
Boolean useBotToken;
|
||||
String mode;
|
||||
do {
|
||||
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()
|
||||
.toLowerCase(Locale.ROOT);
|
||||
if ("phone".equals(choice)) {
|
||||
useBotToken = false;
|
||||
mode = "PHONE";
|
||||
} else if ("token".equals(choice)) {
|
||||
useBotToken = true;
|
||||
mode = "TOKEN";
|
||||
} else if ("qr".equals(choice)) {
|
||||
mode = "QR";
|
||||
} else {
|
||||
useBotToken = null;
|
||||
mode = null;
|
||||
}
|
||||
} while (useBotToken == null);
|
||||
} while (mode == null);
|
||||
|
||||
if (useBotToken) {
|
||||
if ("TOKEN".equals(mode)) {
|
||||
String token;
|
||||
do {
|
||||
token = ScannerUtils.askParameter("login", "Please type the bot token");
|
||||
@ -82,7 +91,8 @@ final class ConsoleInteractiveAuthenticationData implements AuthenticationData {
|
||||
this.isBot = true;
|
||||
this.phoneNumber = -1;
|
||||
this.botToken = token;
|
||||
} else {
|
||||
this.isQr = false;
|
||||
} else if ("PHONE".equals(mode)) {
|
||||
String phoneNumber;
|
||||
do {
|
||||
phoneNumber = ScannerUtils.askParameter("login", "Please type your phone number");
|
||||
@ -97,6 +107,13 @@ final class ConsoleInteractiveAuthenticationData implements AuthenticationData {
|
||||
this.isBot = false;
|
||||
this.phoneNumber = phoneNumberLong;
|
||||
this.botToken = null;
|
||||
this.isQr = false;
|
||||
} else {
|
||||
|
||||
this.isBot = false;
|
||||
this.phoneNumber = -1;
|
||||
this.botToken = null;
|
||||
this.isQr = true;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
|
@ -23,6 +23,8 @@ final class ScannerClientInteraction implements ClientInteraction {
|
||||
}
|
||||
if (!useRealWho) {
|
||||
who = "login";
|
||||
} else if (authenticationData.isQrCode()) {
|
||||
who = "QR login";
|
||||
} else if (authenticationData.isBot()) {
|
||||
who = authenticationData.getBotToken().split(":", 2)[0];
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user