Commit b255de89 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Implement registering of generated instance id token on login and also on token rotation

parent f1dc23f6
...@@ -3,12 +3,15 @@ package chat.rocket.android.authentication.login.presentation ...@@ -3,12 +3,15 @@ package chat.rocket.android.authentication.login.presentation
import chat.rocket.android.authentication.infraestructure.AuthTokenRepository import chat.rocket.android.authentication.infraestructure.AuthTokenRepository
import chat.rocket.android.authentication.presentation.AuthenticationNavigator import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.core.lifecycle.CancelStrategy import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.infrastructure.SharedPreferencesRepository
import chat.rocket.android.util.launchUI import chat.rocket.android.util.launchUI
import chat.rocket.common.RocketChatException import chat.rocket.common.RocketChatException
import chat.rocket.common.RocketChatTwoFactorException import chat.rocket.common.RocketChatTwoFactorException
import chat.rocket.common.util.PlatformLogger import chat.rocket.common.util.PlatformLogger
import chat.rocket.core.RocketChatClient import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.login import chat.rocket.core.internal.rest.login
import chat.rocket.core.internal.rest.registerPushToken
import okhttp3.HttpUrl import okhttp3.HttpUrl
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import javax.inject.Inject import javax.inject.Inject
...@@ -35,10 +38,10 @@ class LoginPresenter @Inject constructor(private val view: LoginView, ...@@ -35,10 +38,10 @@ class LoginPresenter @Inject constructor(private val view: LoginView,
view.showLoading() view.showLoading()
try { try {
val token = client.login(username, password) val token = client.login(username, password)
registerPushToken()
navigator.toChatList() navigator.toChatList()
} catch (ex: RocketChatException) { } catch (ex: RocketChatException) {
when(ex) { when (ex) {
is RocketChatTwoFactorException -> is RocketChatTwoFactorException ->
navigator.toTwoFA(navigator.currentServer!!, username, password) navigator.toTwoFA(navigator.currentServer!!, username, password)
else -> else ->
...@@ -53,4 +56,15 @@ class LoginPresenter @Inject constructor(private val view: LoginView, ...@@ -53,4 +56,15 @@ class LoginPresenter @Inject constructor(private val view: LoginView,
fun signup() { fun signup() {
navigator.toSignUp(navigator.currentServer!!) navigator.toSignUp(navigator.currentServer!!)
} }
private suspend fun registerPushToken() {
// TODO: put it on constructor
val localRepository: LocalRepository = SharedPreferencesRepository(navigator.activity)
localRepository.get(LocalRepository.KEY_PUSH_TOKEN)?.let {
client.registerPushToken(it)
}
// TODO: Schedule push token registering when it comes up null
}
} }
\ No newline at end of file
...@@ -3,11 +3,14 @@ package chat.rocket.android.authentication.twofactor.presentation ...@@ -3,11 +3,14 @@ package chat.rocket.android.authentication.twofactor.presentation
import chat.rocket.android.authentication.infraestructure.AuthTokenRepository import chat.rocket.android.authentication.infraestructure.AuthTokenRepository
import chat.rocket.android.authentication.presentation.AuthenticationNavigator import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.core.lifecycle.CancelStrategy import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.infrastructure.SharedPreferencesRepository
import chat.rocket.android.util.launchUI import chat.rocket.android.util.launchUI
import chat.rocket.common.RocketChatException import chat.rocket.common.RocketChatException
import chat.rocket.common.util.PlatformLogger import chat.rocket.common.util.PlatformLogger
import chat.rocket.core.RocketChatClient import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.login import chat.rocket.core.internal.rest.login
import chat.rocket.core.internal.rest.registerPushToken
import okhttp3.HttpUrl import okhttp3.HttpUrl
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import javax.inject.Inject import javax.inject.Inject
...@@ -34,7 +37,7 @@ class TwoFAPresenter @Inject constructor(private val view: TwoFAView, ...@@ -34,7 +37,7 @@ class TwoFAPresenter @Inject constructor(private val view: TwoFAView,
view.showLoading() view.showLoading()
try { try {
val token = client.login(username, password, pin) val token = client.login(username, password, pin)
registerPushToken()
navigator.toChatList() navigator.toChatList()
} catch (ex: RocketChatException) { } catch (ex: RocketChatException) {
view.onLoginError(ex.message) view.onLoginError(ex.message)
...@@ -47,4 +50,15 @@ class TwoFAPresenter @Inject constructor(private val view: TwoFAView, ...@@ -47,4 +50,15 @@ class TwoFAPresenter @Inject constructor(private val view: TwoFAView,
fun signup() { fun signup() {
navigator.toSignUp(navigator.currentServer!!) navigator.toSignUp(navigator.currentServer!!)
} }
private suspend fun registerPushToken() {
// TODO: put it on constructor
val localRepository: LocalRepository = SharedPreferencesRepository(navigator.activity)
localRepository.get(LocalRepository.KEY_PUSH_TOKEN)?.let {
client.registerPushToken(it)
}
// TODO: Schedule push token registering when it comes up null
}
} }
\ No newline at end of file
package chat.rocket.android.push
import chat.rocket.android.R
import chat.rocket.android.dagger.module.AppModule
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.push.di.DaggerPushComponent
import chat.rocket.android.push.di.PushModule
import chat.rocket.common.RocketChatException
import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.registerPushToken
import com.google.android.gms.gcm.GoogleCloudMessaging
import com.google.android.gms.iid.InstanceID
import com.google.firebase.iid.FirebaseInstanceIdService
import kotlinx.coroutines.experimental.launch
import timber.log.Timber
import javax.inject.Inject
class PushTokenService : FirebaseInstanceIdService() {
@Inject
lateinit var client: RocketChatClient
@Inject
lateinit var localRepository: LocalRepository
override fun onCreate() {
super.onCreate()
DaggerPushComponent.builder()
.appModule(AppModule())
.pushModule(PushModule(this))
.build()
.inject(this)
}
override fun onTokenRefresh() {
val gcmToken = InstanceID.getInstance(this)
.getToken(getString(R.string.gcm_defaultSenderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE), null)
gcmToken?.let {
localRepository.save(LocalRepository.KEY_PUSH_TOKEN, gcmToken)
launch {
try {
client.registerPushToken(gcmToken)
} catch (ex: RocketChatException) {
Timber.e(ex)
}
}
}
}
}
\ No newline at end of file
package chat.rocket.android.push.di
import chat.rocket.android.dagger.module.AppModule
import chat.rocket.android.push.PushTokenService
import dagger.Component
import javax.inject.Singleton
@Singleton
@Component(modules = arrayOf(AppModule::class, PushModule::class))
interface PushComponent {
fun inject(service: PushTokenService)
}
\ No newline at end of file
package chat.rocket.android.push.di
import chat.rocket.android.authentication.infraestructure.AuthTokenRepository
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.infrastructure.SharedPreferencesRepository
import chat.rocket.android.push.PushTokenService
import chat.rocket.common.util.PlatformLogger
import chat.rocket.core.RocketChatClient
import chat.rocket.core.TokenRepository
import dagger.Module
import dagger.Provides
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import javax.inject.Singleton
@Module
@Singleton
class PushModule(val context: PushTokenService) {
@Provides
fun provideAuthTokenRepository(): TokenRepository = AuthTokenRepository()
@Provides
fun provideLocalRepository(): LocalRepository = SharedPreferencesRepository(context)
// @Provides
// fun provideCurrentServer() = "https://open.rocket.chat"
@Provides
fun provideRocketChatClient(okHttpClient: OkHttpClient, repository: TokenRepository, logger: PlatformLogger): RocketChatClient {
return RocketChatClient.create {
httpClient = okHttpClient
restUrl = HttpUrl.parse("https://unstable.rocket.chat")!!
websocketUrl = "https://unstable.rocket.chat"
tokenRepository = repository
platformLogger = logger
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment