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

Some suggested improvements #1

Merged
merged 4 commits into from
Sep 23, 2020
Merged
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
44 changes: 0 additions & 44 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,3 @@
.pub-cache/
.pub/
build/

# Android related
**/android/**/gradle-wrapper.jar
**/android/.gradle
**/android/captures/
**/android/gradlew
**/android/gradlew.bat
**/android/local.properties
**/android/**/GeneratedPluginRegistrant.java

# iOS/XCode related
**/ios/**/*.mode1v3
**/ios/**/*.mode2v3
**/ios/**/*.moved-aside
**/ios/**/*.pbxuser
**/ios/**/*.perspectivev3
**/ios/**/*sync/
**/ios/**/.sconsign.dblite
**/ios/**/.tags*
**/ios/**/.vagrant/
**/ios/**/DerivedData/
**/ios/**/Icon?
**/ios/**/Pods/
**/ios/**/.symlinks/
**/ios/**/profile
**/ios/**/xcuserdata
**/ios/.generated/
**/ios/Flutter/App.framework
**/ios/Flutter/Flutter.framework
**/ios/Flutter/Flutter.podspec
**/ios/Flutter/Generated.xcconfig
**/ios/Flutter/app.flx
**/ios/Flutter/app.zip
**/ios/Flutter/flutter_assets/
**/ios/Flutter/flutter_export_environment.sh
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*

# Exceptions to above rules.
!**/ios/**/default.mode1v3
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
10 changes: 0 additions & 10 deletions .metadata

This file was deleted.

21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# auth0

Flutter package for authentication using Auth0 API.Contains basic set of methods like passwordRealm, getUser, logout etc.
Dart package for authentication using Auth0 API.Contains basic set of methods like passwordRealm, getUser, logout etc.

## Usage

Expand All @@ -12,14 +12,13 @@ Auth0Client(clientId, baseUrl,
## Available methods


`updateToken` - Updates current access token for Auth0 connection
`authorizeUrl` - Builds the full authorize endpoint url in the Authorization Server (AS) with given parameters.
`passwordRealm` - Performs Auth with user credentials using the Password Realm Grant
`refreshToken` - Obtains new tokens using the Refresh Token obtained during Auth (requesting offline_access scope)
`getUserInfo` - Returns user information using an access token
`resetPassword` - Requests an email with instructions to change password of a user
`logout` - Makes logout API call
`createUser` - Performs creating user with specified values
`revoke` - Revokes an issued refresh token
`updateToken` - Updates current access token for Auth0 connection
`authorizeUrl` - Builds the full authorize endpoint url in the Authorization Server (AS) with given parameters.
`passwordRealm` - Performs Auth with user credentials using the Password Realm Grant
`refreshToken` - Obtains new tokens using the Refresh Token obtained during Auth (requesting offline_access scope)
`getUserInfo` - Returns user information using an access token
`resetPassword` - Requests an email with instructions to change password of a user
`logout` - Makes logout API call
`createUser` - Performs creating user with specified values
`revoke` - Revokes an issued refresh token
`exchange` - Exchanges a code obtained via /authorize (w/PKCE) for the user's tokens

48 changes: 39 additions & 9 deletions lib/data/auth0_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,33 @@ part of auth0;
class Auth0Client {
final DioWrapper dioWrapper = DioWrapper();
final String clientId;
final String url;
final String clientSecret;
final String domain;

int connectTimeout;
int sendTimeout;
int receiveTimeout;

Auth0Client(this.clientId, this.url,
{String accessToken,
this.connectTimeout,
this.sendTimeout,
this.receiveTimeout}) {
Auth0Client({
this.clientId,
this.clientSecret,
this.domain,
String accessToken,
this.connectTimeout,
this.sendTimeout,
this.receiveTimeout,
}) {
assert(clientId != null);
assert(url != null);
assert(domain != null);

dioWrapper.configure(
url, connectTimeout, sendTimeout, receiveTimeout, accessToken);
'https://$domain', connectTimeout, sendTimeout, receiveTimeout, accessToken);
}

/// Updates current access token for Auth0 connection
void updateToken(String newAccessToken) {
dioWrapper.configure(
url, connectTimeout, sendTimeout, receiveTimeout, newAccessToken);
'https://$domain', connectTimeout, sendTimeout, receiveTimeout, newAccessToken);
}

/// Builds the full authorize endpoint url in the Authorization Server (AS) with given parameters.
Expand Down Expand Up @@ -52,6 +57,31 @@ class Auth0Client {
);
}

/// Performs Auth with user credentials using the Password Realm Grant
/// [params] to send realm parameters
/// @param [String] params.username user's username or email
/// @param [String] params.password user's password
/// @param [String] params.realm name of the Realm where to Auth (or connection name)
/// @param [String] - [params.audience] identifier of Resource Server (RS) to be included as audience (aud claim) of the issued access token
/// @param [String] - [params.scope] scopes requested for the issued tokens. e.g. openid profile
/// @returns a [Future] with [Auth0User]
/// [ref link]: https://auth0.com/docs/api-auth/grant/password#realm-support
Future<Auth0User> passwordGrant(Map<String, String> params) async {
assert(params['username'] != null && params['password'] != null);

var payload = {
...params,
'client_id': this.clientId,
'client_secret': this.clientSecret,
'grant_type': params['realm'] != null
? 'http://auth0.com/oauth/grant-type/password-realm'
: 'password'
};

Response res = await dioWrapper.post('/oauth/token', body: payload);
return Auth0User.fromMap(res.data as Map);
}

/// Performs Auth with user credentials using the Password Realm Grant
/// [params] to send realm parameters
/// @param [String] params.username user's username or email
Expand Down
1 change: 0 additions & 1 deletion lib/data/network/dio_wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class DioWrapper {
/// take [url], concrete route
Future<Response> post(String url, {Map headers, body, encoding}) async =>
await dio.post(url, data: body).then((response) {
print(response);
return response;
}).catchError((error) {
handleError(error, _decoder);
Expand Down
6 changes: 3 additions & 3 deletions lib/domain/user_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ class Auth0User {
DateTime expiresDate;
String tokenType;

Auth0User.fromMap(Map<dynamic, dynamic> snapshot)
Auth0User.fromMap(Map snapshot)
: accessToken = snapshot['access_token'],
refreshToken = snapshot['refresh_token'],
idToken = snapshot['id_token'],
scope = snapshot['scope'],
expiresDate =
DateTime.now().add(Duration(seconds: snapshot['expires_in'] = 0)),
DateTime.now().add(Duration(seconds: snapshot['expires_in'] ?? 0)),
tokenType = snapshot['token_type'];

Map<String, dynamic> toJson() {
Expand All @@ -23,7 +23,7 @@ class Auth0User {
'refresh_token': refreshToken,
'id_token': idToken,
'scope': scope,
'expires_in': expiresDate,
'expires_in': expiresDate.difference(DateTime.now()).inSeconds,
'token_type': tokenType
};
}
Expand Down
129 changes: 1 addition & 128 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,34 +1,6 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
archive:
dependency: transitive
description:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.11"
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.2"
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.5"
charcode:
dependency: transitive
description:
Expand All @@ -43,58 +15,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.11"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
dio:
dependency: "direct main"
description:
name: dio
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.9"
flutter:
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
flutter_test:
dependency: "direct dev"
description: flutter
source: sdk
version: "0.0.0"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.3"
image:
dependency: transitive
description:
name: image
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.4"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.6"
meta:
dependency: transitive
description:
Expand All @@ -109,53 +43,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.4"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0+1"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.5"
sky_engine:
dependency: transitive
description: flutter
source: sdk
version: "0.0.99"
source_span:
dependency: transitive
description:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.5"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.3"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
string_scanner:
dependency: transitive
description:
Expand All @@ -170,33 +64,12 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.11"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.6"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
xml:
dependency: transitive
description:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "3.5.0"
sdks:
dart: ">2.4.0 <3.0.0"
dart: ">=2.8.0 <3.0.0"
Loading