-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for TLS1.2 on pre-lollipop devices. #128
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
232a2dd
Add support for TLS1.2 on pre-lollipop devices.
dj-mal 557545a
Add PowerMockIgnore conditions on API client tests.
dj-mal 99f2be0
Add tests on TLS support utils.
dj-mal f9eb054
Rename TLS related methods; make TLS12SocketFactory package private.
dj-mal f0d8ce0
Make OkHttpTLS12Compat non static.
dj-mal c931684
Refactor OkHttpTLS12Compat to OkHttpClientFactory.
dj-mal 318b95c
Clean up OkHttpClientFactory.
dj-mal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
package com.auth0.android.util; | ||
package com.auth0.android.request.internal; | ||
|
||
import android.os.Build; | ||
import android.support.annotation.VisibleForTesting; | ||
import android.util.Log; | ||
|
||
import com.squareup.okhttp.ConnectionSpec; | ||
import com.squareup.okhttp.Interceptor; | ||
import com.squareup.okhttp.OkHttpClient; | ||
import com.squareup.okhttp.TlsVersion; | ||
import com.squareup.okhttp.logging.HttpLoggingInterceptor; | ||
|
||
import java.security.KeyManagementException; | ||
import java.security.NoSuchAlgorithmException; | ||
|
@@ -14,32 +17,49 @@ | |
|
||
import javax.net.ssl.SSLContext; | ||
|
||
public class OkHttpTLS12Compat { | ||
public class OkHttpClientFactory { | ||
|
||
private static final String TAG = OkHttpTLS12Compat.class.getSimpleName(); | ||
|
||
private OkHttpClient client = null; | ||
private static final String TAG = OkHttpClientFactory.class.getSimpleName(); | ||
|
||
/** | ||
* Sets the OkHttp client instance | ||
* @param client OkHttpClient instance to be modified | ||
* This method creates an instance of OKHttpClient according to the provided parameters. | ||
* It is used internally and is not intended to be used directly. | ||
* @param loggingEnabled Enable logging in the created OkHttpClient. | ||
* @param tls12Enforced Enforce TLS 1.2 in the created OkHttpClient on devices with API 16-21 | ||
* @return new OkHttpClient instance created according to the parameters. | ||
*/ | ||
public OkHttpTLS12Compat setClient(OkHttpClient client) { | ||
this.client = client; | ||
return this; | ||
public OkHttpClient createClient(boolean loggingEnabled, boolean tls12Enforced) { | ||
return modifyClient(new OkHttpClient(), loggingEnabled, tls12Enforced); | ||
} | ||
|
||
@VisibleForTesting | ||
OkHttpClient modifyClient(OkHttpClient client, boolean loggingEnabled, boolean tls12Enforced) { | ||
if (loggingEnabled) { | ||
enableLogging(client); | ||
} | ||
if (tls12Enforced) { | ||
enforceTls12(client); | ||
} | ||
return client; | ||
} | ||
|
||
private void enableLogging(OkHttpClient client) { | ||
Interceptor interceptor = new HttpLoggingInterceptor() | ||
.setLevel(HttpLoggingInterceptor.Level.BODY); | ||
client.interceptors().add(interceptor); | ||
} | ||
|
||
/** | ||
* Enable TLS 1.2 on the OkHttpClient on API 16-21, which is supported but not enabled by default. | ||
* @link https://github.com/square/okhttp/issues/2372 | ||
* @see TLS12SocketFactory | ||
*/ | ||
public OkHttpTLS12Compat enableForClient() { | ||
private void enforceTls12(OkHttpClient client) { | ||
// No need to modify client as TLS 1.2 is enabled by default on API21+ | ||
// Lollipop is included because some Samsung devices face the same problem on API 21. | ||
if (client == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
|| Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { | ||
return this; | ||
return; | ||
} | ||
try { | ||
SSLContext sc = SSLContext.getInstance("TLSv1.2"); | ||
|
@@ -59,6 +79,5 @@ public OkHttpTLS12Compat enableForClient() { | |
} catch (NoSuchAlgorithmException | KeyManagementException e) { | ||
Log.e(TAG, "Error while setting TLS 1.2", e); | ||
} | ||
return this; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...uth0/android/util/TLS12SocketFactory.java → .../request/internal/TLS12SocketFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add an additional javadoc at the class level:
or something like that 🙂