Skip to content

Commit

Permalink
Encode channels
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Aug 12, 2023
1 parent f9c31a7 commit f2d8471
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ integration_test/screenshots/
# Firebase
.firebase/

# Config
config/local.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import 'dart:async';
import 'dart:convert';

import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';
import 'package:spinify/spinify.dart';
import 'package:spinifyapp/src/feature/authentication/model/sign_in_data.dart';
import 'package:spinifyapp/src/feature/authentication/model/user.dart';

Expand All @@ -25,7 +29,31 @@ class AuthenticationRepositoryImpl implements IAuthenticationRepository {
Future<String> getToken() async {
switch (_user) {
case AuthenticatedUser user:
return user.token;
final AuthenticatedUser(
:String username,
:String token,
:String channel,
:String? secret
) = user;
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
String encodeChannel(String secret) => '$channel'
'#'
'${hex.encode(utf8.encoder.fuse(sha256).convert(secret).bytes)}';
SpinifyJWT jwt = SpinifyJWT(
sub: username,
exp: now + (24 * 60 * 60),
iat: now,
info: <String, Object?>{
'username': username,
},
channels: <String>[
switch (secret) {
String secret => encodeChannel(secret),
null => channel,
}
],
);
return jwt.encode(token);
case UnauthenticatedUser _:
throw Exception('User is not authenticated');
}
Expand Down
2 changes: 2 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ dependencies:
path: any
platform_info: ^4.0.2
win32: ^5.0.6
crypto: ^3.0.3
convert: ^3.1.1

# Desktop
window_manager: ^0.3.5
Expand Down

0 comments on commit f2d8471

Please sign in to comment.