-
Notifications
You must be signed in to change notification settings - Fork 162
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
Added digest auth #56
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ | |
import android.text.TextUtils; | ||
import android.util.Log; | ||
|
||
import com.albroco.barebonesdigest.DigestAuthentication; | ||
import com.albroco.barebonesdigest.DigestChallengeResponse; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.Authenticator; | ||
|
@@ -94,15 +97,25 @@ public Mjpeg sendConnectionCloseHeader() { | |
} | ||
|
||
@NonNull | ||
private Observable<MjpegInputStream> connect(String url) { | ||
private Observable<MjpegInputStream> connect(String url, String username, String password) { | ||
return Observable.defer(() -> { | ||
try { | ||
HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection(); | ||
urlConnection.setRequestProperty("Cache-Control", "no-cache"); | ||
|
||
if (sendConnectionCloseHeader) { | ||
urlConnection.setRequestProperty("Connection", "close"); | ||
} | ||
|
||
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED && !TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) { | ||
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. if is worth it, maybe extract it in a method and add comment for why is needed, otherwise is fine. 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. Agreed, I will do it later. Thanks for your feedback |
||
DigestAuthentication auth = DigestAuthentication.fromResponse(urlConnection); | ||
auth.username(username).password(password); | ||
|
||
urlConnection = (HttpURLConnection) new URL(url).openConnection(); | ||
String authentication = auth.getAuthorizationForRequest("GET", urlConnection.getURL().getPath()); | ||
urlConnection.setRequestProperty(DigestChallengeResponse.HTTP_HEADER_AUTHORIZATION, authentication); | ||
} | ||
|
||
InputStream inputStream = urlConnection.getInputStream(); | ||
switch (type) { | ||
// handle multiple implementations | ||
|
@@ -126,7 +139,7 @@ private Observable<MjpegInputStream> connect(String url) { | |
* @return Observable Mjpeg stream | ||
*/ | ||
public Observable<MjpegInputStream> open(String url) { | ||
return connect(url) | ||
return connect(url, null, null) | ||
.subscribeOn(Schedulers.io()) | ||
.observeOn(AndroidSchedulers.mainThread()); | ||
} | ||
|
@@ -139,10 +152,29 @@ public Observable<MjpegInputStream> open(String url) { | |
* @return Observable Mjpeg stream | ||
*/ | ||
public Observable<MjpegInputStream> open(String url, int timeout) { | ||
return connect(url) | ||
return connect(url, null, null) | ||
.timeout(timeout, TimeUnit.SECONDS) | ||
.subscribeOn(Schedulers.io()) | ||
.observeOn(AndroidSchedulers.mainThread()); | ||
} | ||
|
||
/** | ||
* Connect to a Mjpeg stream. | ||
* | ||
* @param url source | ||
* @param timeout in seconds | ||
* @param userDigestAuth in seconds | ||
* @param passDigestAuth in seconds | ||
* @return Observable Mjpeg stream | ||
*/ | ||
public Observable<MjpegInputStream> openWithDigestAuth(String url, | ||
int timeout, | ||
String userDigestAuth, | ||
String passDigestAuth) { | ||
return connect(url, userDigestAuth, passDigestAuth) | ||
.timeout(timeout, TimeUnit.SECONDS) | ||
.subscribeOn(Schedulers.io()) | ||
.observeOn(AndroidSchedulers.mainThread()); | ||
} | ||
|
||
} |
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 revert this, I will change it when release the lib
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.
Okay
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.
Actually, It could be done by gradle task.
@niqdev
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.
nice, so to use it properly i need to tag it just before release the lib i.e.
./gradlew build bintrayUpload
. correct? can you add it in a different PR unless you think is going to be different in #59?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.
hi @niqdev
I will do another PR, for that change.
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.
Right, this should be set automatically.
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.
Here you go #115