Skip to content

Commit

Permalink
✨ Bring blowfish back to random and storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Feb 18, 2020
1 parent 6e7db04 commit 76af1d5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
27 changes: 15 additions & 12 deletions lib/constants/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,24 @@ class Constants {
};

static Map<String, dynamic> loginParams({
@required String blowfish,
String username,
String password,
String ticket,
}) =>
{
'appid': appId,
'blowfish': DeviceUtils.deviceUuid,
if (ticket != null) 'ticket': '$ticket',
if (username != null) 'account': '$username',
if (password != null) 'password': '${sha1.convert(password.toUtf8())}',
if (password != null) 'encrypt': 1,
if (username != null) 'unitid': unitId,
if (username != null) 'unitcode': 'jmu',
'clientinfo': jsonEncode(loginClientInfo),
};
}) {
assert(blowfish != null, 'blowfish cannot be null');
return {
'appid': appId,
'blowfish': blowfish,
if (ticket != null) 'ticket': '$ticket',
if (username != null) 'account': '$username',
if (password != null) 'password': '${sha1.convert(password.toUtf8())}',
if (password != null) 'encrypt': 1,
if (username != null) 'unitid': unitId,
if (username != null) 'unitcode': 'jmu',
'clientinfo': jsonEncode(loginClientInfo),
};
}

static Iterable<LocalizationsDelegate<dynamic>> get localizationsDelegates => [
GlobalWidgetsLocalizations.delegate,
Expand Down
6 changes: 5 additions & 1 deletion lib/model/user_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ part of 'beans.dart';

/// 用户信息实体
///
/// [sid] 用户token, [ticket] 用户当前token, [blowfish] 用户设备uuid
/// [sid] 用户token, [ticket] 用户用于更新token的凭证, [blowfish] 用户设备随机uuid,
/// [uid] 用户uid, [unitId] 组织/学校id, [workId] 工号/学号, [classId] 班级id,
/// [name] 名字, [signature] 签名, [gender] 性别, [isFollowing] 是否已关注
class UserInfo {
/// For Login Process
String sid;
String ticket;
String blowfish;
bool isTeacher;

/// Common Object
Expand All @@ -31,6 +32,7 @@ class UserInfo {
this.name,
this.signature,
this.ticket,
this.blowfish,
this.isTeacher,
this.unitId,
this.workId,
Expand All @@ -54,6 +56,7 @@ class UserInfo {
'name': name,
'signature': signature,
'ticket': ticket,
'blowfish': blowfish,
'isTeacher': isTeacher,
'unitId': unitId,
'workId': workId,
Expand Down Expand Up @@ -114,6 +117,7 @@ class UserInfo {
name: json['username'] ?? json['uid'].toString(),
signature: json['signature'],
ticket: json['ticket'],
blowfish: json['blowfish'],
isTeacher: json['isTeacher'] ?? int.parse(json['type'].toString()) == 1,
unitId: json['unitId'] ?? json['unitid'],
workId: (json['workId'] ?? json['workid'] ?? json['uid']).toString(),
Expand Down
21 changes: 17 additions & 4 deletions lib/utils/data_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:dio/dio.dart';
import 'package:uuid/uuid.dart';

import 'package:openjmu/constants/constants.dart';

Expand All @@ -12,14 +13,19 @@ class DataUtils {

static final settingsBox = HiveBoxes.settingsBox;

static final spBlowfish = 'blowfish';
static final spIsLogin = 'isLogin';
static final spTicket = 'ticket';

static final spUserUid = 'userUid';
static final spUserWorkId = 'userWorkId';

static Future<bool> login(String username, String password) async {
final params = Constants.loginParams(username: username, password: password);
final blowfish = Uuid().v4();
final params = Constants.loginParams(
username: username,
password: password,
blowfish: blowfish,
);
try {
final loginData = (await UserAPI.login(params)).data;
UserAPI.currentUser.sid = loginData['sid'];
Expand All @@ -31,6 +37,7 @@ class DataUtils {
'username': user['username'],
'signature': user['signature'],
'ticket': loginData['ticket'],
'blowfish': blowfish,
'isTeacher': int.parse(user['type'].toString()) == 1,
'unitId': loginData['unitid'],
'workId': user['workid'],
Expand Down Expand Up @@ -77,7 +84,7 @@ class DataUtils {

static Future recoverLoginInfo() async {
final info = getSpTicket();
UserAPI.currentUser.sid = info['ticket'];
UserAPI.currentUser.ticket = info['ticket'];
}

static Future reFetchTicket() async {
Expand Down Expand Up @@ -109,6 +116,7 @@ class DataUtils {
'uid': currentUser.uid,
'username': data['username'],
'signature': data['signature'],
'blowfish': settingsBox.get(spBlowfish),
'ticket': settingsBox.get(spTicket),
'isTeacher': int.parse(data['type'].toString()) == 1,
'unitId': data['unitid'],
Expand All @@ -131,6 +139,7 @@ class DataUtils {
if (data != null) {
setUserInfo(data);
await settingsBox.putAll({
spBlowfish: data['blowfish'],
spIsLogin: true,
spTicket: data['ticket'],
spUserUid: data['uid'],
Expand All @@ -155,7 +164,11 @@ class DataUtils {
static Future<bool> getTicket() async {
try {
debugPrint('Fetch new ticket with: ${settingsBox.get(spTicket)}');
final params = Constants.loginParams(ticket: settingsBox.get(spTicket));
final params = Constants.loginParams(
blowfish: settingsBox.get(spBlowfish),
ticket: settingsBox.get(spTicket),
);
NetUtils.cookieJar.deleteAll();
NetUtils.tokenCookieJar.deleteAll();
final response = (await NetUtils.tokenDio.post(API.loginTicket, data: params)).data;
updateSid(response);
Expand Down

0 comments on commit 76af1d5

Please sign in to comment.