Skip to content

Commit

Permalink
Support UIImplementation authgear#180
Browse files Browse the repository at this point in the history
  • Loading branch information
tung2744 authored Feb 22, 2024
2 parents c555fe7 + a40acbf commit 82ddd85
Show file tree
Hide file tree
Showing 15 changed files with 540 additions and 160 deletions.
28 changes: 4 additions & 24 deletions javasample/src/main/java/com/oursky/authgeartest/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.oursky.authgear.PersistentTokenStorage;
import com.oursky.authgear.SessionState;
import com.oursky.authgear.TransientTokenStorage;
import com.oursky.authgear.UIVariant;
import com.oursky.authgear.UserInfo;

@SuppressWarnings("ConstantConditions")
Expand All @@ -40,7 +39,7 @@ public class MainActivity extends AppCompatActivity {
private Spinner mPage;
private Spinner mTokenStorage;
private Spinner mColorScheme;
private Spinner mUIVariant;
private CheckBox mUseWebKitWebView;
private CheckBox mIsSsoEnabled;
private TextView mSessionState;
private TextView mLoading;
Expand Down Expand Up @@ -90,6 +89,7 @@ protected void onCreate(Bundle savedInstanceState) {
mFetchUserInfo = findViewById(R.id.fetchUserInfo);
mShowAuthTime = findViewById(R.id.showAuthTime);
mLogout = findViewById(R.id.logout);
mUseWebKitWebView = findViewById(R.id.useWebKitWebView);
mIsSsoEnabled = findViewById(R.id.isSsoEnabled);
mSessionState = findViewById(R.id.sessionStateInput);

Expand Down Expand Up @@ -123,19 +123,12 @@ protected void onCreate(Bundle savedInstanceState) {
colorSchemeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mColorScheme.setAdapter(colorSchemeAdapter);

String[] uiVariants = {
UIVariant.CUSTOM_TABS.name(),
};
mUIVariant = findViewById(R.id.uiVariantSpinner);
ArrayAdapter<String> mUIVariantAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, uiVariants);
mUIVariantAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mUIVariant.setAdapter(mUIVariantAdapter);

mConfigure.setOnClickListener(
view -> viewModel.configure(
mClientId.getText().toString(),
mEndpoint.getText().toString(),
mIsSsoEnabled.isChecked(),
mUseWebKitWebView.isChecked(),
mApp2AppEndpoint.getText().toString()
)
);
Expand Down Expand Up @@ -199,25 +192,12 @@ public void onNothingSelected(AdapterView<?> parent) {

}
});
mUIVariant.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String value = (String) parent.getItemAtPosition(position);
if (UIVariant.CUSTOM_TABS.name().equals(value)) {
viewModel.setUIVariant(UIVariant.CUSTOM_TABS);
}
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});

mClientId.setText(viewModel.clientID().getValue());
mEndpoint.setText(viewModel.endpoint().getValue());
mApp2AppEndpoint.setText(viewModel.app2appEndpoint().getValue());
mIsSsoEnabled.setChecked(viewModel.isSsoEnabled().getValue());
mUseWebKitWebView.setChecked(viewModel.useWebKitWebView().getValue());
mSessionState.setText(viewModel.sessionState().getValue().toString());

viewModel.isConfigured().observe(this, isConfigured -> {
Expand Down
62 changes: 35 additions & 27 deletions javasample/src/main/java/com/oursky/authgeartest/MainViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.oursky.authgear.BiometricOptions;
import com.oursky.authgear.CancelException;
import com.oursky.authgear.ColorScheme;
import com.oursky.authgear.CustomTabsUIImplementation;
import com.oursky.authgear.OnAuthenticateAnonymouslyListener;
import com.oursky.authgear.OnAuthenticateBiometricListener;
import com.oursky.authgear.OnAuthenticateListener;
Expand All @@ -45,9 +46,11 @@
import com.oursky.authgear.SessionState;
import com.oursky.authgear.SessionStateChangeReason;
import com.oursky.authgear.SettingOptions;
import com.oursky.authgear.TokenStorage;
import com.oursky.authgear.TransientTokenStorage;
import com.oursky.authgear.UIVariant;
import com.oursky.authgear.UIImplementation;
import com.oursky.authgear.UserInfo;
import com.oursky.authgear.WebKitWebViewUIImplementation;
import com.oursky.authgear.app2app.App2AppAuthenticateOptions;
import com.oursky.authgear.app2app.App2AppAuthenticateRequest;
import com.oursky.authgear.app2app.App2AppOptions;
Expand All @@ -71,7 +74,8 @@ public class MainViewModel extends AndroidViewModel {
final private MutableLiveData<String> mPage = new MutableLiveData<>("");
final private MutableLiveData<String> mTokenStorage = new MutableLiveData<>("");
final private MutableLiveData<ColorScheme> mColorScheme = new MutableLiveData<>(null);
final private MutableLiveData<UIVariant> mUIVariant = new MutableLiveData<>(UIVariant.CUSTOM_TABS);

final private MutableLiveData<Boolean> mUseWebKitWebView = new MutableLiveData<>(false);
final private MutableLiveData<Boolean> mIsSsoEnabled = new MutableLiveData<>(false);
final private MutableLiveData<Boolean> mIsLoading = new MutableLiveData<>(false);
final private MutableLiveData<Boolean> mBiometricEnable = new MutableLiveData<>(false);
Expand All @@ -94,12 +98,14 @@ public MainViewModel(Application application) {
String storedPage = preferences.getString("page", "");
String storedTokenStorage = preferences.getString("tokenStorage", PersistentTokenStorage.class.getSimpleName());
Boolean storedIsSsoEnabled = preferences.getBoolean("isSsoEnabled", false);
Boolean storedUseWebKitWebView = preferences.getBoolean("useWebKitWebView", false);
mClientID.setValue(storedClientID);
mEndpoint.setValue(storedEndpoint);
mApp2AppEndpoint.setValue(storedApp2AppEndpoint);
mPage.setValue(storedPage);
mTokenStorage.setValue(storedTokenStorage);
mIsSsoEnabled.setValue(storedIsSsoEnabled);
mUseWebKitWebView.setValue(storedUseWebKitWebView);
}
}

Expand Down Expand Up @@ -152,10 +158,6 @@ public void setColorScheme(ColorScheme colorScheme) {
mColorScheme.setValue(colorScheme);
}

public void setUIVariant(UIVariant uiVariant) {
mUIVariant.setValue(uiVariant);
}

public LiveData<String> clientID() {
return mClientID;
}
Expand All @@ -169,6 +171,8 @@ public LiveData<String> app2appEndpoint() {

public LiveData<String> tokenStorage() { return mTokenStorage; }

public LiveData<Boolean> useWebKitWebView() { return mUseWebKitWebView; }

public LiveData<Boolean> isSsoEnabled() { return mIsSsoEnabled; }

public LiveData<Boolean> isConfigured() {
Expand All @@ -194,46 +198,50 @@ public LiveData<Throwable> error() {
}
public LiveData<ConfirmationViewModel> app2appConfirmation() { return mApp2AppConfirmation; }

public void configure(String clientID, String endpoint, Boolean isSsoEnabled, String app2appEndpoint) {
public void configure(String clientID, String endpoint, Boolean isSsoEnabled, Boolean useWebKitWebView, String app2appEndpoint) {
if (mIsLoading.getValue()) return;
mIsLoading.setValue(true);
MainApplication app = getApplication();
mClientID.setValue(clientID);
mEndpoint.setValue(endpoint);
mApp2AppEndpoint.setValue(app2appEndpoint);
mIsSsoEnabled.setValue(isSsoEnabled);
mUseWebKitWebView.setValue(useWebKitWebView);
app.getSharedPreferences("authgear.demo", Context.MODE_PRIVATE)
.edit()
.putString("clientID", clientID)
.putString("endpoint", endpoint)
.putString("app2appendpoint", app2appEndpoint)
.putBoolean("isSsoEnabled", isSsoEnabled)
.putBoolean("useWebKitWebView", useWebKitWebView)
.apply();
Boolean isApp2AppEnabled = !app2appEndpoint.isEmpty();
App2AppOptions app2appOptions = new App2AppOptions(isApp2AppEnabled);

TokenStorage tokenStorage;
if (mTokenStorage.getValue().equals(TransientTokenStorage.class.getSimpleName())) {
mAuthgear = new Authgear(
getApplication(),
clientID,
endpoint,
new TransientTokenStorage(),
isSsoEnabled,
mUIVariant.getValue(),
null,
app2appOptions
);
tokenStorage = new TransientTokenStorage();
} else {
mAuthgear = new Authgear(
getApplication(),
clientID,
endpoint,
new PersistentTokenStorage(getApplication()),
isSsoEnabled,
mUIVariant.getValue(),
null,
app2appOptions
);
tokenStorage = new PersistentTokenStorage(getApplication());
}

UIImplementation uiImplementation;
if (mUseWebKitWebView.getValue()) {
uiImplementation = new WebKitWebViewUIImplementation();
} else {
uiImplementation = new CustomTabsUIImplementation();
}

mAuthgear = new Authgear(
getApplication(),
clientID,
endpoint,
tokenStorage,
uiImplementation,
isSsoEnabled,
null,
app2appOptions
);
mAuthgear.configure(new OnConfigureListener() {
@Override
public void onConfigured() {
Expand Down
30 changes: 4 additions & 26 deletions javasample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -257,34 +257,12 @@

</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/uiVariant"
<CheckBox
android:id="@+id/useWebKitWebView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<TextView
android:id="@+id/uiVariantLabel"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:text="UI Variant"
tools:ignore="HardcodedText" />

<Spinner android:id="@+id/uiVariantSpinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintStart_toEndOf="@+id/uiVariantLabel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
android:text="Use WebKit WebView"
tools:ignore="HardcodedText" />

<CheckBox
android:id="@+id/isSsoEnabled"
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<activity android:name=".OAuthActivity"
android:exported="false"
android:launchMode="singleTask" />
<activity android:name=".WebKitWebViewActivity"
android:exported="false"
android:launchMode="singleTask" />
<activity android:name=".WebViewActivity"
android:exported="false"
android:theme="@style/AuthgearTheme"
android:configChanges="orientation|screenSize" />
<activity android:name=".CustomTabActivity"
android:exported="false"
android:launchMode="singleTask" />
</application>
</manifest>
4 changes: 2 additions & 2 deletions sdk/src/main/java/com/oursky/authgear/Authgear.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ constructor(
clientId: String,
authgearEndpoint: String,
tokenStorage: TokenStorage = PersistentTokenStorage(application),
uiImplementation: UIImplementation = CustomTabsUIImplementation(),
isSsoEnabled: Boolean = false,
uiVariant: UIVariant = UIVariant.CUSTOM_TABS,
name: String? = null,
app2AppOptions: App2AppOptions = App2AppOptions(isEnabled = false)
) {
Expand All @@ -44,8 +44,8 @@ constructor(
authgearEndpoint,
isSsoEnabled,
app2AppOptions,
uiVariant,
tokenStorage,
uiImplementation,
PersistentContainerStorage(application),
OAuthRepoHttp(),
KeyRepoKeystore(),
Expand Down
39 changes: 11 additions & 28 deletions sdk/src/main/java/com/oursky/authgear/AuthgearCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ internal class AuthgearCore(
private val authgearEndpoint: String,
private val isSsoEnabled: Boolean,
private val app2AppOptions: App2AppOptions,
private val uiVariant: UIVariant,
private val tokenStorage: TokenStorage,
private val uiImplementation: UIImplementation,
private val storage: ContainerStorage,
private val oauthRepo: OAuthRepo,
private val keyRepo: KeyRepo,
Expand Down Expand Up @@ -716,35 +716,18 @@ internal class AuthgearCore(
redirectUrl: String,
authorizeUri: Uri
): String {
return suspendCoroutine { k ->
val action = newRandomAction()
val intentFilter = IntentFilter(action)
val br = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val type = intent?.getStringExtra(KEY_OAUTH_BOARDCAST_TYPE) ?: return
when (type) {
OAuthBroadcastType.REDIRECT_URL.name -> {
application.unregisterReceiver(this)
val output = intent.getStringExtra(KEY_REDIRECT_URL)
if (output != null) {
k.resume(output)
} else {
k.resumeWithException(CancelException())
}
}
}
return suspendCoroutine {
val options = OpenAuthorizationURLOptions(authorizeUri, Uri.parse(redirectUrl))
val listener = object : OnOpenAuthorizationURLListener {
override fun onSuccess(url: Uri) {
it.resume(url.toString())
}

override fun onFailed(throwable: Throwable) {
it.resumeWithException(throwable)
}
}
application.registerReceiver(br, intentFilter)
val redirectUri = Uri.parse(redirectUrl)
application.startActivity(
OAuthActivity.createAuthorizationIntent(
application,
action,
redirectUrl,
authorizeUri.toString()
)
)
this.uiImplementation.openAuthorizationURL(this.application, options, listener)
}
}

Expand Down
45 changes: 0 additions & 45 deletions sdk/src/main/java/com/oursky/authgear/CustomTabActivity.kt

This file was deleted.

Loading

0 comments on commit 82ddd85

Please sign in to comment.