Search code examples
cordovahttp-redirectoauth-2.0cordova-pluginsionic4

Ionic 4 oauth redirect connection refused


I am building an Android & iOS app using ionic/cordova: ionic version is : 4.11.0 & Cordova version is : 8.1.2

I created the app and tested in browser on chrome and the whole app worked fine including authorization, fetching data, etc.

My problems started when I created the android app to test. No matter what I do, I can't get the authorization workflow to redirect to the app.

Here are the things I tried:

1- callback "http/localhost" gives connection refused no matter what I tried in config.xml, cordova web view plugin, and cordova custom url scheme plugin.

2- callback "myapp://callback" gives err_unkown_url_schema" no matter what I tried modifying config.xml and using the same cordova plugins above.

I am testing on an LGG6 Android version 8.0.0. Didn't test on iOS yet.

Code snippet:

var uniqueWindowId = 'authorization-' + this.client_id
var params = 'titlebar=no, location=yes'
    var loginWindow = window.open(this.authCodeURL, uniqueWindowId, params);

    loginWindow.addEventListener(
      'message',
      e => {
        var oauthMessage = e.data;
        alert(oauthMessage);
        loginWindow.close();
        loginWindow.opener.postMessage(oauthMessage, 'https://localhost/');
      },
      false
    );

and here is the log from the device via Android Studio

Connecting to io.ionic.starter
Connected to the target VM, address: 'localhost:8600', transport: 'socket'
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/zygote64: Late-enabling -Xcheck:jni
W/ActivityThread: Application io.ionic.starter is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
I/zygote64: Debugger is active
I/System.out: Debugger has connected
    waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1419)
I/InstantRun: starting instant run server: is main process
I/CordovaLog: Changing log level to DEBUG(3)
I/CordovaActivity: Apache Cordova native platform version 7.1.4 is starting
D/CordovaActivity: CordovaActivity.onCreate()
I/WebViewFactory: Loading com.android.chrome version 72.0.3626.121 (code 362612152)
I/cr_LibraryLoader: Time to load native libraries: 7 ms (timestamps 8989-8996)
I/chromium: [INFO:library_loader_hooks.cc(42)] Chromium logging enabled: level = 0, default verbosity = 0
I/cr_LibraryLoader: Expected native library version number "72.0.3626.121", actual native library version number "72.0.3626.121"
W/cr_ChildProcLH: Create a new ChildConnectionAllocator with package name = com.android.chrome, sandboxed = true
I/cr_BrowserStartup: Initializing chromium process, singleProcess=false
W/ResourceType: Failure getting entry for 0x7f120538 (t=17 e=1336) (error -2147483647)
D/IonicWebViewEngine: Ionic Web View Engine Starting Right Up 1...
D/PhoneWindow: windowLightStatusBar : false, disable View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
D/SystemWebViewEngine: CordovaWebView is running on device made by: LGE
D/PluginManager: init()
D/PhoneWindow: windowLightStatusBar : false, disable View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
D/OpenGLRenderer: HWUI GL Pipeline
D/BezellessGripSuppressionFilter: getdisplaysize, x : 1440 y : 2880
D/PhoneWindow: windowLightStatusBar : false, disable View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
I/zygote64: Do partial code cache collection, code=28KB, data=25KB
I/zygote64: After code cache collection, code=27KB, data=25KB
    Increasing code cache capacity to 128KB
D/BezellessGripSuppressionFilter: getdisplaysize, x : 1440 y : 2880
D/CordovaWebViewImpl: >>> loadUrl(file:///android_asset/www/index.html)
I/AudioManagerEx: AudioManagerEx created
W/cr_media: Requires BLUETOOTH permission
D/CordovaActivity: Started the activity.
I/Adreno: QUALCOMM build                   : 14c0496, Ida6448821d
    Build Date                       : 01/08/18
    OpenGL ES Shader Compiler Version: EV031.22.00.01
    Local Branch                     : 
    Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.UM.6.5.R1.08.00.00.312.043
    Remote Branch                    : NONE
    Reconstruct Branch               : NOTHING
D/CordovaActivity: Resumed the activity.
I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8996.so from the current namespace instead.
D/BezellessGripSuppressionFilter: getdisplaysize, x : 1440 y : 2880
I/Adreno: PFP: 0x005ff087, ME: 0x005ff063
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 2
W/AudioCapabilities: Unsupported mime audio/x-lg-alac
W/AudioCapabilities: Unsupported mime audio/x-lg-flac
W/AudioCapabilities: Unsupported mime audio/adpcm
W/AudioCapabilities: Unsupported mime audio/g726
W/AudioCapabilities: Unsupported mime audio/lg-wma-voice
W/AudioCapabilities: Unsupported mime audio/x-lg-ms-wma
W/VideoCapabilities: Unsupported mime video/mjpg
W/VideoCapabilities: Unsupported mime video/theora
W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc
    Unrecognized profile 2130706434 for video/avc
I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8996.so from the current namespace instead.
W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc
    Unrecognized profile 2130706434 for video/avc
W/VideoCapabilities: Unrecognized profile 2130706433 for video/avc
W/VideoCapabilities: Unrecognized profile 2130706434 for video/avc
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@fdd775d time:801221046
W/VideoCapabilities: Unsupported mime video/dolby-vision
W/VideoCapabilities: Unsupported mime video/dolby-vision
W/VideoCapabilities: Unsupported mime video/mp4v-esdp
I/VideoCapabilities: Unsupported profile 4 for video/mp4v-es
W/cr_CrashFileManager: /data/user/0/io.ionic.starter/cache/WebView/Crash Reports does not exist or is not a directory
D/CordovaWebViewImpl: onPageDidNavigate(file:///android_asset/www/index.html)
D/CordovaWebViewImpl: onPageFinished(file:///android_asset/www/index.html)
D/SERVER: Handling local request: http://localhost/
D/CordovaWebViewImpl: onPageDidNavigate(http://localhost/)
D/SERVER: Handling local request: http://localhost/cordova.js
D/SERVER: Handling local request: http://localhost/build/main.css
D/SERVER: Handling local request: http://localhost/build/polyfills.js
D/SERVER: Handling local request: http://localhost/build/vendor.js
D/SERVER: Handling local request: http://localhost/build/main.js
D/JsMessageQueue: Set native->JS mode to EvalBridgeMode
D/SERVER: Handling local request: http://localhost/cordova_plugins.js
D/SERVER: Handling local request: http://localhost/plugins/cordova-plugin-device/www/device.js
D/SERVER: Handling local request: http://localhost/plugins/cordova-plugin-inappbrowser/www/inappbrowser.js
D/SERVER: Handling local request: http://localhost/plugins/cordova-plugin-ionic-keyboard/www/android/keyboard.js
D/SERVER: Handling local request: http://localhost/plugins/cordova-plugin-ionic-webview/src/www/util.js
D/SERVER: Handling local request: http://localhost/plugins/cordova-plugin-splashscreen/www/splashscreen.js
I/zygote64: Do partial code cache collection, code=61KB, data=56KB
    After code cache collection, code=61KB, data=56KB
    Increasing code cache capacity to 256KB
D/SERVER: Handling local request: http://localhost/plugins/cordova-plugin-statusbar/www/statusbar.js
D/SERVER: Handling local request: http://localhost/plugins/cordova-sqlite-storage/www/SQLitePlugin.js
D/SystemWebChromeClient: http://localhost/build/vendor.js: Line 4325 : Angular is running in the development mode. Call enableProdMode() to enable the production mode.
I/chromium: [INFO:CONSOLE(4325)] "Angular is running in the development mode. Call enableProdMode() to enable the production mode.", source: http://localhost/build/vendor.js (4325)
D/SystemWebChromeClient: http://localhost/build/main.js: Line 998 : ################################################################################ home.ts constructor
I/chromium: [INFO:CONSOLE(998)] "################################################################################ home.ts constructor", source: http://localhost/build/main.js (998)
D/SERVER: Handling local request: http://localhost/assets/icon/icon.png
D/SERVER: Handling local request: http://localhost/assets/fonts/ionicons.woff2?v=3.0.0-alpha.3
D/SERVER: Handling local request: http://localhost/assets/fonts/roboto-medium.woff2
D/SERVER: Handling local request: http://localhost/assets/fonts/roboto-regular.woff2
D/SystemWebChromeClient: http://localhost/build/vendor.js: Line 140284 : Ionic Native: deviceready event fired after 1716 ms
I/chromium: [INFO:CONSOLE(140284)] "Ionic Native: deviceready event fired after 1716 ms", source: http://localhost/build/vendor.js (140284)
I/App: WARNING: Back Button Default Behavior will be overridden.  The backbutton event will be fired!
D/CordovaWebViewImpl: onPageFinished(http://localhost/)
D/SERVER: Handling local request: http://localhost/assets/icon/favicon.ico
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/zygote64: Compiler allocated 6MB to compile void android.view.ViewRootImpl.performTraversals()
I/zygote64: Do full code cache collection, code=123KB, data=107KB
I/zygote64: After code cache collection, code=94KB, data=64KB
D/InAppBrowser: target = _self
D/InAppBrowser: in self
D/InAppBrowser: isUrlWhiteListed [class java.lang.String]
D/InAppBrowser: loading in InAppBrowser
D/PhoneWindow: windowLightStatusBar : false, disable View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
D/BezellessGripSuppressionFilter: getdisplaysize, x : 1440 y : 2880
I/zygote64: Do partial code cache collection, code=118KB, data=94KB
    After code cache collection, code=118KB, data=94KB
    Increasing code cache capacity to 512KB
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
I/AssistStructure: Flattened final assist data: 520 bytes, containing 1 windows, 2 views
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP

The message I get on the screen is:

the webpage at http://localhost/?code=......@state=..... could nopt be loaded becuase:

net::ERR_CONNECTION_REFUSED

Solution

  • I would suggest using the inappbrowser ionic native plugin in the case that you are doing oauth through a url external to the app.

    https://ionicframework.com/docs/native/in-app-browser/

    Have you tried that? In general this method of using the inapp browser as oauth is becoming an anti-pattern, moving forward with Ionic Capacitor, instead of using the inappbrowser they have implemented a solution around SafariViewController for iOS and Chrome Custom Tabs for Android, but this is still very much in beta in my opinion.

    https://capacitor.ionicframework.com/docs/apis/browser/

    Here is a somewhat dated article on OAuth with the inappbrowser, but it does well at covering the basic use of the plugin:

    https://www.thepolyglotdeveloper.com/2016/01/using-an-oauth-2-0-service-within-an-ionic-2-mobile-app/

    An article on why it is becoming an anti-pattern: https://medium.com/@jlchereau/stop-using-inappbrowser-for-your-cordova-phonegap-oauth-flow-a806b61a2dc5

    In terms of your specific issue I would check the console logs as well, and be sure that it is not a CORS issue, also check your whitelisting for navigation in your config.xml.