Skip to content

Instantly share code, notes, and snippets.

View whytarun's full-sized avatar

Tarun Yekbote whytarun

View GitHub Profile
@whytarun
whytarun / secureCodingPractice.java
Last active October 3, 2023 05:35
Secure Coding Practices
# Bad practice: Hardcoded UserName and Password
String username ="admin"
String password ="p@ssw0rd"
# Good practice: Retrive credential from a secure source
String username = CredentialManager.getUsername()
String password =CredentialManager.getPassword()
@whytarun
whytarun / NetworkModule.kt
Last active October 8, 2023 13:52
Including support for TLS/SSL
@Singleton
@Provides
fun provideOkhttpClient(authInterceptor: AuthInterceptor,
certificateHelper: CertificateHelper
) :OkHttpClient{
val trustManagers = certificateHelper.createTrustManagers()
val sslContext = SSLContext.getInstance("TLS")
sslContext.init(null, trustManagers, null)
return OkHttpClient().newBuilder()
.readTimeout(2, TimeUnit.MINUTES)
@whytarun
whytarun / FirebaseAuth.kt
Created October 3, 2023 06:09
Implementing Firebase Authentication for user authentication.
FirebaseAuth mAuth = FirebaseAuth.getInstance();
// Sign in with email and password
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// User is authenticated
} else {
@whytarun
whytarun / build.gradle
Last active October 3, 2023 06:53
Employ ProGuard to obfuscate
buildTypes {
release {
buildConfigField "String", "BASE_URL", "\"https://mbl.test.com\""
minifyEnabled true
shrinkResources true
debuggable false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),
'proguard-rules.pro'
}
@whytarun
whytarun / CertificateHelper.kt
Created October 3, 2023 06:46
SSL Support
class CertificateHelper @Inject constructor(@ApplicationContext context: Context) {
private val applicationContext = context
fun createTrustManagers(): Array<TrustManager> {
val certificateInputStream = applicationContext.resources.openRawResource(
R.raw.test)
val certificateFactory = CertificateFactory.getInstance("X.509")
val certificate = certificateFactory.generateCertificate(certificateInputStream)
val trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm())
val keyStore = KeyStore.getInstance(KeyStore.getDefaultType())
if (!usbManager!!.hasPermission(usbDevice)) {
usbManager!!.requestPermission(usbDevice, mPendingIntent)
}