Skip to content

Commit

Permalink
Mossad
Browse files Browse the repository at this point in the history
  • Loading branch information
Dvd848 committed May 21, 2019
1 parent c3be295 commit d011aaf
Show file tree
Hide file tree
Showing 25 changed files with 28,733 additions and 0 deletions.
601 changes: 601 additions & 0 deletions 2019_Mossad/Challenge1.md

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions 2019_Mossad/Challenge1_files/AuthURL.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class AuthURL {
String _url;

AuthURL(this._url);

AuthURL.map(dynamic obj) {
this._url = obj["AuthURL"];
}

String get url => _url;

Map<String, dynamic> toMap() {
var map = new Map<String, dynamic>();
map["Url"] = _url;

return map;
}
}
17 changes: 17 additions & 0 deletions 2019_Mossad/Challenge1_files/cookie_jar.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:locksmither/models/token.dart';

class CookieJar {
static CookieJar _instance = new CookieJar.internal();
CookieJar.internal();
factory CookieJar() => _instance;

Token _token;

void setToken(Token token) {
this._token = token;
}

Token getToken() {
return this._token;
}
}
36 changes: 36 additions & 0 deletions 2019_Mossad/Challenge1_files/home_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:flutter/material.dart';
import 'package:locksmither/network/cookie_jar.dart';
import 'package:locksmither/models/token.dart';

class HomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new HomePageState();
}
}

class HomePageState extends State<HomePage> {
Token _token;

HomePageState() {
CookieJar jar = new CookieJar();
_token = jar.getToken();
}

String get lockURL => _token.lockURL;
int get time => _token.time;


@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text("Home"),),
body: new Center(

child: new Text("Success!\nLock Url: $lockURL\nObtained in: $time nanoseconds"
),
),
);
}

}
120 changes: 120 additions & 0 deletions 2019_Mossad/Challenge1_files/login_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:locksmither/network/network_actions.dart';
import 'package:locksmither/network/cookie_jar.dart';
import 'package:locksmither/models/token.dart';

class LoginPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new LoginPageState();
}
}

class LoginPageState extends State<LoginPage> {
BuildContext _ctx;

bool _isLoading = false;
final formKey = new GlobalKey<FormState>();
final scaffoldKey = new GlobalKey<ScaffoldState>();
String _password;
String _seed;

NetworkActions _networkActions = new NetworkActions();

LoginPageState();

void _submit() async {
final form = formKey.currentState;

if (form.validate()) {
setState(() => _isLoading = true);
form.save();

_networkActions.login(_seed, _password)
.then((result) => _loginCompleted(result))
.catchError((e) {
_loginCompleted(new Token("", false, 0));
});
}
}

void _loginCompleted(Token result) async {
Duration dur = new Duration(microseconds: (result.time/1000).round());
_showSnackBar("Completed in: " + dur.inMilliseconds.toString() + " milliseconds result is: " + result.isValid.toString());
setState(() => _isLoading = false);
if (result != null && result.isValid) {
CookieJar jar = new CookieJar();
jar.setToken(result);
Navigator.of(_ctx).pushReplacementNamed("/home");
}
}


void _showSnackBar(String text) {
scaffoldKey.currentState
.showSnackBar(new SnackBar(content: new Text(text)));
}

@override
Widget build(BuildContext context) {
_ctx = context;
var loginBtn = new RaisedButton(
onPressed: _submit,
child: new Text("LOGIN"),
color: Colors.blue,
);
var loginForm = new Column(
children: <Widget>[
new Text(
"iWalk-LockSmither App",
textScaleFactor: 2.0,
),
new Form(
key: formKey,
child: new Column(
children: <Widget>[
new Padding(
padding: const EdgeInsets.all(8.0),
child: new TextFormField(
onSaved: (val) => _seed = val,
decoration: new InputDecoration(labelText: "Seed"),
),
),
new Padding(
padding: const EdgeInsets.all(8.0),
child: new TextFormField(
onSaved: (val) => _password = val,
decoration: new InputDecoration(labelText: "Password"),
),
),
],
),
),
_isLoading ? new CircularProgressIndicator() : loginBtn
],
crossAxisAlignment: CrossAxisAlignment.center,
);

return new Scaffold(
appBar: null,
key: scaffoldKey,
body: new Container(
child: new Center(
child: new ClipRect(
child: new BackdropFilter(
filter: new ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
child: new Container(
child: loginForm,
height: 300.0,
width: 300.0,
decoration: new BoxDecoration(
color: Colors.grey.shade200.withOpacity(0.5)),
),
),
),
),
),
);
}
}
20 changes: 20 additions & 0 deletions 2019_Mossad/Challenge1_files/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:flutter/material.dart';
import 'package:locksmither/routes.dart';

void main() => runApp(LockSmitherApp());

class LockSmitherApp extends StatelessWidget {
@override

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'iWalk-LockSmither App',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
routes: routes,
);
}
}
33 changes: 33 additions & 0 deletions 2019_Mossad/Challenge1_files/network_actions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'dart:async';

import 'dart:convert';
import 'package:locksmither/network/network_wrapper.dart';
import 'package:locksmither/models/token.dart';
import 'package:locksmither/models/AuthURL.dart';

class NetworkActions {
NetworkWrapper _netUtil = new NetworkWrapper();
static const BASE_URL = "http://35.246.158.51:8070";
static const LOGIN_URL = BASE_URL + "/auth/getUrl";

Future<Token> login(String seed, String password) {
var headers = new Map<String,String>();
return _netUtil.get(LOGIN_URL, headers:headers).then((dynamic authUrl) {
try {
if (authUrl == null) {
return Future<Token>.sync(() => new Token("", false, 0));
}
var loginUrl = BASE_URL + AuthURL.map(json.decode(authUrl.body)).url;
Map<String,String> body = { "Seed": seed, "Password": password };
Map<String,String> headers = {"content-type": "application/json"};
return _netUtil.post(loginUrl,body: json.encode(body), headers:headers).then((dynamic token) {
return Token.map(token);
});
} catch (e) {
return Future<Token>.sync(() => new Token("", false, 0));
}
}).catchError((e) {
return null;
});
}
}
66 changes: 66 additions & 0 deletions 2019_Mossad/Challenge1_files/network_wrapper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;

class NetworkWrapper {
// next three lines makes this class a Singleton
static const USER_AGENT = "iWalk-v2";
static NetworkWrapper _instance = new NetworkWrapper.internal();
NetworkWrapper.internal();
factory NetworkWrapper() => _instance;

final JsonDecoder _decoder = new JsonDecoder();

void addUserAgent(Map headers) {
if (!headers.containsKey("User-Agent")) {
headers["User-Agent"] = USER_AGENT;
}
}

Future<dynamic> getObject(String url, {Map headers}) {
addUserAgent(headers);
try {
return http.get(url, headers: headers).then((http.Response response) {
final String res = response.body;
final int statusCode = response.statusCode;

if (statusCode < 200 || statusCode > 400 || json == null) {
return null;
}
return _decoder.convert(res);
}).catchError((e) { return null; });
} catch (e) {
return null;
}
}

Future<http.Response> get(String url, {Map<String,String> headers}) {
addUserAgent(headers);
try {
return http.get(url, headers: headers).then((http.Response response) {
return response;
}).catchError((e) { return null; });
} catch (e) {
return null;
}
}

Future<dynamic> post(String url, {Map<String,String> headers, body, encoding}) {
addUserAgent(headers);
try {
return http
.post(url, body: body, headers: headers, encoding: encoding)
.then((http.Response response) {
final String res = response.body;
final int statusCode = response.statusCode;

if (statusCode < 200 || statusCode > 400 || json == null) {
return null;
}
return _decoder.convert(res);
}).catchError((e) { return null; });
} catch (e) {
return null;
}
}
}
9 changes: 9 additions & 0 deletions 2019_Mossad/Challenge1_files/routes.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:flutter/material.dart';
import 'package:locksmither/pages/login_page.dart';
import 'package:locksmither/pages/home_page.dart';

final routes = {
'/login': (BuildContext context) => new LoginPage(),
'/home': (BuildContext context) => new HomePage(),
'/' : (BuildContext context) => new LoginPage(),
};
26 changes: 26 additions & 0 deletions 2019_Mossad/Challenge1_files/token.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Token {
String _lockURL;
bool _isValid;
int _time;

Token(this._lockURL, this._isValid, this._time);

Token.map(dynamic obj) {
this._lockURL = obj["LockURL"];
this._isValid = obj["IsValid"];
this._time = obj["Time"];
}

String get lockURL => _lockURL;
bool get isValid => _isValid;
int get time => _time != null ? _time : 0;

Map<String, dynamic> toMap() {
var map = new Map<String, dynamic>();
map["LockURL"] = _lockURL;
map["IsValid"] = _isValid;
map["Time"] = _time;

return map;
}
}
Loading

0 comments on commit d011aaf

Please sign in to comment.