OMH Auth is an Android client library that makes it easy to integrate auth providers on all types of devices running different OS platforms. It eliminates the need for separate codebases for different app builds.
The OMH Auth Client Library simplifies authentication integration for app developers across devices, supporting differnet OS platforms. With a unified interface, it enables easy incorporation of Google Sign-in and other third-party authentication providers without maintaining separate codebases.
With the OMH Auth Client Library, you can easily add Google Sign in and other third-party authentication providers to your applications, regardless of the devices types or the OS platforms. The library takes care of the technical details, providing a unified interface and components for a consistent auth experience.
For instance, the following screenshots showcase multiple devices with Android, both with GMS and Non-GMS. The same app works without changing a single line of code, supporting multiple auth provider implementations.
This section describes how to setup an Android Studio project to use the OMH Auth SDK for Android. For greater ease, a base code will be used within the repository.
Note: To quickly run a full-featured app with all OMH Auth functionality, refer to the Sample App
section and follow the provided steps.
Go to the branch “starter-code”. The easiest way is cloning the repository.
Type git clone, and then paste the URL:
git clone --branch code-starter https://github.com/openmobilehub/omh-auth.git
You can always check what the final result should be in the module sample-app
in the main or
develop branches.
To access Google APIs, generate a unique client_id for your app in the Google API Console. Add the client_id to your app’s code and complete the required Cloud Console setup steps:
Update the debug/release SHA-1 certificate fingerprint for Android’s Client ID.
Note: The debug build is automatically signed with the debug keystore. Obtain the certificate fingerprint from it by following the guidelines in the official Google Developers documentation: “Using keytool on the certificate”.
You should not check your Client ID into your version control system, so it is recommended
storing it in the local.properties
file, which is located in the root directory of your project.
For more information about the local.properties
file,
see Gradle properties
files.
local.properties
in your project level directory, and then add the following code.
Replace YOUR_CLIENT_ID
with your API key.
CLIENT_ID=YOUR_CLIENT_ID
To incorporate OMH Auth into your project, you have two options: utilize the OMH Core Plugin or directly include the OMH Client libraries dependencies. This plugin simplifies the addition of Gradle dependencies, allowing you to effortlessly manage and include the necessary dependencies for seamless integration.
The subsequent instructions will outline the necessary steps for including the OMH Core Plugin as a Gradle dependency.
In your “auth-starter-sample” module-level build.gradle
under the plugins
element add the
plugin id.
plugins {
...
id("com.openmobilehub.android.omh-core")
}
Save the file and sync Project with Gradle Files.
In your auth-starter-sample
module-level build.gradle
file add the following code at the end of
the file.
omhConfig {
bundle("singleBuild") {
auth {
gmsService {
dependency = "com.openmobilehub.android:auth-api-gms:1.0.1-beta"
}
nonGmsService {
dependency = "com.openmobilehub.android:auth-api-non-gms:1.0.1-beta"
}
}
}
bundle("gms") {
auth {
gmsService {
dependency = "com.openmobilehub.android:auth-api-gms:1.0.1-beta"
}
}
}
bundle("nongms") {
auth {
nonGmsService {
dependency = "com.openmobilehub.android:auth-api-non-gms:1.0.1-beta"
}
}
}
}
NOTE: This section covers concepts about the core plugin
In your “auth-starter-sample” module-level build.gradle
file is required to configure
the omhConfig
. The omhConfig
definition is used to extend the existing Android Studio
variants in the core plugin. For more details omhConfig
see OMH Core.
In this step, you will define the OMH Core Plugin bundles to generate multiple build variants with specific suffixes as their names. For example, if your project has release
and debug
variants with singleBuild
, gms
, and nonGms
OMH bundles, the following build variants will be generated:
releaseSingleBuild
, releaseGms
, and releaseNonGms
debugSingleBuild
, debugGms
, and debugNonGms
- Define the `Service`. In this example is auth.
- Define the `ServiceDetails`. In this example are `gmsService` and `nonGmsService`.
- Define the dependency and the path. In this example
are `com.openmobilehub.android:auth-api-gms:1.0.1-beta"`
and `com.openmobilehub.android:auth-api-non-gms:1.0.1-beta`.
Note: It’s important to observe how a single build encompasses both GMS (Google MobileServices) and Non-GMS configurations.
- Define the `Service`. In this example is auth.
- Define the `ServiceDetails` . In this example is `gmsService`.
- Define the dependency and the path. In this example
is `com.openmobilehub.android:auth-api-gms:1.0.1-beta"`.
Note: gms build covers only GMS (Google Mobile Services).
- Define the `Service`. In this example is auth.
- Define the `ServiceDetails` . In this example is `nonGmsService`.
- Define the dependency and the path. In this example
is `com.openmobilehub.android:auth-api-non-gms:1.0.1-beta`.
Note: nongms build covers only Non-GMS configurations.
BuildConfig.AUTH_GMS_PATH
and BuildConfig.AUTH_NON_GMS_PATH
variables.:auth-starter-sample
:
In the SingletonModule.kt
file in the :auth-starter-sample
module add the following code to
provide the OMH Auth Client.
val omhAuthProvider = OmhAuthProvider.Builder()
.addNonGmsPath(BuildConfig.AUTH_NON_GMS_PATH)
.addGmsPath(BuildConfig.AUTH_GMS_PATH)
.build()
return omhAuthProvider.provideAuthClient(
scopes = listOf("openid", "email", "profile"),
clientId = BuildConfig.CLIENT_ID,
context = context
)
Note: we’d recommend to store the client as a singleton with your preferred dependency injection library as this will be your only gateway to the OMH Auth SDK and it doesn’t change in runtime at all.
First and foremost, the main interface that you’ll be interacting with is called OmhAuthClient
. In
contains all your basic authentication functionalities like login, sign out and check for a user
profile.
The snippet below shows how to check if there’s a signed in user already in your application. If no
one has logged in yet, then it will return a null value. A successful fetch will return an object of
the class OmhUserProfile
. In the MainActivity.kt
, add the following code to the selectStartDestination()
function:
omhAuthClient.getUser() == null
If no user is found, then we should request a login intent which will redirect the user to the
provider’s auth screen, be it the Google SignIn UI or a custom tab that redirects the user to
Google’s Auth page. This should be used to start an activity for result. In the LoginFragment.kt
,
add the following code to the handleLoginResult(result: ActivityResult)
:
try {
omhAuthClient.getAccountFromIntent(result.data)
navigateToLoggedIn()
} catch (exception: OmhAuthException) {
handleException(exception)
}
In the LoginFragment.kt
, add the following code to the startLogin
function:
// This will trigger the login flow.
val loginIntent = omhAuthClient.getLoginIntent()
loginLauncher.launch(loginIntent)
If the returned result
contains the account, then you can continue to the logged in activity of
your application.
To sign-out the SDK provides a straightforward functionality that returns an OmhTask
. This is the
interface to interact with async functionalities and subscribe to the success or error results. To
cancel a running OMH task a cancellable token is provided after the execute()
function is called.
This can be stored in the CancellableCollector
class similar to the CompositeDisposable
in
RxJava. The sign-out action will removes any and all relevant data to the user from the application
storage. In the LoggedInFragment.kt
, add the following code to the logout
function:
val cancellable = omhAuthClient.signOut()
.addOnSuccess { navigateToLogin() }
.addOnFailure(::showErrorDialog)
.execute()
cancellableCollector.addCancellable(cancellable)
Note: you can cancel all emitted cancellables within the collector running
cancellableCollector.clear()
The SDK also provides a way to revoke the access token provided to the application. This works
similar to the sign-out functionality but on top of clearing all local data, this also makes a
request to the auth provider to revoke the token from the server. In the LoggedInFragment.kt
,
add the following code to the revokeToken
function:
val cancellable = omhAuthClient.revokeToken()
.addOnFailure(::showErrorDialog)
.addOnSuccess { navigateToLogin() }
.execute()
cancellableCollector.addCancellable(cancellable)
This repository includes a auth-sample that demonstrates the functionality of the OMH Auth Client Library. By cloning the repo and executing the app, you can explore the various features offered by the library. However, if you prefer a step-by-step approach to learn the SDK from scratch, we recommend following the detailed Getting Started guide provided in this repository. The guide will walk you through the implementation process and help you integrate the OMH Auth Client Library into your projects effectively.
Note: Before running the sample application, make sure to follow the steps in Setup your Google Cloud project for application with Google Services to configure your Google Cloud project.
See example and check the full documentation and add custom implementation at our Wiki.
Additionally for more information about the OMH Auth functions, Docs.
OMH Auth SDK is open-source, promoting community collaboration and plugin support from other auth providers to enhance capabilities and expand supported auth services. More details can be found at the wiki.
Please contribute! We will gladly review any pull requests. Make sure to read the CONTRIBUTING page first though.
Copyright © Linux Foundation Europe. Open Mobile Hub is a project of Linux Foundation Europe.
For applicable policies including privacy policy, terms of use and trademark usage guidelines,
please see https://linuxfoundation.eu.
Linux is a registered trademark of Linus Torvalds.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.