Skip to content

Commit 51ffadd

Browse files
committed
More refactoring
1 parent 08b36eb commit 51ffadd

2 files changed

Lines changed: 41 additions & 52 deletions

File tree

play-services-constellation/core/src/main/kotlin/org/microg/gms/constellation/core/AuthManager.kt

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import java.security.Signature
1313
import java.security.spec.PKCS8EncodedKeySpec
1414
import java.security.spec.X509EncodedKeySpec
1515

16-
class AuthManager(context: Context) {
16+
val Context.authManager: AuthManager get() = AuthManager.get(this)
17+
18+
class AuthManager private constructor(context: Context) {
1719
private val context = context.applicationContext
20+
private val sharedPrefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
1821

1922
companion object {
2023
private const val PREFS_NAME = "constellation_prefs"
@@ -24,22 +27,14 @@ class AuthManager(context: Context) {
2427
@Volatile
2528
private var instance: AuthManager? = null
2629

27-
fun get(context: Context): AuthManager {
28-
val existing = instance
29-
if (existing != null) return existing
30-
31-
return synchronized(this) {
32-
instance ?: AuthManager(context).also { instance = it }
33-
}
30+
fun get(context: Context): AuthManager = instance ?: synchronized(this) {
31+
instance ?: AuthManager(context).also { instance = it }
3432
}
3533
}
3634

37-
private val sharedPrefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
38-
3935
// GMS signing format: {iidToken}:{seconds}:{nanos}
4036
fun signIidToken(iidToken: String): Pair<ByteArray, Instant> {
41-
val now = System.currentTimeMillis()
42-
val timestamp = Instant.ofEpochMilli(now)
37+
val timestamp = Instant.ofEpochMilli(System.currentTimeMillis())
4338
val content = "$iidToken:${timestamp.epochSecond}:${timestamp.nano}"
4439
return sign(content) to timestamp
4540
}
@@ -61,10 +56,20 @@ class AuthManager(context: Context) {
6156
try {
6257
val kf = KeyFactory.getInstance("EC")
6358
val privateKey = kf.generatePrivate(
64-
PKCS8EncodedKeySpec(Base64.decode(privateKeyStr, Base64.DEFAULT))
59+
PKCS8EncodedKeySpec(
60+
Base64.decode(
61+
privateKeyStr,
62+
Base64.DEFAULT
63+
)
64+
)
6565
)
6666
val publicKey = kf.generatePublic(
67-
X509EncodedKeySpec(Base64.decode(publicKeyStr, Base64.DEFAULT))
67+
X509EncodedKeySpec(
68+
Base64.decode(
69+
publicKeyStr,
70+
Base64.DEFAULT
71+
)
72+
)
6873
)
6974
return KeyPair(publicKey, privateKey)
7075
} catch (_: Exception) {
@@ -96,7 +101,5 @@ class AuthManager(context: Context) {
96101
}
97102
}
98103

99-
fun getFid(): String {
100-
return InstanceID.getInstance(context).id
101-
}
102-
}
104+
fun getFid(): String = InstanceID.getInstance(context).id
105+
}
Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,33 @@
11
package org.microg.gms.constellation.core
22

33
import com.squareup.wire.GrpcClient
4-
import okhttp3.Interceptor
54
import okhttp3.OkHttpClient
6-
import okhttp3.Response
75
import org.microg.gms.common.Constants
86
import org.microg.gms.constellation.core.proto.PhoneDeviceVerificationClient
97
import org.microg.gms.constellation.core.proto.PhoneNumberClient
108

11-
private class AuthInterceptor : Interceptor {
12-
override fun intercept(chain: Interceptor.Chain): Response {
13-
val originalRequest = chain.request()
14-
15-
val builder = originalRequest.newBuilder()
16-
.header("X-Goog-Api-Key", "AIzaSyAP-gfH3qvi6vgHZbSYwQ_XHqV_mXHhzIk")
17-
.header("X-Android-Package", Constants.GMS_PACKAGE_NAME)
18-
.header("X-Android-Cert", Constants.GMS_PACKAGE_SIGNATURE_SHA1.uppercase())
19-
20-
return chain.proceed(builder.build())
21-
}
22-
}
23-
249
object RpcClient {
25-
private val client: OkHttpClient by lazy {
26-
OkHttpClient.Builder()
27-
.addInterceptor(AuthInterceptor())
28-
.build()
29-
}
30-
31-
private val grpcClient: GrpcClient by lazy {
32-
GrpcClient.Builder()
33-
.client(client)
34-
// Google's constellationserver does NOT like compressed requests
35-
.minMessageToCompress(Long.MAX_VALUE)
36-
.baseUrl("https://phonedeviceverification-pa.googleapis.com/")
37-
.build()
38-
}
39-
40-
val phoneDeviceVerificationClient: PhoneDeviceVerificationClient by lazy {
10+
private val client: OkHttpClient = OkHttpClient.Builder()
11+
.addInterceptor { chain ->
12+
val originalRequest = chain.request()
13+
val builder = originalRequest.newBuilder()
14+
.header("X-Goog-Api-Key", "AIzaSyAP-gfH3qvi6vgHZbSYwQ_XHqV_mXHhzIk")
15+
.header("X-Android-Package", Constants.GMS_PACKAGE_NAME)
16+
.header("X-Android-Cert", Constants.GMS_PACKAGE_SIGNATURE_SHA1.uppercase())
17+
chain.proceed(builder.build())
18+
}
19+
.build()
20+
21+
private val grpcClient: GrpcClient = GrpcClient.Builder()
22+
.client(client)
23+
// Google's constellationserver does NOT like compressed requests
24+
.minMessageToCompress(Long.MAX_VALUE)
25+
.baseUrl("https://phonedeviceverification-pa.googleapis.com/")
26+
.build()
27+
28+
val phoneDeviceVerificationClient: PhoneDeviceVerificationClient =
4129
grpcClient.create(PhoneDeviceVerificationClient::class)
42-
}
4330

44-
val phoneNumberClient: PhoneNumberClient by lazy {
31+
val phoneNumberClient: PhoneNumberClient =
4532
grpcClient.create(PhoneNumberClient::class)
46-
}
4733
}

0 commit comments

Comments
 (0)