-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from nimblehq/release/0.4.0
- Loading branch information
Showing
48 changed files
with
1,136 additions
and
154 deletions.
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:survey_flutter/api/authentication_api_service.dart'; | ||
import 'package:survey_flutter/di/provider/dio_provider.dart'; | ||
import 'package:survey_flutter/env.dart'; | ||
import 'package:survey_flutter/model/api_token.dart'; | ||
import 'package:survey_flutter/model/request/refresh_token_request.dart'; | ||
import 'package:survey_flutter/storage/secure_storage.dart'; | ||
import 'package:survey_flutter/storage/secure_storage_impl.dart'; | ||
import 'package:survey_flutter/utils/serializer/api_token_serializer.dart'; | ||
|
||
final tokenDataSourceProvider = Provider<TokenDataSource>((ref) { | ||
return TokenDataSourceImpl(ref.watch(secureStorageProvider), | ||
AuthenticationApiService(DioProvider().getDio())); | ||
}); | ||
|
||
abstract class TokenDataSource { | ||
Future<ApiToken> getToken({bool forceRefresh}); | ||
Future<void> setToken(ApiToken token); | ||
} | ||
|
||
class TokenDataSourceImpl extends TokenDataSource { | ||
final SecureStorage _secureStorage; | ||
final AuthenticationApiService _authenticationApiService; | ||
final String _grantType = 'refresh_token'; | ||
|
||
TokenDataSourceImpl( | ||
this._secureStorage, | ||
this._authenticationApiService, | ||
); | ||
|
||
@override | ||
Future<ApiToken> getToken({bool forceRefresh = false}) async { | ||
final currentToken = await _secureStorage.getValue( | ||
key: SecureStorageKey.apiToken, | ||
serializer: ApiTokenSerializer(), | ||
); | ||
|
||
if (!forceRefresh) { | ||
return currentToken; | ||
} | ||
|
||
final tokenResponse = await _authenticationApiService.refreshToken( | ||
RefreshTokenRequest( | ||
grantType: _grantType, | ||
refreshToken: currentToken.refreshToken, | ||
clientId: Env.clientId, | ||
clientSecret: Env.clientSecret, | ||
), | ||
); | ||
final newToken = tokenResponse.toApiToken(); | ||
await _secureStorage.save( | ||
value: newToken, | ||
key: SecureStorageKey.apiToken, | ||
); | ||
return newToken; | ||
} | ||
|
||
@override | ||
Future<void> setToken(ApiToken token) async { | ||
await _secureStorage.save(value: token, key: SecureStorageKey.apiToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:dio/dio.dart'; | ||
import 'package:survey_flutter/api/data_sources/token_data_source.dart'; | ||
|
||
const String _headerAuthorization = 'Authorization'; | ||
const String _retryCountOption = 'Retry-Count'; | ||
|
||
class AuthInterceptor extends Interceptor { | ||
final Dio _dio; | ||
final TokenDataSource _tokenDataSource; | ||
|
||
AuthInterceptor( | ||
this._dio, | ||
this._tokenDataSource, | ||
); | ||
|
||
@override | ||
void onRequest( | ||
RequestOptions options, | ||
RequestInterceptorHandler handler, | ||
) async { | ||
final token = await _tokenDataSource.getToken(); | ||
options.headers.putIfAbsent( | ||
_headerAuthorization, () => "${token.tokenType} ${token.accessToken}"); | ||
super.onRequest(options, handler); | ||
} | ||
|
||
@override | ||
void onError( | ||
DioError err, | ||
ErrorInterceptorHandler handler, | ||
) { | ||
final statusCode = err.response?.statusCode; | ||
final requestOptions = err.requestOptions; | ||
|
||
if (statusCode != HttpStatus.forbidden && | ||
statusCode != HttpStatus.unauthorized && | ||
requestOptions.extra[_retryCountOption] != 1) { | ||
handler.next(err); | ||
return; | ||
} | ||
|
||
requestOptions.extra[_retryCountOption] = 1; | ||
_refreshTokenAndRetry(requestOptions, handler); | ||
} | ||
|
||
Future<void> _refreshTokenAndRetry( | ||
RequestOptions options, | ||
ErrorInterceptorHandler handler, | ||
) async { | ||
final token = await _tokenDataSource.getToken(forceRefresh: true); | ||
final headers = options.headers; | ||
headers[_headerAuthorization] = "${token.tokenType} ${token.accessToken}"; | ||
final newOptions = options.copyWith(headers: headers); | ||
await _dio.fetch(newOptions).then((response) => handler.resolve(response)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
|
||
part 'refresh_token_request.g.dart'; | ||
|
||
@JsonSerializable() | ||
class RefreshTokenRequest { | ||
@JsonKey(name: 'grant_type') | ||
final String grantType; | ||
@JsonKey(name: 'refresh_token') | ||
final String refreshToken; | ||
@JsonKey(name: 'client_id') | ||
final String clientId; | ||
@JsonKey(name: 'client_secret') | ||
final String clientSecret; | ||
|
||
RefreshTokenRequest({ | ||
required this.grantType, | ||
required this.refreshToken, | ||
required this.clientId, | ||
required this.clientSecret, | ||
}); | ||
|
||
Map<String, dynamic> toJson() => _$RefreshTokenRequestToJson(this); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.