mirror of
https://gitlab.com/agosto182/p2play
synced 2025-01-31 18:54:50 +01:00
Add 2fa login support
This commit is contained in:
parent
6a8d3baccb
commit
4d2644674e
@ -6,7 +6,9 @@ import android.os.AsyncTask
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.os.Bundle
|
||||
import android.os.Looper
|
||||
import android.preference.PreferenceManager
|
||||
import android.widget.EditText
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.preference.PreferenceManager
|
||||
import kotlinx.android.synthetic.main.activity_login.*
|
||||
import org.libre.agosto.p2play.ajax.Auth
|
||||
|
||||
@ -16,6 +18,7 @@ class LoginActivity : AppCompatActivity() {
|
||||
lateinit var client_id: String
|
||||
lateinit var client_secret: String
|
||||
private lateinit var _db: Database
|
||||
private var optCode: String? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@ -40,12 +43,11 @@ class LoginActivity : AppCompatActivity() {
|
||||
if (Looper.myLooper()==null)
|
||||
Looper.prepare()
|
||||
|
||||
val token = _auth.login(username, password, client_id, client_secret)
|
||||
val token = _auth.login(username, password, client_id, client_secret, optCode)
|
||||
|
||||
// Log.d("token", token.token )
|
||||
// Log.d("status", token.status.toString() )
|
||||
|
||||
|
||||
when(token.status.toString()){
|
||||
"1" -> {
|
||||
_db.newToken(token)
|
||||
@ -63,6 +65,29 @@ class LoginActivity : AppCompatActivity() {
|
||||
ManagerSingleton.Toast(getString(R.string.loginFailed_msg), this)
|
||||
}
|
||||
}
|
||||
"-2" -> {
|
||||
// TODO: Start 2FA modal
|
||||
runOnUiThread {
|
||||
val builder = AlertDialog.Builder(this)
|
||||
val dialog = layoutInflater.inflate(R.layout.two_factor_dialog, null)
|
||||
|
||||
val inputTwoFactor = dialog.findViewById<EditText>(R.id.twoFactorText)
|
||||
|
||||
builder.setView(dialog)
|
||||
// Add action buttons
|
||||
.setPositiveButton(R.string.loginBtn) { d, _ ->
|
||||
this.optCode = inputTwoFactor.text.toString()
|
||||
this.tryLogin()
|
||||
d.dismiss()
|
||||
}
|
||||
.setNegativeButton("Cancel") { d, _ ->
|
||||
dialog.run { d.cancel() }
|
||||
loginBtn.isEnabled = true
|
||||
}
|
||||
val alertDialog = builder.create()
|
||||
alertDialog.show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,35 +12,46 @@ import java.io.InputStreamReader
|
||||
class Auth: Client() {
|
||||
private val stockParams = "grant_type=password"
|
||||
|
||||
fun login(username: String, password: String, client_id: String, client_secret: String): TokenModel{
|
||||
fun login(username: String, password: String, client_id: String, client_secret: String, twoFactorCode: String? = null): TokenModel{
|
||||
val con = this._newCon("users/token","POST")
|
||||
val params = "$stockParams&username=$username&password=$password&client_id=$client_id&client_secret=$client_secret"
|
||||
|
||||
if (twoFactorCode !== null) {
|
||||
con.setRequestProperty("x-peertube-otp", twoFactorCode)
|
||||
}
|
||||
|
||||
con.outputStream.write(params.toByteArray())
|
||||
val token = TokenModel()
|
||||
|
||||
try {
|
||||
|
||||
if(con.responseCode==200){
|
||||
val response = InputStreamReader(con.inputStream)
|
||||
val data = JsonReader(response)
|
||||
data.beginObject()
|
||||
when (con.responseCode) {
|
||||
200 -> {
|
||||
val response = InputStreamReader(con.inputStream)
|
||||
val data = JsonReader(response)
|
||||
data.beginObject()
|
||||
|
||||
while(data.hasNext()){
|
||||
val k = data.nextName()
|
||||
when(k.toString()){
|
||||
"access_token" -> token.token = data.nextString()
|
||||
"refresh_token" -> token.refresh_token = data.nextString()
|
||||
else -> data.skipValue()
|
||||
while(data.hasNext()){
|
||||
val k = data.nextName()
|
||||
when(k.toString()){
|
||||
"access_token" -> token.token = data.nextString()
|
||||
"refresh_token" -> token.refresh_token = data.nextString()
|
||||
else -> data.skipValue()
|
||||
}
|
||||
}
|
||||
|
||||
data.endObject()
|
||||
data.close()
|
||||
token.status = 1
|
||||
|
||||
}
|
||||
401 -> {
|
||||
// User require 2FA code
|
||||
token.status = -2
|
||||
}
|
||||
else -> {
|
||||
Log.d("Status", con.responseMessage)
|
||||
}
|
||||
|
||||
data.endObject()
|
||||
data.close()
|
||||
token.status = 1
|
||||
|
||||
}
|
||||
else{
|
||||
Log.d("Status", con.responseMessage)
|
||||
}
|
||||
}
|
||||
catch (err: Exception){
|
||||
|
@ -9,7 +9,7 @@
|
||||
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_height="0dp"
|
||||
android:layout_margin="4dp"
|
||||
android:layout_weight="3"
|
||||
android:hint="@string/reportDialog">
|
||||
|
30
app/src/main/res/layout/two_factor_dialog.xml
Normal file
30
app/src/main/res/layout/two_factor_dialog.xml
Normal file
@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
android:orientation="vertical"
|
||||
android:padding="10dp" >
|
||||
|
||||
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_margin="4dp"
|
||||
android:layout_weight="3"
|
||||
android:hint="@string/twoFactorLabel">
|
||||
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/twoFactorText"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:inputType="textMultiLine" >
|
||||
|
||||
<requestFocus />
|
||||
|
||||
</com.google.android.material.textfield.TextInputEditText>
|
||||
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
|
||||
|
||||
|
||||
</LinearLayout>
|
@ -34,6 +34,7 @@
|
||||
<string name="passwordText" translatable="false"> </string>
|
||||
<string name="loginBtn">Login now</string>
|
||||
<string name="registerActionBtn">Create new account</string>
|
||||
<string name="twoFactorLabel">Two factor code</string>
|
||||
<!-- Toast msg -->
|
||||
<string name="loginSuccess_msg">You are now logged in</string>
|
||||
<string name="loginError_msg">An error has occurred</string>
|
||||
|
Loading…
x
Reference in New Issue
Block a user