Skip to content

Commit

Permalink
Merge pull request #12128 from nextcloud/refactor/convert-Authenticat…
Browse files Browse the repository at this point in the history
…or-to-kt

Convert Authenticator to Kotlin
  • Loading branch information
alperozturk96 authored Mar 19, 2024
2 parents c35b63e + 36e02b1 commit 1a35df4
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ private void parseAndLoginFromWebView(String dataString) {
if (accountSetupBinding != null) {
accountSetupBinding.hostUrlInput.setText("");
}
mServerInfo.mBaseUrl = AuthenticatorUrlUtils.normalizeUrlSuffix(loginUrlInfo.serverAddress);
mServerInfo.mBaseUrl = AuthenticatorUrlUtils.INSTANCE.normalizeUrlSuffix(loginUrlInfo.serverAddress);
webViewUser = loginUrlInfo.username;
webViewPassword = loginUrlInfo.password;
} catch (Exception e) {
Expand Down Expand Up @@ -783,12 +783,12 @@ private void checkOcServer() {

if (uri.length() != 0) {
if (accountSetupBinding != null) {
uri = AuthenticatorUrlUtils.stripIndexPhpOrAppsFiles(uri);
uri = AuthenticatorUrlUtils.INSTANCE.stripIndexPhpOrAppsFiles(uri);
accountSetupBinding.hostUrlInput.setText(uri);
}

try {
uri = AuthenticatorUrlUtils.normalizeScheme(uri);
uri = AuthenticatorUrlUtils.INSTANCE.normalizeScheme(uri);
} catch (IllegalArgumentException ex) {
// Let the Nextcloud library check the error of the malformed URI
Log_OC.e(TAG, "Invalid URL", ex);
Expand All @@ -812,7 +812,7 @@ private void checkOcServer() {
Intent getServerInfoIntent = new Intent();
getServerInfoIntent.setAction(OperationsService.ACTION_GET_SERVER_INFO);
getServerInfoIntent.putExtra(OperationsService.EXTRA_SERVER_URL,
AuthenticatorUrlUtils.normalizeUrlSuffix(uri));
AuthenticatorUrlUtils.INSTANCE.normalizeUrlSuffix(uri));

if (mOperationsServiceBinder != null) {
mWaitingForOpId = mOperationsServiceBinder.queueNewOperation(getServerInfoIntent);
Expand Down Expand Up @@ -1291,7 +1291,7 @@ protected boolean createAccount(RemoteOperationResult<UserInfo> authResult) {
// create and save new ownCloud account
String lastPermanentLocation = authResult.getLastPermanentLocation();
if (lastPermanentLocation != null) {
mServerInfo.mBaseUrl = AuthenticatorUrlUtils.trimWebdavSuffix(lastPermanentLocation);
mServerInfo.mBaseUrl = AuthenticatorUrlUtils.INSTANCE.trimWebdavSuffix(lastPermanentLocation);
}

Uri uri = Uri.parse(mServerInfo.mBaseUrl);
Expand Down Expand Up @@ -1488,7 +1488,7 @@ public void onServiceConnected(ComponentName component, IBinder service) {
String prefix = getString(R.string.login_data_own_scheme) + PROTOCOL_SUFFIX + "login/";
LoginUrlInfo loginUrlInfo = parseLoginDataUrl(prefix, data.toString());

mServerInfo.mBaseUrl = AuthenticatorUrlUtils.normalizeUrlSuffix(loginUrlInfo.serverAddress);
mServerInfo.mBaseUrl = AuthenticatorUrlUtils.INSTANCE.normalizeUrlSuffix(loginUrlInfo.serverAddress);
webViewUser = loginUrlInfo.username;
webViewPassword = loginUrlInfo.password;
doOnResumeAndBound();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* ownCloud Android client application
*
* @author masensio on 09/02/2015.
* Copyright (C) 2015 ownCloud Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
@file:Suppress("DEPRECATION")

package com.owncloud.android.authentication

import android.app.Activity
import android.content.Context
import android.net.Uri
import android.os.AsyncTask
import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.lib.common.OwnCloudClientFactory
import com.owncloud.android.lib.common.OwnCloudCredentials
import com.owncloud.android.lib.common.UserInfo
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation
import com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation
import java.lang.ref.WeakReference

/**
* Async Task to verify the credentials of a user
*/

class AuthenticatorAsyncTask(activity: Activity) : AsyncTask<Any?, Void?, RemoteOperationResult<UserInfo?>?>() {
private val mWeakContext: WeakReference<Context?>
private val mListener: WeakReference<OnAuthenticatorTaskListener>

init {
mWeakContext = WeakReference(activity.applicationContext)
mListener = WeakReference(activity as OnAuthenticatorTaskListener)
}

@Deprecated("Deprecated in Java")
override fun doInBackground(vararg params: Any?): RemoteOperationResult<UserInfo?> {
val result: RemoteOperationResult<UserInfo?>

if (params.size == 2 && mWeakContext.get() != null) {
val url = params[0] as String
val credentials = params[1] as OwnCloudCredentials
val context = mWeakContext.get()

// Client
val uri = Uri.parse(url)
val nextcloudClient = OwnCloudClientFactory.createNextcloudClient(
uri,
credentials.username,
credentials.toOkHttpCredentials(),
context,
true
)

// Operation - get display name
val userInfoResult = GetUserInfoRemoteOperation().execute(nextcloudClient)

// Operation - try credentials
if (userInfoResult.isSuccess) {
val client = OwnCloudClientFactory.createOwnCloudClient(uri, context, true)
client.userId = userInfoResult.resultData?.id
client.credentials = credentials
val operation = ExistenceCheckRemoteOperation(OCFile.ROOT_PATH, SUCCESS_IF_ABSENT)

@Suppress("UNCHECKED_CAST")
result = operation.execute(client) as RemoteOperationResult<UserInfo?>
if (operation.wasRedirected()) {
val redirectionPath = operation.redirectionPath
val permanentLocation = redirectionPath.lastPermanentLocation
result.lastPermanentLocation = permanentLocation
}
result.setResultData(userInfoResult.resultData)
} else {
result = userInfoResult
}
} else {
result = RemoteOperationResult<UserInfo?>(RemoteOperationResult.ResultCode.UNKNOWN_ERROR)
}

return result
}

@Deprecated("Deprecated in Java")
override fun onPostExecute(result: RemoteOperationResult<UserInfo?>?) {
result?.let {
val listener = mListener.get()
listener?.onAuthenticatorTaskCallback(it)
}
}

/*
* Interface to retrieve data from recognition task
*/
interface OnAuthenticatorTaskListener {
fun onAuthenticatorTaskCallback(result: RemoteOperationResult<UserInfo?>?)
}

companion object {
private const val SUCCESS_IF_ABSENT = false
}
}
Loading

0 comments on commit 1a35df4

Please sign in to comment.