From fa89d8b32c85e1eda2fbc73603a875f71046f9be Mon Sep 17 00:00:00 2001 From: senthilkumar Date: Fri, 18 Feb 2022 11:32:53 +0530 Subject: [PATCH] first commit --- .idea/.name | 1 + .idea/misc.xml | 1 + app/build.gradle | 4 + app/google-services.json | 114 +++++++++ app/src/main/AndroidManifest.xml | 12 +- .../AuthenticationInfoModel.java | 35 +++ .../example/two_fact_auth/MainActivity.java | 14 -- .../TwoFactorAuthentication.java | 230 ++++++++++++++++++ build.gradle | 1 + 9 files changed, 387 insertions(+), 25 deletions(-) create mode 100644 .idea/.name create mode 100644 app/google-services.json create mode 100644 app/src/main/java/com/example/two_fact_auth/AuthenticationInfoModel.java delete mode 100644 app/src/main/java/com/example/two_fact_auth/MainActivity.java create mode 100644 app/src/main/java/com/example/two_fact_auth/TwoFactorAuthentication.java diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..075fa6a --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +two_fact_auth \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 71fb772..8796a2c 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,6 +3,7 @@ diff --git a/app/build.gradle b/app/build.gradle index 2c09431..dd230a5 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,5 +1,6 @@ plugins { id 'com.android.application' + id 'com.google.gms.google-services' } android { @@ -32,7 +33,10 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'com.google.android.material:material:1.5.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' + implementation 'com.google.firebase:firebase-database:20.0.3' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + implementation "androidmads.library.qrgenearator:QRGenearator:1.0.3" + } \ No newline at end of file diff --git a/app/google-services.json b/app/google-services.json new file mode 100644 index 0000000..20fe79c --- /dev/null +++ b/app/google-services.json @@ -0,0 +1,114 @@ +{ + "project_info": { + "project_number": "187896902330", + "firebase_url": "https://authenticator-72be5-default-rtdb.firebaseio.com", + "project_id": "authenticator-72be5", + "storage_bucket": "authenticator-72be5.appspot.com" + }, + "client": [ + { + "client_info": { + "mobilesdk_app_id": "1:187896902330:android:d0fb18ce4c725cf4550883", + "android_client_info": { + "package_name": "com.example.random_no" + } + }, + "oauth_client": [ + { + "client_id": "187896902330-mki9n9r2pkab2s7gdt4mo30nmlnmgfqr.apps.googleusercontent.com", + "client_type": 1, + "android_info": { + "package_name": "com.example.random_no", + "certificate_hash": "dbd67a08fc4abd5c173fcf0ac1c14b168caf6ad9" + } + }, + { + "client_id": "187896902330-4atg67qd7cti36hcasou54j5q0fgve1t.apps.googleusercontent.com", + "client_type": 3 + } + ], + "api_key": [ + { + "current_key": "AIzaSyBAq2brhljD6WJVhmJC-QnlALsEo5VfotI" + } + ], + "services": { + "appinvite_service": { + "other_platform_oauth_client": [ + { + "client_id": "187896902330-4atg67qd7cti36hcasou54j5q0fgve1t.apps.googleusercontent.com", + "client_type": 3 + } + ] + } + } + }, + { + "client_info": { + "mobilesdk_app_id": "1:187896902330:android:92b5933513c8c6b5550883", + "android_client_info": { + "package_name": "com.example.two_fact_auth" + } + }, + "oauth_client": [ + { + "client_id": "187896902330-4atg67qd7cti36hcasou54j5q0fgve1t.apps.googleusercontent.com", + "client_type": 3 + } + ], + "api_key": [ + { + "current_key": "AIzaSyBAq2brhljD6WJVhmJC-QnlALsEo5VfotI" + } + ], + "services": { + "appinvite_service": { + "other_platform_oauth_client": [ + { + "client_id": "187896902330-4atg67qd7cti36hcasou54j5q0fgve1t.apps.googleusercontent.com", + "client_type": 3 + } + ] + } + } + }, + { + "client_info": { + "mobilesdk_app_id": "1:187896902330:android:a9b97778b1cae551550883", + "android_client_info": { + "package_name": "com.example.two_factor_authentication" + } + }, + "oauth_client": [ + { + "client_id": "187896902330-7husnt6gaoovn7l4n2kohf6s1jil88c8.apps.googleusercontent.com", + "client_type": 1, + "android_info": { + "package_name": "com.example.two_factor_authentication", + "certificate_hash": "dbd67a08fc4abd5c173fcf0ac1c14b168caf6ad9" + } + }, + { + "client_id": "187896902330-4atg67qd7cti36hcasou54j5q0fgve1t.apps.googleusercontent.com", + "client_type": 3 + } + ], + "api_key": [ + { + "current_key": "AIzaSyBAq2brhljD6WJVhmJC-QnlALsEo5VfotI" + } + ], + "services": { + "appinvite_service": { + "other_platform_oauth_client": [ + { + "client_id": "187896902330-4atg67qd7cti36hcasou54j5q0fgve1t.apps.googleusercontent.com", + "client_type": 3 + } + ] + } + } + } + ], + "configuration_version": "1" +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c73fdb2..d5dd7e8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,16 +8,6 @@ android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" - android:theme="@style/Theme.Two_fact_auth"> - - - - - - - - + android:theme="@style/Theme.Two_fact_auth"> \ No newline at end of file diff --git a/app/src/main/java/com/example/two_fact_auth/AuthenticationInfoModel.java b/app/src/main/java/com/example/two_fact_auth/AuthenticationInfoModel.java new file mode 100644 index 0000000..10ba1b6 --- /dev/null +++ b/app/src/main/java/com/example/two_fact_auth/AuthenticationInfoModel.java @@ -0,0 +1,35 @@ +package com.example.two_fact_auth; + +public class AuthenticationInfoModel { + public int status; + public String uniqueNo; + public int autoGenCode; + + public AuthenticationInfoModel() { + } + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } + + public String getUniqueNo() { + return uniqueNo; + } + + public void setUniqueNo(String uniqueNo) { + this.uniqueNo = uniqueNo; + } + + public int getAutoGenCode() { + return autoGenCode; + } + + public void setAutoGenCode(int autoGenCode) { + this.autoGenCode = autoGenCode; + } + +} diff --git a/app/src/main/java/com/example/two_fact_auth/MainActivity.java b/app/src/main/java/com/example/two_fact_auth/MainActivity.java deleted file mode 100644 index add0ed1..0000000 --- a/app/src/main/java/com/example/two_fact_auth/MainActivity.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.example.two_fact_auth; - -import androidx.appcompat.app.AppCompatActivity; - -import android.os.Bundle; - -public class MainActivity extends AppCompatActivity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate( savedInstanceState ); - setContentView( R.layout.activity_main ); - } -} \ No newline at end of file diff --git a/app/src/main/java/com/example/two_fact_auth/TwoFactorAuthentication.java b/app/src/main/java/com/example/two_fact_auth/TwoFactorAuthentication.java new file mode 100644 index 0000000..c1b05dc --- /dev/null +++ b/app/src/main/java/com/example/two_fact_auth/TwoFactorAuthentication.java @@ -0,0 +1,230 @@ +package com.example.two_fact_auth; + +import android.content.ClipData; +import android.content.ClipboardManager; +import android.content.Context; +import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.Point; +import android.os.Bundle; +import android.util.Log; +import android.view.Display; +import android.view.View; +import android.view.WindowManager; +import android.widget.RelativeLayout; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.AppCompatButton; +import androidx.appcompat.widget.AppCompatEditText; +import androidx.appcompat.widget.AppCompatImageView; +import androidx.appcompat.widget.AppCompatTextView; +import androidx.appcompat.widget.SwitchCompat; + +import com.google.android.gms.tasks.OnCompleteListener; +import com.google.android.gms.tasks.Task; +import com.google.firebase.database.DataSnapshot; +import com.google.firebase.database.DatabaseError; +import com.google.firebase.database.DatabaseReference; +import com.google.firebase.database.FirebaseDatabase; +import com.google.firebase.database.ValueEventListener; +import com.google.zxing.WriterException; + +import java.util.HashMap; +import java.util.Random; + +import androidmads.library.qrgenearator.QRGContents; +import androidmads.library.qrgenearator.QRGEncoder; + +public class TwoFactorAuthentication extends AppCompatActivity { + SwitchCompat switchCompat; + AppCompatButton btnGetOtp; + AppCompatEditText etPhoneNo; + AppCompatButton btnVerifyOtp; + AppCompatButton btnSave; + AppCompatEditText etOTP; + RelativeLayout layoutSecureCode; + AppCompatEditText etGenerateCode; + AppCompatTextView tvCopy; + ClipboardManager clipboardManager; + AppCompatImageView ivQrCode; + QRGEncoder qrgEncoder; + Bitmap bitmap; + String mStrUniqueKey; + String enableAuthentication = ""; + String yourKey; + // creating a variable for our + // Firebase Database. + FirebaseDatabase firebaseDatabase; + + // creating a variable for our Database + // Reference for Firebase. + DatabaseReference databaseReference; + + // creating a variable for + // our object class + AuthenticationInfoModel authenticationInfoClass; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate( savedInstanceState ); + setContentView( R.layout.activity_main ); +// switchCompat = findViewById( R.id.switch_authentication ); +// btnGetOtp = findViewById( R.id.btn_get_otp ); +// btnSave = findViewById( R.id.btn_save ); +// etPhoneNo = findViewById( R.id.et_phone_no ); +// btnVerifyOtp = findViewById( R.id.btn_otp_verification ); +// etOTP = findViewById( R.id.et_otp ); +// layoutSecureCode = findViewById( R.id.layout_security_code ); +// etGenerateCode = findViewById( R.id.et_unique_code ); +// tvCopy = findViewById( R.id.tv_copy ); +// ivQrCode = findViewById( R.id.iv_qr_code ); +// // below line is used to get the +// // instance of our FIrebase database. +// firebaseDatabase = FirebaseDatabase.getInstance(); +// +// // below line is used to get reference for our database. +// databaseReference = firebaseDatabase.getReference( "AuthenticationInfo" ); +// +// // initializing our object +// // class variable. +// authenticationInfoClass = new AuthenticationInfoModel(); +// sharedPreferenceEditor = new SharedPreferenceEditor(); +// enableAuthentication = sharedPreferenceEditor.getString( TwoFactorAuthenticationActivity.this, "authentication_enable" ); +// Log.e( "2FA", "onCreate: " + enableAuthentication ); +// yourKey = sharedPreferenceEditor.getString( TwoFactorAuthenticationActivity.this, "unique_code" ); +// Log.e( "2232", "onCreate2fa : " + enableAuthentication ); +// +// tvCopy.setOnClickListener( new View.OnClickListener() { +// @Override +// public void onClick(View view) { +// String text = etGenerateCode.getText().toString(); +// if (!text.isEmpty()) { +// clipboardManager = (ClipboardManager) getSystemService( Context.CLIPBOARD_SERVICE ); +// ClipData clipData = ClipData.newPlainText( "key", text ); +// clipboardManager.setPrimaryClip( clipData ); +// Toast.makeText( getApplicationContext(), "Copied", Toast.LENGTH_SHORT ).show(); +// } else { +// Toast.makeText( getApplicationContext(), "No text to be copied", Toast.LENGTH_SHORT ).show(); +// } +// } +// } ); +// +// btnSave.setOnClickListener( new View.OnClickListener() { +// @Override +// public void onClick(View view) { +// addDataToFirebase( 1, mStrUniqueKey, Integer.parseInt( getRandomNumber() ) ); +// } +// } ); + } + +// private static String getRandomNumber() { +// Log.e( "TAG", "getRandomNumber: " ); +// Random random = new Random(); +// int randomNumber = random.nextInt( 999999 - 100000 ); +// return String.format( "%06d", randomNumber ); +// } +// +// public static long generateRandom(int length) { +// Random random = new Random(); +// char[] digits = new char[length]; +// digits[0] = (char) (random.nextInt( 9 ) + '1'); +// for (int i = 1; i < length; i++) { +// digits[i] = (char) (random.nextInt( 10 ) + '0'); +// } +// return Long.parseLong( new String( digits ) ); +// } + + public Bitmap getQrCode(String uniqueKey) { + // below line is for getting + // the windowmanager service. + WindowManager manager = (WindowManager) getSystemService( WINDOW_SERVICE ); + + // initializing a variable for default display. + Display display = manager.getDefaultDisplay(); + + // creating a variable for point which + // is to be displayed in QR Code. + Point point = new Point(); + display.getSize( point ); + + // getting width and + // height of a point + int width = point.x; + int height = point.y; + + // generating dimension from width and height. + int dimen = width < height ? width : height; + dimen = dimen * 3 / 4; + + // setting this dimensions inside our qr code + // encoder to generate our qr code. + qrgEncoder = new QRGEncoder( uniqueKey, null, QRGContents.Type.TEXT, dimen ); + try { + // getting our qrcode in the form of bitmap. + bitmap = qrgEncoder.encodeAsBitmap(); + } catch (WriterException e) { + // this method is called for + // exception handling. + Log.e( "Tag", e.toString() ); + } + return bitmap; + } + + private void addDataToFirebase(int status, String code, int randomCode) { + // below 3 lines of code is used to set + // data in our object class. + authenticationInfoClass.setStatus( status ); + authenticationInfoClass.setAutoGenCode( randomCode ); + authenticationInfoClass.setUniqueNo( code ); + // we are use add value event listener method + // which is called with database reference. + databaseReference.addValueEventListener( new ValueEventListener() { + @Override + public void onDataChange(@NonNull DataSnapshot snapshot) { + databaseReference.child( code ).setValue( authenticationInfoClass ); + } + + @Override + public void onCancelled(@NonNull DatabaseError error) { + } + } ); + } +// +// public void updateDisableStatusInFirebase() { +// HashMap statusUpdate = new HashMap(); +// statusUpdate.put( "status", 0 ); +// databaseReference.child( yourKey ).updateChildren( statusUpdate ).addOnCompleteListener( new OnCompleteListener() { +// @Override +// public void onComplete(@NonNull Task task) { +// if (task.isSuccessful()) { +// Log.e( "2FA", "onComplete: " ); +// sharedPreferenceEditor.putString( TwoFactorAuthenticationActivity.this, "authentication_enable", "0" ); +// } else { +// Toast.makeText( TwoFactorAuthenticationActivity.this, "Fail to add data ", Toast.LENGTH_SHORT ).show(); +// } +// } +// } ); +// } +// +// public void updateEnableStatusInFirebase() { +// HashMap statusUpdate = new HashMap(); +// statusUpdate.put( "status", 1 ); +// databaseReference.child( yourKey ).updateChildren( statusUpdate ).addOnCompleteListener( new OnCompleteListener() { +// @Override +// public void onComplete(@NonNull Task task) { +// if (task.isSuccessful()) { +// Log.e( "2FA", "onComplete: " ); +// sharedPreferenceEditor.putString( TwoFactorAuthenticationActivity.this, "authentication_enable", "1" ); +// yourKey = sharedPreferenceEditor.getString( TwoFactorAuthenticationActivity.this, "unique_code" ); +// etGenerateCode.setVisibility( View.VISIBLE ); +// etGenerateCode.setText( yourKey ); +// getSecurityCode2(); +// } else { +// Toast.makeText( TwoFactorAuthenticationActivity.this, "Fail to add data ", Toast.LENGTH_SHORT ).show(); +// } +// } +// } ); +// } +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 9eb1ea6..240c20e 100644 --- a/build.gradle +++ b/build.gradle @@ -6,6 +6,7 @@ buildscript { } dependencies { classpath "com.android.tools.build:gradle:7.0.1" + classpath 'com.google.gms:google-services:4.3.10' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files