Netease cloud music login

This commit is contained in:
LittleSheep 2024-09-06 12:41:35 +08:00
parent 6cdc025c40
commit 6509cd2511
10 changed files with 602 additions and 170 deletions

View File

@ -9,15 +9,23 @@ import 'package:rhythm_box/providers/database.dart';
import 'package:rhythm_box/services/database/database.dart'; import 'package:rhythm_box/services/database/database.dart';
extension ExpirationAuthenticationTableData on AuthenticationTableData { extension ExpirationAuthenticationTableData on AuthenticationTableData {
bool get isExpired => DateTime.now().isAfter(expiration); bool get isExpired => DateTime.now().isAfter(spotifyExpiration);
String? getCookie(String key) => cookie.value String? getCookie(String key) => spotifyCookie.value
.split('; ') .split('; ')
.firstWhereOrNull((c) => c.trim().startsWith('$key=')) .firstWhereOrNull((c) => c.trim().startsWith('$key='))
?.trim() ?.trim()
.split('=') .split('=')
.last .last
.replaceAll(';', ''); .replaceAll(';', '');
String? getNeteaseCookie(String key) => neteaseCookie?.value
.split(';')
.firstWhereOrNull((c) => c.trim().startsWith('$key='))
?.trim()
.split('=')
.last
.replaceAll(';', '');
} }
class AuthenticationProvider extends GetxController { class AuthenticationProvider extends GetxController {
@ -55,18 +63,18 @@ class AuthenticationProvider extends GetxController {
void _setRefreshTimer() { void _setRefreshTimer() {
refreshTimer?.cancel(); refreshTimer?.cancel();
if (auth.value != null && auth.value!.isExpired) { if (auth.value != null && auth.value!.isExpired) {
refreshCredentials(); refreshSpotifyCredentials();
} }
refreshTimer = Timer( refreshTimer = Timer(
auth.value!.expiration.difference(DateTime.now()), auth.value!.spotifyExpiration.difference(DateTime.now()),
() => refreshCredentials(), () => refreshSpotifyCredentials(),
); );
} }
Future<void> refreshCredentials() async { Future<void> refreshSpotifyCredentials() async {
final database = Get.find<DatabaseProvider>().database; final database = Get.find<DatabaseProvider>().database;
final refreshedCredentials = final refreshedCredentials =
await credentialsFromCookie(auth.value!.cookie.value); await credentialsFromCookie(auth.value!.spotifyCookie.value);
await database await database
.update(database.authenticationTable) .update(database.authenticationTable)
@ -112,9 +120,10 @@ class AuthenticationProvider extends GetxController {
return AuthenticationTableCompanion.insert( return AuthenticationTableCompanion.insert(
id: const Value(0), id: const Value(0),
cookie: DecryptedText("${res.headers["set-cookie"]?.join(";")}; $spDc"), spotifyCookie:
accessToken: DecryptedText(body['accessToken']), DecryptedText("${res.headers["set-cookie"]?.join(";")}; $spDc"),
expiration: DateTime.fromMillisecondsSinceEpoch( spotifyAccessToken: DecryptedText(body['accessToken']),
spotifyExpiration: DateTime.fromMillisecondsSinceEpoch(
body['accessTokenExpirationTimestampMs']), body['accessTokenExpirationTimestampMs']),
); );
} catch (e) { } catch (e) {
@ -123,6 +132,21 @@ class AuthenticationProvider extends GetxController {
} }
} }
Future<void> setNeteaseCredentials(String cookie) async {
final database = Get.find<DatabaseProvider>().database;
await database.update(database.authenticationTable).replace(
AuthenticationTableCompanion.insert(
id: const Value(0),
spotifyCookie: auth.value!.spotifyCookie,
spotifyAccessToken: auth.value!.spotifyAccessToken,
spotifyExpiration: auth.value!.spotifyExpiration,
neteaseCookie: Value(DecryptedText(cookie)),
neteaseExpiration: const Value(null),
),
);
await loadAuthenticationData();
}
Future<void> logout() async { Future<void> logout() async {
auth.value = null; auth.value = null;
final database = Get.find<DatabaseProvider>().database; final database = Get.find<DatabaseProvider>().database;
@ -132,6 +156,21 @@ class AuthenticationProvider extends GetxController {
// Additional cleanup if necessary // Additional cleanup if necessary
} }
Future<void> logoutNetease() async {
final database = Get.find<DatabaseProvider>().database;
await database.update(database.authenticationTable).replace(
AuthenticationTableCompanion.insert(
id: const Value(0),
spotifyCookie: auth.value!.spotifyCookie,
spotifyAccessToken: auth.value!.spotifyAccessToken,
spotifyExpiration: auth.value!.spotifyExpiration,
neteaseCookie: const Value(null),
neteaseExpiration: const Value(null),
),
);
await loadAuthenticationData();
}
@override @override
void onClose() { void onClose() {
refreshTimer?.cancel(); refreshTimer?.cancel();

View File

@ -44,7 +44,7 @@ class SpotifyProvider extends GetxController {
log('[SpotifyApi] Using user credentials...'); log('[SpotifyApi] Using user credentials...');
final AuthenticationProvider authenticate = Get.find(); final AuthenticationProvider authenticate = Get.find();
return SpotifyApi.withAccessToken( return SpotifyApi.withAccessToken(
authenticate.auth.value!.accessToken.value); authenticate.auth.value!.spotifyAccessToken.value);
} }
@override @override

View File

@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import 'package:rhythm_box/screens/about.dart'; import 'package:rhythm_box/screens/about.dart';
import 'package:rhythm_box/screens/album/view.dart'; import 'package:rhythm_box/screens/album/view.dart';
import 'package:rhythm_box/screens/auth/login_netease.dart';
import 'package:rhythm_box/screens/auth/mobile_login.dart'; import 'package:rhythm_box/screens/auth/mobile_login.dart';
import 'package:rhythm_box/screens/explore.dart'; import 'package:rhythm_box/screens/explore.dart';
import 'package:rhythm_box/screens/library/view.dart'; import 'package:rhythm_box/screens/library/view.dart';
@ -104,6 +105,11 @@ final router = GoRouter(routes: [
name: 'authMobileLogin', name: 'authMobileLogin',
builder: (context, state) => const MobileLogin(), builder: (context, state) => const MobileLogin(),
), ),
GoRoute(
path: '/auth/netease/login',
name: 'authMobileLoginNetease',
builder: (context, state) => const LoginNeteaseScreen(),
),
], ],
), ),
]); ]);

View File

@ -0,0 +1,156 @@
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:get/get.dart';
import 'package:go_router/go_router.dart';
import 'package:rhythm_box/providers/auth.dart';
import 'package:rhythm_box/providers/error_notifier.dart';
import 'package:rhythm_box/services/sourced_track/sources/netease.dart';
import 'package:rhythm_box/widgets/sized_container.dart';
class LoginNeteaseScreen extends StatefulWidget {
const LoginNeteaseScreen({super.key});
@override
State<LoginNeteaseScreen> createState() => _LoginNeteaseScreenState();
}
class _LoginNeteaseScreenState extends State<LoginNeteaseScreen> {
final TextEditingController _phoneController = TextEditingController();
final TextEditingController _phoneRegionController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
late final AuthenticationProvider _auth = Get.find();
bool _isLogging = false;
Future<void> _sentCaptcha() async {
setState(() => _isLogging = true);
final phone = _phoneController.text;
var region = _phoneRegionController.text;
if (region.isEmpty) region = '86';
final client = NeteaseSourcedTrack.getClient();
final resp = await client.get(
'/captcha/sent?phone=$phone&ctcode=$region&timestamp=${DateTime.now().millisecondsSinceEpoch}',
);
if (resp.statusCode != 200 || resp.body?['code'] != 200) {
Get.find<ErrorNotifier>().showError(
resp.bodyString ?? resp.status.toString(),
);
}
setState(() => _isLogging = false);
}
Future<void> _performLogin() async {
setState(() => _isLogging = true);
final phone = _phoneController.text;
final password = _passwordController.text;
var region = _phoneRegionController.text;
if (region.isEmpty) region = '86';
final client = NeteaseSourcedTrack.getClient();
final resp = await client.get(
'/login/cellphone?phone=$phone&captcha=$password&countrycode=$region&timestamp=${DateTime.now().millisecondsSinceEpoch}',
);
if (resp.statusCode != 200 || resp.body?['code'] != 200) {
Get.find<ErrorNotifier>().showError(
resp.bodyString ?? resp.status.toString(),
);
setState(() => _isLogging = false);
return;
}
await _auth.setNeteaseCredentials(resp.body['cookie']);
setState(() => _isLogging = false);
GoRouter.of(context).goNamed('settings');
}
@override
void dispose() {
_phoneController.dispose();
_phoneRegionController.dispose();
_passwordController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Connect with Netease Cloud Music'),
),
body: CenteredContainer(
maxWidth: 320,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
children: [
SizedBox(
width: 64,
child: TextField(
controller: _phoneRegionController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: '86',
isDense: true,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
),
),
const Gap(8),
Expanded(
child: TextField(
controller: _phoneController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
label: Text('Phone Number'),
isDense: true,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
),
),
],
),
const Gap(8),
Row(
children: [
Expanded(
child: TextField(
controller: _passwordController,
obscureText: true,
decoration: const InputDecoration(
border: OutlineInputBorder(),
label: Text('Captcha Code'),
isDense: true,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
),
),
const Gap(8),
IconButton(
onPressed: _isLogging ? null : () => _sentCaptcha(),
icon: const Icon(Icons.sms),
tooltip: 'Get Captcha',
),
],
),
const Gap(8),
TextButton(
onPressed: _isLogging ? null : () => _performLogin(),
child: const Text('Login'),
)
],
),
),
);
}
}

View File

@ -72,8 +72,8 @@ class _ExploreScreenState extends State<ExploreScreen> {
} }
if (_auth.auth.value != null) { if (_auth.auth.value != null) {
final customEndpoint = final customEndpoint = CustomSpotifyEndpoints(
CustomSpotifyEndpoints(_auth.auth.value?.accessToken.value ?? ''); _auth.auth.value?.spotifyAccessToken.value ?? '');
final forYouView = await customEndpoint.getView( final forYouView = await customEndpoint.getView(
'made-for-x-hub', 'made-for-x-hub',
market: market, market: market,

View File

@ -7,6 +7,7 @@ import 'package:rhythm_box/providers/spotify.dart';
import 'package:rhythm_box/providers/user_preferences.dart'; import 'package:rhythm_box/providers/user_preferences.dart';
import 'package:rhythm_box/screens/auth/login.dart'; import 'package:rhythm_box/screens/auth/login.dart';
import 'package:rhythm_box/services/database/database.dart'; import 'package:rhythm_box/services/database/database.dart';
import 'package:rhythm_box/services/sourced_track/sources/netease.dart';
import 'package:rhythm_box/widgets/auto_cache_image.dart'; import 'package:rhythm_box/widgets/auto_cache_image.dart';
import 'package:rhythm_box/widgets/sized_container.dart'; import 'package:rhythm_box/widgets/sized_container.dart';
@ -86,6 +87,75 @@ class _SettingsScreenState extends State<SettingsScreen> {
}, },
); );
}), }),
Obx(() {
if (_authenticate.auth.value == null) {
return const SizedBox();
}
if (_authenticate.auth.value?.neteaseCookie == null) {
return ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
leading: const Icon(Icons.cloud_outlined),
title: const Text('Connect with Netease Cloud Music'),
subtitle: const Text(
'Make us able to play more music from Netease Cloud Music'),
trailing: const Icon(Icons.chevron_right),
enabled: !_isLoggingIn,
onTap: () async {
setState(() => _isLoggingIn = true);
await GoRouter.of(context)
.pushNamed('authMobileLoginNetease');
setState(() => _isLoggingIn = false);
},
);
}
return FutureBuilder(
future: NeteaseSourcedTrack.getClient().get('/user/account'),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return ListTile(
contentPadding:
const EdgeInsets.symmetric(horizontal: 24),
leading: const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 3,
),
),
title: const Text('Loading...'),
trailing: IconButton(
onPressed: () {
_authenticate.logoutNetease();
},
icon: const Icon(Icons.link_off),
),
);
}
return ListTile(
leading:
snapshot.data!.body['profile']['avatarUrl'] != null
? CircleAvatar(
backgroundImage: AutoCacheImage.provider(
snapshot.data!.body['profile']['avatarUrl'],
),
)
: const Icon(Icons.account_circle),
title: Text(snapshot.data!.body['profile']['nickname']),
subtitle: const Text(
'Connected with your Netease Cloud Music account',
),
trailing: IconButton(
onPressed: () {
_authenticate.logoutNetease();
},
icon: const Icon(Icons.link_off),
),
);
},
);
}),
Obx(() { Obx(() {
if (_authenticate.auth.value == null) { if (_authenticate.auth.value == null) {
return const SizedBox(); return const SizedBox();
@ -95,7 +165,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
contentPadding: const EdgeInsets.symmetric(horizontal: 24), contentPadding: const EdgeInsets.symmetric(horizontal: 24),
leading: const Icon(Icons.logout), leading: const Icon(Icons.logout),
title: const Text('Log out'), title: const Text('Log out'),
subtitle: const Text('Disconnect with this Spotify account'), subtitle: const Text('Disconnect with every account'),
trailing: const Icon(Icons.chevron_right), trailing: const Icon(Icons.chevron_right),
onTap: () async { onTap: () async {
_authenticate.logout(); _authenticate.logout();

View File

@ -55,7 +55,7 @@ class AppDatabase extends _$AppDatabase {
AppDatabase() : super(_openConnection()); AppDatabase() : super(_openConnection());
@override @override
int get schemaVersion => 3; int get schemaVersion => 1;
@override @override
MigrationStrategy get migration { MigrationStrategy get migration {
@ -63,18 +63,7 @@ class AppDatabase extends _$AppDatabase {
onCreate: (Migrator m) async { onCreate: (Migrator m) async {
await m.createAll(); await m.createAll();
}, },
onUpgrade: (Migrator m, int from, int to) async { onUpgrade: (Migrator m, int from, int to) async {},
if (from < 2) {
await m.addColumn(preferencesTable, preferencesTable.playerWakelock);
}
if (from > 3) {
await m.addColumn(
preferencesTable, preferencesTable.neteaseApiInstance);
await m.dropColumn(
preferencesTable, preferencesTable.audioSource.name);
await m.addColumn(preferencesTable, preferencesTable.audioSource);
}
},
); );
} }
} }

View File

@ -18,29 +18,54 @@ class $AuthenticationTableTable extends AuthenticationTable
requiredDuringInsert: false, requiredDuringInsert: false,
defaultConstraints: defaultConstraints:
GeneratedColumn.constraintIsAlways('PRIMARY KEY AUTOINCREMENT')); GeneratedColumn.constraintIsAlways('PRIMARY KEY AUTOINCREMENT'));
static const VerificationMeta _cookieMeta = const VerificationMeta('cookie'); static const VerificationMeta _spotifyCookieMeta =
@override const VerificationMeta('spotifyCookie');
late final GeneratedColumnWithTypeConverter<DecryptedText, String> cookie =
GeneratedColumn<String>('cookie', aliasedName, false,
type: DriftSqlType.string, requiredDuringInsert: true)
.withConverter<DecryptedText>(
$AuthenticationTableTable.$convertercookie);
static const VerificationMeta _accessTokenMeta =
const VerificationMeta('accessToken');
@override @override
late final GeneratedColumnWithTypeConverter<DecryptedText, String> late final GeneratedColumnWithTypeConverter<DecryptedText, String>
accessToken = GeneratedColumn<String>('access_token', aliasedName, false, spotifyCookie = GeneratedColumn<String>(
'spotify_cookie', aliasedName, false,
type: DriftSqlType.string, requiredDuringInsert: true) type: DriftSqlType.string, requiredDuringInsert: true)
.withConverter<DecryptedText>( .withConverter<DecryptedText>(
$AuthenticationTableTable.$converteraccessToken); $AuthenticationTableTable.$converterspotifyCookie);
static const VerificationMeta _expirationMeta = static const VerificationMeta _spotifyAccessTokenMeta =
const VerificationMeta('expiration'); const VerificationMeta('spotifyAccessToken');
@override @override
late final GeneratedColumn<DateTime> expiration = GeneratedColumn<DateTime>( late final GeneratedColumnWithTypeConverter<DecryptedText, String>
'expiration', aliasedName, false, spotifyAccessToken = GeneratedColumn<String>(
type: DriftSqlType.dateTime, requiredDuringInsert: true); 'spotify_access_token', aliasedName, false,
type: DriftSqlType.string, requiredDuringInsert: true)
.withConverter<DecryptedText>(
$AuthenticationTableTable.$converterspotifyAccessToken);
static const VerificationMeta _spotifyExpirationMeta =
const VerificationMeta('spotifyExpiration');
@override @override
List<GeneratedColumn> get $columns => [id, cookie, accessToken, expiration]; late final GeneratedColumn<DateTime> spotifyExpiration =
GeneratedColumn<DateTime>('spotify_expiration', aliasedName, false,
type: DriftSqlType.dateTime, requiredDuringInsert: true);
static const VerificationMeta _neteaseCookieMeta =
const VerificationMeta('neteaseCookie');
@override
late final GeneratedColumnWithTypeConverter<DecryptedText?, String>
neteaseCookie = GeneratedColumn<String>(
'netease_cookie', aliasedName, true,
type: DriftSqlType.string, requiredDuringInsert: false)
.withConverter<DecryptedText?>(
$AuthenticationTableTable.$converterneteaseCookien);
static const VerificationMeta _neteaseExpirationMeta =
const VerificationMeta('neteaseExpiration');
@override
late final GeneratedColumn<DateTime> neteaseExpiration =
GeneratedColumn<DateTime>('netease_expiration', aliasedName, true,
type: DriftSqlType.dateTime, requiredDuringInsert: false);
@override
List<GeneratedColumn> get $columns => [
id,
spotifyCookie,
spotifyAccessToken,
spotifyExpiration,
neteaseCookie,
neteaseExpiration
];
@override @override
String get aliasedName => _alias ?? actualTableName; String get aliasedName => _alias ?? actualTableName;
@override @override
@ -55,15 +80,22 @@ class $AuthenticationTableTable extends AuthenticationTable
if (data.containsKey('id')) { if (data.containsKey('id')) {
context.handle(_idMeta, id.isAcceptableOrUnknown(data['id']!, _idMeta)); context.handle(_idMeta, id.isAcceptableOrUnknown(data['id']!, _idMeta));
} }
context.handle(_cookieMeta, const VerificationResult.success()); context.handle(_spotifyCookieMeta, const VerificationResult.success());
context.handle(_accessTokenMeta, const VerificationResult.success()); context.handle(_spotifyAccessTokenMeta, const VerificationResult.success());
if (data.containsKey('expiration')) { if (data.containsKey('spotify_expiration')) {
context.handle( context.handle(
_expirationMeta, _spotifyExpirationMeta,
expiration.isAcceptableOrUnknown( spotifyExpiration.isAcceptableOrUnknown(
data['expiration']!, _expirationMeta)); data['spotify_expiration']!, _spotifyExpirationMeta));
} else if (isInserting) { } else if (isInserting) {
context.missing(_expirationMeta); context.missing(_spotifyExpirationMeta);
}
context.handle(_neteaseCookieMeta, const VerificationResult.success());
if (data.containsKey('netease_expiration')) {
context.handle(
_neteaseExpirationMeta,
neteaseExpiration.isAcceptableOrUnknown(
data['netease_expiration']!, _neteaseExpirationMeta));
} }
return context; return context;
} }
@ -77,14 +109,19 @@ class $AuthenticationTableTable extends AuthenticationTable
return AuthenticationTableData( return AuthenticationTableData(
id: attachedDatabase.typeMapping id: attachedDatabase.typeMapping
.read(DriftSqlType.int, data['${effectivePrefix}id'])!, .read(DriftSqlType.int, data['${effectivePrefix}id'])!,
cookie: $AuthenticationTableTable.$convertercookie.fromSql( spotifyCookie: $AuthenticationTableTable.$converterspotifyCookie.fromSql(
attachedDatabase.typeMapping
.read(DriftSqlType.string, data['${effectivePrefix}cookie'])!),
accessToken: $AuthenticationTableTable.$converteraccessToken.fromSql(
attachedDatabase.typeMapping.read( attachedDatabase.typeMapping.read(
DriftSqlType.string, data['${effectivePrefix}access_token'])!), DriftSqlType.string, data['${effectivePrefix}spotify_cookie'])!),
expiration: attachedDatabase.typeMapping spotifyAccessToken: $AuthenticationTableTable.$converterspotifyAccessToken
.read(DriftSqlType.dateTime, data['${effectivePrefix}expiration'])!, .fromSql(attachedDatabase.typeMapping.read(DriftSqlType.string,
data['${effectivePrefix}spotify_access_token'])!),
spotifyExpiration: attachedDatabase.typeMapping.read(
DriftSqlType.dateTime, data['${effectivePrefix}spotify_expiration'])!,
neteaseCookie: $AuthenticationTableTable.$converterneteaseCookien.fromSql(
attachedDatabase.typeMapping.read(
DriftSqlType.string, data['${effectivePrefix}netease_cookie'])),
neteaseExpiration: attachedDatabase.typeMapping.read(
DriftSqlType.dateTime, data['${effectivePrefix}netease_expiration']),
); );
} }
@ -93,45 +130,69 @@ class $AuthenticationTableTable extends AuthenticationTable
return $AuthenticationTableTable(attachedDatabase, alias); return $AuthenticationTableTable(attachedDatabase, alias);
} }
static TypeConverter<DecryptedText, String> $convertercookie = static TypeConverter<DecryptedText, String> $converterspotifyCookie =
EncryptedTextConverter(); EncryptedTextConverter();
static TypeConverter<DecryptedText, String> $converteraccessToken = static TypeConverter<DecryptedText, String> $converterspotifyAccessToken =
EncryptedTextConverter(); EncryptedTextConverter();
static TypeConverter<DecryptedText, String> $converterneteaseCookie =
EncryptedTextConverter();
static TypeConverter<DecryptedText?, String?> $converterneteaseCookien =
NullAwareTypeConverter.wrap($converterneteaseCookie);
} }
class AuthenticationTableData extends DataClass class AuthenticationTableData extends DataClass
implements Insertable<AuthenticationTableData> { implements Insertable<AuthenticationTableData> {
final int id; final int id;
final DecryptedText cookie; final DecryptedText spotifyCookie;
final DecryptedText accessToken; final DecryptedText spotifyAccessToken;
final DateTime expiration; final DateTime spotifyExpiration;
final DecryptedText? neteaseCookie;
final DateTime? neteaseExpiration;
const AuthenticationTableData( const AuthenticationTableData(
{required this.id, {required this.id,
required this.cookie, required this.spotifyCookie,
required this.accessToken, required this.spotifyAccessToken,
required this.expiration}); required this.spotifyExpiration,
this.neteaseCookie,
this.neteaseExpiration});
@override @override
Map<String, Expression> toColumns(bool nullToAbsent) { Map<String, Expression> toColumns(bool nullToAbsent) {
final map = <String, Expression>{}; final map = <String, Expression>{};
map['id'] = Variable<int>(id); map['id'] = Variable<int>(id);
{ {
map['cookie'] = Variable<String>( map['spotify_cookie'] = Variable<String>($AuthenticationTableTable
$AuthenticationTableTable.$convertercookie.toSql(cookie)); .$converterspotifyCookie
.toSql(spotifyCookie));
} }
{ {
map['access_token'] = Variable<String>( map['spotify_access_token'] = Variable<String>($AuthenticationTableTable
$AuthenticationTableTable.$converteraccessToken.toSql(accessToken)); .$converterspotifyAccessToken
.toSql(spotifyAccessToken));
}
map['spotify_expiration'] = Variable<DateTime>(spotifyExpiration);
if (!nullToAbsent || neteaseCookie != null) {
map['netease_cookie'] = Variable<String>($AuthenticationTableTable
.$converterneteaseCookien
.toSql(neteaseCookie));
}
if (!nullToAbsent || neteaseExpiration != null) {
map['netease_expiration'] = Variable<DateTime>(neteaseExpiration);
} }
map['expiration'] = Variable<DateTime>(expiration);
return map; return map;
} }
AuthenticationTableCompanion toCompanion(bool nullToAbsent) { AuthenticationTableCompanion toCompanion(bool nullToAbsent) {
return AuthenticationTableCompanion( return AuthenticationTableCompanion(
id: Value(id), id: Value(id),
cookie: Value(cookie), spotifyCookie: Value(spotifyCookie),
accessToken: Value(accessToken), spotifyAccessToken: Value(spotifyAccessToken),
expiration: Value(expiration), spotifyExpiration: Value(spotifyExpiration),
neteaseCookie: neteaseCookie == null && nullToAbsent
? const Value.absent()
: Value(neteaseCookie),
neteaseExpiration: neteaseExpiration == null && nullToAbsent
? const Value.absent()
: Value(neteaseExpiration),
); );
} }
@ -140,9 +201,14 @@ class AuthenticationTableData extends DataClass
serializer ??= driftRuntimeOptions.defaultSerializer; serializer ??= driftRuntimeOptions.defaultSerializer;
return AuthenticationTableData( return AuthenticationTableData(
id: serializer.fromJson<int>(json['id']), id: serializer.fromJson<int>(json['id']),
cookie: serializer.fromJson<DecryptedText>(json['cookie']), spotifyCookie: serializer.fromJson<DecryptedText>(json['spotifyCookie']),
accessToken: serializer.fromJson<DecryptedText>(json['accessToken']), spotifyAccessToken:
expiration: serializer.fromJson<DateTime>(json['expiration']), serializer.fromJson<DecryptedText>(json['spotifyAccessToken']),
spotifyExpiration:
serializer.fromJson<DateTime>(json['spotifyExpiration']),
neteaseCookie: serializer.fromJson<DecryptedText?>(json['neteaseCookie']),
neteaseExpiration:
serializer.fromJson<DateTime?>(json['neteaseExpiration']),
); );
} }
@override @override
@ -150,31 +216,51 @@ class AuthenticationTableData extends DataClass
serializer ??= driftRuntimeOptions.defaultSerializer; serializer ??= driftRuntimeOptions.defaultSerializer;
return <String, dynamic>{ return <String, dynamic>{
'id': serializer.toJson<int>(id), 'id': serializer.toJson<int>(id),
'cookie': serializer.toJson<DecryptedText>(cookie), 'spotifyCookie': serializer.toJson<DecryptedText>(spotifyCookie),
'accessToken': serializer.toJson<DecryptedText>(accessToken), 'spotifyAccessToken':
'expiration': serializer.toJson<DateTime>(expiration), serializer.toJson<DecryptedText>(spotifyAccessToken),
'spotifyExpiration': serializer.toJson<DateTime>(spotifyExpiration),
'neteaseCookie': serializer.toJson<DecryptedText?>(neteaseCookie),
'neteaseExpiration': serializer.toJson<DateTime?>(neteaseExpiration),
}; };
} }
AuthenticationTableData copyWith( AuthenticationTableData copyWith(
{int? id, {int? id,
DecryptedText? cookie, DecryptedText? spotifyCookie,
DecryptedText? accessToken, DecryptedText? spotifyAccessToken,
DateTime? expiration}) => DateTime? spotifyExpiration,
Value<DecryptedText?> neteaseCookie = const Value.absent(),
Value<DateTime?> neteaseExpiration = const Value.absent()}) =>
AuthenticationTableData( AuthenticationTableData(
id: id ?? this.id, id: id ?? this.id,
cookie: cookie ?? this.cookie, spotifyCookie: spotifyCookie ?? this.spotifyCookie,
accessToken: accessToken ?? this.accessToken, spotifyAccessToken: spotifyAccessToken ?? this.spotifyAccessToken,
expiration: expiration ?? this.expiration, spotifyExpiration: spotifyExpiration ?? this.spotifyExpiration,
neteaseCookie:
neteaseCookie.present ? neteaseCookie.value : this.neteaseCookie,
neteaseExpiration: neteaseExpiration.present
? neteaseExpiration.value
: this.neteaseExpiration,
); );
AuthenticationTableData copyWithCompanion(AuthenticationTableCompanion data) { AuthenticationTableData copyWithCompanion(AuthenticationTableCompanion data) {
return AuthenticationTableData( return AuthenticationTableData(
id: data.id.present ? data.id.value : this.id, id: data.id.present ? data.id.value : this.id,
cookie: data.cookie.present ? data.cookie.value : this.cookie, spotifyCookie: data.spotifyCookie.present
accessToken: ? data.spotifyCookie.value
data.accessToken.present ? data.accessToken.value : this.accessToken, : this.spotifyCookie,
expiration: spotifyAccessToken: data.spotifyAccessToken.present
data.expiration.present ? data.expiration.value : this.expiration, ? data.spotifyAccessToken.value
: this.spotifyAccessToken,
spotifyExpiration: data.spotifyExpiration.present
? data.spotifyExpiration.value
: this.spotifyExpiration,
neteaseCookie: data.neteaseCookie.present
? data.neteaseCookie.value
: this.neteaseCookie,
neteaseExpiration: data.neteaseExpiration.present
? data.neteaseExpiration.value
: this.neteaseExpiration,
); );
} }
@ -182,69 +268,89 @@ class AuthenticationTableData extends DataClass
String toString() { String toString() {
return (StringBuffer('AuthenticationTableData(') return (StringBuffer('AuthenticationTableData(')
..write('id: $id, ') ..write('id: $id, ')
..write('cookie: $cookie, ') ..write('spotifyCookie: $spotifyCookie, ')
..write('accessToken: $accessToken, ') ..write('spotifyAccessToken: $spotifyAccessToken, ')
..write('expiration: $expiration') ..write('spotifyExpiration: $spotifyExpiration, ')
..write('neteaseCookie: $neteaseCookie, ')
..write('neteaseExpiration: $neteaseExpiration')
..write(')')) ..write(')'))
.toString(); .toString();
} }
@override @override
int get hashCode => Object.hash(id, cookie, accessToken, expiration); int get hashCode => Object.hash(id, spotifyCookie, spotifyAccessToken,
spotifyExpiration, neteaseCookie, neteaseExpiration);
@override @override
bool operator ==(Object other) => bool operator ==(Object other) =>
identical(this, other) || identical(this, other) ||
(other is AuthenticationTableData && (other is AuthenticationTableData &&
other.id == this.id && other.id == this.id &&
other.cookie == this.cookie && other.spotifyCookie == this.spotifyCookie &&
other.accessToken == this.accessToken && other.spotifyAccessToken == this.spotifyAccessToken &&
other.expiration == this.expiration); other.spotifyExpiration == this.spotifyExpiration &&
other.neteaseCookie == this.neteaseCookie &&
other.neteaseExpiration == this.neteaseExpiration);
} }
class AuthenticationTableCompanion class AuthenticationTableCompanion
extends UpdateCompanion<AuthenticationTableData> { extends UpdateCompanion<AuthenticationTableData> {
final Value<int> id; final Value<int> id;
final Value<DecryptedText> cookie; final Value<DecryptedText> spotifyCookie;
final Value<DecryptedText> accessToken; final Value<DecryptedText> spotifyAccessToken;
final Value<DateTime> expiration; final Value<DateTime> spotifyExpiration;
final Value<DecryptedText?> neteaseCookie;
final Value<DateTime?> neteaseExpiration;
const AuthenticationTableCompanion({ const AuthenticationTableCompanion({
this.id = const Value.absent(), this.id = const Value.absent(),
this.cookie = const Value.absent(), this.spotifyCookie = const Value.absent(),
this.accessToken = const Value.absent(), this.spotifyAccessToken = const Value.absent(),
this.expiration = const Value.absent(), this.spotifyExpiration = const Value.absent(),
this.neteaseCookie = const Value.absent(),
this.neteaseExpiration = const Value.absent(),
}); });
AuthenticationTableCompanion.insert({ AuthenticationTableCompanion.insert({
this.id = const Value.absent(), this.id = const Value.absent(),
required DecryptedText cookie, required DecryptedText spotifyCookie,
required DecryptedText accessToken, required DecryptedText spotifyAccessToken,
required DateTime expiration, required DateTime spotifyExpiration,
}) : cookie = Value(cookie), this.neteaseCookie = const Value.absent(),
accessToken = Value(accessToken), this.neteaseExpiration = const Value.absent(),
expiration = Value(expiration); }) : spotifyCookie = Value(spotifyCookie),
spotifyAccessToken = Value(spotifyAccessToken),
spotifyExpiration = Value(spotifyExpiration);
static Insertable<AuthenticationTableData> custom({ static Insertable<AuthenticationTableData> custom({
Expression<int>? id, Expression<int>? id,
Expression<String>? cookie, Expression<String>? spotifyCookie,
Expression<String>? accessToken, Expression<String>? spotifyAccessToken,
Expression<DateTime>? expiration, Expression<DateTime>? spotifyExpiration,
Expression<String>? neteaseCookie,
Expression<DateTime>? neteaseExpiration,
}) { }) {
return RawValuesInsertable({ return RawValuesInsertable({
if (id != null) 'id': id, if (id != null) 'id': id,
if (cookie != null) 'cookie': cookie, if (spotifyCookie != null) 'spotify_cookie': spotifyCookie,
if (accessToken != null) 'access_token': accessToken, if (spotifyAccessToken != null)
if (expiration != null) 'expiration': expiration, 'spotify_access_token': spotifyAccessToken,
if (spotifyExpiration != null) 'spotify_expiration': spotifyExpiration,
if (neteaseCookie != null) 'netease_cookie': neteaseCookie,
if (neteaseExpiration != null) 'netease_expiration': neteaseExpiration,
}); });
} }
AuthenticationTableCompanion copyWith( AuthenticationTableCompanion copyWith(
{Value<int>? id, {Value<int>? id,
Value<DecryptedText>? cookie, Value<DecryptedText>? spotifyCookie,
Value<DecryptedText>? accessToken, Value<DecryptedText>? spotifyAccessToken,
Value<DateTime>? expiration}) { Value<DateTime>? spotifyExpiration,
Value<DecryptedText?>? neteaseCookie,
Value<DateTime?>? neteaseExpiration}) {
return AuthenticationTableCompanion( return AuthenticationTableCompanion(
id: id ?? this.id, id: id ?? this.id,
cookie: cookie ?? this.cookie, spotifyCookie: spotifyCookie ?? this.spotifyCookie,
accessToken: accessToken ?? this.accessToken, spotifyAccessToken: spotifyAccessToken ?? this.spotifyAccessToken,
expiration: expiration ?? this.expiration, spotifyExpiration: spotifyExpiration ?? this.spotifyExpiration,
neteaseCookie: neteaseCookie ?? this.neteaseCookie,
neteaseExpiration: neteaseExpiration ?? this.neteaseExpiration,
); );
} }
@ -254,17 +360,26 @@ class AuthenticationTableCompanion
if (id.present) { if (id.present) {
map['id'] = Variable<int>(id.value); map['id'] = Variable<int>(id.value);
} }
if (cookie.present) { if (spotifyCookie.present) {
map['cookie'] = Variable<String>( map['spotify_cookie'] = Variable<String>($AuthenticationTableTable
$AuthenticationTableTable.$convertercookie.toSql(cookie.value)); .$converterspotifyCookie
.toSql(spotifyCookie.value));
} }
if (accessToken.present) { if (spotifyAccessToken.present) {
map['access_token'] = Variable<String>($AuthenticationTableTable map['spotify_access_token'] = Variable<String>($AuthenticationTableTable
.$converteraccessToken .$converterspotifyAccessToken
.toSql(accessToken.value)); .toSql(spotifyAccessToken.value));
} }
if (expiration.present) { if (spotifyExpiration.present) {
map['expiration'] = Variable<DateTime>(expiration.value); map['spotify_expiration'] = Variable<DateTime>(spotifyExpiration.value);
}
if (neteaseCookie.present) {
map['netease_cookie'] = Variable<String>($AuthenticationTableTable
.$converterneteaseCookien
.toSql(neteaseCookie.value));
}
if (neteaseExpiration.present) {
map['netease_expiration'] = Variable<DateTime>(neteaseExpiration.value);
} }
return map; return map;
} }
@ -273,9 +388,11 @@ class AuthenticationTableCompanion
String toString() { String toString() {
return (StringBuffer('AuthenticationTableCompanion(') return (StringBuffer('AuthenticationTableCompanion(')
..write('id: $id, ') ..write('id: $id, ')
..write('cookie: $cookie, ') ..write('spotifyCookie: $spotifyCookie, ')
..write('accessToken: $accessToken, ') ..write('spotifyAccessToken: $spotifyAccessToken, ')
..write('expiration: $expiration') ..write('spotifyExpiration: $spotifyExpiration, ')
..write('neteaseCookie: $neteaseCookie, ')
..write('neteaseExpiration: $neteaseExpiration')
..write(')')) ..write(')'))
.toString(); .toString();
} }
@ -3824,16 +3941,20 @@ abstract class _$AppDatabase extends GeneratedDatabase {
typedef $$AuthenticationTableTableCreateCompanionBuilder typedef $$AuthenticationTableTableCreateCompanionBuilder
= AuthenticationTableCompanion Function({ = AuthenticationTableCompanion Function({
Value<int> id, Value<int> id,
required DecryptedText cookie, required DecryptedText spotifyCookie,
required DecryptedText accessToken, required DecryptedText spotifyAccessToken,
required DateTime expiration, required DateTime spotifyExpiration,
Value<DecryptedText?> neteaseCookie,
Value<DateTime?> neteaseExpiration,
}); });
typedef $$AuthenticationTableTableUpdateCompanionBuilder typedef $$AuthenticationTableTableUpdateCompanionBuilder
= AuthenticationTableCompanion Function({ = AuthenticationTableCompanion Function({
Value<int> id, Value<int> id,
Value<DecryptedText> cookie, Value<DecryptedText> spotifyCookie,
Value<DecryptedText> accessToken, Value<DecryptedText> spotifyAccessToken,
Value<DateTime> expiration, Value<DateTime> spotifyExpiration,
Value<DecryptedText?> neteaseCookie,
Value<DateTime?> neteaseExpiration,
}); });
class $$AuthenticationTableTableTableManager extends RootTableManager< class $$AuthenticationTableTableTableManager extends RootTableManager<
@ -3855,27 +3976,35 @@ class $$AuthenticationTableTableTableManager extends RootTableManager<
ComposerState(db, table)), ComposerState(db, table)),
updateCompanionCallback: ({ updateCompanionCallback: ({
Value<int> id = const Value.absent(), Value<int> id = const Value.absent(),
Value<DecryptedText> cookie = const Value.absent(), Value<DecryptedText> spotifyCookie = const Value.absent(),
Value<DecryptedText> accessToken = const Value.absent(), Value<DecryptedText> spotifyAccessToken = const Value.absent(),
Value<DateTime> expiration = const Value.absent(), Value<DateTime> spotifyExpiration = const Value.absent(),
Value<DecryptedText?> neteaseCookie = const Value.absent(),
Value<DateTime?> neteaseExpiration = const Value.absent(),
}) => }) =>
AuthenticationTableCompanion( AuthenticationTableCompanion(
id: id, id: id,
cookie: cookie, spotifyCookie: spotifyCookie,
accessToken: accessToken, spotifyAccessToken: spotifyAccessToken,
expiration: expiration, spotifyExpiration: spotifyExpiration,
neteaseCookie: neteaseCookie,
neteaseExpiration: neteaseExpiration,
), ),
createCompanionCallback: ({ createCompanionCallback: ({
Value<int> id = const Value.absent(), Value<int> id = const Value.absent(),
required DecryptedText cookie, required DecryptedText spotifyCookie,
required DecryptedText accessToken, required DecryptedText spotifyAccessToken,
required DateTime expiration, required DateTime spotifyExpiration,
Value<DecryptedText?> neteaseCookie = const Value.absent(),
Value<DateTime?> neteaseExpiration = const Value.absent(),
}) => }) =>
AuthenticationTableCompanion.insert( AuthenticationTableCompanion.insert(
id: id, id: id,
cookie: cookie, spotifyCookie: spotifyCookie,
accessToken: accessToken, spotifyAccessToken: spotifyAccessToken,
expiration: expiration, spotifyExpiration: spotifyExpiration,
neteaseCookie: neteaseCookie,
neteaseExpiration: neteaseExpiration,
), ),
)); ));
} }
@ -3889,21 +4018,33 @@ class $$AuthenticationTableTableFilterComposer
ColumnFilters(column, joinBuilders: joinBuilders)); ColumnFilters(column, joinBuilders: joinBuilders));
ColumnWithTypeConverterFilters<DecryptedText, DecryptedText, String> ColumnWithTypeConverterFilters<DecryptedText, DecryptedText, String>
get cookie => $state.composableBuilder( get spotifyCookie => $state.composableBuilder(
column: $state.table.cookie, column: $state.table.spotifyCookie,
builder: (column, joinBuilders) => ColumnWithTypeConverterFilters( builder: (column, joinBuilders) => ColumnWithTypeConverterFilters(
column, column,
joinBuilders: joinBuilders)); joinBuilders: joinBuilders));
ColumnWithTypeConverterFilters<DecryptedText, DecryptedText, String> ColumnWithTypeConverterFilters<DecryptedText, DecryptedText, String>
get accessToken => $state.composableBuilder( get spotifyAccessToken => $state.composableBuilder(
column: $state.table.accessToken, column: $state.table.spotifyAccessToken,
builder: (column, joinBuilders) => ColumnWithTypeConverterFilters( builder: (column, joinBuilders) => ColumnWithTypeConverterFilters(
column, column,
joinBuilders: joinBuilders)); joinBuilders: joinBuilders));
ColumnFilters<DateTime> get expiration => $state.composableBuilder( ColumnFilters<DateTime> get spotifyExpiration => $state.composableBuilder(
column: $state.table.expiration, column: $state.table.spotifyExpiration,
builder: (column, joinBuilders) =>
ColumnFilters(column, joinBuilders: joinBuilders));
ColumnWithTypeConverterFilters<DecryptedText?, DecryptedText, String>
get neteaseCookie => $state.composableBuilder(
column: $state.table.neteaseCookie,
builder: (column, joinBuilders) => ColumnWithTypeConverterFilters(
column,
joinBuilders: joinBuilders));
ColumnFilters<DateTime> get neteaseExpiration => $state.composableBuilder(
column: $state.table.neteaseExpiration,
builder: (column, joinBuilders) => builder: (column, joinBuilders) =>
ColumnFilters(column, joinBuilders: joinBuilders)); ColumnFilters(column, joinBuilders: joinBuilders));
} }
@ -3916,18 +4057,28 @@ class $$AuthenticationTableTableOrderingComposer
builder: (column, joinBuilders) => builder: (column, joinBuilders) =>
ColumnOrderings(column, joinBuilders: joinBuilders)); ColumnOrderings(column, joinBuilders: joinBuilders));
ColumnOrderings<String> get cookie => $state.composableBuilder( ColumnOrderings<String> get spotifyCookie => $state.composableBuilder(
column: $state.table.cookie, column: $state.table.spotifyCookie,
builder: (column, joinBuilders) => builder: (column, joinBuilders) =>
ColumnOrderings(column, joinBuilders: joinBuilders)); ColumnOrderings(column, joinBuilders: joinBuilders));
ColumnOrderings<String> get accessToken => $state.composableBuilder( ColumnOrderings<String> get spotifyAccessToken => $state.composableBuilder(
column: $state.table.accessToken, column: $state.table.spotifyAccessToken,
builder: (column, joinBuilders) => builder: (column, joinBuilders) =>
ColumnOrderings(column, joinBuilders: joinBuilders)); ColumnOrderings(column, joinBuilders: joinBuilders));
ColumnOrderings<DateTime> get expiration => $state.composableBuilder( ColumnOrderings<DateTime> get spotifyExpiration => $state.composableBuilder(
column: $state.table.expiration, column: $state.table.spotifyExpiration,
builder: (column, joinBuilders) =>
ColumnOrderings(column, joinBuilders: joinBuilders));
ColumnOrderings<String> get neteaseCookie => $state.composableBuilder(
column: $state.table.neteaseCookie,
builder: (column, joinBuilders) =>
ColumnOrderings(column, joinBuilders: joinBuilders));
ColumnOrderings<DateTime> get neteaseExpiration => $state.composableBuilder(
column: $state.table.neteaseExpiration,
builder: (column, joinBuilders) => builder: (column, joinBuilders) =>
ColumnOrderings(column, joinBuilders: joinBuilders)); ColumnOrderings(column, joinBuilders: joinBuilders));
} }

View File

@ -2,7 +2,10 @@ part of '../database.dart';
class AuthenticationTable extends Table { class AuthenticationTable extends Table {
IntColumn get id => integer().autoIncrement()(); IntColumn get id => integer().autoIncrement()();
TextColumn get cookie => text().map(EncryptedTextConverter())(); TextColumn get spotifyCookie => text().map(EncryptedTextConverter())();
TextColumn get accessToken => text().map(EncryptedTextConverter())(); TextColumn get spotifyAccessToken => text().map(EncryptedTextConverter())();
DateTimeColumn get expiration => dateTime()(); DateTimeColumn get spotifyExpiration => dateTime()();
TextColumn get neteaseCookie =>
text().map(EncryptedTextConverter()).nullable()();
DateTimeColumn get neteaseExpiration => dateTime().nullable()();
} }

View File

@ -1,6 +1,8 @@
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
import 'package:drift/drift.dart'; import 'package:drift/drift.dart';
import 'package:get/get.dart' hide Value; import 'package:get/get.dart' hide Value;
import 'package:get/get_connect/http/src/request/request.dart';
import 'package:rhythm_box/providers/auth.dart';
import 'package:rhythm_box/providers/database.dart'; import 'package:rhythm_box/providers/database.dart';
import 'package:rhythm_box/providers/user_preferences.dart'; import 'package:rhythm_box/providers/user_preferences.dart';
import 'package:rhythm_box/services/database/database.dart'; import 'package:rhythm_box/services/database/database.dart';
@ -38,9 +40,25 @@ class NeteaseSourcedTrack extends SourcedTrack {
} }
static GetConnect getClient() { static GetConnect getClient() {
final client = GetConnect(); final client = GetConnect(
withCredentials: true,
timeout: const Duration(seconds: 30),
);
client.baseUrl = getBaseUrl(); client.baseUrl = getBaseUrl();
client.timeout = const Duration(seconds: 30); client.httpClient.addRequestModifier((Request request) async {
final AuthenticationProvider auth = Get.find();
if (auth.auth.value!.neteaseCookie != null) {
final cookie =
'MUSIC_U=${auth.auth.value!.getNeteaseCookie('MUSIC_U')}';
if (request.headers['Cookie'] == null) {
request.headers['Cookie'] = cookie;
} else {
request.headers['Cookie'] = request.headers['Cookie']! + cookie;
}
}
return request;
});
return client; return client;
} }
@ -80,7 +98,7 @@ class NeteaseSourcedTrack extends SourcedTrack {
await client.get('/check/music?id=${siblings.first.info.id}'); await client.get('/check/music?id=${siblings.first.info.id}');
if (checkResp.body['success'] != true) throw TrackNotFoundError(track); if (checkResp.body['success'] != true) throw TrackNotFoundError(track);
await await db.database.into(db.database.sourceMatchTable).insert( await db.database.into(db.database.sourceMatchTable).insert(
SourceMatchTableCompanion.insert( SourceMatchTableCompanion.insert(
trackId: track.id!, trackId: track.id!,
sourceId: siblings.first.info.id, sourceId: siblings.first.info.id,