Skip to content

Commit

Permalink
Nullsafety
Browse files Browse the repository at this point in the history
  • Loading branch information
Diefferson Santos committed Apr 16, 2021
1 parent bed9068 commit e232cb1
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 154 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ Downgrade Alamofire version

## 1.0.4

Support iOS 9.0 and above
Support iOS 9.0 and above

## 2.0.0

Flutter 2 support and null-safety
69 changes: 31 additions & 38 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ class _MyAppState extends State<MyApp> {
}

// Platform messages are asynchronous, so we initialize in an async method.
check(String url, String fingerprint, SHA sha, Map<String, String> headerHttp, int timeout) async {

List<String> allowedShA1FingerprintList = new List();
check(String url, String fingerprint, SHA sha, Map<String, String> headerHttp,
int timeout) async {
List<String> allowedShA1FingerprintList = [];
allowedShA1FingerprintList.add(fingerprint);

try {
// Platform messages may fail, so we use a try/catch PlatformException.
String checkMsg = await HttpCertificatePinning.check(serverURL: url,
String checkMsg = await HttpCertificatePinning.check(
serverURL: url,
headerHttp: headerHttp,
sha: sha,
allowedSHAFingerprints: allowedShA1FingerprintList,
Expand All @@ -44,36 +45,33 @@ class _MyAppState extends State<MyApp> {
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted)
return;
if (!mounted) return;

Scaffold.of(scaffoldContext).showSnackBar(
new SnackBar(
content: new Text(checkMsg),
duration: Duration(seconds: 1),
backgroundColor: Colors.green,
),

);
}catch (e){
} catch (e) {
Scaffold.of(scaffoldContext).showSnackBar(
new SnackBar(
content: new Text(e.toString()),
duration: Duration(seconds: 1),
backgroundColor: Colors.red,
),

);
}

}

void submit() {
// First validate form.
if (_formKey.currentState.validate()) {
_formKey.currentState.save(); // Save our form now.

this.check(_data.serverURL, _data.allowedSHAFingerprint, _data.sha, _data.headerHttp, _data.timeout);
this.check(_data.serverURL, _data.allowedSHAFingerprint, _data.sha,
_data.headerHttp, _data.timeout);
}
}

Expand All @@ -85,8 +83,7 @@ class _MyAppState extends State<MyApp> {
appBar: new AppBar(
title: new Text('Ssl Pinning Plugin'),
),
body:
new Builder(builder: (BuildContext context) {
body: new Builder(builder: (BuildContext context) {
this.scaffoldContext = context;
return Container(
padding: EdgeInsets.all(20.0),
Expand All @@ -98,22 +95,29 @@ class _MyAppState extends State<MyApp> {
keyboardType: TextInputType.url,
decoration: InputDecoration(
hintText: 'https://yourdomain.com',
labelText: 'URL'
),
labelText: 'URL'),
validator: (value) {
if (value.isEmpty) {
return 'Please enter some url';
}
},
onSaved: (String value) {
this._data.serverURL = value;
}
),
}),
DropdownButton(
items: [DropdownMenuItem(child: Text(SHA.SHA1.toString()), value: SHA.SHA1,), DropdownMenuItem(child: Text(SHA.SHA256.toString()), value: SHA.SHA256,)],
items: [
DropdownMenuItem(
child: Text(SHA.SHA1.toString()),
value: SHA.SHA1,
),
DropdownMenuItem(
child: Text(SHA.SHA256.toString()),
value: SHA.SHA256,
)
],
value: _data.sha,
isExpanded: true,
onChanged: (SHA val){
onChanged: (SHA val) {
setState(() {
this._data.sha = val;
});
Expand All @@ -123,54 +127,43 @@ class _MyAppState extends State<MyApp> {
keyboardType: TextInputType.text,
decoration: InputDecoration(
hintText: 'OO OO OO OO OO OO OO OO OO OO',
labelText: 'Fingerprint'
),
labelText: 'Fingerprint'),
validator: (value) {
if (value.isEmpty) {
return 'Please enter some fingerprint';
}
},
onSaved: (String value) {
this._data.allowedSHAFingerprint = value;
}
),
}),
TextFormField(
keyboardType: TextInputType.number,
initialValue: '60',
decoration: InputDecoration(
hintText: '60',
labelText: 'Timeout'
),
hintText: '60', labelText: 'Timeout'),
validator: (value) {
if (value.isEmpty) {
return 'Please enter some timeout';
}
},
onSaved: (String value) {
this._data.timeout = int.parse(value);
}
),
}),
Container(
child: RaisedButton(
child: Text(
'Check',
style: TextStyle(
color: Colors.white
),
style: TextStyle(color: Colors.white),
),
onPressed: () => submit(),
color: Colors.blue,
),
margin: EdgeInsets.only(
top: 20.0
),
margin: EdgeInsets.only(top: 20.0),
)
],
),
)
);
})
),
));
})),
);
}
}
17 changes: 5 additions & 12 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
cupertino_icons:
dependency: "direct main"
description:
name: cupertino_icons
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
dio:
dependency: transitive
description:
name: dio
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.10"
version: "4.0.0"
fake_async:
dependency: transitive
description:
Expand All @@ -80,21 +73,21 @@ packages:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.2"
version: "0.13.1"
http_certificate_pinning:
dependency: "direct dev"
description:
path: ".."
relative: true
source: path
version: "1.0.4"
version: "2.0.0"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.4"
version: "4.0.0"
matcher:
dependency: transitive
description:
Expand Down Expand Up @@ -185,5 +178,5 @@ packages:
source: hosted
version: "2.1.0"
sdks:
dart: ">=2.12.0-0.0 <3.0.0"
dart: ">=2.12.0 <3.0.0"
flutter: ">=1.12.0"
45 changes: 1 addition & 44 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,12 @@ dependencies:
flutter:
sdk: flutter

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2

dev_dependencies:
flutter_test:
sdk: flutter

http_certificate_pinning:
path: ../

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true

# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg

# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.

# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages

# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
uses-material-design: true
21 changes: 10 additions & 11 deletions lib/certificate_pinning_interceptor.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import 'dart:async';

import 'package:dio/dio.dart';
import 'package:http_certificate_pinning/http_certificate_pinning.dart';

class CertificatePinningInterceptor extends Interceptor{

class CertificatePinningInterceptor extends Interceptor {
final List<String> _allowedSHAFingerprints;

CertificatePinningInterceptor(this._allowedSHAFingerprints);

@override
Future onRequest(RequestOptions options) async {

Future onRequest(
RequestOptions options, RequestInterceptorHandler handler) async {
final secure = await HttpCertificatePinning.check(
serverURL: options.baseUrl,
headerHttp: options.headers.map((a,b)=> MapEntry(a, b.toString())),
headerHttp: options.headers.map((a, b) => MapEntry(a, b.toString())),
sha: SHA.SHA256,
allowedSHAFingerprints:_allowedSHAFingerprints,
timeout : 50
);
allowedSHAFingerprints: _allowedSHAFingerprints,
timeout: 50);

if(secure.contains("CONNECTION_SECURE")){
return super.onRequest(options);
}else{
if (secure.contains("CONNECTION_SECURE")) {
return super.onRequest(options, handler);
} else {
throw Exception("CONNECTION_NOT_SECURE");
}
}
Expand Down
25 changes: 16 additions & 9 deletions lib/http_certificate_pinning.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,35 @@ import 'dart:async';

import 'package:flutter/services.dart';

enum SHA {SHA1, SHA256}
enum SHA { SHA1, SHA256 }

class HttpCertificatePinning {

static const MethodChannel _channel =
const MethodChannel('http_certificate_pinning');
const MethodChannel('http_certificate_pinning');

static final HttpCertificatePinning _sslPinning = HttpCertificatePinning._internal();
static final HttpCertificatePinning _sslPinning =
HttpCertificatePinning._internal();

factory HttpCertificatePinning() => _sslPinning;

HttpCertificatePinning._internal() {
_channel.setMethodCallHandler(_platformCallHandler);
}

static Future<String> check({ String serverURL, Map<String, String> headerHttp, SHA sha, List<String> allowedSHAFingerprints, int timeout }) async {
static Future<String> check({
required String serverURL,
required SHA sha,
required List<String> allowedSHAFingerprints,
Map<String, String>? headerHttp,
int? timeout,
}) async {
final Map<String, dynamic> params = <String, dynamic>{
"url" : serverURL,
"headers" : headerHttp,
"url": serverURL,
"headers": headerHttp,
"type": sha.toString().split(".").last,
"fingerprints" : allowedSHAFingerprints.map((a) => a.replaceAll(":", "")).toList(),
"timeout" : timeout
"fingerprints":
allowedSHAFingerprints.map((a) => a.replaceAll(":", "")).toList(),
"timeout": timeout
};
String resp = await _channel.invokeMethod('check', params);
return resp;
Expand Down
Loading

0 comments on commit e232cb1

Please sign in to comment.