Skip to content
Snippets Groups Projects
Commit c085b554 authored by Jawnnypoo's avatar Jawnnypoo
Browse files

Make lastFailedHostname not static

parent c2c41c40
No related branches found
No related tags found
No related merge requests found
Pipeline #
Loading
Loading
@@ -5,7 +5,10 @@ import android.content.Context
import android.content.res.Resources
import android.support.annotation.VisibleForTesting
import android.support.multidex.MultiDex
import com.commit451.gitlab.api.*
import com.commit451.gitlab.api.GitLab
import com.commit451.gitlab.api.GitLabFactory
import com.commit451.gitlab.api.OkHttpClientFactory
import com.commit451.gitlab.api.PicassoFactory
import com.commit451.gitlab.data.Prefs
import com.commit451.gitlab.model.Account
import com.commit451.gitlab.util.FabricUtil
Loading
Loading
@@ -139,9 +142,7 @@ open class App : Application() {
}
 
private fun initGitLab(account: Account, client: OkHttpClient) {
val gitLabService = GitLabFactory.create(account, client)
val gitLabRss = GitLabRssFactory.create(account, client)
gitLab = GitLab(gitLabService, gitLabRss)
gitLab = GitLabFactory.createGitLab(account, client)
}
 
private fun initPicasso(client: OkHttpClient) {
Loading
Loading
Loading
Loading
@@ -23,6 +23,7 @@ import com.bluelinelabs.logansquare.LoganSquare
import com.commit451.gitlab.App
import com.commit451.gitlab.BuildConfig
import com.commit451.gitlab.R
import com.commit451.gitlab.api.GitLab
import com.commit451.gitlab.api.GitLabFactory
import com.commit451.gitlab.api.OkHttpClientFactory
import com.commit451.gitlab.api.request.SessionRequest
Loading
Loading
@@ -52,7 +53,6 @@ import retrofit2.Response
import timber.log.Timber
import java.io.IOException
import java.net.ConnectException
import java.security.cert.CertificateEncodingException
import java.util.*
import java.util.regex.Pattern
import javax.net.ssl.SSLHandshakeException
Loading
Loading
@@ -66,9 +66,8 @@ class LoginActivity : BaseActivity() {
private val EXTRA_SHOW_CLOSE = "show_close"
 
private val REQUEST_PRIVATE_TOKEN = 123
private val sTokenPattern = Pattern.compile("^[A-Za-z0-9-_]*$")
 
@JvmOverloads fun newIntent(context: Context, showClose: Boolean = false): Intent {
fun newIntent(context: Context, showClose: Boolean = false): Intent {
val intent = Intent(context, LoginActivity::class.java)
intent.putExtra(EXTRA_SHOW_CLOSE, showClose)
return intent
Loading
Loading
@@ -92,10 +91,14 @@ class LoginActivity : BaseActivity() {
lateinit var teleprinter: Teleprinter
 
var isNormalLogin = true
val emailPattern : Pattern by lazy {
val emailPattern: Pattern by lazy {
Patterns.EMAIL_ADDRESS
}
val tokenPattern: Pattern by lazy {
Pattern.compile("^[A-Za-z0-9-_]*$")
}
var account: Account = Account()
var gitLab: GitLab? = null
 
@OnEditorAction(R.id.password_input, R.id.token_input)
fun onPasswordEditorAction(): Boolean {
Loading
Loading
@@ -125,7 +128,7 @@ class LoginActivity : BaseActivity() {
if (!textInputLayoutToken.checkValid()) {
return
}
if (!sTokenPattern.matcher(textToken.text).matches()) {
if (!tokenPattern.matcher(textToken.text).matches()) {
textInputLayoutToken.error = getString(R.string.not_a_valid_private_token)
return
} else {
Loading
Loading
@@ -233,12 +236,14 @@ class LoginActivity : BaseActivity() {
}
 
fun attemptLogin(request: SessionRequest) {
val gitlabClientBuilder = OkHttpClientFactory.create(account)
val clientBuilder = OkHttpClientFactory.create(account)
if (BuildConfig.DEBUG) {
gitlabClientBuilder.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
clientBuilder.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
}
val gitLab = GitLabFactory.create(account, gitlabClientBuilder.build())
gitLab.login(request)
gitLab = GitLabFactory.createGitLab(account, clientBuilder.build())
gitLab!!.login(request)
.setup(bindToLifecycle())
.subscribe(object : CustomResponseSingleObserver<UserLogin>() {
 
Loading
Loading
@@ -370,12 +375,7 @@ class LoginActivity : BaseActivity() {
 
if (t is SSLHandshakeException && t.cause is X509CertificateException) {
account.trustedCertificate = null
var fingerprint: String? = null
try {
fingerprint = X509Util.getFingerPrint((t.cause as X509CertificateException).chain[0])
} catch (e: CertificateEncodingException) {
Timber.e(e)
}
val fingerprint = X509Util.getFingerPrint((t.cause as X509CertificateException).chain[0])
 
val finalFingerprint = fingerprint
 
Loading
Loading
@@ -383,11 +383,8 @@ class LoginActivity : BaseActivity() {
.setTitle(R.string.certificate_title)
.setMessage(String.format(resources.getString(R.string.certificate_message), finalFingerprint))
.setPositiveButton(R.string.ok_button) { dialog, _ ->
if (finalFingerprint != null) {
account.trustedCertificate = finalFingerprint
login()
}
account.trustedCertificate = finalFingerprint
login()
dialog.dismiss()
}
.setNegativeButton(R.string.cancel_button) { dialog, _ -> dialog.dismiss() }
Loading
Loading
@@ -396,7 +393,8 @@ class LoginActivity : BaseActivity() {
(d.findViewById(android.R.id.message) as TextView).movementMethod = LinkMovementMethod.getInstance()
} else if (t is SSLPeerUnverifiedException && t.message?.toLowerCase()!!.contains("hostname")) {
account.trustedHostname = null
val finalHostname = CustomHostnameVerifier.lastFailedHostname
val hostNameVerifier = gitLab?.client?.hostnameVerifier() as CustomHostnameVerifier
val finalHostname = hostNameVerifier.lastFailedHostname
val d = AlertDialog.Builder(this)
.setTitle(R.string.hostname_title)
.setMessage(R.string.hostname_message)
Loading
Loading
@@ -484,15 +482,10 @@ class LoginActivity : BaseActivity() {
 
fun isAlreadySignedIn(url: String, usernameOrEmailOrPrivateToken: String): Boolean {
val accounts = Prefs.getAccounts()
for (account in accounts) {
if (account.serverUrl == Uri.parse(url)) {
if (usernameOrEmailOrPrivateToken == account.user.username
|| usernameOrEmailOrPrivateToken.equals(account.user.email, ignoreCase = true)
|| usernameOrEmailOrPrivateToken.equals(account.privateToken, ignoreCase = true)) {
return true
}
}
return accounts.any {
it.serverUrl == Uri.parse(url) && (usernameOrEmailOrPrivateToken == it.user.username
|| usernameOrEmailOrPrivateToken.equals(it.user.email, ignoreCase = true)
|| usernameOrEmailOrPrivateToken.equals(it.privateToken, ignoreCase = true))
}
return false
}
}
Loading
Loading
@@ -3,12 +3,13 @@ package com.commit451.gitlab.api
import android.net.Uri
import com.bluelinelabs.logansquare.LoganSquare
import com.commit451.gitlab.api.converter.UriTypeConverter
import okhttp3.OkHttpClient
 
/**
* Provides access to all the GitLab things. Wraps RSS and the Retrofit service, in
* case we need to do overrides or global
*/
class GitLab(gitLabService: GitLabService, gitLabRss: GitLabRss): GitLabService by gitLabService,
class GitLab(val client: OkHttpClient, gitLabService: GitLabService, gitLabRss: GitLabRss): GitLabService by gitLabService,
GitLabRss by gitLabRss {
 
companion object {
Loading
Loading
Loading
Loading
@@ -13,6 +13,12 @@ import retrofit2.converter.scalars.ScalarsConverterFactory
*/
object GitLabFactory {
 
fun createGitLab(account: Account, client: OkHttpClient): GitLab {
val gitLabService = GitLabFactory.create(account, client)
val gitLabRss = GitLabRssFactory.create(account, client)
return GitLab(client, gitLabService, gitLabRss)
}
/**
* Create a GitLabService get with the current account passed.
* @param account the account to try and log in with
Loading
Loading
Loading
Loading
@@ -19,7 +19,7 @@ object OkHttpClientFactory {
* *
* @return a configured [okhttp3.OkHttpClient.Builder]
*/
@JvmOverloads fun create(account: Account, includeSignInAuthenticator: Boolean = true): OkHttpClient.Builder {
fun create(account: Account, includeSignInAuthenticator: Boolean = true): OkHttpClient.Builder {
// Do we even need a custom trust manager?
// Yep. Otherwise SSL won't work properly with some configurations :) -Michi
val customTrustManager = CustomTrustManager()
Loading
Loading
Loading
Loading
@@ -11,16 +11,11 @@ import com.commit451.gitlab.R
 
class HttpLoginDialog(context: Context, realm: String, loginListener: HttpLoginDialog.LoginListener) : AppCompatDialog(context) {
 
@BindView(R.id.message_text)
lateinit var textMessage: TextView
@BindView(R.id.login_username)
lateinit var textUsername: EditText
@BindView(R.id.login_password)
lateinit var textPassword: EditText
@BindView(R.id.ok_button)
lateinit var buttonOk: Button
@BindView(R.id.cancel_button)
lateinit var buttonCancel: Button
@BindView(R.id.message_text) lateinit var textMessage: TextView
@BindView(R.id.login_username) lateinit var textUsername: EditText
@BindView(R.id.login_password) lateinit var textPassword: EditText
@BindView(R.id.ok_button) lateinit var buttonOk: Button
@BindView(R.id.cancel_button) lateinit var buttonCancel: Button
 
init {
setContentView(R.layout.dialog_http_login)
Loading
Loading
Loading
Loading
@@ -8,12 +8,11 @@ class CustomHostnameVerifier(private val trustedHostname: String?) : HostnameVer
 
companion object {
private val DEFAULT_HOSTNAME_VERIFIER = OkHostnameVerifier.INSTANCE
//TODO make this not static, its kinda dirty
var lastFailedHostname: String? = null
private set
}
 
var lastFailedHostname: String? = null
private set
override fun verify(hostname: String, session: SSLSession): Boolean {
if (DEFAULT_HOSTNAME_VERIFIER.verify(hostname, session)) {
lastFailedHostname = null
Loading
Loading
package com.commit451.gitlab.ssl;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
public final class X509Util {
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
private X509Util() {}
public static String getFingerPrint(X509Certificate certificate) throws CertificateEncodingException {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
}
digest.update(certificate.getEncoded());
return hexify(digest.digest());
}
public static String hexify(byte[] bytes) {
char[] hexChars = new char[bytes.length * 3];
for (int i = 0; i < bytes.length; i++) {
int v = bytes[i] & 0xFF;
hexChars[i * 3] = HEX_ARRAY[v >>> 4];
hexChars[i * 3 + 1] = HEX_ARRAY[v & 0x0F];
hexChars[i * 3 + 2] = ':';
}
int length = hexChars.length;
if (length > 0) {
return new String(hexChars, 0, length - 1);
} else {
return "";
}
}
}
package com.commit451.gitlab.ssl
import com.commit451.gitlab.util.Hash
import java.security.cert.X509Certificate
object X509Util {
fun getFingerPrint(certificate: X509Certificate): String {
return Hash.sha1(certificate.encoded)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment