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

Dependencies upgrade + Code Improvements #193

Open
wants to merge 2 commits 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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ AndroidManifest.xml
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="my.test.app" /> <!-- This must correspond to the custom scheme used for instantiatng the client... See below -->
<data android:scheme="my.test.app" /> <!-- This must correspond to the custom scheme used for instantiating the client... See below -->
</intent-filter>
</activity>
```
Expand Down Expand Up @@ -155,7 +155,7 @@ import 'package:http/http.dart' as http;
//Instantiate an OAuth2Client...
GoogleOAuth2Client client = GoogleOAuth2Client(
customUriScheme: 'my.test.app' //Must correspond to the AndroidManifest's "android:scheme" attribute
redirectUri: 'my.test.app:/oauth2redirect', //Can be any URI, but the scheme part must correspond to the customeUriScheme
redirectUri: 'my.test.app:/oauth2redirect', //Can be any URI, but the scheme part must correspond to the customUriScheme
);

//Then, instantiate the helper passing the previously instantiated client
Expand Down Expand Up @@ -217,7 +217,7 @@ if(tknResp.isExpired()) {
}
```

## Acessing custom/non standard response fields ##
## Accessing custom/non standard response fields ##
You can access non standard fields in the response by calling the ```getRespField``` method.

For example:
Expand Down Expand Up @@ -302,7 +302,7 @@ Then you can instantiate an helper class or directly use the client methods to a

## GitHub client ##

In order to use this client you need to first create a new OAuth2 App in the GittHub Developer Settings (https://github.com/settings/developers)
In order to use this client you need to first create a new OAuth2 App in the GitHub Developer Settings (https://github.com/settings/developers)

Then in your code:

Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repository: https://github.com/teranetsrl/oauth2_client
publish_to: none

environment:
sdk: '>=2.12.0 <3.11.0'
sdk: '>=3.0.0 <4.0.0'

dependencies:
flutter:
Expand Down
24 changes: 13 additions & 11 deletions lib/access_token_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ class AccessTokenResponse extends OAuth2Response {
AccessTokenResponse.fromMap(Map<String, dynamic> map) : super.fromMap(map);

@override
factory AccessTokenResponse.fromHttpResponse(http.Response response,
{List<String>? requestedScopes}) {
factory AccessTokenResponse.fromHttpResponse(
http.Response response, {
List<String>? requestedScopes,
}) {
AccessTokenResponse resp;

var defMap = {'http_status_code': response.statusCode};
final defMap = {'http_status_code': response.statusCode};
if (response.body != '') {
Map<String, dynamic> rMap = jsonDecode(response.body);
final Map<String, dynamic> rMap = jsonDecode(response.body);
//From Section 4.2.2. (Access Token Response) of OAuth2 rfc, the "scope" parameter in the Access Token Response is
//"OPTIONAL, if identical to the scope requested by the client; otherwise, REQUIRED."
if ((!rMap.containsKey('scope') ||
if (!rMap.containsKey('scope') ||
rMap['scope'] == null ||
rMap['scope'].isEmpty)) {
rMap['scope'].isEmpty) {
if (requestedScopes != null) {
rMap['scope'] = requestedScopes;
}
Expand Down Expand Up @@ -53,7 +55,7 @@ class AccessTokenResponse extends OAuth2Response {
} else {
resp = AccessTokenResponse.fromMap({
...defMap,
...{'scope': requestedScopes}
...{'scope': requestedScopes},
});
}

Expand All @@ -64,7 +66,7 @@ class AccessTokenResponse extends OAuth2Response {
Map<String, dynamic> toMap() {
return {
...respMap,
...{'scope': scope}
...{'scope': scope},
};
}

Expand All @@ -73,19 +75,19 @@ class AccessTokenResponse extends OAuth2Response {
var expired = false;

if (expirationDate != null) {
var now = DateTime.now();
final now = DateTime.now();
expired = expirationDate!.difference(now).inSeconds < 0;
}

return expired;
}

///Checks if the access token must be refreshed
bool refreshNeeded({secondsToExpiration = 30}) {
bool refreshNeeded({int secondsToExpiration = 30}) {
var needsRefresh = false;

if (expirationDate != null) {
var now = DateTime.now();
final now = DateTime.now();
needsRefresh =
expirationDate!.difference(now).inSeconds < secondsToExpiration;
}
Expand Down
22 changes: 12 additions & 10 deletions lib/authorization_response.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
/// Represents the response to an Authorization Request.
/// see https://tools.ietf.org/html/rfc6749#page-26
class AuthorizationResponse {
String? code;
String? state;
late Map<String, String> queryParams;

String? error;
String? errorDescription;

AuthorizationResponse.fromRedirectUri(
String redirectUri, String? checkState) {
String redirectUri,
String? checkState,
) {
queryParams = Uri.parse(redirectUri).queryParameters;

error = getQueryParam('error');
Expand All @@ -30,11 +25,18 @@ class AuthorizationResponse {

if (state != checkState) {
throw Exception(
'"state" parameter in response doesn\'t correspond to the expected value');
'"state" parameter in response doesn\'t correspond to the expected value',
);
}
}
}
}
String? code;
String? state;
late Map<String, String> queryParams;

String? error;
String? errorDescription;

/// Returns the value of the [paramName] key in the queryParams map
dynamic getQueryParam(String paramName) {
Expand All @@ -45,6 +47,6 @@ class AuthorizationResponse {
}

bool isAccessGranted() {
return error != null ? error!.isEmpty : true;
return error?.isEmpty ?? true;
}
}
14 changes: 7 additions & 7 deletions lib/facebook_oauth2_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import 'package:oauth2_client/oauth2_client.dart';
/// In order to use this client you need to first configure OAuth2 credentials in the Facebook dashboard.
///
class FacebookOAuth2Client extends OAuth2Client {
FacebookOAuth2Client(
{required String redirectUri, required String customUriScheme})
: super(
authorizeUrl: 'https://www.facebook.com/v5.0/dialog/oauth',
tokenUrl: 'https://graph.facebook.com/oauth/access_token',
redirectUri: redirectUri,
customUriScheme: customUriScheme);
FacebookOAuth2Client({
required super.redirectUri,
required super.customUriScheme,
}) : super(
authorizeUrl: 'https://www.facebook.com/v5.0/dialog/oauth',
tokenUrl: 'https://graph.facebook.com/oauth/access_token',
);
}
83 changes: 43 additions & 40 deletions lib/github_oauth2_client.dart
Original file line number Diff line number Diff line change
@@ -1,53 +1,56 @@
import 'package:http/http.dart';
import 'package:oauth2_client/access_token_response.dart';
import 'package:oauth2_client/oauth2_client.dart';
import 'src/base_web_auth.dart';
import 'package:oauth2_client/src/base_web_auth.dart';

/// Implements an OAuth2 client against GitHub
///
/// In order to use this client you need to first create a new OAuth2 App in the GittHub Developer Settings (https://github.com/settings/developers)
///
class GitHubOAuth2Client extends OAuth2Client {
GitHubOAuth2Client(
{required String redirectUri, required String customUriScheme})
: super(
authorizeUrl: 'https://github.com/login/oauth/authorize',
tokenUrl: 'https://github.com/login/oauth/access_token',
redirectUri: redirectUri,
customUriScheme: customUriScheme);
GitHubOAuth2Client({
required super.redirectUri,
required super.customUriScheme,
}) : super(
authorizeUrl: 'https://github.com/login/oauth/authorize',
tokenUrl: 'https://github.com/login/oauth/access_token',
);

@override
Future<AccessTokenResponse> getTokenWithAuthCodeFlow(
{required String clientId,
List<String>? scopes,
String? clientSecret,
bool enablePKCE = true,
bool enableState = true,
String? state,
String? codeVerifier,
Function? afterAuthorizationCodeCb,
Map<String, dynamic>? authCodeParams,
Map<String, dynamic>? accessTokenParams,
Map<String, String>? accessTokenHeaders,
httpClient,
BaseWebAuth? webAuthClient,
Map<String, dynamic>? webAuthOpts}) async {
Future<AccessTokenResponse> getTokenWithAuthCodeFlow({
required String clientId,
Client? httpClient,
List<String>? scopes,
String? clientSecret,
bool enablePKCE = true,
bool enableState = true,
String? state,
String? codeVerifier,
Function? afterAuthorizationCodeCb,
Map<String, dynamic>? authCodeParams,
Map<String, dynamic>? accessTokenParams,
Map<String, String>? accessTokenHeaders,
BaseWebAuth? webAuthClient,
Map<String, dynamic>? webAuthOpts,
}) async {
return super.getTokenWithAuthCodeFlow(
clientId: clientId,
scopes: scopes,
clientSecret: clientSecret,
enablePKCE: enablePKCE,
enableState: enableState,
state: state,
codeVerifier: codeVerifier,
afterAuthorizationCodeCb: afterAuthorizationCodeCb,
authCodeParams: authCodeParams,
accessTokenParams: accessTokenParams,
accessTokenHeaders: {
...?accessTokenHeaders,
...{'Accept': 'application/json'}
},
httpClient: httpClient,
webAuthClient: webAuthClient,
webAuthOpts: webAuthOpts);
clientId: clientId,
scopes: scopes,
clientSecret: clientSecret,
enablePKCE: enablePKCE,
enableState: enableState,
state: state,
codeVerifier: codeVerifier,
afterAuthorizationCodeCb: afterAuthorizationCodeCb,
authCodeParams: authCodeParams,
accessTokenParams: accessTokenParams,
accessTokenHeaders: {
...?accessTokenHeaders,
...{'Accept': 'application/json'},
},
httpClient: httpClient,
webAuthClient: webAuthClient,
webAuthOpts: webAuthOpts,
);
}
}
16 changes: 8 additions & 8 deletions lib/google_oauth2_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import 'package:oauth2_client/oauth2_client.dart';
/// (for example 'com.example.app', but you can use whatever uri scheme you want).
///
class GoogleOAuth2Client extends OAuth2Client {
GoogleOAuth2Client(
{required String redirectUri, required String customUriScheme})
: super(
authorizeUrl: 'https://accounts.google.com/o/oauth2/v2/auth',
tokenUrl: 'https://oauth2.googleapis.com/token',
revokeUrl: 'https://oauth2.googleapis.com/revoke',
redirectUri: redirectUri,
customUriScheme: customUriScheme);
GoogleOAuth2Client({
required super.redirectUri,
required super.customUriScheme,
}) : super(
authorizeUrl: 'https://accounts.google.com/o/oauth2/v2/auth',
tokenUrl: 'https://oauth2.googleapis.com/token',
revokeUrl: 'https://oauth2.googleapis.com/revoke',
);
}
9 changes: 4 additions & 5 deletions lib/linkedin_oauth2_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import 'package:oauth2_client/oauth2_client.dart';
/// In order to use this client you need to first configure OAuth2 credentials (see https://docs.microsoft.com/it-it/linkedin/shared/authentication/authorization-code-flow)
///
class LinkedInOAuth2Client extends OAuth2Client {
LinkedInOAuth2Client(
{required String redirectUri, required String customUriScheme})
: super(
LinkedInOAuth2Client({
required super.redirectUri,
required super.customUriScheme,
}) : super(
authorizeUrl: 'https://www.linkedin.com/oauth/v2/authorization',
tokenUrl: 'https://www.linkedin.com/oauth/v2/accessToken',
redirectUri: redirectUri,
customUriScheme: customUriScheme,
credentialsLocation: CredentialsLocation.body,
);
}
9 changes: 3 additions & 6 deletions lib/microsoft_oauth2_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ import 'package:oauth2_client/oauth2_client.dart';
///
///
class MicrosoftOauth2Client extends OAuth2Client {
static const String _myAuthority = "https://login.microsoftonline.com/";

MicrosoftOauth2Client({
required String tenant,
required String redirectUri,
required String customUriScheme,
required super.redirectUri,
required super.customUriScheme,
}) : super(
authorizeUrl: '$_myAuthority$tenant/oauth2/v2.0/authorize',
tokenUrl: '$_myAuthority$tenant/oauth2/v2.0/token',
redirectUri: redirectUri,
customUriScheme: customUriScheme,
);
static const String _myAuthority = 'https://login.microsoftonline.com/';
}
Loading
Loading