Skip to content

Commit

Permalink
follow pedantic rules
Browse files Browse the repository at this point in the history
  • Loading branch information
loetsphi committed Oct 12, 2020
1 parent 4b4ef51 commit f7d6991
Show file tree
Hide file tree
Showing 15 changed files with 300 additions and 273 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
## [0.2.0] - 20201007

- Add additional config options https://github.com/Earlybyte/aad_oauth/pull/66
- Throw Exception on return https://github.com/Earlybyte/aad_oauth/pull/55
- Fix Example App https://github.com/Earlybyte/aad_oauth/pull/65
- Add additional config options #66
- Throw Exception on return #55
- Fix Example App #65
- Comply Pub Dev Requirements #67

## [0.1.9] - 20200529

Expand Down
54 changes: 54 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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.
#
# Google internally enforced rules. See README.md for more information,
# including a list of lints that are intentionally _not_ enforced.

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
53 changes: 22 additions & 31 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,17 @@ import 'package:aad_oauth/aad_oauth.dart';
import 'package:aad_oauth/model/config.dart';
import 'package:flutter/material.dart';

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

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

class _MyAppState extends State<MyApp> {
@override
initState() {
super.initState();
}
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
return MaterialApp(
title: 'AAD OAuth Demo',
theme: new ThemeData(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'AAD OAuth Home'),
home: MyHomePage(title: 'AAD OAuth Home'),
);
}
}
Expand All @@ -32,34 +22,35 @@ class MyHomePage extends StatefulWidget {
final String title;

@override
_MyHomePageState createState() => new _MyHomePageState();
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
static final Config config = new Config(
tenant: "YOUR_TENANT_ID",
clientId: "YOUR_CLIENT_ID",
scope: "openid profile offline_access",
redirectUri: "https://login.live.com/oauth20_desktop.srf",
static final Config config = Config(
tenant: 'YOUR_TENANT_ID',
clientId: 'YOUR_CLIENT_ID',
scope: 'openid profile offline_access',
redirectUri: 'https://login.live.com/oauth20_desktop.srf',
);
final AadOAuth oauth = AadOAuth(config);

@override
Widget build(BuildContext context) {
// adjust window size for browser login
var screenSize = MediaQuery.of(context).size;
var rectSize =
Rect.fromLTWH(0.0, 25.0, screenSize.width, screenSize.height - 25);
oauth.setWebViewScreenSize(rectSize);

return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView(
children: <Widget>[
ListTile(
title: Text(
"AzureAD OAuth",
'AzureAD OAuth',
style: Theme.of(context).textTheme.headline5,
),
),
Expand Down Expand Up @@ -87,9 +78,9 @@ class _MyHomePageState extends State<MyHomePage> {
}

void showMessage(String text) {
var alert = new AlertDialog(content: new Text(text), actions: <Widget>[
new FlatButton(
child: const Text("Ok"),
var alert = AlertDialog(content: Text(text), actions: <Widget>[
FlatButton(
child: const Text('Ok'),
onPressed: () {
Navigator.pop(context);
})
Expand All @@ -100,15 +91,15 @@ class _MyHomePageState extends State<MyHomePage> {
void login() async {
try {
await oauth.login();
String accessToken = await oauth.getAccessToken();
showMessage("Logged in successfully, your access token: $accessToken");
var accessToken = await oauth.getAccessToken();
showMessage('Logged in successfully, your access token: $accessToken');
} catch (e) {
showError(e);
}
}

void logout() async {
await oauth.logout();
showMessage("Logged out");
showMessage('Logged out');
}
}
66 changes: 33 additions & 33 deletions example_b2c/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import 'package:aad_oauth/aad_oauth.dart';
import 'package:aad_oauth/model/config.dart';
import 'package:flutter/material.dart';

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

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

class _MyAppState extends State<MyApp> {
Expand All @@ -17,12 +17,12 @@ class _MyAppState extends State<MyApp> {

@override
Widget build(BuildContext context) {
return new MaterialApp(
return MaterialApp(
title: 'AAD B2C Demo',
theme: new ThemeData(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'AAD B2C Home'),
home: MyHomePage(title: 'AAD B2C Home'),
);
}
}
Expand All @@ -32,29 +32,29 @@ class MyHomePage extends StatefulWidget {
final String title;

@override
_MyHomePageState createState() => new _MyHomePageState();
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
static final Config configB2Ca = new Config(
tenant: "YOUR_TENANT_NAME",
clientId: "YOUR_CLIENT_ID",
scope: "YOUR_CLIENT_ID offline_access",
redirectUri: "https://login.live.com/oauth20_desktop.srf",
clientSecret: "YOUR_CLIENT_SECRET",
static final Config configB2Ca = Config(
tenant: 'YOUR_TENANT_NAME',
clientId: 'YOUR_CLIENT_ID',
scope: 'YOUR_CLIENT_ID offline_access',
redirectUri: 'https://login.live.com/oauth20_desktop.srf',
clientSecret: 'YOUR_CLIENT_SECRET',
isB2C: true,
policy: "YOUR_USER_FLOW___USER_FLOW_A",
tokenIdentifier: "UNIQUE IDENTIFIER A");

static final Config configB2Cb = new Config(
tenant: "YOUR_TENANT_NAME",
clientId: "YOUR_CLIENT_ID",
scope: "YOUR_CLIENT_ID offline_access",
redirectUri: "https://login.live.com/oauth20_desktop.srf",
clientSecret: "YOUR_CLIENT_SECRET",
policy: 'YOUR_USER_FLOW___USER_FLOW_A',
tokenIdentifier: 'UNIQUE IDENTIFIER A');

static final Config configB2Cb = Config(
tenant: 'YOUR_TENANT_NAME',
clientId: 'YOUR_CLIENT_ID',
scope: 'YOUR_CLIENT_ID offline_access',
redirectUri: 'https://login.live.com/oauth20_desktop.srf',
clientSecret: 'YOUR_CLIENT_SECRET',
isB2C: true,
policy: "YOUR_USER_FLOW___USER_FLOW_B",
tokenIdentifier: "UNIQUE IDENTIFIER B");
policy: 'YOUR_USER_FLOW___USER_FLOW_B',
tokenIdentifier: 'UNIQUE IDENTIFIER B');

//You can have as many B2C flows as you want

Expand All @@ -68,15 +68,15 @@ class _MyHomePageState extends State<MyHomePage> {
Rect.fromLTWH(0.0, 25.0, screenSize.width, screenSize.height - 25);
oauthB2Ca.setWebViewScreenSize(rectSize);

return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView(
children: <Widget>[
ListTile(
title: Text(
"AzureAD B2C A",
'AzureAD B2C A',
style: Theme.of(context).textTheme.headline5,
),
),
Expand All @@ -97,7 +97,7 @@ class _MyHomePageState extends State<MyHomePage> {
Divider(),
ListTile(
title: Text(
"AzureAD B2C B",
'AzureAD B2C B',
style: Theme.of(context).textTheme.headline5,
),
),
Expand Down Expand Up @@ -125,9 +125,9 @@ class _MyHomePageState extends State<MyHomePage> {
}

void showMessage(String text) {
var alert = new AlertDialog(content: new Text(text), actions: <Widget>[
new FlatButton(
child: const Text("Ok"),
var alert = AlertDialog(content: Text(text), actions: <Widget>[
FlatButton(
child: const Text('Ok'),
onPressed: () {
Navigator.pop(context);
})
Expand All @@ -139,14 +139,14 @@ class _MyHomePageState extends State<MyHomePage> {
try {
await oAuth.login();
String accessToken = await oAuth.getAccessToken();
showMessage("Logged in successfully, your access token: $accessToken");
showMessage('Logged in successfully, your access token: $accessToken');
} catch (e) {
showError(e);
}
}

void logout(AadOAuth oAuth) async {
await oAuth.logout();
showMessage("Logged out");
showMessage('Logged out');
}
}
31 changes: 14 additions & 17 deletions lib/aad_oauth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class AadOAuth {

AadOAuth(Config config) {
_config = config;
_authStorage = new AuthStorage(tokenIdentifier: config.tokenIdentifier);
_requestCode = new RequestCode(_config);
_requestToken = new RequestToken(_config);
_authStorage = AuthStorage(tokenIdentifier: config.tokenIdentifier);
_requestCode = RequestCode(_config);
_requestToken = RequestToken(_config);
}

void setWebViewScreenSize(Rect screenSize) {
Expand All @@ -29,20 +29,19 @@ class AadOAuth {

Future<void> login() async {
await _removeOldTokenOnFirstLogin();
if (!Token.tokenIsValid(_token) )
if (!Token.tokenIsValid(_token)) {
await _performAuthorization();
}
}

Future<String> getAccessToken() async {
if (!Token.tokenIsValid(_token) )
await _performAuthorization();
if (!Token.tokenIsValid(_token)) await _performAuthorization();

return _token.accessToken;
}

Future<String> getIdToken() async {
if (!Token.tokenIsValid(_token) )
await _performAuthorization();
if (!Token.tokenIsValid(_token)) await _performAuthorization();

return _token.idToken;
}
Expand All @@ -62,12 +61,10 @@ class AadOAuth {
// load token from cache
_token = await _authStorage.loadTokenToCache();

//still have refreh token / try to get new access token with refresh token
if (_token != null)
//still have refreh token / try to get access token with refresh token
if (_token != null) {
await _performRefreshAuthFlow();

// if we have no refresh token try to perform full request code oauth flow
else {
} else {
try {
await _performFullAuthFlow();
} catch (e) {
Expand All @@ -84,7 +81,7 @@ class AadOAuth {
try {
code = await _requestCode.requestCode();
if (code == null) {
throw new Exception("Access denied or authentation canceled.");
throw Exception('Access denied or authentation canceled.');
}
_token = await _requestToken.requestToken(code);
} catch (e) {
Expand All @@ -103,10 +100,10 @@ class AadOAuth {
}

Future<void> _removeOldTokenOnFirstLogin() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final _keyFreshInstall = "freshInstall";
var prefs = await SharedPreferences.getInstance();
final _keyFreshInstall = 'freshInstall';
if (!prefs.getKeys().contains(_keyFreshInstall)) {
logout();
await logout();
await prefs.setBool(_keyFreshInstall, false);
}
}
Expand Down
Loading

0 comments on commit f7d6991

Please sign in to comment.