checkpoint
This commit is contained in:
@@ -5,20 +5,51 @@ import 'package:http_interceptor/http_interceptor.dart';
|
||||
import 'package:jwt_decoder/jwt_decoder.dart';
|
||||
import '../models/user_profile.dart';
|
||||
import '../utils/logging_interceptor.dart';
|
||||
import '../utils/http_client_factory.dart';
|
||||
|
||||
class AuthConfig {
|
||||
final String realm;
|
||||
final String authServerUrl;
|
||||
final String clientId;
|
||||
final bool allowSelfSignedCertificate;
|
||||
|
||||
const AuthConfig({
|
||||
required this.realm,
|
||||
required this.authServerUrl,
|
||||
required this.clientId,
|
||||
this.allowSelfSignedCertificate = false,
|
||||
});
|
||||
|
||||
static const AuthConfig development = AuthConfig(
|
||||
realm: 'dev',
|
||||
authServerUrl: 'https://auth.goutezplanb.com',
|
||||
clientId: 'delivery-mobile-app',
|
||||
allowSelfSignedCertificate: true,
|
||||
);
|
||||
|
||||
static const AuthConfig production = AuthConfig(
|
||||
realm: 'planb-internal',
|
||||
authServerUrl: 'https://auth.goutezplanb.com',
|
||||
clientId: 'delivery-mobile-app',
|
||||
);
|
||||
|
||||
String get tokenEndpoint => '$authServerUrl/realms/$realm/protocol/openid-connect/token';
|
||||
}
|
||||
|
||||
class AuthService {
|
||||
static const String _tokenKey = 'auth_token';
|
||||
static const String _refreshTokenKey = 'refresh_token';
|
||||
static const String _tokenEndpoint = 'https://auth.goutezplanb.com/realms/planb-internal/protocol/openid-connect/token';
|
||||
static const String _clientId = 'delivery-mobile-app';
|
||||
|
||||
final AuthConfig _config;
|
||||
final FlutterSecureStorage _secureStorage;
|
||||
final http.Client _httpClient;
|
||||
|
||||
AuthService({
|
||||
AuthConfig config = AuthConfig.development,
|
||||
FlutterSecureStorage? secureStorage,
|
||||
http.Client? httpClient,
|
||||
}) : _secureStorage = secureStorage ?? const FlutterSecureStorage(
|
||||
}) : _config = config,
|
||||
_secureStorage = secureStorage ?? const FlutterSecureStorage(
|
||||
aOptions: AndroidOptions(
|
||||
encryptedSharedPreferences: true,
|
||||
),
|
||||
@@ -31,6 +62,9 @@ class AuthService {
|
||||
),
|
||||
_httpClient = httpClient ?? InterceptedClient.build(
|
||||
interceptors: [LoggingInterceptor()],
|
||||
client: HttpClientFactory.createClient(
|
||||
allowSelfSigned: config.allowSelfSignedCertificate,
|
||||
),
|
||||
);
|
||||
|
||||
Future<AuthResult> login({
|
||||
@@ -39,13 +73,13 @@ class AuthService {
|
||||
}) async {
|
||||
try {
|
||||
final response = await _httpClient.post(
|
||||
Uri.parse(_tokenEndpoint),
|
||||
Uri.parse(_config.tokenEndpoint),
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: {
|
||||
'grant_type': 'password',
|
||||
'client_id': _clientId,
|
||||
'client_id': _config.clientId,
|
||||
'username': username,
|
||||
'password': password,
|
||||
'scope': 'openid profile offline_access',
|
||||
@@ -81,13 +115,13 @@ class AuthService {
|
||||
}
|
||||
|
||||
final response = await _httpClient.post(
|
||||
Uri.parse(_tokenEndpoint),
|
||||
Uri.parse(_config.tokenEndpoint),
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: {
|
||||
'grant_type': 'refresh_token',
|
||||
'client_id': _clientId,
|
||||
'client_id': _config.clientId,
|
||||
'refresh_token': refreshToken,
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user