-
Notifications
You must be signed in to change notification settings - Fork 1
Creating a custom implementation
For creating a custom implementation of our OMH Auth interfaces you'll need to get our OMH Auth API dependency:
implementation 'com.openmobilehub.android:auth-api:$version'Once you've downloaded the dependencies, it's time to extend each interface. Below you can find a step by step guide on how to do it.
As explained in the OMH Auth Client page, this will be the main interactor for the library. You will need to provide an implementation for each of the functionalities so that the developers can interact with the Auth provider you'll be implementing.
Here you'll have to implement two functions:
fun getLoginIntent(): Intent
@Throws(OmhAuthException::class)
fun getAccountFromIntent(data: Intent?): OmhUserProfileThe function getLoginIntent() should return an intent for you auth provider login screen. This will be expected to return some kind of a result which you'll have to parse with the getAccountFromIntent(data: Intent?) function to retrieve the login result and return the user profile or an exception in case the sign in fails or is canceled.
Here you only need to provide an implementation for this function:
fun getUser(): OmhUserProfile?The function should return the user data as a nullable OmhUserProfile object. This will help the devs identify if there was a previous login or not.
Here you'll have to provide two implementations:
fun signOut(): OmhTask<Unit>
fun revokeToken(): OmhTask<Unit>The functions signOut() and revokeToken() perform two variations of the signout functionality and you should connect them to your auth provider's respective features.
The only function you have to implement here is:
fun getCredentials(): Any?If your provider doesn't extend some type of credential object already that you'll need to interact with other libraries, then use the OMH Credentials class here. You can read more about it here
This will be the most important part of your implementation as this will be how we reflect your library with the provider. Save the pathn to this class, as we'll be using it further down the line. This class will be responsible of instantiating your OMH Auth Client implementation and returning it to the user as the interface OmhAuthClient. The only function to implement here is:
fun getAuthClient(context: Context, scopes: Collection<String>, clientId: String): OmhAuthClientYou'll have access to the application context in case you need it and the OAuth scopes that the user will be requesting.
Note: here are some examples of the path that you should be storing: com.example.app.factories.MyOwnFactoryImplementation
Here you'll need to provide a function and a parameter:
fun blockingRefreshToken(): String?
val accessToken: String?blockingRefreshToken should be a blocking sync call that requests a refresh of the access token from the provider. This is designed to be used in a Retrofit Interceptor class and should never be run from the main thread.
The accessToken variable should return the stored token received in the login use case.
This is an abstraction for the async layer of your library. The idea is to avoid forcing the user to use a specific async library and give the more flexibility with your OMH Auth implementation. You can read more about it here. Here the only function you need to implement is:
abstract fun execute(): OmhCancellable?This should execute your async code and return a way to cancel the operation with the OmhCancellable interface if possible. The cancellable interface can be represented as a lambda for convenience.
To use your newly created implementation with out plugin you just need to pass the reflection path and the dependency string in the Service section like this:
omhConfig {
bundle("gms") {
auth {
gmsService {
dependency = "com.example.app:custom-implementation:1.0"
path = "com.example.app.factories.MyOwnFactoryImplementation"
}
}
}
}If you are not using the core plugin the you can always pass the path manually to the provider like this:
val omhAuthProvider = OmhAuthProvider.Builder()
.addGmsPath(com.example.app.factories.MyOwnFactoryImplementation)
.build()Just don't forget to add your custom implementation as a dependency to the project.
You can always look into our own implementations of the OMH Auth API (GMS and non GMS) as a reference to help you develop your own implementation.