-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiitg_auth.dart
115 lines (110 loc) · 3.58 KB
/
iitg_auth.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import 'package:http/http.dart' as http;
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';
class Auth {
String username = "",
password = "",
logoutUrl = "https://192.168.193.1:1442/logout?090706070f040c06",
keepAliveUrl = "",
message = "Not logged In.";
bool isLoggedIn = false;
Auth() {}
static const agnigarhLoginUrl = "https://agnigarh.iitg.ac.in:1442/login?";
Future<void> login() async {
// update the credentials
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString('username', username);
await prefs.setString('password', password);
String msg = message;
bool loginSuccess = false;
try {
var resp = await http.get(Uri.parse(agnigarhLoginUrl));
if (resp.statusCode == 200) {
final regExp = RegExp(r'value="[a-zA-Z0-9.\/:\?]*');
final matches = regExp.allMatches(resp.body).take(2);
String tredir = matches.first.group(0).toString().substring(7);
String magic = matches.elementAt(1).group(0).toString().substring(7);
Map<String, String> data = {
"4Tredir": tredir,
"magic": magic,
"username": username,
"password": password
};
print("Logging in as $username.....\n");
resp = await http.post(Uri.parse("https://agnigarh.iitg.ac.in:1442"),
body: data);
if (resp.body.contains("logged in as")) {
loginSuccess = true;
msg = "You are logged in as $username.";
final logoutRegex =
RegExp(r'https:\/\/[a-zA-Z0-9.:]*\/logout\?[a-zA-Z0-9]*');
final keepAliveRegex =
RegExp(r'https:\/\/[a-zA-Z0-9.:]*\/keepalive\?[a-zA-Z0-9]*');
var match = logoutRegex.firstMatch(resp.body);
if (match != null) {
logoutUrl = match.group(0).toString();
}
match = keepAliveRegex.firstMatch(resp.body);
if (match != null) {
keepAliveUrl = match.group(0).toString();
}
} else if (resp.body.contains("Firewall authentication failed")) {
loginSuccess = false;
msg = "Firewall authentication failed. Please try again.";
} else if (resp.body.contains("concurrent authentication")) {
loginSuccess = false;
msg = "You are logged in somewhere else too.";
} else {
msg = "Couldn't login.";
}
} else {
loginSuccess = false;
msg = "Login failed. Please try again!";
}
} catch (e) {
print(e.toString());
loginSuccess = false;
msg = "Can't connect with agnigarh server.";
}
isLoggedIn = loginSuccess;
message = msg;
print(message);
}
Future<void> logout() async {
if (logoutUrl == "" || isLoggedIn == false) {
return;
}
try {
final resp = await http.get(Uri.parse(logoutUrl));
if (resp.statusCode == 200) {
isLoggedIn = false;
message = "Logged out successfully!";
} else {
message = "Unable to logout";
}
} catch (e) {
print(e.toString());
message = "Unable to logout";
}
}
Future<bool> keepAlive() async {
try {
print("keeping alive...");
if (keepAliveUrl == "") {
return false;
}
final resp = await http.get(Uri.parse(keepAliveUrl));
if (resp.statusCode == 200) {
print("refreshed!");
return true;
} else {
throw Exception();
}
} catch (e) {
print(e);
isLoggedIn = false;
message = "Could not login.";
return false;
}
}
}