ERPNext.com Frappe Cloud Support Partners Foundation Frappe School

[How To] Developing Apps for Android

Develop Android Apps using Frappe Authenticator

Prerequisites:

Add OAuth Client on frappe server
Android - using Volley for jsonrequest (ready ApplicationController, using FrappeServerCallback to get response, ERPNextContactProvider endpoints example UTSL :smile:)

Sample App: https://github.com/revant/FrappeAuthExample

Check if Frappe Authenticator is installed.

If one account found, select it, get token and use it

if multiple accounts found, ask to select one.

Use token as header in API to interact with frappe REST endpoints.

Frappe Authenticator will seamlessly take care of :

  1. storing OAuth 2 bearer token - access_token / refresh_token
  2. when getAuthToken is called it checks if access_token is valid
  3. if access_token has expired it renews bearer_token, stores it and returns new access token
  4. if refresh_token paired with access_token is revoked/deleted, ask user to login again.
9 Likes

Example repository covers

Notifications and BroadcastReceiver,
REST API GET/POST/PUT calls, using Authenticator.
Navigation Drawer has accounts listing.

5 Likes

OAuth 2 Account Authenticator Library

1 Like

Hi @revant_one

Great job! I’ve been hoping to see projects that help with integration of the Frappe framework with native mobile apps. This looks to be a good step in the right direction

Thanks a lot

Cheers!

1 Like

Hi @revant_one

Trust you’re doing great. On the issue of Mobile App Development, I’m not sure if you’re familiar with the Liferay Screens project. It’s actually a collection of fully native mobile components for rapid native app development. It’s obviously designed for the Liferay Portal but one of it’s key features is that it works with other backends too

I think it would be awesome if we could have something like this for the Frappe framework. This would cut the time and cost required to develop native mobile apps for Frappe (and ERPNext) significantly. What do you think? References:

https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/android-apps-with-liferay-screens

https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/adding-custom-interactors-to-android-screenlets

Kind regards,

1 Like

Use Case :

  1. Mobile App logs into frappe for consuming REST API
  2. App user is not expected to remember passwords. Password won’t be asked. Only the OTP will be verified and bearer token will be handed.
  3. Mobile number is username

Server Side Frappe App :

Basic endpoints for non-standard otp generation and authentication.

NOTE: this is not standard TOTP mentioned here https://tools.ietf.org/html/rfc6238. This was developed specifically because enter password step needed to be dropped

Standards based OTP (pyotp) generation and 2FA is being contributed and is recommended.

Mobile OTP Authenticator Library for Android

This is Kotlin based library, can be imported in Java app.

XMLs designed are very basic, after importing the library override them with your own cool designs and keep the names same in your app.

It uses SmsVerifyCatcher to read the SMS. Login screen copies OTP into OTP input and proceeds automatically once SMS is received.

Everthing else is same as OAuth2Authenticator

2 Likes

@revant_one Is this enable google login in Android app if we enable frappe oauth?

tldr; No, you can only login to any other app with frappe credentials.

As a part of the flow it shows the /login page of your server.

So it will show all the social logins enabled on /login page.

but I don’t think following sequence will work.

user lands on /login > user clicks login with google > logs in to desk.

because we need following flow for bearer token

user lands on /login > user logs in with frappe account > redirect with auth code back to redirect uri.

If user first logins into frappe with google account and then sets the password for the frappe user. It will work with 2nd flow.

1 Like

Is this android mobile app available on the android store or any other similar apps? How do I access the app in order to view the app functionality which exists? Appreciate if you can direct me on how it requires to be installed?

These are source code references and libraries. Use them to develop android apps. If you’ve android studio clone the source, build the apk and try them out.

1 Like

Thanks. Is it correct to say accessing an android /ios app which connects to ErpNext does not add to the existing count of erpnext user licences?

Are there any android or ios apps on the Google or Apple store I can check out which have made use of the frappe authenticator? Alternatively, something I can install outside of the stores.

Thanks

download one of the sync .apk and test.

add oauth client on frappe server
enter the oauth client data on android app
to sync you need to go to apps > frappe authenticator on android phone and enable contacts and event permission

2 Likes

Yes. You can login to the app as any of your existing ERPNext users.

1 Like

Some great Kotlin tutorials to learn how to make Android apps.

1 Like

Hi revant_one,

I’m sure FrappeAuthenticator#getAuthToken handles valid/invalid/refresh token and It’s attached to the Service.
Do I need to manage/call it manually? or It’s handled by AccountManager automatically?

Thanks.

Whenever you need the valid token to make calls use am.getAuthtoken(...)
It will either

  • give you unexpired token
  • if token is expired it’ll refresh it and give unexpired token
  • if token is revoked it’ll show Notification and ask you to sign in again.
2 Likes

Get access token but expired and FrappeAuthenticator#getAuthToken never called

AccountManager am = AccountManager().get(context);
final AccountManagerFuture future = am.getAuthToken(account, AUTHTOKEN_TYPE_FULL_ACCESS, null, false, null,null);

FrappeAuthenticator#getAuthToken called and work as expected.

FrappeAuthenticator frappeAuthenticator = new FrappeAuthenticator(context);
Bundle future = frappeAuthenticator.getAuthToken(null,account, AUTHTOKEN_TYPE_FULL_ACCESS,null);

Am I missing something or confusing how the code work?

In manifest.xml

<service
        android:name="PACKAGE_NAME.authenticator.FrappeAuthenticatorService"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.accounts.AccountAuthenticator" />
        </intent-filter>

        <meta-data
            android:name="android.accounts.AccountAuthenticator"
            android:resource="@xml/authenticator" /> 
    </service>

I have to manage invalid token manually as code below, otherwise FrappeAuthenticator#getAuthToken not call.

am.invalidateAuthToken(accounType,authToken);

Oh! I think you’re not using the library?

Refer this. It’s way easier!!

Easy jitpack import

https://jitpack.io/#mntechnique/OAuth2Authenticator

1 Like

It’s a bit weird for me that your FrappeAuthenticator look easier than OAuth2Authenticator :wink:
I learn a lot from it. I’m trying to make it works, otherwise I’ll look into OAuth2Authenticator.