Search code examples
androidflutterkeycloakopenid-connect

Flutter OpenID Connect "Invalid paramter: redirect_uri" using Keycloak


I am building an App in Flutter and want to use Keycloak (quay.io/keycloak/keycloak:23.0.1) for my login. I set Keycloak up locally with docker and it works. Also, the login from the App with Keycloak works perfectly fine. But the Valid redirect URIs was * now I want to add a real URI behind it. I added the following line

    defaultConfig {
        ...
        manifestPlaceholders += [appAuthRedirectScheme: 'com.example.frontend']
    }

in App\frontend\android\app\build.gradle and in Keycloak I added enter image description here

in but I get the error message Invalid parameter: redirect_uri. I also tried to add a deepLink in the Manifest.xml located at App\frontend\android\app\src\main\AndroidManifest.xml

        <activity
            android:name=".MainActivity"
            ...

            <meta-data 
                android:name="flutter_deeplinking_enabled"
                android:value="true" />
            <intent-filter>
                <data
                    android:scheme="com.example.frontend"
                    android:host="frontend" />

                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

            </intent-filter>
        </activity>

but it still did not work for me.

My authentication code looks like this:

Future<Credential?> authenticate(
    Client client, {
    List<String> scopes = const [],
  }) async {
    try {
      var authenticator = io.Authenticator(
        client,
        scopes: scopes,
        port: 4000,
        urlLancher: _urlLauncher,
      );

      return await authenticator.authorize();
    } catch (e) {
      log("Authorize error: $e");
      return null;
    }
  }

For the authentication I use openid_client: ^0.4.8 and to launch Keycloak I use url_launcher: ^6.2.2.

I also searched on the internet and found some links but they did not work for me maybe I missed something.


Solution

  • I found the problem. The problem is that I did not specify the redirectUri in my authenticate function. With that it works fine.

    Future<Credential?> authenticate(
        Client client, {
        List<String> scopes = const [],
      }) async {
        try {
          var authenticator = io.Authenticator(
            client,
            scopes: scopes,
            port: 4000,
            urlLancher: _urlLauncher,
            redirectUri: Uri.parse('com.example.frontend'), // <--- added line
          );
    
          return await authenticator.authorize();
        } catch (e) {
          log("Authorize error: $e");
          return null;
        }
      }