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

make null safe #18

Open
wants to merge 1 commit into
base: master
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
32 changes: 17 additions & 15 deletions lib/auth-data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@ class AuthData {
this.response,
});

final String userID; // User's profile id
final String clientID; // OAuth client id
final String accessToken; // OAuth access token
final String firstName; // User's first name
final String lastName; // User's last name
final String email; // User's email
final String profileImgUrl; // User's profile image url
final Map<String, dynamic> userJson; // Full returned user json
final Map<String, String> response; // Full returned auth response.
final String? userID; // User's profile id
final String? clientID; // OAuth client id
final String? accessToken; // OAuth access token
final String? firstName; // User's first name
final String? lastName; // User's last name
final String? email; // User's email
final String? profileImgUrl; // User's profile image url
final Map<String, dynamic>? userJson; // Full returned user json
final Map<String, String>? response; // Full returned auth response.

/// Creates a formatted string from
/// the response data.
String _formatResponse() {
StringBuffer result = StringBuffer('\n');

for (MapEntry data in response.entries) {
result.write('\t\t\t\t');
result.write(data.key);
result.write(' = ');
result.write(data.value);
result.write('\n');
if (response != null) {
for (MapEntry data in response!.entries) {
result.write('\t\t\t\t');
result.write(data.key);
result.write(' = ');
result.write(data.value);
result.write('\n');
}
}

return result.toString();
Expand Down
7 changes: 3 additions & 4 deletions lib/discord.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class DiscordAuth extends Visa {
final Debug _debug = Debug(prefix: 'In DiscordAuth ->');

@override
SimpleAuth visa;

late SimpleAuth visa;
DiscordAuth() {
visa = SimpleAuth(
baseUrl: baseUrl,
Expand All @@ -24,7 +23,7 @@ class DiscordAuth extends Visa {
getAuthData: (Map<String, String> oauthData) async {
if (debugMode) _debug.info('OAuth Data: $oauthData');

final String token = oauthData[OAuth.TOKEN_KEY];
final String? token = oauthData[OAuth.TOKEN_KEY];
if (debugMode) _debug.info('OAuth token: $token');

// User profile API endpoint.
Expand All @@ -47,7 +46,7 @@ class DiscordAuth extends Visa {
@override
AuthData authData(
Map<String, dynamic> profileJson, Map<String, String> oauthData) {
final String accessToken = oauthData[OAuth.TOKEN_KEY];
final String? accessToken = oauthData[OAuth.TOKEN_KEY];
final String userId = profileJson['id'] as String;
final String avatar = profileJson['avatar'] as String;
final String profileImgUrl = 'https://cdn.discordapp.com/'
Expand Down
2 changes: 1 addition & 1 deletion lib/engine/debug.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';

class Debug {
Debug({@required this.prefix});
Debug({required this.prefix});

String prefix;

Expand Down
28 changes: 14 additions & 14 deletions lib/engine/oauth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import 'package:webview_flutter/webview_flutter.dart';
/// for OAuth 2.0 Authentication.
class OAuth {
OAuth(
{@required this.baseUrl,
@required this.clientID,
@required this.redirectUri,
@required this.state,
@required this.scope,
@required this.debugMode,
{required this.baseUrl,
required this.clientID,
required this.redirectUri,
required this.state,
required this.scope,
required this.debugMode,
this.clientSecret,
this.responseType,
this.otherQueryParams});

final String baseUrl; // OAuth url
final String clientID; // OAuth clientID
final String clientSecret; // OAuth clientSecret
final String responseType; // OAuth clientSecret
final String? clientSecret; // OAuth clientSecret
final String? responseType; // OAuth clientSecret
final String redirectUri; // OAuth redirectUri
final String state; // OAuth state
final String scope; // OAuth scope
final Map<String, String> otherQueryParams;
final Map<String, String>? otherQueryParams;
final bool debugMode; // Debug mode?
static const String TOKEN_KEY = 'access_token'; // OAuth token key
static const String CODE_KEY = 'code'; // OAuth code key
Expand All @@ -45,16 +45,16 @@ class OAuth {
/// Sets up a [WebView] for OAuth authentication.
/// [onDone] is called when authentication is
/// completed successfully.
WebView authenticate({@required Function onDone, bool clearCache = false}) {
WebView authenticate({required Function onDone, bool clearCache = false}) {
String clientSecretQuery =
clientSecret != null ? '&client_secret=$clientSecret' : '';
String responseTypeQuery =
'&response_type=${responseType == null ? 'token' : responseType}';
String otherParams = '';

if (otherQueryParams != null) {
for (String key in otherQueryParams.keys) {
otherParams += '&$key=${otherQueryParams[key]}';
for (String key in otherQueryParams!.keys) {
otherParams += '&$key=${otherQueryParams![key]}';
}
}

Expand Down Expand Up @@ -103,14 +103,14 @@ class OAuth {
returnedData[STATE_KEY] = state;

if (clientSecret != null) {
returnedData[CLIENT_SECRET_KEY] = clientSecret;
returnedData[CLIENT_SECRET_KEY] = clientSecret!;
}

onDone(returnedData);
} else if (debugMode) {
_debug.info('Redirect Url Not Found');
_debug.info('Url = $url');
_debug.info('Redirect Url = $redirectUri');
//_debug.info('Redirect Url = $redirectUri');
}

return NavigationDecision.navigate;
Expand Down
25 changes: 12 additions & 13 deletions lib/engine/simple-auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class SimpleAuth {
/// Creates a new instance based on the given OAuth
/// baseUrl and getAuthData function.
SimpleAuth(
{@required this.baseUrl,
@required this.getAuthData,
{required this.baseUrl,
required this.getAuthData,
this.responseType,
this.otherQueryParams});

final String baseUrl; // OAuth base url
final String responseType;
final Map<String, String> otherQueryParams;
final String? responseType;
final Map<String, String>? otherQueryParams;

/// This function makes the necessary api calls to
/// get a user's profile data. It accepts a single
Expand All @@ -43,12 +43,12 @@ class SimpleAuth {
/// provided credentials. Returns a WebView
/// That's been set up for authentication
WebView authenticate(
{@required String clientID,
String clientSecret,
@required String redirectUri,
@required String state,
@required String scope,
@required Function onDone,
{required String clientID,
String? clientSecret,
required String redirectUri,
required String state,
required String scope,
required Function onDone,
bool newSession = false}) {
final OAuth oAuth = OAuth(
baseUrl: baseUrl,
Expand All @@ -65,9 +65,8 @@ class SimpleAuth {
clearCache: newSession,
onDone: (responseData) async {
if (debugMode) _debug.info('Response: $responseData');

final String token = responseData[OAuth.TOKEN_KEY];
final String code = responseData[OAuth.CODE_KEY];
final String? token = responseData[OAuth.TOKEN_KEY];
final String? code = responseData[OAuth.CODE_KEY];

AuthData authData = token == null && code == null
? AuthData(response: responseData)
Expand Down
2 changes: 1 addition & 1 deletion lib/engine/visa.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'simple-auth.dart';
/// Visa abstract class
abstract class Visa {
/// a [SimpleAuth] instance
SimpleAuth visa;
late SimpleAuth visa;

/// Debug mode?
bool debugMode = false;
Expand Down
6 changes: 3 additions & 3 deletions lib/fb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FacebookAuth extends Visa {
final Debug _debug = Debug(prefix: 'In FacebookAuth ->');

@override
SimpleAuth visa;
late SimpleAuth visa;

FacebookAuth() {
visa = SimpleAuth(
Expand All @@ -24,7 +24,7 @@ class FacebookAuth extends Visa {
getAuthData: (Map<String, String> oauthData) async {
if (debugMode) _debug.info('OAuth Data: $oauthData');

final String token = oauthData[OAuth.TOKEN_KEY];
final String? token = oauthData[OAuth.TOKEN_KEY];
if (debugMode) _debug.info('OAuth token: $token');

// User profile API endpoint.
Expand All @@ -47,7 +47,7 @@ class FacebookAuth extends Visa {
/// to build an [AuthData] object.
@override
AuthData authData(Map<String, dynamic> json, Map<String, String> data) {
final String accessToken = data[OAuth.TOKEN_KEY];
final String? accessToken = data[OAuth.TOKEN_KEY];
final String profileImgUrl = 'https://graph.facebook.com/me/picture'
'?type=large'
'&access_token=$accessToken';
Expand Down
10 changes: 5 additions & 5 deletions lib/github.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class GithubAuth extends Visa {
final Debug _debug = Debug(prefix: 'In GithubAuth ->');

@override
SimpleAuth visa;
late SimpleAuth visa;

GithubAuth() {
visa = SimpleAuth(
Expand All @@ -27,7 +27,7 @@ class GithubAuth extends Visa {
if (debugMode) _debug.info('OAuth Data: $oauthData');

await _getToken(oauthData);
final String token = oauthData[OAuth.TOKEN_KEY];
final String? token = oauthData[OAuth.TOKEN_KEY];
if (debugMode) _debug.info('OAuth token: $token');

// User profile API endpoint.
Expand All @@ -51,7 +51,7 @@ class GithubAuth extends Visa {
/// to build an [AuthData] object.
AuthData authData(
Map<String, dynamic> profileJson, Map<String, String> oauthData) {
final String accessToken = oauthData[OAuth.TOKEN_KEY];
final String? accessToken = oauthData[OAuth.TOKEN_KEY];

return AuthData(
clientID: oauthData[OAuth.CLIENT_ID_KEY],
Expand Down Expand Up @@ -108,7 +108,7 @@ class GithubAuth extends Visa {

if (debugMode) _debug.info('Returned Profile Json: $profileJson');

if (profileJson['name'] != null){
if (profileJson['name'] != null) {
final List<String> name = profileJson['name'].split(' ');
profileJson['first_name'] = name[0];
profileJson['last_name'] = name[1];
Expand All @@ -131,7 +131,7 @@ class GithubAuth extends Visa {
_debug.info(
'In GithubAuth -> Returned Email Response: ${emailResponse.body}');

String email;
String? email;

for (var _email in emailJson) {
if (_email['primary']) {
Expand Down
6 changes: 3 additions & 3 deletions lib/google.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class GoogleAuth extends Visa {
String personFields;

@override
SimpleAuth visa;
late SimpleAuth visa;

GoogleAuth({this.personFields = ""}) {
personFields = _getPersonFields(personFields);
Expand All @@ -27,7 +27,7 @@ class GoogleAuth extends Visa {
getAuthData: (Map<String, String> oauthData) async {
if (debugMode) _debug.info('OAuth Data: $oauthData');

final String token = oauthData[OAuth.TOKEN_KEY];
final String? token = oauthData[OAuth.TOKEN_KEY];
if (debugMode) _debug.info('OAuth token: $token');

// User profile API endpoint.
Expand All @@ -51,7 +51,7 @@ class GoogleAuth extends Visa {
/// to build an [AuthData] object.
AuthData authData(
Map<String, dynamic> profileJson, Map<String, String> oauthData) {
final String accessToken = oauthData[OAuth.TOKEN_KEY];
final String? accessToken = oauthData[OAuth.TOKEN_KEY];

return AuthData(
clientID: oauthData['clientID'],
Expand Down
8 changes: 4 additions & 4 deletions lib/linkedin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class LinkedInAuth extends Visa {
final Debug _debug = Debug(prefix: 'In LinkedInAuth ->');

@override
SimpleAuth visa;
late SimpleAuth visa;

LinkedInAuth() {
visa = SimpleAuth(
Expand All @@ -28,7 +28,7 @@ class LinkedInAuth extends Visa {
if (debugMode) _debug.info('OAuth Data: $oauthData');

await _getToken(oauthData);
final String token = oauthData[OAuth.TOKEN_KEY];
final String? token = oauthData[OAuth.TOKEN_KEY];
if (debugMode) _debug.info('OAuth token: $token');

final String baseApiUrl = 'https://api.linkedin.com/v2';
Expand All @@ -52,7 +52,7 @@ class LinkedInAuth extends Visa {
/// to build an [AuthData] object.
AuthData authData(
Map<String, dynamic> profileJson, Map<String, String> oauthData) {
final String accessToken = oauthData[OAuth.TOKEN_KEY];
final String? accessToken = oauthData[OAuth.TOKEN_KEY];

return AuthData(
clientID: oauthData[OAuth.CLIENT_ID_KEY],
Expand Down Expand Up @@ -141,7 +141,7 @@ class LinkedInAuth extends Visa {
_debug.info('Returned Email Response: ${emailResponse.body}');
}

String email;
String? email;
List<dynamic> elements = emailJson['elements'];

for (Map<String, dynamic> contact in elements) {
Expand Down
8 changes: 4 additions & 4 deletions lib/spotify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SpotifyAuth extends Visa {
final Debug _debug = Debug(prefix: 'In SpotifyAuth ->');

@override
SimpleAuth visa;
late SimpleAuth visa;

SpotifyAuth() {
visa = SimpleAuth(
Expand All @@ -30,7 +30,7 @@ class SpotifyAuth extends Visa {
if (debugMode) _debug.info('OAuth Data: $oauthData');

await _getToken(oauthData);
final String token = oauthData[OAuth.TOKEN_KEY];
final String? token = oauthData[OAuth.TOKEN_KEY];
if (debugMode) _debug.info('OAuth token: $token');

final Map<String, dynamic> profileJson = await _getProfile(token);
Expand All @@ -45,7 +45,7 @@ class SpotifyAuth extends Visa {
/// to build an [AuthData] object.
AuthData authData(
Map<String, dynamic> profileJson, Map<String, String> oauthData) {
final String accessToken = oauthData[OAuth.TOKEN_KEY];
final String? accessToken = oauthData[OAuth.TOKEN_KEY];

return AuthData(
clientID: oauthData[OAuth.CLIENT_ID_KEY],
Expand Down Expand Up @@ -95,7 +95,7 @@ class SpotifyAuth extends Visa {

/// Get's a user's Spotify profile data and
/// isolates the first and last name.
Future<Map<String, dynamic>> _getProfile(String token) async {
Future<Map<String, dynamic>> _getProfile(String? token) async {
// User profile API endpoint.
final String profileUrlString = 'https://api.spotify.com/v1/me';
final Uri profileUrl = Uri.parse(profileUrlString);
Expand Down
Loading