TF-2387 Handle sign up twake workplace

This commit is contained in:
dab246
2024-10-31 14:11:15 +07:00
committed by Dat H. Pham
parent 2de503d990
commit e13f86a037
25 changed files with 427 additions and 59 deletions
@@ -0,0 +1,7 @@
class AccessTokenIsNullException implements Exception {}
class RefreshTokenIsNullException implements Exception {}
class TokenIdIsNullException implements Exception {}
class ExpiresTimeIsNullException implements Exception {}
+33
View File
@@ -2,6 +2,7 @@
import 'package:core/utils/app_logger.dart';
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:model/exceptions/token_oidc_exceptions.dart';
import 'package:model/oidc/converter/token_id_converter.dart';
import 'package:model/oidc/token_id.dart';
@@ -27,6 +28,38 @@ class TokenOIDC with EquatableMixin {
Map<String, dynamic> toJson() => _$TokenOIDCToJson(this);
factory TokenOIDC.fromUri(String uriString) {
final uri = Uri.parse(uriString);
final queryParams = uri.queryParameters;
final accessToken = queryParams['access_token'];
if (accessToken == null || accessToken.isEmpty) {
throw AccessTokenIsNullException();
}
final refreshToken = queryParams['refresh_token'];
if (refreshToken == null || refreshToken.isEmpty) {
throw RefreshTokenIsNullException();
}
final idToken = queryParams['id_token'];
if (idToken == null || idToken.isEmpty) {
throw TokenIdIsNullException();
}
final expiresIn = queryParams['expires_in'];
if (expiresIn == null || expiresIn.isEmpty) {
throw ExpiresTimeIsNullException();
}
return TokenOIDC(
accessToken,
TokenId(idToken),
refreshToken,
expiredTime: DateTime.now().add(Duration(seconds: int.parse(expiresIn))),
);
}
@override
List<Object?> get props => [token, tokenId, expiredTime, refreshToken];
}
+48
View File
@@ -0,0 +1,48 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:model/exceptions/token_oidc_exceptions.dart';
import 'package:model/oidc/token_oidc.dart';
void main() {
group('TokenOIDC.fromUrl', () {
test('returns TokenOIDC instance on successful parsing', () {
const uriString = 'https://example.com/callback?access_token=valid_access&refresh_token=valid_refresh&id_token=valid_id&expires_in=3600';
final token = TokenOIDC.fromUri(uriString);
expect(token.token, 'valid_access');
expect(token.refreshToken, 'valid_refresh');
expect(token.tokenId.uuid, 'valid_id');
expect(token.expiredTime?.isAfter(DateTime.now()), isTrue);
});
test('throws AccessTokenIsNullException if access_token is missing', () {
const uriString = 'https://example.com/callback?refresh_token=valid_refresh&id_token=valid_id&expires_in=3600';
expect(() => TokenOIDC.fromUri(uriString), throwsA(isA<AccessTokenIsNullException>()));
});
test('throws RefreshTokenIsNullException if refresh_token is missing', () {
const uriString = 'https://example.com/callback?access_token=valid_access&id_token=valid_id&expires_in=3600';
expect(() => TokenOIDC.fromUri(uriString), throwsA(isA<RefreshTokenIsNullException>()));
});
test('throws TokenIdIsNullException if id_token is missing', () {
const uriString = 'https://example.com/callback?access_token=valid_access&refresh_token=valid_refresh&expires_in=3600';
expect(() => TokenOIDC.fromUri(uriString), throwsA(isA<TokenIdIsNullException>()));
});
test('throws ExpiresTimeIsNullException if expires_in is missing', () {
const uriString = 'https://example.com/callback?access_token=valid_access&refresh_token=valid_refresh&id_token=valid_id';
expect(() => TokenOIDC.fromUri(uriString), throwsA(isA<ExpiresTimeIsNullException>()));
});
test('throws FormatException on invalid URL format', () {
const uriString = '::Not valid URI::';
expect(() => TokenOIDC.fromUri(uriString), throwsA(isA<FormatException>()));
});
});
}