Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
AloqaIM-Android
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
AloqaIM-Android
Commits
228759b5
Commit
228759b5
authored
Oct 07, 2018
by
Rafael
Committed by
Jean-Baptiste Guerraz
Oct 12, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Provides support for http basic authentication
parent
5a472099
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
162 additions
and
2 deletions
+162
-2
ServerFragment.kt
...rocket/android/authentication/server/ui/ServerFragment.kt
+1
-1
AppModule.kt
.../main/java/chat/rocket/android/dagger/module/AppModule.kt
+27
-1
BasicAuthRepository.kt
.../chat/rocket/android/server/domain/BasicAuthRepository.kt
+8
-0
GetBasicAuthInteractor.kt
...at/rocket/android/server/domain/GetBasicAuthInteractor.kt
+7
-0
SaveBasicAuthInteractor.kt
...t/rocket/android/server/domain/SaveBasicAuthInteractor.kt
+8
-0
BasicAuth.kt
...java/chat/rocket/android/server/domain/model/BasicAuth.kt
+9
-0
SharedPrefsBasicAuthRepository.kt
.../server/infraestructure/SharedPrefsBasicAuthRepository.kt
+39
-0
BasicAuthenticatorInterceptor.kt
...chat/rocket/android/util/BasicAuthenticatorInterceptor.kt
+63
-0
No files found.
app/src/main/java/chat/rocket/android/authentication/server/ui/ServerFragment.kt
View file @
228759b5
...
...
@@ -242,7 +242,7 @@ class ServerFragment : Fragment(), ServerView {
serverUrlDisposable
=
text_server_url
.
asObservable
()
.
filter
{
it
.
isNotBlank
()
}
.
subscribe
{
if
(
it
.
toString
()
.
isValidUrl
())
{
if
(
"$protocol${it.toString()}"
.
isValidUrl
())
{
enableButtonConnect
()
}
else
{
disableButtonConnect
()
...
...
app/src/main/java/chat/rocket/android/dagger/module/AppModule.kt
View file @
228759b5
...
...
@@ -40,6 +40,10 @@ import chat.rocket.android.server.domain.PermissionsRepository
import
chat.rocket.android.server.domain.SettingsRepository
import
chat.rocket.android.server.domain.TokenRepository
import
chat.rocket.android.server.domain.UsersRepository
import
chat.rocket.android.server.domain.BasicAuthRepository
import
chat.rocket.android.server.domain.GetBasicAuthInteractor
import
chat.rocket.android.server.domain.SaveBasicAuthInteractor
import
chat.rocket.android.server.infraestructure.SharedPrefsBasicAuthRepository
import
chat.rocket.android.server.infraestructure.DatabaseMessageMapper
import
chat.rocket.android.server.infraestructure.DatabaseMessagesRepository
import
chat.rocket.android.server.infraestructure.JobSchedulerInteractorImpl
...
...
@@ -53,6 +57,7 @@ import chat.rocket.android.server.infraestructure.SharedPrefsConnectingServerRep
import
chat.rocket.android.server.infraestructure.SharedPrefsCurrentServerRepository
import
chat.rocket.android.util.AppJsonAdapterFactory
import
chat.rocket.android.util.HttpLoggingInterceptor
import
chat.rocket.android.util.BasicAuthenticatorInterceptor
import
chat.rocket.android.util.TimberLogger
import
chat.rocket.common.internal.FallbackSealedClassJsonAdapter
import
chat.rocket.common.internal.ISO8601Date
...
...
@@ -106,9 +111,22 @@ class AppModule {
@Provides
@Singleton
fun
provideOkHttpClient
(
logger
:
HttpLoggingInterceptor
):
OkHttpClient
{
fun
provideBasicAuthenticatorInterceptor
(
getBasicAuthInteractor
:
GetBasicAuthInteractor
,
saveBasicAuthInteractor
:
SaveBasicAuthInteractor
):
BasicAuthenticatorInterceptor
{
return
BasicAuthenticatorInterceptor
(
getBasicAuthInteractor
,
saveBasicAuthInteractor
)
}
@Provides
@Singleton
fun
provideOkHttpClient
(
logger
:
HttpLoggingInterceptor
,
basicAuthenticator
:
BasicAuthenticatorInterceptor
):
OkHttpClient
{
return
OkHttpClient
.
Builder
()
.
addInterceptor
(
logger
)
.
addInterceptor
(
basicAuthenticator
)
.
connectTimeout
(
15
,
TimeUnit
.
SECONDS
)
.
readTimeout
(
20
,
TimeUnit
.
SECONDS
)
.
writeTimeout
(
15
,
TimeUnit
.
SECONDS
)
...
...
@@ -273,6 +291,14 @@ class AppModule {
return
MessageParser
(
context
,
configuration
,
settingsInteractor
.
get
(
url
))
}
@Provides
@Singleton
fun
provideBasicAuthRepository
(
preferences
:
SharedPreferences
,
moshi
:
Moshi
):
BasicAuthRepository
=
SharedPrefsBasicAuthRepository
(
preferences
,
moshi
)
@Provides
@Singleton
fun
provideAccountsRepository
(
...
...
app/src/main/java/chat/rocket/android/server/domain/BasicAuthRepository.kt
0 → 100644
View file @
228759b5
package
chat.rocket.android.server.domain
import
chat.rocket.android.server.domain.model.BasicAuth
interface
BasicAuthRepository
{
fun
save
(
basicAuth
:
BasicAuth
)
fun
load
():
List
<
BasicAuth
>
}
app/src/main/java/chat/rocket/android/server/domain/GetBasicAuthInteractor.kt
0 → 100644
View file @
228759b5
package
chat.rocket.android.server.domain
import
javax.inject.Inject
class
GetBasicAuthInteractor
@Inject
constructor
(
val
repository
:
BasicAuthRepository
)
{
fun
getAll
()
=
repository
.
load
().
listIterator
()
}
app/src/main/java/chat/rocket/android/server/domain/SaveBasicAuthInteractor.kt
0 → 100644
View file @
228759b5
package
chat.rocket.android.server.domain
import
chat.rocket.android.server.domain.model.BasicAuth
import
javax.inject.Inject
class
SaveBasicAuthInteractor
@Inject
constructor
(
val
repository
:
BasicAuthRepository
)
{
fun
save
(
basicAuth
:
BasicAuth
)
=
repository
.
save
(
basicAuth
)
}
app/src/main/java/chat/rocket/android/server/domain/model/BasicAuth.kt
0 → 100644
View file @
228759b5
package
chat.rocket.android.server.domain.model
import
se.ansman.kotshi.JsonSerializable
@JsonSerializable
data class
BasicAuth
(
val
host
:
String
,
val
credentials
:
String
)
app/src/main/java/chat/rocket/android/server/infraestructure/SharedPrefsBasicAuthRepository.kt
0 → 100644
View file @
228759b5
package
chat.rocket.android.server.infraestructure
import
android.content.SharedPreferences
import
androidx.core.content.edit
import
chat.rocket.android.server.domain.BasicAuthRepository
import
chat.rocket.android.server.domain.model.BasicAuth
import
com.squareup.moshi.Moshi
import
com.squareup.moshi.Types
private
const
val
BASICAUTHS_KEY
=
"BASICAUTHS_KEY"
class
SharedPrefsBasicAuthRepository
(
private
val
preferences
:
SharedPreferences
,
private
val
moshi
:
Moshi
)
:
BasicAuthRepository
{
override
fun
save
(
basicAuth
:
BasicAuth
)
{
val
newList
=
load
().
filter
{
basicAuth
->
basicAuth
.
host
!=
basicAuth
.
host
}
.
toMutableList
()
newList
.
add
(
0
,
basicAuth
)
save
(
newList
)
}
override
fun
load
():
List
<
BasicAuth
>
{
val
json
=
preferences
.
getString
(
BASICAUTHS_KEY
,
"[]"
)
val
type
=
Types
.
newParameterizedType
(
List
::
class
.
java
,
BasicAuth
::
class
.
java
)
val
adapter
=
moshi
.
adapter
<
List
<
BasicAuth
>>(
type
)
return
adapter
.
fromJson
(
json
)
?:
emptyList
()
}
private
fun
save
(
basicAuths
:
List
<
BasicAuth
>)
{
val
type
=
Types
.
newParameterizedType
(
List
::
class
.
java
,
BasicAuth
::
class
.
java
)
val
adapter
=
moshi
.
adapter
<
List
<
BasicAuth
>>(
type
)
preferences
.
edit
{
putString
(
BASICAUTHS_KEY
,
adapter
.
toJson
(
basicAuths
))
}
}
}
app/src/main/java/chat/rocket/android/util/BasicAuthenticatorInterceptor.kt
0 → 100644
View file @
228759b5
package
chat.rocket.android.util
import
okhttp3.Interceptor
import
okhttp3.OkHttpClient
import
okhttp3.Response
import
okhttp3.Credentials
import
java.io.IOException
import
chat.rocket.android.server.domain.model.BasicAuth
import
chat.rocket.android.server.domain.GetBasicAuthInteractor
import
chat.rocket.android.server.domain.SaveBasicAuthInteractor
import
javax.inject.Inject
/**
* An OkHttp interceptor which adds Authorization header based on URI userInfo
* part. Can be applied as an
* [application interceptor][OkHttpClient.interceptors]
* or as a [ ][OkHttpClient.networkInterceptors].
*/
class
BasicAuthenticatorInterceptor
@Inject
constructor
(
private
val
getBasicAuthInteractor
:
GetBasicAuthInteractor
,
private
val
saveBasicAuthInteractor
:
SaveBasicAuthInteractor
):
Interceptor
{
private
val
credentials
=
HashMap
<
String
,
String
>()
init
{
val
basicAuths
=
getBasicAuthInteractor
.
getAll
()
for
(
basicAuth
in
basicAuths
){
credentials
[
basicAuth
.
host
]
=
basicAuth
.
credentials
}
}
private
fun
saveCredentials
(
host
:
String
,
basicCredentials
:
String
)
{
saveBasicAuthInteractor
.
save
(
BasicAuth
(
host
,
basicCredentials
)
)
credentials
[
host
]
=
basicCredentials
}
@Throws
(
IOException
::
class
)
override
fun
intercept
(
chain
:
Interceptor
.
Chain
):
Response
{
var
request
=
chain
.
request
()
val
url
=
request
.
url
()
val
host
=
url
.
host
()
val
username
=
url
.
username
()
if
(!
username
.
isNullOrEmpty
())
{
saveCredentials
(
host
,
Credentials
.
basic
(
username
,
url
.
password
()))
request
=
request
.
newBuilder
().
url
(
url
.
newBuilder
().
username
(
""
).
password
(
""
).
build
()
).
build
()
}
credentials
[
host
]
?.
let
{
request
=
request
.
newBuilder
().
header
(
"Authorization"
,
it
).
build
()
}
return
chain
.
proceed
(
request
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment