-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement OTP Autofill from SMS module
- Loading branch information
Showing
8 changed files
with
211 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
autofill/src/main/java/com/asemlab/samples/autofill/AutoFillApp.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.asemlab.samples.autofill | ||
|
||
import android.app.Application | ||
import androidx.lifecycle.MutableLiveData | ||
|
||
class AutoFillApp : Application() { | ||
|
||
val otp = MutableLiveData<String>() | ||
} |
49 changes: 49 additions & 0 deletions
49
autofill/src/main/java/com/asemlab/samples/autofill/services/SMSReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.asemlab.samples.autofill.services | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.util.Log | ||
import com.asemlab.samples.autofill.AutoFillApp | ||
import com.google.android.gms.auth.api.phone.SmsRetriever | ||
import com.google.android.gms.common.api.CommonStatusCodes | ||
import com.google.android.gms.common.api.Status | ||
import java.util.regex.Pattern | ||
|
||
|
||
/** | ||
* BroadcastReceiver to wait for SMS messages. This can be registered either | ||
* in the AndroidManifest or at runtime. Should filter Intents on | ||
* SmsRetriever.SMS_RETRIEVED_ACTION. | ||
*/ | ||
class SMSReceiver : BroadcastReceiver() { | ||
|
||
override fun onReceive(context: Context, intent: Intent) { | ||
Log.d("SMS Receiver", "OnReceive") | ||
|
||
if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) { | ||
val extras = intent.extras | ||
val status = extras?.get(SmsRetriever.EXTRA_STATUS) as Status | ||
|
||
when (status.statusCode) { | ||
CommonStatusCodes.SUCCESS -> { | ||
// TODO 5. Get SMS message contents and parse the otp | ||
val message = extras.getString(SmsRetriever.EXTRA_SMS_MESSAGE) | ||
val pattern = (Pattern.compile("Your code is: (\\d{4}).*")) | ||
message?.let { | ||
val matcher = pattern.matcher(it) | ||
if (matcher.find()) { | ||
(context.applicationContext as AutoFillApp).otp.postValue(matcher.group(1)) | ||
} | ||
} | ||
|
||
Log.d("SMS Receiver", "message: $message") | ||
} | ||
|
||
CommonStatusCodes.TIMEOUT -> { | ||
Log.e("SMS Receiver", "Timeout: ${status.statusMessage}") | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
autofill/src/main/java/com/asemlab/samples/autofill/utils/AppSignatureHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.asemlab.samples.autofill.utils; | ||
|
||
import android.content.Context; | ||
import android.content.ContextWrapper; | ||
import android.content.pm.PackageManager; | ||
import android.content.pm.Signature; | ||
import android.util.Base64; | ||
import android.util.Log; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* This is a helper class to generate your message hash to be included in your SMS message. | ||
* | ||
* Without the correct hash, your app won't receive the message callback. This only needs to be | ||
* generated once per app and stored. Then you can remove this helper class from your code. | ||
*/ | ||
public class AppSignatureHelper extends ContextWrapper { | ||
public static final String TAG = AppSignatureHelper.class.getSimpleName(); | ||
|
||
private static final String HASH_TYPE = "SHA-256"; | ||
public static final int NUM_HASHED_BYTES = 9; | ||
public static final int NUM_BASE64_CHAR = 11; | ||
|
||
public AppSignatureHelper(Context context) { | ||
super(context); | ||
} | ||
|
||
|
||
public ArrayList<String> getAppSignatures() { | ||
ArrayList<String> appCodes = new ArrayList<>(); | ||
|
||
try { | ||
// Get all package signatures for the current package | ||
String packageName = getPackageName(); | ||
PackageManager packageManager = getPackageManager(); | ||
Signature[] signatures = packageManager.getPackageInfo(packageName, | ||
PackageManager.GET_SIGNATURES).signatures; | ||
|
||
// For each signature create a compatible hash | ||
for (Signature signature : signatures) { | ||
String hash = hash(packageName, signature.toCharsString()); | ||
if (hash != null) { | ||
appCodes.add(String.format("%s", hash)); | ||
} | ||
} | ||
} catch (PackageManager.NameNotFoundException e) { | ||
Log.e(TAG, "Unable to find package to obtain hash.", e); | ||
} | ||
return appCodes; | ||
} | ||
|
||
private static String hash(String packageName, String signature) { | ||
String appInfo = packageName + " " + signature; | ||
try { | ||
MessageDigest messageDigest = MessageDigest.getInstance(HASH_TYPE); | ||
messageDigest.update(appInfo.getBytes(StandardCharsets.UTF_8)); | ||
byte[] hashSignature = messageDigest.digest(); | ||
|
||
// truncated into NUM_HASHED_BYTES | ||
hashSignature = Arrays.copyOfRange(hashSignature, 0, NUM_HASHED_BYTES); | ||
// encode into Base64 | ||
String base64Hash = Base64.encodeToString(hashSignature, Base64.NO_PADDING | Base64.NO_WRAP); | ||
base64Hash = base64Hash.substring(0, NUM_BASE64_CHAR); | ||
|
||
Log.d(TAG, String.format("pkg: %s -- hash: %s", packageName, base64Hash)); | ||
return base64Hash; | ||
} catch (NoSuchAlgorithmException e) { | ||
Log.e(TAG, "hash:NoSuchAlgorithm", e); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters