desktopoauth2 1.0.4 copy "desktopoauth2: ^1.0.4" to clipboard
desktopoauth2: ^1.0.4 copied to clipboard

OAuth2.0 authentication for desktop apps

example/lib/main.dart

import 'package:desktopoauth2/desktopoauth2.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const DeskOAuth2ExampleApp());
}

class DeskOAuth2ExampleApp extends StatelessWidget {
  const DeskOAuth2ExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter OAuth2 Desktop Sample',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const OauthPage(),
      debugShowCheckedModeBanner: false,
    );
  }
}


class OauthPage extends StatefulWidget {
  const OauthPage({Key? key}) : super(key: key);

  @override
  // ignore: library_private_types_in_public_api
  _OauthState createState() => _OauthState();
}

class _OauthState extends State<OauthPage> {
  String? accessToken;
  @override
  Widget build(BuildContext context) {
    return Container(
        color: Colors.grey.shade100,
        child: Center(
          child: Column(children: [
            Container(
                color: Colors.grey.shade100,
                height: 50,
                width: 130,
                child: ElevatedButton(
                  style: ButtonStyle(
                    elevation: MaterialStateProperty.resolveWith<double?>((Set<MaterialState> states) {
                      if (states.contains(MaterialState.pressed)) return 16;
                      return null;
                    }),
                  ),
                  onPressed: () async {
                  
                    // Optional Feature
                   Map<String,String> additionalParameters = {};
                   additionalParameters['companyId'] = "company_name";
                   additionalParameters['tenantId'] = "tenant_name";
                   

                    DesktopOAuth2 desktopOAuth2 = DesktopOAuth2();

                    DesktopAuthorizationCodeFlow desktopAuthCodeFlow = DesktopAuthorizationCodeFlow();

                    desktopAuthCodeFlow.authState = 'xcoivjuywkdkhvusuye3kch';
                    desktopAuthCodeFlow.authorizationUrl = 'https://dev-7932731.okta.com/oauth2/aus12p5r7eLEams5g5d7/v1/authorize';
                    desktopAuthCodeFlow.clientId = '0oa7v0y4zkpnSMR175d7';
                    desktopAuthCodeFlow.localPort = 9298;
                    desktopAuthCodeFlow.pkce = true;
                    desktopAuthCodeFlow.redirectUri = 'http://localhost:9298/code';
                    desktopAuthCodeFlow.scopes = ['openid'];
                    desktopAuthCodeFlow.tokenUrl = 'https://dev-7932731.okta.com/oauth2/aus12p5r7eLEams5g5d7/v1/token';
                    //Optional Parameter
                    desktopAuthCodeFlow.additionalParameters = additionalParameters;

                    // We can use clientScrect instead of PKCE feature. You disable PKCE and put client screct as follows
                    //desktopAuthCodeFlow.pkce = false;
                    //desktopAuthCodeFlow.clientSecret = "x-value";

                    desktopOAuth2.oauthorizeCode(desktopAuthCodeFlow).then((token) {
                      if (token != null && token.isNotEmpty) {
                        setState(() {
                          accessToken = token["access_token"];
                        });
                      }
                    });

                  
                  },
                  child: const Text('Authenticate'),
                )),
            Text(
              accessToken ?? '',
              style: const TextStyle(fontSize: 15, fontFamily: "poppins"),
            )
          ]),
        ));
  }
}
5
likes
30
points
34
downloads

Publisher

unverified uploader

Weekly Downloads

OAuth2.0 authentication for desktop apps

Repository (GitHub)

Topics

#desktop-oauth2 #oauth2-pkce

License

MIT (license)

Dependencies

flutter, http, pkce, url_launcher, window_to_front

More

Packages that depend on desktopoauth2