Compare commits

..

No commits in common. "2fa-support" and "master" have entirely different histories.

7 changed files with 18 additions and 133 deletions

View File

@ -1,7 +1,5 @@
# Building
Clone the repo, then open the project in Android Studio.
Make sure the required Android NDK platforms are installed, and the environment
variable `ANDROID_NDK_TOOLCHAIN_DIR` is configured correctly.
@ -10,9 +8,3 @@ On Windows, this might be a path such as the following (NDK "Side by side" SDK t
```
%LOCALAPPDATA%\Android\Sdk\ndk\25.1.8937393\toolchains\llvm\prebuilt\windows-x86_64\bin
```
Set your API ID and hash in the rust code.
Change the Cargo.toml to point to a local grammers source tree and remove the version argument.
Sync gradle files, and Android Studio's "build" and "run" should Just Work.

View File

@ -72,7 +72,7 @@ fun TalariaApp() {
ChatScreen(selectedDialog)
}
composable(route = TalariaScreen.Login.name) {
LoginScreen(onLoginComplete = {
LoginScreen(onConfirmOtp = {
navController.navigate(TalariaScreen.Dialog.name)
})
}

View File

@ -8,10 +8,7 @@ object Native {
external fun initClient()
external fun needLogin(): Boolean
external fun requestLoginCode(phone: String): Long
// Returns true if sign in complete, false if 2fa password needed
// TODO: more rich return type including password hint / type of error
external fun signIn(tokenPtr: Long, code: String): Boolean
external fun checkPassword(password: String)
external fun signIn(tokenPtr: Long, code: String)
external fun getDialogs(): Long
external fun dialogCount(dialogsPtr: Long): Int
external fun dialogPacked(dialogsPtr: Long, index: Int): String

View File

@ -24,12 +24,10 @@ import dev.lonami.talaria.ui.theme.TalariaTheme
enum class LoginStage {
ASK_PHONE,
ASK_CODE,
ASK_PASSWORD,
}
fun isPhoneValid(phone: String): Boolean = phone.trim('+', ' ').isNotEmpty()
fun isLoginCodeValid(code: String): Boolean = code.trim().count { it.isDigit() } == 5
fun isPasswordValid(password: String): Boolean = password.isNotEmpty()
@Composable
fun PhoneInput(phone: String, onPhoneChanged: (String) -> Unit, onSendCode: () -> Unit) {
@ -82,50 +80,16 @@ fun OtpInput(otp: String, onOtpChanged: (String) -> Unit, onConfirmOtp: () -> Un
enabled = isLoginCodeValid(otp),
modifier = Modifier.fillMaxWidth(),
onClick = onConfirmOtp
) {
Text(stringResource(R.string.do_continue))
}
}
@Composable
fun PasswordInput(
password: String,
onPasswordChanged: (String) -> Unit,
onConfirmPassword: () -> Unit
) {
val focusManager = LocalFocusManager.current
Text(stringResource(R.string.enter_2fa_password))
TextField(
password,
label = { Text(stringResource(R.string.password)) },
placeholder = { Text(stringResource(R.string.password_example)) },
singleLine = true,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Password,
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
modifier = Modifier.fillMaxWidth(),
onValueChange = onPasswordChanged
)
Spacer(Modifier.height(16.dp))
Button(
enabled = isPasswordValid(password),
modifier = Modifier.fillMaxWidth(),
onClick = onConfirmPassword
) {
Text(stringResource(R.string.do_login))
}
}
@Composable
fun LoginScreen(onLoginComplete: () -> Unit) {
fun LoginScreen(onConfirmOtp: () -> Unit) {
var stage by remember { mutableStateOf(LoginStage.ASK_PHONE) }
var phone by remember { mutableStateOf("") }
var otp by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }
var tokenPtr by remember { mutableStateOf(0L) }
@ -157,21 +121,10 @@ fun LoginScreen(onLoginComplete: () -> Unit) {
otp,
onOtpChanged = { otp = it },
onConfirmOtp = {
val signInComplete = Native.signIn(tokenPtr, otp)
if (signInComplete) {
onLoginComplete()
} else {
stage = LoginStage.ASK_PASSWORD;
}
Native.signIn(tokenPtr, otp)
onConfirmOtp()
}
)
LoginStage.ASK_PASSWORD -> PasswordInput(
password,
onPasswordChanged = { password = it },
onConfirmPassword = {
Native.checkPassword(password)
onLoginComplete();
})
}
}
}
@ -180,6 +133,6 @@ fun LoginScreen(onLoginComplete: () -> Unit) {
@Composable
fun LoginPreview() {
TalariaTheme {
LoginScreen(onLoginComplete = { })
LoginScreen(onConfirmOtp = { })
}
}

View File

@ -8,10 +8,6 @@
<string name="enter_otp">Enter the code you received:</string>
<string name="otp">Code</string>
<string name="otp_example">12345</string>
<string name="enter_2fa_password">Enter your 2-factor authentication password:</string>
<string name="password">Password</string>
<string name="password_example">hunter2</string>
<string name="do_continue">Continue</string>
<string name="do_login">Login</string>
<string name="profile_photo">Profile Picture</string>
<string name="write_message">Write a message…</string>

0
gradlew vendored Executable file → Normal file
View File

View File

@ -4,12 +4,12 @@
use std::ffi::{CStr, CString};
use std::future::Future;
use grammers_client::types::{Dialog, LoginToken, PasswordToken};
use grammers_client::{Client, Config, SignInError};
use grammers_client::{Client, Config};
use grammers_client::types::{Dialog, LoginToken};
use grammers_session::{PackedChat, Session};
use jni::JNIEnv;
use jni::objects::{JObject, JString};
use jni::sys::{jboolean, jint, jlong, jstring};
use jni::JNIEnv;
use log;
use log::{error, info, Level};
use once_cell::sync::OnceCell;
@ -83,31 +83,10 @@ async fn request_login_code(phone: &str) -> Result<LoginToken> {
Ok(token)
}
async fn sign_in(token: LoginToken, code: &str) -> Result<bool> {
async fn sign_in(token: LoginToken, code: &str) -> Result<()> {
let client = CLIENT.get().ok_or("Client not initialized")?;
return match client.sign_in(&token, &code).await {
Err(SignInError::PasswordRequired(token)) => {
info!("Sign in 2fa password required. Hint: {:?}", token.hint());
Ok(false)
}
Err(e) => {
client.sign_out().await?;
Err(Box::new(e))
}
Ok(_) => Ok(true),
};
}
async fn check_password(password: &str) -> Result<()> {
let client = CLIENT.get().ok_or("Client not initialized")?;
let token = PasswordToken::new("hint");
match client.check_password(token, &password).await {
Err(e) => {
client.sign_out().await?;
Err(Box::new(e))
}
Ok(_) => Ok(())
}
client.sign_in(&token, &code).await?;
Ok(())
}
async fn get_dialogs() -> Result<Vec<Dialog>> {
@ -172,46 +151,14 @@ pub unsafe extern "C" fn Java_dev_lonami_talaria_bindings_Native_signIn(
_: JObject,
token_ptr: jlong,
code: JString,
) -> jboolean {
) {
let token = *Box::from_raw(token_ptr as *mut LoginToken);
let code = CString::from(CStr::from_ptr(env.get_string(code).unwrap().as_ptr()));
return match block_on(sign_in(token, code.to_str().unwrap())) {
Ok(sign_in_complete) => {
if sign_in_complete {
info!("Sign in success");
match block_on(sign_in(token, code.to_str().unwrap())) {
Ok(_) => info!("Sign in success"),
Err(e) => error!("Failed to sign in: {}", e),
}
sign_in_complete.into()
}
Err(e) => {
error!("Failed to sign in: {}", e);
true.into()
}
};
}
#[no_mangle]
pub unsafe extern "C" fn Java_dev_lonami_talaria_bindings_Native_checkPassword(
env: JNIEnv,
_: JObject,
token_ptr: jlong,
code: JString,
) -> jboolean {
let token = *Box::from_raw(token_ptr as *mut LoginToken);
let code = CString::from(CStr::from_ptr(env.get_string(code).unwrap().as_ptr()));
return match block_on(sign_in(token, code.to_str().unwrap())) {
Ok(sign_in_complete) => {
if sign_in_complete {
info!("Sign in success");
}
sign_in_complete.into()
}
Err(e) => {
error!("Failed to sign in: {}", e);
true.into()
}
};
}
#[no_mangle]