1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-10 09:26:19 +00:00

Merge pull request #315 from owncloud/oidc_new_arch

Open Id Connect along with new arch in login
This commit is contained in:
Abel García de Prada 2020-04-29 18:31:30 +02:00 committed by GitHub
commit 8f62925312
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 98 deletions

View File

@ -292,6 +292,8 @@ public class AccountUtils {
/**
* Flag signaling if the ownCloud server can be accessed with OAuth2 access tokens.
*/
// TODO Please review this constants, move them out of the library, the rest of OAuth variables are in data layer
public static final String KEY_SUPPORTS_OAUTH2 = "oc_supports_oauth2";
public static final String OAUTH_SUPPORTED_TRUE = "TRUE";
@ -316,21 +318,6 @@ public class AccountUtils {
*/
public static final String KEY_DISPLAY_NAME = "oc_display_name";
/**
* OAuth2 user id
**/
public static final String KEY_USER_ID = "user_id";
/**
* OAuth2 refresh token
**/
public static final String KEY_OAUTH2_REFRESH_TOKEN = "oc_oauth2_refresh_token";
/**
* OAuth2 scope
*/
public static final String KEY_OAUTH2_SCOPE = "oc_oauth2_scope";
public static final int ACCOUNT_VERSION = 1;
}
}

View File

@ -1,73 +0,0 @@
package com.owncloud.android.lib.common.authentication.oauth
import android.content.Context
import android.net.Uri
import com.owncloud.android.lib.common.network.AdvancedX509TrustManager
import com.owncloud.android.lib.common.network.NetworkUtils
import net.openid.appauth.connectivity.ConnectionBuilder
import timber.log.Timber
import java.io.IOException
import java.net.HttpURLConnection
import java.net.URL
import java.security.NoSuchAlgorithmException
import java.util.Objects
import java.util.concurrent.TimeUnit
import javax.net.ssl.HostnameVerifier
import javax.net.ssl.HttpsURLConnection
import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager
/**
* Based on [net.openid.appauth.connectivity.DefaultConnectionBuilder] but permitting http connections in addition
* to https connections
*/
class OAuthConnectionBuilder(val context: Context) : ConnectionBuilder {
/**
* The singleton instance of the default connection builder.
*/
private val CONNECTION_TIMEOUT_MS = TimeUnit.SECONDS.toMillis(15).toInt()
private val READ_TIMEOUT_MS = TimeUnit.SECONDS.toMillis(10).toInt()
private val HTTPS_SCHEME = "https"
@Throws(IOException::class)
override fun openConnection(uri: Uri): HttpURLConnection {
val conn: HttpURLConnection
if (Objects.equals(uri.scheme, HTTPS_SCHEME)) {
conn = URL(uri.toString()).openConnection() as HttpsURLConnection
try {
val trustManager: X509TrustManager = AdvancedX509TrustManager(
NetworkUtils.getKnownServersStore(context)
)
val sslContext: SSLContext
sslContext = try {
SSLContext.getInstance("TLSv1.2")
} catch (tlsv12Exception: NoSuchAlgorithmException) {
try {
Timber.w("TLSv1.2 is not supported in this device; falling through TLSv1.1")
SSLContext.getInstance("TLSv1.1")
} catch (tlsv11Exception: NoSuchAlgorithmException) {
Timber.w("TLSv1.1 is not supported in this device; falling through TLSv1.0")
SSLContext.getInstance("TLSv1")
// should be available in any device; see reference of supported protocols in
// http://developer.android.com/reference/javax/net/ssl/SSLSocket.html
}
}
sslContext.init(null, arrayOf<TrustManager>(trustManager), null)
conn.hostnameVerifier = HostnameVerifier { _, _ -> true } // Do not verify the host for now
conn.sslSocketFactory = sslContext.socketFactory
} catch (e: Exception) {
Timber.e(e, "Could not setup SSL system")
}
} else {
conn = URL(uri.toString()).openConnection() as HttpURLConnection
}
return conn.apply {
connectTimeout = CONNECTION_TIMEOUT_MS
readTimeout = READ_TIMEOUT_MS
instanceFollowRedirects = false
}
}
}

View File

@ -78,14 +78,18 @@ public abstract class DavMethod extends HttpBaseMethod {
.build();
} else if (mResponse != null) {
ResponseBody responseBody = ResponseBody.create(
mResponse.body().contentType(),
httpException.getResponseBody()
);
// The check below should be included in okhttp library, method ResponseBody.create(
// TODO check most recent versions of okhttp to see if this is already fixed and try to update if so
if (mResponse.body().contentType() != null) {
ResponseBody responseBody = ResponseBody.create(
mResponse.body().contentType(),
httpException.getResponseBody()
);
mResponse = mResponse.newBuilder()
.body(responseBody)
.build();
mResponse = mResponse.newBuilder()
.body(responseBody)
.build();
}
}
return httpException.getCode();

View File

@ -66,7 +66,7 @@ public class RemoteOperationResult<T>
private Exception mException = null;
private ResultCode mCode = ResultCode.UNKNOWN_ERROR;
private String mRedirectedLocation;
private String mAuthenticate;
private List<String> mAuthenticate = new ArrayList<>();
private String mLastPermanentLocation = null;
private T mData = null;
@ -253,7 +253,9 @@ public class RemoteOperationResult<T>
continue;
}
if ("www-authenticate".equals(header.getKey().toLowerCase())) {
mAuthenticate = header.getValue().get(0).toLowerCase();
for (String value: header.getValue()) {
mAuthenticate.add(value.toLowerCase());
}
}
}
}
@ -494,7 +496,7 @@ public class RemoteOperationResult<T>
return (mRedirectedLocation != null && !(mRedirectedLocation.toLowerCase().startsWith("https://")));
}
public String getAuthenticateHeaders() {
public List<String> getAuthenticateHeaders() {
return mAuthenticate;
}