Skip to content
This repository was archived by the owner on Jul 25, 2022. It is now read-only.

Commit 7fc697a

Browse files
committed
Add set/get RefreshToken capabilities to oauth_proxy_flow
1 parent 7e3e0bb commit 7fc697a

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

lib/src/auth/oauth_proxy_flow.dart

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'dart:async';
22
import 'dart:convert';
33

4-
import 'package:flutter/foundation.dart';
54
import 'package:http/http.dart' as http;
65
import 'package:s3i_flutter/src/auth/authentication_manager.dart';
76
import 'package:s3i_flutter/src/auth/client_identity.dart';
@@ -24,6 +23,7 @@ class OAuthProxyFlow extends AuthenticationManager {
2423
OAuthProxyFlow(ClientIdentity clientIdentity,
2524
{required this.openUrlCallback,
2625
this.onAuthSuccess,
26+
this.onNewRefreshToken,
2727
this.maxRetryPickup = 100,
2828
this.retryWaitingTimeMilliSec = 200,
2929
List<String> scopes = const <String>[]})
@@ -63,11 +63,17 @@ class OAuthProxyFlow extends AuthenticationManager {
6363
/// Is called everytime the [_accessToken] is changed.
6464
Function(AccessToken)? onAuthSuccess;
6565

66-
// TODO(poq): enable storage and loading of refresh token
67-
// Is invoked if a new refresh token is available. Could be used to store
68-
// the token in an external database.
69-
// Function(RefreshToken)? onNewRefreshToken;
70-
// void setRefreshToken(RefreshToken token){}
66+
/// Is invoked if a new refresh token is available. Could be used to store
67+
/// the token in an external database.
68+
Function(RefreshToken)? onNewRefreshToken;
69+
70+
/// Overwrites the current [_refreshToken]. Could be used to set an older
71+
/// token from an external database. To receive new [RefreshToken]s see
72+
/// the [onNewRefreshToken] callback.
73+
// ignore: avoid_setters_without_getters
74+
set refreshToken(RefreshToken token) {
75+
_refreshToken = token;
76+
}
7177

7278
/// Returns a valid [AccessToken] for the [clientIdentity]
7379
/// which is at least valid for the time specified in [tokenValidBuffer].
@@ -102,6 +108,7 @@ class OAuthProxyFlow extends AuthenticationManager {
102108
try {
103109
_parseAndSetTokenResponse(response.body);
104110
//_accessToken and _refreshToken should be valid if this is reached
111+
if (onNewRefreshToken != null) onNewRefreshToken!(_refreshToken!);
105112
if (onAuthSuccess != null) onAuthSuccess!(_accessToken!);
106113
return _accessToken!;
107114
// TODO(poq): test _accessToken.isNotExpired
@@ -146,6 +153,7 @@ class OAuthProxyFlow extends AuthenticationManager {
146153
_parseAndSetTokenResponse(response.body);
147154
//_accessToken and _refreshToken should be
148155
// valid if this code is reached
156+
if (onNewRefreshToken != null) onNewRefreshToken!(_refreshToken!);
149157
if (onAuthSuccess != null) onAuthSuccess!(_accessToken!);
150158
return _accessToken!;
151159
// TODO(poq): test _accessToken.isNotExpired
@@ -170,9 +178,11 @@ class OAuthProxyFlow extends AuthenticationManager {
170178
}
171179

172180
@override
173-
Future<bool> logout() {
181+
Future<bool> logout() async {
174182
// TODO(poq): implement logout
175-
throw UnimplementedError();
183+
//throw UnimplementedError();
184+
_invalidateLoginState();
185+
return true;
176186
}
177187

178188
@override

0 commit comments

Comments
 (0)