From 43628661c5e8777f897864a481a321842bc903c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Lo=CC=88tscher?= Date: Wed, 11 Oct 2023 10:36:45 +0200 Subject: [PATCH] use lints ^2.1.1 instead of pedantic, introduce and resolve recommended rules --- analysis_options.yaml | 80 +++++++++----------------- lib/helper/core_oauth.dart | 10 ++-- lib/helper/mobile_oauth.dart | 12 ++-- lib/helper/web_oauth.dart | 16 +++--- lib/model/config.dart | 5 +- lib/model/failure.dart | 8 +-- lib/model/token.dart | 6 +- lib/request/authorization_request.dart | 6 +- lib/request_code.dart | 2 +- lib/request_token.dart | 14 ++--- pubspec.yaml | 2 +- 11 files changed, 66 insertions(+), 95 deletions(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index 909283be..55c78226 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1,54 +1,30 @@ -# Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file -# for details. All rights reserved. Use of this source code is governed by a -# BSD-style license that can be found in the LICENSE file. +# This file configures the static analysis results for your project (errors, +# warnings, and lints). # -# Google internally enforced rules. See README.md for more information, -# including a list of lints that are intentionally _not_ enforced. +# This enables the 'recommended' set of lints from `package:lints`. +# This set helps identify many issues that may lead to problems when running +# or consuming Dart code, and enforces writing Dart using a single, idiomatic +# style and format. +# +# If you want a smaller set of lints you can change this to specify +# 'package:lints/core.yaml'. These are just the most critical lints +# (the recommended set includes the core lints). +# The core lints are also what is used by pub.dev for scoring packages. + +include: package:lints/recommended.yaml + +# Uncomment the following section to specify additional rules. + +# linter: +# rules: +# - camel_case_types + +# analyzer: +# exclude: +# - path/to/excluded/files/** + +# For more information about the core and recommended set of lints, see +# https://dart.dev/go/core-lints -linter: - rules: - - always_declare_return_types - - always_require_non_null_named_parameters - - annotate_overrides - - avoid_empty_else - - avoid_init_to_null - - avoid_null_checks_in_equality_operators - - avoid_relative_lib_imports - - avoid_return_types_on_setters - - avoid_shadowing_type_parameters - - avoid_types_as_parameter_names - - camel_case_extensions - - curly_braces_in_flow_control_structures - - empty_catches - - empty_constructor_bodies - - library_names - - library_prefixes - - no_duplicate_case_values - - null_closures - - omit_local_variable_types - - prefer_adjacent_string_concatenation - - prefer_collection_literals - - prefer_conditional_assignment - - prefer_contains - - prefer_equal_for_default_values - - prefer_final_fields - - prefer_for_elements_to_map_fromIterable - - prefer_generic_function_type_aliases - - prefer_if_null_operators - - prefer_is_empty - - prefer_is_not_empty - - prefer_iterable_whereType - - prefer_single_quotes - - prefer_spread_collections - - recursive_getters - - slash_for_doc_comments - - type_init_formals - - unawaited_futures - - unnecessary_const - - unnecessary_new - - unnecessary_null_in_if_null_operators - - unnecessary_this - - unrelated_type_equality_checks - - use_function_type_syntax_for_parameters - - use_rethrow_when_possible - - valid_regexps +# For additional information about configuring this file, see +# https://dart.dev/guides/language/analysis-options \ No newline at end of file diff --git a/lib/helper/core_oauth.dart b/lib/helper/core_oauth.dart index c96a9a23..f3cf265d 100644 --- a/lib/helper/core_oauth.dart +++ b/lib/helper/core_oauth.dart @@ -13,22 +13,22 @@ class CoreOAuth { Future> login( {bool refreshIfAvailable = false}) async => - throw UnsupportedFailure(ErrorType.Unsupported, 'Unsupported login'); + throw UnsupportedFailure(ErrorType.unsupported, 'Unsupported login'); Future> refreshToken() async => throw UnsupportedFailure( - ErrorType.Unsupported, 'Unsupported silentlyLogin'); + ErrorType.unsupported, 'Unsupported silentlyLogin'); Future logout() async => - throw UnsupportedFailure(ErrorType.Unsupported, 'Unsupported logout'); + throw UnsupportedFailure(ErrorType.unsupported, 'Unsupported logout'); Future get hasCachedAccountInformation async => false; Future getAccessToken() async => throw UnsupportedFailure( - ErrorType.Unsupported, 'Unsupported getAccessToken'); + ErrorType.unsupported, 'Unsupported getAccessToken'); Future getIdToken() async => throw UnsupportedFailure( - ErrorType.Unsupported, 'Unsupported getAccessToken'); + ErrorType.unsupported, 'Unsupported getAccessToken'); factory CoreOAuth.fromConfig(Config config) => config.isStub ? MockCoreOAuth() : getOAuthConfig(config); diff --git a/lib/helper/mobile_oauth.dart b/lib/helper/mobile_oauth.dart index 86b629d1..87f31f7b 100644 --- a/lib/helper/mobile_oauth.dart +++ b/lib/helper/mobile_oauth.dart @@ -141,13 +141,13 @@ class MobileOAuth extends CoreOAuth { if (!token.hasValidAccessToken()) { final result = await _performFullAuthFlow(); - var failure; + Failure? failure; result.fold( (l) => failure = l, (r) => token = r, ); if (failure != null) { - return Left(failure); + return Left(failure!); } } @@ -160,7 +160,7 @@ class MobileOAuth extends CoreOAuth { var code = await _requestCode.requestCode(); if (code == null) { return Left(AadOauthFailure( - ErrorType.AccessDeniedOrAuthenticationCanceled, + ErrorType.accessDeniedOrAuthenticationCanceled, 'Access denied or authentication canceled.', )); } @@ -169,10 +169,10 @@ class MobileOAuth extends CoreOAuth { Future _removeOldTokenOnFirstLogin() async { var prefs = await SharedPreferences.getInstance(); - final _keyFreshInstall = 'freshInstall'; - if (!prefs.getKeys().contains(_keyFreshInstall)) { + final keyFreshInstall = 'freshInstall'; + if (!prefs.getKeys().contains(keyFreshInstall)) { await logout(); - await prefs.setBool(_keyFreshInstall, false); + await prefs.setBool(keyFreshInstall, false); } } } diff --git a/lib/helper/web_oauth.dart b/lib/helper/web_oauth.dart index 3dc179fe..d0a9f207 100644 --- a/lib/helper/web_oauth.dart +++ b/lib/helper/web_oauth.dart @@ -102,10 +102,10 @@ class WebOAuth extends CoreOAuth { refreshIfAvailable, config.webUseRedirect, allowInterop( - (_value) => completer.complete(Right(Token(accessToken: _value)))), - allowInterop((_error) => completer.complete(Left(AadOauthFailure( - ErrorType.AccessDeniedOrAuthenticationCanceled, - 'Access denied or authentication canceled. Error: ${_error.toString()}', + (value) => completer.complete(Right(Token(accessToken: value)))), + allowInterop((error) => completer.complete(Left(AadOauthFailure( + ErrorType.accessDeniedOrAuthenticationCanceled, + 'Access denied or authentication canceled. Error: ${error.toString()}', )))), ); @@ -118,10 +118,10 @@ class WebOAuth extends CoreOAuth { jsRefreshToken( allowInterop( - (_value) => completer.complete(Right(Token(accessToken: _value)))), - allowInterop((_error) => completer.complete(Left(AadOauthFailure( - ErrorType.AccessDeniedOrAuthenticationCanceled, - 'Access denied or authentication canceled. Error: ${_error.toString()}', + (value) => completer.complete(Right(Token(accessToken: value)))), + allowInterop((error) => completer.complete(Left(AadOauthFailure( + ErrorType.accessDeniedOrAuthenticationCanceled, + 'Access denied or authentication canceled. Error: ${error.toString()}', )))), ); diff --git a/lib/model/config.dart b/lib/model/config.dart index 58c5623d..8d3af817 100644 --- a/lib/model/config.dart +++ b/lib/model/config.dart @@ -175,7 +175,7 @@ class Config { base = base.substring(0, idx); } if (!base.endsWith('/')) { - base = base + '/'; + base = '$base/'; } return base; } else { @@ -216,7 +216,7 @@ class Config { required this.navigatorKey, this.origin, this.customParameters = const {}, - String? postLogoutRedirectUri, + this.postLogoutRedirectUri, this.appBar, }) : authorizationUrl = customAuthorizationUrl ?? (isB2C @@ -230,7 +230,6 @@ class Config { ? 'https://$tenant.b2clogin.com/$tenant.onmicrosoft.com/$policy/oauth2/v2.0/token' : '$customDomainUrlWithTenantId/$policy/oauth2/v2.0/token') : 'https://login.microsoftonline.com/$tenant/oauth2/v2.0/token'), - postLogoutRedirectUri = postLogoutRedirectUri, aOptions = aOptions ?? AndroidOptions(encryptedSharedPreferences: true), cacheLocation = cacheLocation ?? CacheLocation.localStorage, redirectUri = redirectUri ?? getDefaultRedirectUri(); diff --git a/lib/model/failure.dart b/lib/model/failure.dart index 57ee8d15..957ba313 100644 --- a/lib/model/failure.dart +++ b/lib/model/failure.dart @@ -1,10 +1,10 @@ import 'package:equatable/equatable.dart'; enum ErrorType { - AccessDeniedOrAuthenticationCanceled, - InvalidJson, - Unsupported, - UnexpectedError, + accessDeniedOrAuthenticationCanceled, + invalidJson, + unsupported, + unexpectedError, } abstract class Failure extends Equatable { diff --git a/lib/model/token.dart b/lib/model/token.dart index 32ea261e..93f98357 100644 --- a/lib/model/token.dart +++ b/lib/model/token.dart @@ -69,10 +69,8 @@ class Token { if (map == null) throw Exception('No token from received'); //error handling as described in https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow#error-response-1 if (map['error'] != null) { - throw Exception('Error during token request: ' + - map['error'] + - ': ' + - map['error_description']); + throw Exception( + 'Error during token request: ${map['error']}: ${map['error_description']}'); } var model = Token(); diff --git a/lib/request/authorization_request.dart b/lib/request/authorization_request.dart index bf758c2f..12e3abce 100644 --- a/lib/request/authorization_request.dart +++ b/lib/request/authorization_request.dart @@ -8,7 +8,7 @@ class AuthorizationRequest { final bool clearCookies; AuthorizationRequest(Config config, - {bool fullScreen = true, bool clearCookies = false}) + {this.fullScreen = true, this.clearCookies = false}) : url = config.authorizationUrl, redirectUrl = config.redirectUri, parameters = { @@ -16,9 +16,7 @@ class AuthorizationRequest { 'response_type': config.responseType, 'redirect_uri': config.redirectUri, 'scope': config.scope, - }, - fullScreen = fullScreen, - clearCookies = clearCookies { + } { if (config.state != null) { parameters.putIfAbsent('state', () => config.state!); } diff --git a/lib/request_code.dart b/lib/request_code.dart index a8e5c81a..e938a3f3 100644 --- a/lib/request_code.dart +++ b/lib/request_code.dart @@ -9,7 +9,7 @@ import 'request/authorization_request.dart'; class RequestCode { final Config _config; final AuthorizationRequest _authorizationRequest; - final _redirectUriHost; + final String _redirectUriHost; late NavigationDelegate _navigationDelegate; String? _code; diff --git a/lib/request_token.dart b/lib/request_token.dart index 6a90099a..3463fe2c 100644 --- a/lib/request_token.dart +++ b/lib/request_token.dart @@ -16,17 +16,17 @@ class RequestToken { RequestToken(this.config); Future> requestToken(String code) async { - final _tokenRequest = TokenRequestDetails(config, code); + final tokenRequest = TokenRequestDetails(config, code); return await _sendTokenRequest( - _tokenRequest.url, _tokenRequest.params, _tokenRequest.headers); + tokenRequest.url, tokenRequest.params, tokenRequest.headers); } Future> requestRefreshToken( String refreshToken) async { - final _tokenRefreshRequest = + final tokenRefreshRequest = TokenRefreshRequestDetails(config, refreshToken); - return await _sendTokenRequest(_tokenRefreshRequest.url, - _tokenRefreshRequest.params, _tokenRefreshRequest.headers); + return await _sendTokenRequest(tokenRefreshRequest.url, + tokenRefreshRequest.params, tokenRefreshRequest.headers); } Future> _sendTokenRequest(String url, @@ -39,10 +39,10 @@ class RequestToken { return Right(token); } return Left( - RequestFailure(ErrorType.InvalidJson, 'Token json is invalid')); + RequestFailure(ErrorType.invalidJson, 'Token json is invalid')); } catch (e) { return Left(RequestFailure( - ErrorType.InvalidJson, 'Token json is invalid: ${e.toString()}')); + ErrorType.invalidJson, 'Token json is invalid: ${e.toString()}')); } } } diff --git a/pubspec.yaml b/pubspec.yaml index d8deaefb..40af9bf2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -14,7 +14,7 @@ dependencies: webview_flutter: ^4.4.1 js: ^0.6.7 http: ^1.1.0 - pedantic: ^1.11.1 + lints: ^2.1.1 shared_preferences: ^2.2.1 dartz: ^0.10.1 equatable: ^2.0.5