capacitor-community / generic-oauth2

Generic Capacitor OAuth 2 client plugin. Stop the war in Ukraine!
MIT License
234 stars 115 forks source link

Azure B2C ui_locales is directly supported via the auth request builder exception #237

Open porojanmadalin opened 1 year ago

porojanmadalin commented 1 year ago

Description

When I try to access the Azure B2C login page with a specific language for the ui_locales parameter, the application throws the following error and the Sign in/Sign up flow continues but the Sign in/Sign Up user flow language falls back to the default language (en). This only happens on Android. I haven't tested on iOS yet but the web app works as expected. I have not found any post regarding this issue. I have no idea on how I could even debug this.

java.lang.IllegalArgumentException: Parameter ui_locales is directly supported via the authorization request builder, use the builder method instead
                                                                                                        at net.openid.appauth.Preconditions.checkArgument(Preconditions.java:132)
                                                                                                        at net.openid.appauth.AdditionalParamsProcessor.checkAdditionalParams(AdditionalParamsProcessor.java:62)
                                                                                                        at net.openid.appauth.AuthorizationRequest$Builder.setAdditionalParameters(AuthorizationRequest.java:957)
                                                                                                        at com.byteowls.capacitor.oauth2.OAuth2ClientPlugin.authenticate(OAuth2ClientPlugin.java:268)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138)
                                                                                                        at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:780)
                                                                                                        at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0)
                                                                                                        at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:942)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:201)
                                                                                                        at android.os.Looper.loop(Looper.java:288)
                                                                                                        at android.os.HandlerThread.run(HandlerThread.java:67)

Capacitor version:

Run npx cap doctor:

   Capacitor Doctor   

Latest Dependencies:

  @capacitor/cli: 5.2.3
  @capacitor/core: 5.2.3
  @capacitor/android: 5.2.3
  @capacitor/ios: 5.2.3

Installed Dependencies:

  @capacitor/cli: 5.0.5
  @capacitor/core: 5.0.5
  @capacitor/android: 5.0.5
  @capacitor/ios: 5.0.5

[success] Android looking great! 👌
[error] Xcode is not installed

Library version:

OAuth Provider:

Your Plugin Configuration

{
  appId: ENVIRONMENT.auth.clientId,
  scope: `${ENVIRONMENT.auth.clientId} ${ENVIRONMENT.auth.scopes}`, 
  responseType: 'code',
  authorizationBaseUrl: ENVIRONMENT.auth.authBaseUrl,
  accessTokenEndpoint: ENVIRONMENT.auth.tokenEndpoint,
  additionalParameters: { ui_locales: currentAppLang, },
  web: {
    redirectUrl: ENVIRONMENT.auth.webRedirectUrl,
    additionalParameters: { ui_locales: currentAppLang, },
  },
  android: {
    pkceEnabled: true,
    redirectUrl: ENVIRONMENT.auth.androidRedirectUrl,
    handleResultOnActivityResult: true,
    handleResultOnNewIntent: true,
    additionalParameters: { ui_locales: currentAppLang, },
  },
  ios: {
    pkceEnabled: true, 
    redirectUrl: ENVIRONMENT.auth.iosRedirectUrl,
    additionalParameters: { ui_locales: currentAppLang, },
  },
}

Affected Platform(s):

porojanmadalin commented 1 year ago

I think it is an issue from the AppAuth Android sdk. Changing the following lines fixed my problem, but these modifications do not persist on a new call of npm install image

porojanmadalin commented 1 year ago

Solved by using the following workaround on Android:

if (additionalParams) {
    if (Capacitor.getPlatform() === PLATFORM.android) {
      if (UI_LOCALES in additionalParams) {
        config.authorizationBaseUrl = `${config.authorizationBaseUrl}?ui_locales=${additionalParams.ui_locales}&`;
      }
    } else {
      config.additionalParameters = additionalParams;
    }
  }

Note: the trailing & is necessary, otherwise it won't work for me