oh_auth_2 1.0.3+1

OhAuth2 #

OhAuth2 is a Flutter library for the industry-standard protocol for authorization.

Disclaimer: This library has been tested with a few services like Reddit and Strava. If you find OhAuth2 to be incompatible with a service you're trying to use this library with, please open an Issue or create a PR

Installation #

Installing OhAuth2 is simple using Dart's package management system https://pub.dartlang.org/packages/oh_auth_2.

Getting Started #

(Assuming you already have your OAuth credentials)

To retrieve the access token:

import 'dart:async';
import 'package:oh_auth_2/authenticator.dart';
import 'package:oh_auth_2/models/token.dart';

Future<Token> tokenResult = Authenticator(context)
    .getAccessToken(
        AuthenticationConfig.REDIRECT_URI,
        AuthenticationConfig.AUTH_URL,
        AuthenticationConfig.ACCESS_TOKEN_URL,
        AuthenticationConfig.CLIENT_ID,
        userAgent: AuthenticationConfig.USER_AGENT, //optional
        clientSecret: AuthenticationConfig.CLIENT_SECRET, //optional
)
    .then((token) {
  this.token = token;
  debugPrint(token.toJson().toString());
});

To refresh the access token:

import 'dart:async';
import 'package:oh_auth_2/authenticator.dart';
import 'package:oh_auth_2/models/token.dart';

Token token = await Authenticator(context).refreshAccessToken(
    AuthenticationConfig.ACCESS_TOKEN_URL,
    this.token.refreshToken,
    AuthenticationConfig.CLIENT_ID,
    userAgent: AuthenticationConfig.USER_AGENT, //optional
    clientSecret: AuthenticationConfig.CLIENT_SECRET, //optional
);
this.token.accessToken = token.accessToken;
debugPrint("Refreshed access token " + token.accessToken);

To revoke the access/refresh token:

import 'dart:async';
import 'package:oh_auth_2/authenticator.dart';
import 'package:oh_auth_2/models/token.dart';

bool isRevoked = await Authenticator(context).revokeAccessToken(
    AuthenticationConfig.REVOKE_TOKEN_URL,
    token.refreshToken,
    AuthenticationConfig.CLIENT_ID,
    userAgent: AuthenticationConfig.USER_AGENT, //optional
    clientSecret: AuthenticationConfig.CLIENT_SECRET //optional
);
debugPrint("Revoked access token " + (isRevoked.toString()));

License #

OhAuth2 is provided under a MIT License. Copyright (c) 2018 Murtaza Akbari.

[1.0.0]

example/README.md

Example #

(Assuming you already have your OAuth credentials)

To retrieve the access token:

import 'dart:async';
import 'package:oh_auth_2/authenticator.dart';
import 'package:oh_auth_2/models/token.dart';

Future<Token> tokenResult = Authenticator(context)
    .getAccessToken(
        AuthenticationConfig.REDIRECT_URI,
        AuthenticationConfig.AUTH_URL,
        AuthenticationConfig.ACCESS_TOKEN_URL,
        AuthenticationConfig.CLIENT_ID,
        userAgent: AuthenticationConfig.USER_AGENT, //optional
        clientSecret: AuthenticationConfig.CLIENT_SECRET, //optional
)
    .then((token) {
  this.token = token;
  debugPrint(token.toJson().toString());
});

To refresh the access token:

import 'dart:async';
import 'package:oh_auth_2/authenticator.dart';
import 'package:oh_auth_2/models/token.dart';

Token token = await Authenticator(context).refreshAccessToken(
    AuthenticationConfig.ACCESS_TOKEN_URL,
    this.token.refreshToken,
    AuthenticationConfig.CLIENT_ID,
    userAgent: AuthenticationConfig.USER_AGENT, //optional
    clientSecret: AuthenticationConfig.CLIENT_SECRET, //optional
);
this.token.accessToken = token.accessToken;
debugPrint("Refreshed access token " + token.accessToken);

To revoke the access/refresh token:

import 'dart:async';
import 'package:oh_auth_2/authenticator.dart';
import 'package:oh_auth_2/models/token.dart';

bool isRevoked = await Authenticator(context).revokeAccessToken(
    AuthenticationConfig.REVOKE_TOKEN_URL,
    token.refreshToken,
    AuthenticationConfig.CLIENT_ID,
    userAgent: AuthenticationConfig.USER_AGENT, //optional
    clientSecret: AuthenticationConfig.CLIENT_SECRET //optional
);
debugPrint("Revoked access token " + (isRevoked.toString()));

Use this package as a library

1. Depend on it

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


dependencies:
  oh_auth_2: ^1.0.3+1

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:oh_auth_2/access_token_response_listener.dart';
import 'package:oh_auth_2/access_token_service.dart';
import 'package:oh_auth_2/auth_view.dart';
import 'package:oh_auth_2/authenticator.dart';
import 'package:oh_auth_2/exceptions.dart';
import 'package:oh_auth_2/models/token.dart';
import 'package:oh_auth_2/models/token_generated.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
70
Health:
Code health derived from static analysis. [more]
96
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
82
Learn more about scoring.

We analyzed this package on Aug 22, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/access_token_service.dart. (-1.49 points)

Analysis of lib/access_token_service.dart reported 3 hints:

line 30 col 25: Avoid using braces in interpolation when not needed.

line 44 col 3: The class 'Future' was not exported from 'dart:core' until version 2.1, but this code is required to be able to run on earlier versions.

line 56 col 3: The class 'Future' was not exported from 'dart:core' until version 2.1, but this code is required to be able to run on earlier versions.

Fix lib/authenticator.dart. (-1.49 points)

Analysis of lib/authenticator.dart reported 3 hints:

line 28 col 3: The class 'Future' was not exported from 'dart:core' until version 2.1, but this code is required to be able to run on earlier versions.

line 59 col 3: The class 'Future' was not exported from 'dart:core' until version 2.1, but this code is required to be able to run on earlier versions.

line 86 col 3: The class 'Future' was not exported from 'dart:core' until version 2.1, but this code is required to be able to run on earlier versions.

Fix lib/auth_view.dart. (-1 points)

Analysis of lib/auth_view.dart reported 2 hints:

line 31 col 43: Cancel instances of dart.async.StreamSubscription.

line 32 col 30: Cancel instances of dart.async.StreamSubscription.

Format lib/exceptions.dart.

Run flutter format to format lib/exceptions.dart.

Format lib/models/token.dart.

Run flutter format to format lib/models/token.dart.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (json_annotation).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
flutter_webview_plugin ^0.3.0+2 0.3.7
http ^0.12.0 0.12.0+2
json_annotation ^2.0.0 2.4.0 3.0.0
Transitive dependencies
async 2.3.0
charcode 1.1.2
collection 1.14.11 1.14.12
http_parser 3.1.3
meta 1.1.6 1.1.7
path 1.6.4
pedantic 1.8.0+1
sky_engine 0.0.99
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
build_runner ^1.0.0
flutter_test
json_serializable ^2.0.0