simple_auth icon indicating copy to clipboard operation
simple_auth copied to clipboard

Able to authenticate with Keycloak, unable to make further authenticated requests

Open mtchllbrrn opened this issue 5 years ago • 1 comments

I'm putting together a simple example app with Keycloak authentication. I can .authenticate() successfully with my KeycloakApi class below, but I can't get further authenticated requests to work. Inspecting the error object in the debugger shows statusCode: 400. I suppose I might be misusing this library in some way, but I can't tell for myself without more documentation.

keycloakApi.dart:

import 'package:simple_auth/simple_auth.dart';
import 'package:http/http.dart' as http;

class KeycloakApi extends OAuthApi {
  KeycloakApi(String identifier, String clientId, String clientSecret, String redirectUrl,
      {
        List<String> scopes,
        http.Client client,
        Converter converter,
        AuthStorage authStorage
      }
  ): super(
    identifier,
    clientId,
    clientSecret,
    "https://auth.env-dev3.vivantehealth.org/auth/realms/vivante/protocol/openid-connect/token",
    "https://auth.env-dev3.vivantehealth.org/auth/realms/vivante/protocol/openid-connect/auth",
    redirectUrl,
    client: client,
    scopes: scopes,
    converter: converter,
    authStorage: authStorage
  ) {
    // this.scopesRequired = false;
  }
}

Example usage:

void doRequest() async {
  final KeycloakApi keycloakApi = new KeycloakApi(
    "keycloak",
    "myClientId",
    "myClientSecret",
    "com.example.myApp:/",
    scopes: ['openid', 'profile']
  );

  var request = new Request(HttpMethod.Get, "https://myKeycloakAuthedApi.com/");
  try {
    var res = await keycloakApi.send(request);
    print('Success');
  } catch(e) {
    debugger();  // e.statusCode == 400
  }
}

mtchllbrrn avatar Jun 28 '19 17:06 mtchllbrrn

Try something like this. Also check if you get your token. You can get it by .currentOauthAccount.token

  final response = await http.get(
            "http://<URL>//auth/realms/<realm-name>/protocol/openid-connect/userinfo",
            headers: <your-jwt-token> );

divjakLab avatar Jul 08 '19 06:07 divjakLab