Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrated to latest versions. #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions packages/flutter_auth_core/lib/src/flutter_auth.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import 'package:flutter_auth_core/src/utils/flutter_auth_exception_code.dart';
import 'package:flutter_auth_core/src/utils/flutter_auth_exception_message.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

import 'utils/flutter_auth_webview.dart';
import 'package:meta/meta.dart' show required, visibleForOverriding;
import 'package:meta/meta.dart' show visibleForOverriding;
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_auth_core/flutter_auth_core.dart';

// ignore: public_member_api_docs
Expand All @@ -14,21 +10,19 @@ class FlutterAuth {
final String clientSecret;
final String callbackUrl;
final bool clearCache;
final String userAgent;
final String? userAgent;

/// Returns an instance of [FlutterAuth].
FlutterAuth(
{@required this.clientId,
@required this.clientSecret,
@required this.callbackUrl,
this.clearCache = false,
this.userAgent}) {
assert(clientId != null && clientId.isNotEmpty,
'ClientId may not be null or empty.');
assert(clientSecret != null && clientSecret.isNotEmpty,
'ClientSecret may not be null or empty.');
assert(callbackUrl != null && callbackUrl.isNotEmpty,
'CallbackUrl may not be null or empty.');
FlutterAuth({
required this.clientId,
required this.clientSecret,
required this.callbackUrl,
this.clearCache = false,
this.userAgent,
}) {
assert(clientId.isNotEmpty, 'ClientId may not be empty.');
assert(clientSecret.isNotEmpty, 'ClientSecret may not be empty.');
assert(callbackUrl.isNotEmpty, 'CallbackUrl may not be empty.');
}

@visibleForOverriding
Expand All @@ -50,8 +44,9 @@ class FlutterAuth {
@visibleForTesting
// ignore: public_member_api_docs
Future<FlutterAuthResult> openLoginPageWithWebview(
BuildContext context, String url) async {
assert(context != null && url != null && url.isNotEmpty);
BuildContext? context, String url) async {
assert(context != null);
assert(url.isNotEmpty);
var authorizedResult;

try {
Expand All @@ -73,14 +68,17 @@ class FlutterAuth {

@visibleForTesting
// ignore: public_member_api_docs
Future<dynamic> navigateToWebview(BuildContext context, String url) async {
return Navigator.of(context).push(MaterialPageRoute(
Future<dynamic> navigateToWebview(BuildContext? context, String url) async {
return Navigator.of(context!).push(
MaterialPageRoute(
builder: (context) => FlutterAuthWebview(
url: url,
redirectUrl: callbackUrl,
userAgent: userAgent,
clearCache: clearCache,
)));
url: url,
redirectUrl: callbackUrl,
userAgent: userAgent,
clearCache: clearCache,
),
),
);
}

@visibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class FlutterAuthException {
FlutterAuthException({this.code, this.message, this.details});

/// The error code
final FlutterAuthExceptionCode code;
final FlutterAuthExceptionCode? code;

/// Description of the error thrown
final String message;
final String? message;

/// Additional details of the error thrown
final dynamic details;
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter_auth_core/lib/src/flutter_auth_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class FlutterAuthResult {
FlutterAuthResult({this.token, this.secret}) : assert(token != null);

/// The token obtained after the user has successfully logged in.
final String token;
final String? token;

/// The secret obtained after the user has successfully logged in.
final String secret;
final String? secret;

/// Returns the current instance as a [Map].
Map<String, dynamic> get asMap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class FlutterAuthWebview extends StatefulWidget {
final String redirectUrl;
final bool clearCache;
final String title;
final String userAgent;
final String? userAgent;

// ignore: public_member_api_docs
const FlutterAuthWebview(
{Key key,
@required this.url,
@required this.redirectUrl,
{Key? key,
required this.url,
required this.redirectUrl,
this.userAgent,
this.clearCache = true,
this.title = ""})
Expand All @@ -29,9 +29,9 @@ class _FlutterAuthWebviewState extends State<FlutterAuthWebview> {
final FlutterWebviewPlugin _wv = FlutterWebviewPlugin();

// On urlChanged stream
StreamSubscription<String> _onUrlChanged;
late StreamSubscription<String> _onUrlChanged;

StreamSubscription<WebViewHttpError> _onHttpError;
late StreamSubscription<WebViewHttpError> _onHttpError;

static const String _userAgentMacOSX =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36";
Expand Down
16 changes: 8 additions & 8 deletions packages/flutter_auth_core/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ version: 0.0.4
homepage: https://github.com/StackTiger/flutter_auth/tree/master/packages/flutter_auth_core

environment:
sdk: ">=2.7.0 <3.0.0"
sdk: ">=2.16.0 <3.0.0"

dependencies:
flutter:
sdk: flutter
quiver: ">=2.0.0 <3.0.0"
meta: ^1.1.8
url_launcher: ^5.5.0
http: ^0.12.2
oauth1: ^1.0.4
flutter_webview_plugin: ^0.3.11
flutter_webview_plugin: ^0.4.0
http: ^0.13.4
meta: ^1.7.0
oauth1: ^2.0.0
quiver: ^3.0.1+1
url_launcher: ^6.0.20

dev_dependencies:
flutter_test:
sdk: flutter
mockito: ^4.1.1
mockito: ^5.1.0
18 changes: 9 additions & 9 deletions packages/flutter_auth_core/test/flutter_auth_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const kAuthorizedResultUrl = 'test-authorized-result-url';
BuildContext kMockBuildContext = MockBuildContext();

void main() {
TestFlutterAuth testFlutterAuth;
late TestFlutterAuth testFlutterAuth;
TestWidgetsFlutterBinding.ensureInitialized();

setUp(() {
Expand Down Expand Up @@ -45,31 +45,31 @@ void main() {
try {
FlutterAuth(
callbackUrl: kCallbackUrl,
clientId: null,
clientId: '',
clientSecret: kClientSecret);
} on AssertionError catch (e) {
expect(e.message, 'ClientId may not be null or empty.');
expect(e.message, 'ClientId may not be empty.');
}
});

test('throws AssertionError if clientSecret is null', () {
try {
FlutterAuth(
callbackUrl: kCallbackUrl, clientId: kClientId, clientSecret: null);
callbackUrl: kCallbackUrl, clientId: kClientId, clientSecret: '');
} on AssertionError catch (e) {
expect(e.message, 'ClientSecret may not be null or empty.');
expect(e.message, 'ClientSecret may not be empty.');
}
});

test('throws AssertionError if callbackUrl is null', () {
try {
FlutterAuth(
callbackUrl: null,
callbackUrl: '',
clientId: kClientId,
clientSecret: kClientSecret,
);
} on AssertionError catch (e) {
expect(e.message, 'CallbackUrl may not be null or empty.');
expect(e.message, 'CallbackUrl may not be empty.');
}
});
});
Expand Down Expand Up @@ -111,7 +111,7 @@ void main() {
test('throws AssertionError if url is null', () {
expect(
() =>
testFlutterAuth.openLoginPageWithWebview(kMockBuildContext, null),
testFlutterAuth.openLoginPageWithWebview(kMockBuildContext, ''),
throwsAssertionError);
});

Expand All @@ -135,7 +135,7 @@ class TestFlutterAuth extends FlutterAuth {
userAgent: kUserAgent);

@override
navigateToWebview(BuildContext context, String url) {
navigateToWebview(BuildContext? context, String url) {
return Future.value('mock-url');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:name="${applicationName}"
android:label="github_auth_example"
android:icon="@mipmap/ic_launcher">
<activity
Expand Down
2 changes: 1 addition & 1 deletion packages/github_auth/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ flutter:
uses-material-design: true

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: ">=2.16.0 <3.0.0"
5 changes: 2 additions & 3 deletions packages/github_auth/example/test_driver/app_test.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart';

void main() {
group('Github Auth App', () {
final titleTextFinder = find.byValueKey('title');
final titleTextFinder = find.text('title');

FlutterDriver driver;

Expand Down
6 changes: 3 additions & 3 deletions packages/github_auth/lib/src/github_api_error.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// https://docs.github.com/en/free-pro-team@latest/developers/apps/authorizing-oauth-apps#error-codes-for-the-device-flow

class GithubAPIError {
String code;
String message;
String uri;
String? code;
String? message;
String? uri;

GithubAPIError({this.code, this.message, this.uri});

Expand Down
22 changes: 14 additions & 8 deletions packages/github_auth/lib/src/github_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,33 @@ class GithubAuth extends FlutterAuth {
final bool clearCache;

/// The user agent to be used for the Webview
final String userAgent;
final String? userAgent;

@visibleForTesting
// ignore: public_member_api_docs
final client = http.Client();

GithubAuth(
{@required this.clientId,
@required this.clientSecret,
@required this.callbackUrl,
{required this.clientId,
required this.clientSecret,
required this.callbackUrl,
this.scope = "user,gist,user:email",
this.allowSignUp = true,
this.clearCache = false,
this.userAgent});
this.userAgent})
: super(
clientId: clientId,
clientSecret: clientSecret,
callbackUrl: callbackUrl,
);

@visibleForTesting
@override
// ignore: public_member_api_docs
Future<FlutterAuthResult> loginComplete(Uri authorizedResultUrl) async {
FlutterAuthResult result;
// exchange for access token
String code = authorizedResultUrl.queryParameters[kCodeConstant];
String? code = authorizedResultUrl.queryParameters[kCodeConstant];

if (code == null || code.isEmpty) {
throw FlutterAuthException(
Expand All @@ -78,7 +83,8 @@ class GithubAuth extends FlutterAuth {

@visibleForTesting
Future<String> getAccessToken(String code) async {
var response = await client.post("$kApiEndpointAccessToken", headers: {
http.Response response =
await client.post(Uri.parse(kApiEndpointAccessToken), headers: {
kAcceptConstant: kAcceptJsonConstant,
}, body: {
kClientIdConstant: clientId,
Expand All @@ -87,7 +93,7 @@ class GithubAuth extends FlutterAuth {
});

if (response.statusCode == 200) {
var body = json.decode(utf8.decode(response.bodyBytes));
Map<String, dynamic> body = json.decode(utf8.decode(response.bodyBytes));
var error = body[kErrorConstant];
if (error != null) {
throw GithubAPIError.parse(body);
Expand Down
42 changes: 31 additions & 11 deletions packages/github_auth/lib/src/utils/constants.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
const kApiBaseEndpoint = 'https://github.com/login/oauth';
const kApiEndpointAccessToken = '$kApiBaseEndpoint/access_token';
const kApiEndpointAuthorize = '$kApiBaseEndpoint/authorize';

const kCodeConstant = 'code';
const kAccessTokenConstant = 'access_token';
const kClientSecretConstant = 'client_secret';
const kClientIdConstant = "client_id";
const kAcceptConstant = "Accept";
const kAcceptJsonConstant = "application/json";
const kErrorConstant = "error";
/// Global constant for Github OAuth Login base url
const String kApiBaseEndpoint = 'https://github.com/login/oauth';

/// Global constant for Github OAuth Login access token endpoint
const String kApiEndpointAccessToken = '$kApiBaseEndpoint/access_token';

/// Global constant for Github OAuth Login authorize endpoint
const String kApiEndpointAuthorize = '$kApiBaseEndpoint/authorize';

/// Global key constant for Github OAuth Login that to be
/// passed to the API call to get the access token
/// while logging in with [GithubAuth.clientId] and [GithubAuth.clientSecret].
const String kCodeConstant = 'code';

/// Global key constant for to get the access token from the json response.
const String kAccessTokenConstant = 'access_token';

/// Global Key constant to put the client secret in the request body.
const String kClientSecretConstant = 'client_secret';

/// Global Key constant to put the client id in the request body.
const String kClientIdConstant = "client_id";

/// Global Key constant to put the headers in the request body.
const String kAcceptConstant = "Accept";

/// Global Value constant to put the headers in the request body.
const String kAcceptJsonConstant = "application/json";

/// Global Key constant to get the error from the json response.
const String kErrorConstant = "error";
Loading