瀏覽代碼

New: authenticator refresh expired token

liweihao 6 年之前
父節點
當前提交
768859216d

+ 10 - 2
src/main/java/com/example/bomocloud/BoMoCloud.kt

@@ -1,15 +1,15 @@
 package com.example.bomocloud
 
 import com.example.bomocloud.auth.AuthService
+import com.example.bomocloud.auth.OauthService
 import com.example.bomocloud.home.HomeService
 import com.example.bomocloud.theme.ThemeService
-import okhttp3.Interceptor
 import okhttp3.OkHttpClient
 import retrofit2.Retrofit
 import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
 import retrofit2.converter.gson.GsonConverterFactory
 
-class BoMoCloud(val clientId: String, val clientSecret: String) {
+class BoMoCloud(private val clientId: String, private val clientSecret: String) {
 
     companion object {
         const val DEBUG_HOST = "https://preparing.bomostory.com/"
@@ -32,13 +32,20 @@ class BoMoCloud(val clientId: String, val clientSecret: String) {
     }
 
     val host = if (BuildConfig.DEBUG) DEBUG_HOST else RELEASE_HOST
+
     val authService: AuthService = buildAuthService()
+    val oAuthService: OauthService = buildOAuthService()
     val homeService: HomeService = buildHomeService()
     val themeService: ThemeService = buildThemeService()
 
+    var refreshToken: String? = null
+
     private fun buildAuthService(): AuthService =
             getRetrofit(host + AuthService.BASE_URL).create(AuthService::class.java)
 
+    private fun buildOAuthService(): OauthService =
+            getRetrofit(host + OauthService.BASE_URL).create(OauthService::class.java)
+
     private fun buildHomeService(): HomeService =
             getRetrofit(host + HomeService.BASE_URL).create(HomeService::class.java)
 
@@ -64,5 +71,6 @@ class BoMoCloud(val clientId: String, val clientSecret: String) {
                         request = request.newBuilder().url(httpUrl).build()
                         it.proceed(request)
                     }
+                    .authenticator(TokenAuthenticator(this))
                     .build()
 }

+ 38 - 0
src/main/java/com/example/bomocloud/TokenAuthenticator.kt

@@ -0,0 +1,38 @@
+package com.example.bomocloud
+
+import okhttp3.HttpUrl
+import okhttp3.Request
+import okhttp3.Response
+import okhttp3.Route
+
+class TokenAuthenticator(private val boMoCloud: BoMoCloud) : okhttp3.Authenticator {
+
+    companion object {
+        const val GRANT_TYPE = "refresh_token"
+        const val ACCESS_TOKEN = "access_token"
+    }
+
+    override fun authenticate(route: Route?, response: Response?): Request? {
+        boMoCloud.refreshToken?.let {
+            var accessToken: String? = null
+            var httpUrl: HttpUrl? = null
+
+            boMoCloud.oAuthService.refreshToken(GRANT_TYPE, it)
+                    .subscribe({
+                        accessToken = it.access_token
+                    }, {
+                        it.printStackTrace()
+                    })
+
+            accessToken?.let {
+                httpUrl = response?.request()?.url()?.newBuilder()?.setQueryParameter(ACCESS_TOKEN, it)?.build()
+            }
+
+            httpUrl?.let {
+                return response?.request()?.newBuilder()?.url(it)?.build()
+            }
+        }
+
+        return null
+    }
+}

+ 18 - 0
src/main/java/com/example/bomocloud/auth/OauthService.kt

@@ -0,0 +1,18 @@
+package com.example.bomocloud.auth
+
+import io.reactivex.Observable
+import retrofit2.http.POST
+import retrofit2.http.Query
+
+interface OauthService {
+
+    companion object {
+        const val BASE_URL = "oauth/"
+    }
+
+    @POST("token")
+    fun refreshToken(
+            @Query("grant_type") grantType: String,
+            @Query("refresh_token") refreshToken: String?
+    ): Observable<RefreshTokenData>
+}

+ 10 - 0
src/main/java/com/example/bomocloud/auth/RefreshTokenData.kt

@@ -0,0 +1,10 @@
+package com.example.bomocloud.auth
+
+
+data class RefreshTokenData(
+        val access_token: String,
+        val token_type: String,
+        val expires_in: Int,
+        val refresh_token: String,
+        val created_at: Int
+)