GithubHelp home page GithubHelp logo

lcpinto / casdoor-flutter-sdk Goto Github PK

View Code? Open in Web Editor NEW

This project forked from casdoor/casdoor-flutter-sdk

0.0 0.0 0.0 4.44 MB

Flutter / Dart client SDK for Casdoor (for Flutter & Dart frontend, for backend see: https://github.com/casdoor/casdoor-dart-sdk)

Home Page: https://github.com/casdoor/casdoor

License: Apache License 2.0

Ruby 3.78% Objective-C 4.60% Kotlin 12.79% Dart 49.14% Swift 23.19% HTML 6.49%

casdoor-flutter-sdk's Introduction

Casdoor Flutter SDK

Pub.dev likes Pub.dev points latest version Platform License

casdoor-flutter-sdk will allow you to easily connect your Flutter-based application to the Casdoor authentication system without having to implement it from scratch.

The following platforms are supported:

  • Android
  • iOS
  • macOS
  • Web
Android iOS Web
Android iOS Web

Features

Use this plugin in your Flutter app to:

  • Connect to casdoor for SSO
  • Get the token after the casdoor authentication

Usage

This section has examples of code for the following tasks:

Initialization requires 6 parameters

Initialization requires 6 parameters, which are all str type:

Name (in order) Must Description
clientId Yes Application.client_id
endpoint Yes Casdoor Server Url, such as door.casdoor.com
organizationName Yes Organization name
appName Yes Application name
redirectUri Yes URI of Web redirection
callbackUrlScheme Yes URL Scheme
  final CasdoorFlutterSdkConfig _config =  CasdoorFlutterSdkConfig(
      clientId: "014ae4bd048734ca2dea",
      endpoint: "door.casdoor.com",
      organizationName: "casbin",
      appName: "app-casnode",
      redirectUri: "http://localhost:9000/callback",
      callbackUrlScheme: "casdoor"
  );

Judgment platform

Set the callbackuri parameter by judging different platforms

 final platform = await CasdoorFlutterSdkPlatform.instance.getPlatformVersion() ?? "";
    String callbackUri;
    if (platform == "web") {
       callbackUri = "${_config.redirectUri}.html";
    } else {
       callbackUri = "${_config.callbackUrlScheme}://callback" ;
    }

Authorize with the Casdoor server

At this point, we should use some ways to verify with the Casdoor server.

To start, we want you understand clearly the verification process of Casdoor. The following paragraphs will mention your app that wants to use Casdoor as a means of verification as APP, and Casdoor as Casdoor.

  1. APP will send a request to Casdoor. Since Casdoor is a UI-based OAuth provider, you cannot use request management service like Postman to send a URL with parameters and get back a JSON file.

  2. The simplest way to try it out is to type the URL in your browser.

  3. Type in the URL in your browser in this format: endpoint/login/oauth/authorize?client_id=xxx&response_type=code&redirect_uri=xxx&scope=read&state=xxx In this URL the endpoint is your Casdoor's location, as mentioned in Step1; then the xxx need to be filled out by yourself.

Get token and parse

After Casdoor verification passed, it will be redirected to your application with code and state, like https://localhost:9000/callback?code=xxx&state=yyyy.

Your application can get the code and call _casdoor.requestOauthAccessToken(code), then parse out jwt token.

Getting Started

Add casdoor-flutter-sdk to the dependencies of your pubspec.yaml.

dependencies:
  casdoor_flutter_sdk: ^1.0.0

Note here that for Android and Web

Android

In order to capture the callback url, the following activity needs to be added to your AndroidManifest.xml. Be sure to relpace YOUR_CALLBACK_URL_SCHEME_HERE with your actual callback url scheme.

 <activity android:name="com.example.casdoor_flutter_sdk.CallbackActivity"
           android:exported="true">
           <intent-filter android:label="casdoor_flutter_sdk">
               <action android:name="android.intent.action.VIEW" />
               <category android:name="android.intent.category.DEFAULT" />
               <category android:name="android.intent.category.BROWSABLE" />
               <data android:scheme="casdoor" />
           </intent-filter>
       </activity>

Web

On the Web platform an endpoint needs to be created that captures the callback URL and sends it to the application using the JavaScript postMessage() method. In the ./web folder of the project, create an HTML file with the name e.g. callback.html with content:

<!DOCTYPE html>
<title>Authentication complete</title>
<p>Authentication is complete. If this does not happen automatically, please
close the window.
<script>
  window.opener.postMessage({
    'casdoor-auth': window.location.href
  }, window.location.origin);
  window.close();
</script>

Redirection URL passed to the authentication service must be the same as the URL on which the application is running (schema, host, port if necessary) and the path must point to created HTML file, /callback.html in this case, like callbackUri = "${_config.redirectUri}.html". The callbackUrlScheme parameter of the authenticate() method does not take into account, so it is possible to use a schema for native platforms in the code.It should be noted that when obtaining a token, cross domain may occur

For the Sign in with Apple in web_message response mode, postMessage from https://appleid.apple.com is also captured, and the authorization object is returned as a URL fragment encoded as a query string (for compatibility with other providers).

API reference interface

Get sign up url

getSignupUrl(enablePassword)

Get sign in url

getSigninUrl()

Get code

show()

Get token

requestOauthAccessToken()

Refresh token

refreshToken()

Log out

tokenLogout()

Get user information

getUserInfo()

Decode token

decodedToken()

Judge whether the token is expired

isTokenExpired()

Verify nonce

isNonce()

Example

See at: https://github.com/casdoor/casdoor-flutter-example

casdoor-flutter-sdk's People

Contributors

ziliangyu avatar semantic-release-bot avatar hsluoyz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.