diff options
author | Thomas <tschneider.ac@gmail.com> | 2023-01-30 17:02:24 +0100 |
---|---|---|
committer | Thomas <tschneider.ac@gmail.com> | 2023-01-30 17:02:24 +0100 |
commit | 408e51c0a6ca09a9b61632d7798e588e78a2bb31 (patch) | |
tree | a15df96a932124f0bbb1116512cbfe50d34e2df6 /app | |
parent | 9fd834eb44612e8747fff5814a95a129e8c1bb01 (diff) |
Peertube 2FA support
Diffstat (limited to 'app')
6 files changed, 58 insertions, 23 deletions
diff --git a/app/src/main/java/app/fedilab/android/peertube/activities/LoginActivity.java b/app/src/main/java/app/fedilab/android/peertube/activities/LoginActivity.java index fe3f09d5e..4d827bc8a 100644 --- a/app/src/main/java/app/fedilab/android/peertube/activities/LoginActivity.java +++ b/app/src/main/java/app/fedilab/android/peertube/activities/LoginActivity.java @@ -17,18 +17,12 @@ package app.fedilab.android.peertube.activities; import static app.fedilab.android.peertube.client.RetrofitPeertubeAPI.updateCredential; import android.annotation.SuppressLint; -import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; -import android.text.SpannableString; -import android.text.Spanned; -import android.text.style.ForegroundColorSpan; -import android.text.style.UnderlineSpan; import android.util.Patterns; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.widget.TextView; import android.widget.Toast; import androidx.preference.PreferenceManager; @@ -160,6 +154,7 @@ public class LoginActivity extends BaseBarActivity { oauthParams.setClient_secret(client_secret); oauthParams.setGrant_type("password"); oauthParams.setScope("user"); + oauthParams.x_peertube_otp = binding.loginOtp.getText().toString().isEmpty() ? null : binding.loginOtp.getText().toString(); if (binding.loginUid.getText() != null) { oauthParams.setUsername(binding.loginUid.getText().toString().trim()); } @@ -169,22 +164,21 @@ public class LoginActivity extends BaseBarActivity { try { Token token = new RetrofitPeertubeAPI(LoginActivity.this, finalInstance, null).manageToken(oauthParams); proceedLogin(token, finalInstance); - } catch (final Exception e) { - oauthParams.setUsername(binding.loginUid.getText().toString().toLowerCase().trim()); - Token token = null; - try { - token = new RetrofitPeertubeAPI(LoginActivity.this, finalInstance, null).manageToken(oauthParams); - } catch (Error ex) { - ex.printStackTrace(); - } - proceedLogin(token, finalInstance); } catch (Error e) { - runOnUiThread(() -> { - Toasty.error(LoginActivity.this, e.getError() != null && !e.getError().isEmpty() ? e.getError() : getString(R.string.toast_error), Toasty.LENGTH_SHORT).show(); - binding.loginButton.setEnabled(true); - }); - - e.printStackTrace(); + if (e.getError() != null && e.getError().contains("missing_two_factor")) { + runOnUiThread(() -> { + binding.loginOtpContainer.setVisibility(View.VISIBLE); + binding.loginOtp.setFocusable(true); + binding.loginOtp.requestFocus(); + binding.loginButton.setEnabled(true); + }); + } else { + runOnUiThread(() -> { + Toasty.error(LoginActivity.this, e.getError() != null && !e.getError().isEmpty() ? e.getError() : getString(R.string.toast_error), Toasty.LENGTH_SHORT).show(); + binding.loginButton.setEnabled(true); + }); + e.printStackTrace(); + } } } diff --git a/app/src/main/java/app/fedilab/android/peertube/client/PeertubeService.java b/app/src/main/java/app/fedilab/android/peertube/client/PeertubeService.java index 32c5b2c56..50e81dc68 100644 --- a/app/src/main/java/app/fedilab/android/peertube/client/PeertubeService.java +++ b/app/src/main/java/app/fedilab/android/peertube/client/PeertubeService.java @@ -122,6 +122,18 @@ public interface PeertubeService { @Field("password") String password, @Field("externalAuthToken") String externalAuthToken); + @FormUrlEncoded + @POST("users/token") + Call<Token> otpConnetion( + @Header("x-peertube-otp") String externalAuthToken, + @Field("client_id") String client_id, + @Field("client_secret") String client_secret, + @Field("response_type") String response_type, + @Field("grant_type") String grant_type, + @Field("scope") String scope, + @Field("username") String username, + @Field("password") String password); + //TOKEN //Refresh @FormUrlEncoded diff --git a/app/src/main/java/app/fedilab/android/peertube/client/RetrofitPeertubeAPI.java b/app/src/main/java/app/fedilab/android/peertube/client/RetrofitPeertubeAPI.java index 6476bdace..4455d7c1e 100644 --- a/app/src/main/java/app/fedilab/android/peertube/client/RetrofitPeertubeAPI.java +++ b/app/src/main/java/app/fedilab/android/peertube/client/RetrofitPeertubeAPI.java @@ -267,7 +267,9 @@ public class RetrofitPeertubeAPI { public Token manageToken(OauthParams oauthParams) throws Error { PeertubeService peertubeService = init(); Call<Token> refreshTokenCall = null; - if (oauthParams.getGrant_type().compareTo("password") == 0) { + if (oauthParams.x_peertube_otp != null) { + refreshTokenCall = peertubeService.otpConnetion(oauthParams.x_peertube_otp, oauthParams.getClient_id(), oauthParams.getClient_secret(), "code", oauthParams.getGrant_type(), "upload", oauthParams.getUsername(), oauthParams.getPassword()); + } else if (oauthParams.getGrant_type().compareTo("password") == 0) { refreshTokenCall = peertubeService.createToken(oauthParams.getClient_id(), oauthParams.getClient_secret(), oauthParams.getGrant_type(), oauthParams.getUsername(), oauthParams.getPassword()); } else if (oauthParams.getGrant_type().compareTo("refresh_token") == 0) { refreshTokenCall = peertubeService.refreshToken(oauthParams.getClient_id(), oauthParams.getClient_secret(), oauthParams.getRefresh_token(), oauthParams.getGrant_type()); diff --git a/app/src/main/java/app/fedilab/android/peertube/client/entities/OauthParams.java b/app/src/main/java/app/fedilab/android/peertube/client/entities/OauthParams.java index 41154ecbb..f4a572831 100644 --- a/app/src/main/java/app/fedilab/android/peertube/client/entities/OauthParams.java +++ b/app/src/main/java/app/fedilab/android/peertube/client/entities/OauthParams.java @@ -44,6 +44,9 @@ public class OauthParams { private String code; @SerializedName("redirect_uri") private String redirect_uri; + @SerializedName("x_peertube_otp") + public String x_peertube_otp; + public String getClient_secret() { return client_secret; diff --git a/app/src/main/res/layouts/mastodon/values/strings.xml b/app/src/main/res/layouts/mastodon/values/strings.xml new file mode 100644 index 000000000..b5290879c --- /dev/null +++ b/app/src/main/res/layouts/mastodon/values/strings.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string name="otp_message">Two factor authentication token</string> +</resources>
\ No newline at end of file diff --git a/app/src/main/res/layouts/peertube/layout/activity_login_peertube.xml b/app/src/main/res/layouts/peertube/layout/activity_login_peertube.xml index cb6e59124..1ad8a3405 100644 --- a/app/src/main/res/layouts/peertube/layout/activity_login_peertube.xml +++ b/app/src/main/res/layouts/peertube/layout/activity_login_peertube.xml @@ -18,6 +18,7 @@ xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" + xmlns:tools="http://schemas.android.com/tools" android:gravity="center" android:orientation="vertical" android:padding="24dp"> @@ -73,6 +74,25 @@ android:singleLine="true" /> </com.google.android.material.textfield.TextInputLayout> + <com.google.android.material.textfield.TextInputLayout + android:id="@+id/login_otp_container" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_marginTop="12dp" + android:visibility="gone" + app:passwordToggleEnabled="true" + tools:visibility="visible"> + + <com.google.android.material.textfield.TextInputEditText + android:id="@+id/login_otp" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:hint="@string/otp_message" + android:importantForAutofill="no" + android:inputType="numberPassword" + android:singleLine="true" /> + </com.google.android.material.textfield.TextInputLayout> + <com.google.android.material.button.MaterialButton android:id="@+id/login_button" android:layout_width="wrap_content" @@ -81,6 +101,6 @@ android:text="@string/login" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toBottomOf="@id/login_passwd_container" /> + app:layout_constraintTop_toBottomOf="@id/login_otp_container" /> </androidx.appcompat.widget.LinearLayoutCompat>
\ No newline at end of file |