Skip to content

Commit 01400ff

Browse files
committed
More refactoring
1 parent 08b36eb commit 01400ff

7 files changed

Lines changed: 49 additions & 58 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+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ suspend fun handleGetIidToken(
1818
request: GetIidTokenRequest
1919
) = withContext(Dispatchers.IO) {
2020
try {
21-
val authManager = AuthManager.get(context)
21+
val authManager = context.authManager
2222
val iidToken = authManager.getIidToken(request.projectNumber?.toString())
2323
val fid = authManager.getFid()
2424
val (signature, timestamp) = authManager.signIidToken(iidToken)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal suspend fun fetchVerifiedPhoneNumbers(
4141
bundle: Bundle,
4242
callingPackage: String = bundle.getString("calling_package") ?: context.packageName
4343
): List<RpcVerifiedPhoneNumber> = withContext(Dispatchers.IO) {
44-
val authManager = AuthManager.get(context)
44+
val authManager = context.authManager
4545
val sessionId = UUID.randomUUID().toString()
4646
val selections = extractPhoneNumberSelections(bundle)
4747
val certificateHash = bundle.getString("certificate_hash") ?: ""
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
}

play-services-constellation/core/src/main/kotlin/org/microg/gms/constellation/core/proto/builders/ClientInfoBuilder.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import org.microg.gms.common.Constants
1414
import org.microg.gms.constellation.core.AuthManager
1515
import org.microg.gms.constellation.core.ConstellationStateStore
1616
import org.microg.gms.constellation.core.GServices
17+
import org.microg.gms.constellation.core.authManager
1718
import org.microg.gms.constellation.core.proto.ClientInfo
1819
import org.microg.gms.constellation.core.proto.CountryInfo
1920
import org.microg.gms.constellation.core.proto.DeviceID
@@ -47,11 +48,10 @@ suspend operator fun ClientInfo.Companion.invoke(
4748
): ClientInfo {
4849
val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)
4950
val locale = Locale.getDefault().let { "${it.language}_${it.country}" }
50-
val authManager = AuthManager.get(context)
5151

5252
return ClientInfo(
5353
device_id = DeviceID(context, buildContext.iidToken),
54-
client_public_key = authManager.getOrCreateKeyPair().public.encoded.toByteString(),
54+
client_public_key = context.authManager.getOrCreateKeyPair().public.encoded.toByteString(),
5555
locale = locale,
5656
gmscore_version_number = Constants.GMS_VERSION_CODE / 1000,
5757
gmscore_version = packageInfo.versionName ?: "",

play-services-constellation/core/src/main/kotlin/org/microg/gms/constellation/core/proto/builders/CommonBuilders.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.Context
44
import android.os.Bundle
55
import okio.ByteString.Companion.toByteString
66
import org.microg.gms.constellation.core.AuthManager
7+
import org.microg.gms.constellation.core.authManager
78
import org.microg.gms.constellation.core.proto.AuditToken
89
import org.microg.gms.constellation.core.proto.AuditTokenMetadata
910
import org.microg.gms.constellation.core.proto.AuditUuid
@@ -46,7 +47,7 @@ suspend operator fun RequestHeader.Companion.invoke(
4647
triggerType: RequestTrigger.Type = RequestTrigger.Type.CONSENT_API_TRIGGER,
4748
includeClientAuth: Boolean = false
4849
): RequestHeader {
49-
val authManager = if (includeClientAuth) AuthManager.get(context) else null
50+
val authManager = if (includeClientAuth) context.authManager else null
5051
val clientAuth = if (includeClientAuth) {
5152
val (signature, timestamp) = authManager!!.signIidToken(buildContext.iidToken)
5253
org.microg.gms.constellation.core.proto.ClientAuth(

play-services-constellation/core/src/main/kotlin/org/microg/gms/constellation/core/proto/builders/RequestBuildContext.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.microg.gms.constellation.core.proto.builders
22

33
import android.content.Context
44
import org.microg.gms.constellation.core.AuthManager
5+
import org.microg.gms.constellation.core.authManager
56
import org.microg.gms.constellation.core.proto.GaiaToken
67

78
data class RequestBuildContext(
@@ -11,7 +12,7 @@ data class RequestBuildContext(
1112

1213
suspend fun buildRequestContext(
1314
context: Context,
14-
authManager: AuthManager = AuthManager.get(context)
15+
authManager: AuthManager = context.authManager
1516
): RequestBuildContext {
1617
return RequestBuildContext(
1718
iidToken = authManager.getIidToken(),

0 commit comments

Comments
 (0)