identity_server 0.0.6

  • Readme
  • Changelog
  • Example
  • Installing
  • 46

identity_server #

autenticaĆ§Ć£o por servidor de identidade com oauth2 e webview

Getting Started #

This project is a starting point for a Flutter plug-in package, a specialized package that includes platform-specific implementation code for Android and/or iOS.

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

0.0.6 #

  • ios config

0.0.5 #

  • ready to use

0.0.4 #

  • refactoring.

0.0.3 #

*TODO: refactoring.

0.0.2 #

  • Add arquitecture.

0.0.1 #

*Add release date.

  • TODO: a flutter plugin that helps auth2 authentication, we follow the identity server documentation. With access token refresh token search methods surrounding excellent code performance and good architecture for future maintenance, it is noteworthy that the library is in the process of developing a lot of functionality yet to be improved.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:identity_server/identity_server.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _isBusy = false;
  FlutterAppAuth _appAuth = FlutterAppAuth();
  String _codeVerifier;
  String _authorizationCode;
  String _refreshToken;
  String _accessToken;
  TextEditingController _authorizationCodeTextController =
      TextEditingController();
  TextEditingController _accessTokenTextController = TextEditingController();
  TextEditingController _accessTokenExpirationTextController =
      TextEditingController();

  TextEditingController _idTokenTextController = TextEditingController();
  TextEditingController _refreshTokenTextController = TextEditingController();
  String _userInfo = '';

  String _clientId = 'native.code';
  String _redirectUrl = 'io.identityserver.demo:/oauthredirect';

  //String _issuer = 'https://demo.identityserver.io';
  String _discoveryUrl =
      'https://demo.identityserver.io/.well-known/openid-configuration';
  List<String> _scopes = [
    'openid',
    'profile',
    'email',
    'offline_access',
    'api'
  ];

  AuthorizationServiceConfiguration _serviceConfiguration =
      AuthorizationServiceConfiguration(
          'https://demo.identityserver.io/connect/authorize',
          'https://demo.identityserver.io/connect/token');

  @override
  void initState() {
    super.initState();
  }

  Future<void> _refresh() async {
    setBusyState();
    var result = await _appAuth.token(TokenRequest(_clientId, _redirectUrl,
        refreshToken: _refreshToken,
        discoveryUrl: _discoveryUrl,
        scopes: _scopes));
    _processTokenResponse(result);
    await _testApi(result);
  }

  Future<void> _exchangeCode() async {
    setBusyState();
    var result = await _appAuth.token(TokenRequest(_clientId, _redirectUrl,
        authorizationCode: _authorizationCode,
        discoveryUrl: _discoveryUrl,
        codeVerifier: _codeVerifier,
        scopes: _scopes));
    _processTokenResponse(result);
    await _testApi(result);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              Visibility(
                visible: _isBusy,
                child: LinearProgressIndicator(),
              ),
              RaisedButton(
                child: Text('Sign in with no code exchange'),
                onPressed: () async {
                  setBusyState();
                  // use the discovery endpoint to find the configuration
                  var result = await _appAuth.authorize(
                    AuthorizationRequest(_clientId, _redirectUrl,
                        discoveryUrl: _discoveryUrl,
                        scopes: _scopes,
                        loginHint: 'bob'),
                  );

                  // or just use the issuer
                  // var result = await _appAuth.authorize(
                  //   AuthorizationRequest(
                  //     _clientId,
                  //     _redirectUrl,
                  //     issuer: _issuer,
                  //     scopes: _scopes,
                  //   ),
                  // );
                  if (result != null) {
                    _processAuthResponse(result);
                  }
                },
              ),
              RaisedButton(
                child: Text('Exchange code'),
                onPressed: _authorizationCode != null ? _exchangeCode : null,
              ),
              RaisedButton(
                child: Text('login'),
                onPressed: () async {
                  setBusyState();

                  // show that we can also explicitly specify the endpoints rather than getting from the details from the discovery document
                  var initialOptions;
                  var initialData;
                  var issuer;
                  List<String> initialHeaders;
                  bool allowInsecureConnections;
                  var result = await _appAuth.authorizeAndExchangeCode(
                    AuthorizationTokenRequest(_clientId, _redirectUrl,
                        serviceConfiguration: _serviceConfiguration,
                        scopes: _scopes,
                        promptValues: ['login']),
                  );
                  // this code bs passing in values for the prompt parameter. in this case it promptslock demonstrate the user
                  // even if theytity provider have already signed in. the list of supported values depends on the iden
                  //                  // var result = await _appAuth.authorizeAndExchangeCode(
                  //   AuthorizationTokenRequest(_clientId, _redirectUrl,
                  //       serviceConfiguration: _serviceConfiguration,
                  //       scopes: _scopes,
                  //       promptValues: ['login']),
                  // );

                  if (result != null) {
                    _processAuthTokenResponse(result);
                    await _testApi(result);
                  }
                },
              ),
              RaisedButton(
                child: Text('Refresh token'),
                onPressed: _refreshToken != null ? _refresh : null,
              ),
              Text('authorization code'),
              TextField(
                controller: _authorizationCodeTextController,
              ),
              Text('access token'),
              TextField(
                controller: _accessTokenTextController,
              ),
              Text('access token expiration'),
              TextField(
                controller: _accessTokenExpirationTextController,
              ),
              Text('id token'),
              TextField(
                controller: _idTokenTextController,
              ),
              Text('refresh token'),
              TextField(
                controller: _refreshTokenTextController,
              ),
              Text('test api results'),
              Text(_userInfo),
            ],
          ),
        ),
      ),
    );
  }

  void setBusyState() {
    setState(() {
      _isBusy = true;
    });
  }

  void _processAuthTokenResponse(AuthorizationTokenResponse response) {
    setState(() {
      _accessToken = _accessTokenTextController.text = response.accessToken;
      _idTokenTextController.text = response.idToken;
      _refreshToken = _refreshTokenTextController.text = response.refreshToken;
      _accessTokenExpirationTextController.text =
          response.accessTokenExpirationDateTime?.toIso8601String();
    });
  }

  void _processAuthResponse(AuthorizationResponse response) {
    setState(() {
      // save the code verifier as it must be used when exchanging the token
      _codeVerifier = response.codeVerifier;
      _authorizationCode =
          _authorizationCodeTextController.text = response.authorizationCode;
      _isBusy = false;
    });
  }

  void _processTokenResponse(TokenResponse response) {
    setState(() {
      _accessToken = _accessTokenTextController.text = response.accessToken;
      _idTokenTextController.text = response.idToken;
      _refreshToken = _refreshTokenTextController.text = response.refreshToken;
      _accessTokenExpirationTextController.text =
          response.accessTokenExpirationDateTime?.toIso8601String();
    });
  }

  Future<void> _testApi(TokenResponse response) async {
    var httpResponse = await http.get('https://demo.identityserver.io/api/test',
        headers: {'Authorization': 'Bearer $_accessToken'});
    setState(() {
      _userInfo = httpResponse.statusCode == 200 ? httpResponse.body : '';
      _isBusy = false;
    });
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  identity_server: ^0.0.6

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:identity_server/identity_server.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
46
Learn more about scoring.

We analyzed this package on Apr 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health suggestions

Fix lib/src/flutter_appauth.dart. (-0.50 points)

Analysis of lib/src/flutter_appauth.dart reported 1 hint:

line 2 col 8: Unused import: 'package:identity_server/src/webview/AppWebView.dart'.

Format lib/identity_server.dart.

Run flutter format to format lib/identity_server.dart.

Format lib/src/webview/AppWebView.dart.

Run flutter format to format lib/src/webview/AppWebView.dart.

Format lib/src/webview/WebViewRequestDetails.dart.

Run flutter format to format lib/src/webview/WebViewRequestDetails.dart.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

The description is too long. (-10 points)

Search engines display only the first part of the description. Try to keep the value of the description field in your package's pubspec.yaml file between 60 and 180 characters.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test