Skip to content

Commit

Permalink
- Code formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
luis901101 committed Dec 8, 2021
1 parent 54bb6a2 commit 6931f0f
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 194 deletions.
153 changes: 82 additions & 71 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@ class MyHomePage extends StatefulWidget {
}

class _MyHomePageState extends State<MyHomePage> {

static const String authorizationEndpointUrl = String.fromEnvironment('AUTHORIZATION_ENDPOINT_URL', defaultValue: 'https://test-auth-endpoint.com');
static const String tokenEndpointUrl = String.fromEnvironment('TOKEN_ENDPOINT_URL', defaultValue: 'https://test-token-endpoint.com');
static const String clientSecret = String.fromEnvironment('CLIENT_SECRET', defaultValue: 'XXXXXXXXX');
static const String clientId = String.fromEnvironment('CLIENT_ID', defaultValue: 'realmClientID');
static const String redirectUrl = String.fromEnvironment('REDIRECT_URL', defaultValue: 'https://test-redirect-to.com');
final List<String> scopes = const String.fromEnvironment('SCOPES', defaultValue: 'https://test-redirect-to.com').split(' ');
static const String authorizationEndpointUrl = String.fromEnvironment(
'AUTHORIZATION_ENDPOINT_URL',
defaultValue: 'https://test-auth-endpoint.com');
static const String tokenEndpointUrl = String.fromEnvironment(
'TOKEN_ENDPOINT_URL',
defaultValue: 'https://test-token-endpoint.com');
static const String clientSecret =
String.fromEnvironment('CLIENT_SECRET', defaultValue: 'XXXXXXXXX');
static const String clientId =
String.fromEnvironment('CLIENT_ID', defaultValue: 'realmClientID');
static const String redirectUrl = String.fromEnvironment('REDIRECT_URL',
defaultValue: 'https://test-redirect-to.com');
final List<String> scopes = const String.fromEnvironment('SCOPES',
defaultValue: 'https://test-redirect-to.com')
.split(' ');

String authResponse = 'Authorization data will be shown here';

Expand All @@ -61,8 +69,7 @@ class _MyHomePageState extends State<MyHomePage> {
onPressed: loginV1,
child: const Text('Login variant 1'),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.green)
),
backgroundColor: MaterialStateProperty.all(Colors.green)),
),
const SizedBox(height: 4),
ElevatedButton(
Expand All @@ -78,31 +85,33 @@ class _MyHomePageState extends State<MyHomePage> {

void loginV1() async {
final result = await OAuthWebScreen.start(
context: context,
authorizationEndpointUrl: authorizationEndpointUrl,
tokenEndpointUrl: tokenEndpointUrl,
clientSecret: clientSecret,
clientId: clientId,
redirectUrl: redirectUrl,
scopes: scopes,
promptValues: const ['login'],
loginHint: '[email protected]',
onCertificateValidate: (certificate) {///This is recommended
/// Do certificate validations here
/// If false is returned then a CertificateException() will be thrown
return true;
},
textLocales: { ///Optionally texts can be localized
OAuthWebView.backButtonTooltipKey: 'Ir atrás',
OAuthWebView.forwardButtonTooltipKey: 'Ir adelante',
OAuthWebView.reloadButtonTooltipKey: 'Recargar',
OAuthWebView.clearCacheButtonTooltipKey: 'Limpiar caché',
OAuthWebView.closeButtonTooltipKey: 'Cerrar',
OAuthWebView.clearCacheWarningMessageKey: '¿Está seguro que desea limpiar la caché?',
}
);
if(result != null) {
if(result is Credentials) {
context: context,
authorizationEndpointUrl: authorizationEndpointUrl,
tokenEndpointUrl: tokenEndpointUrl,
clientSecret: clientSecret,
clientId: clientId,
redirectUrl: redirectUrl,
scopes: scopes,
promptValues: const ['login'],
loginHint: '[email protected]',
onCertificateValidate: (certificate) {
///This is recommended
/// Do certificate validations here
/// If false is returned then a CertificateException() will be thrown
return true;
},
textLocales: {
///Optionally texts can be localized
OAuthWebView.backButtonTooltipKey: 'Ir atrás',
OAuthWebView.forwardButtonTooltipKey: 'Ir adelante',
OAuthWebView.reloadButtonTooltipKey: 'Recargar',
OAuthWebView.clearCacheButtonTooltipKey: 'Limpiar caché',
OAuthWebView.closeButtonTooltipKey: 'Cerrar',
OAuthWebView.clearCacheWarningMessageKey:
'¿Está seguro que desea limpiar la caché?',
});
if (result != null) {
if (result is Credentials) {
authResponse = getPrettyCredentialsJson(result);
} else {
authResponse = result.toString();
Expand All @@ -115,44 +124,46 @@ class _MyHomePageState extends State<MyHomePage> {

void loginV2() {
OAuthWebScreen.start(
context: context,
authorizationEndpointUrl: authorizationEndpointUrl,
tokenEndpointUrl: tokenEndpointUrl,
clientSecret: clientSecret,
clientId: clientId,
redirectUrl: redirectUrl,
scopes: scopes,
promptValues: const ['login'],
loginHint: '[email protected]',
onCertificateValidate: (certificate) { ///This is recommended
/// Do certificate validations here
/// If false is returned then a CertificateException() will be thrown
return true;
},
textLocales: { ///Optionally text can be localized
OAuthWebView.backButtonTooltipKey: 'Ir atrás',
OAuthWebView.forwardButtonTooltipKey: 'Ir adelante',
OAuthWebView.reloadButtonTooltipKey: 'Recargar',
OAuthWebView.clearCacheButtonTooltipKey: 'Limpiar caché',
OAuthWebView.closeButtonTooltipKey: 'Cerrar',
OAuthWebView.clearCacheWarningMessageKey: '¿Está seguro que desea limpiar la caché?',
},
onSuccess: (credentials) {
setState(() {
authResponse = getPrettyCredentialsJson(credentials);
context: context,
authorizationEndpointUrl: authorizationEndpointUrl,
tokenEndpointUrl: tokenEndpointUrl,
clientSecret: clientSecret,
clientId: clientId,
redirectUrl: redirectUrl,
scopes: scopes,
promptValues: const ['login'],
loginHint: '[email protected]',
onCertificateValidate: (certificate) {
///This is recommended
/// Do certificate validations here
/// If false is returned then a CertificateException() will be thrown
return true;
},
textLocales: {
///Optionally text can be localized
OAuthWebView.backButtonTooltipKey: 'Ir atrás',
OAuthWebView.forwardButtonTooltipKey: 'Ir adelante',
OAuthWebView.reloadButtonTooltipKey: 'Recargar',
OAuthWebView.clearCacheButtonTooltipKey: 'Limpiar caché',
OAuthWebView.closeButtonTooltipKey: 'Cerrar',
OAuthWebView.clearCacheWarningMessageKey:
'¿Está seguro que desea limpiar la caché?',
},
onSuccess: (credentials) {
setState(() {
authResponse = getPrettyCredentialsJson(credentials);
});
},
onError: (error) {
setState(() {
authResponse = error.toString();
});
},
onCancel: () {
setState(() {
authResponse = 'User cancelled authentication';
});
});
},
onError: (error) {
setState(() {
authResponse = error.toString();
});
},
onCancel: () {
setState(() {
authResponse = 'User cancelled authentication';
});
}
);
}

String getPrettyCredentialsJson(Credentials credentials) {
Expand Down
78 changes: 37 additions & 41 deletions lib/src/oauth_web_screen.dart
Original file line number Diff line number Diff line change
@@ -1,47 +1,44 @@

import 'package:flutter/material.dart';
import 'package:oauth2/oauth2.dart';
import 'package:oauth_webauth/src/oauth_web_view.dart';

class OAuthWebScreen extends StatelessWidget {

static Future? start({
Key? key,
required BuildContext context,
required String authorizationEndpointUrl,
required String tokenEndpointUrl,
required String redirectUrl,
required String clientId,
String? clientSecret,
List<String>? scopes,
String? loginHint,
List<String>? promptValues,
ValueChanged<Credentials>? onSuccess,
ValueChanged<dynamic>? onError,
VoidCallback? onCancel,
CertificateValidator? onCertificateValidate,
ThemeData? themeData,
Map<String, String>? textLocales
}) => Navigator.push(context,
MaterialPageRoute(builder: (context) =>
OAuthWebScreen(
authorizationEndpointUrl: authorizationEndpointUrl,
tokenEndpointUrl: tokenEndpointUrl,
redirectUrl: redirectUrl,
clientId: clientId,
clientSecret: clientSecret,
scopes: scopes,
loginHint: loginHint,
promptValues: promptValues,
onSuccess: onSuccess,
onError: onError,
onCancel: onCancel,
onCertificateValidate: onCertificateValidate,
themeData: themeData,
textLocales: textLocales,
)
));

static Future? start(
{Key? key,
required BuildContext context,
required String authorizationEndpointUrl,
required String tokenEndpointUrl,
required String redirectUrl,
required String clientId,
String? clientSecret,
List<String>? scopes,
String? loginHint,
List<String>? promptValues,
ValueChanged<Credentials>? onSuccess,
ValueChanged<dynamic>? onError,
VoidCallback? onCancel,
CertificateValidator? onCertificateValidate,
ThemeData? themeData,
Map<String, String>? textLocales}) =>
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => OAuthWebScreen(
authorizationEndpointUrl: authorizationEndpointUrl,
tokenEndpointUrl: tokenEndpointUrl,
redirectUrl: redirectUrl,
clientId: clientId,
clientSecret: clientSecret,
scopes: scopes,
loginHint: loginHint,
promptValues: promptValues,
onSuccess: onSuccess,
onError: onError,
onCancel: onCancel,
onCertificateValidate: onCertificateValidate,
themeData: themeData,
textLocales: textLocales,
)));

final String authorizationEndpointUrl;
final String tokenEndpointUrl;
Expand Down Expand Up @@ -137,11 +134,10 @@ class OAuthWebScreen extends StatelessWidget {
}

Future<bool> onBackPressed() async {
if(!((await paymentViewStateKey.currentState?.onBackPressed()) ?? false)) {
if (!((await paymentViewStateKey.currentState?.onBackPressed()) ?? false)) {
return false;
}
onCancel?.call();
return true;
}

}
Loading

0 comments on commit 6931f0f

Please sign in to comment.