How to logout from SAML authentication?

268 Views Asked by At

I am using a SAML link for authentication.

and here is my code implementation:


class LoginWebView extends StatefulWidget {
  const LoginWebView({super.key});

  @override
  State<LoginWebView> createState() => _LoginWebViewState();
}

class _LoginWebViewState extends State<LoginWebView> {
  final tokenManager = TokenManager();
  InAppWebViewController? inAppWebViewController;
  bool isShowingError = false;
  late var url;
  var initialUrl =
      "https://accounts.google.com/o/saml2/initsso?idpid=C02qtgmcd&spid=858410514048&forceauthn=false";

  //late WebViewController controller;
  String? token;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Login"),
      ),
      body: Column(
        children: [
          Expanded(
            child: InAppWebView(
              initialUrlRequest: URLRequest(url: Uri.parse(initialUrl)),
              onWebViewCreated: (controller) async {
                inAppWebViewController = controller;
              },
              onProgressChanged: onWebViewProgressChanged,
            ),
          ),
        ],
      ),
    );
  }
}

But I am unable to logout the user.

Can anyone please tell me how to logout from SAML based authentication?

1

There are 1 best solutions below

3
Pawneshwer Gupta On

You need to follow some steps to logout user

  1. When a user clicks the logout button, your app should send a SAML Logout Request to the IdP. This request tells the IdP that the user wants to log out.
  2. Now IdP will give you response based on your request, handle it.
  3. Now verify its authenticity.
  4. after successful verification, clear local session of user.
  5. Now redirect user to your further screen.

here is sample code.

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

void initiateSamlLogout() async {
  // 1. Send Logout Request to IdP
  final idpLogoutUrl = 'https://idp.example.com/logout'; // Replace with your IdP's logout URL
  final response = await http.get(Uri.parse(idpLogoutUrl));

  // 2. Handle Logout Response
  if (response.statusCode == 200) {
    // 3. Invalidate the Local Session
    // Clear user session data, tokens, etc.

    // 4. Redirect or Notify the User
    // Redirect or show a confirmation message.
  } else {
    // Handle error in receiving Logout Response.
  }
}