Skip to content

Commit aaa957d

Browse files
authored
feat(dart-dio): add methods to remove auth tokens in api client (#23386)
There is currently no way to easily remove a previously set auth token. Adding these methods will provide a convenient way to 'revoke' tokens. Design-Choice: Adding those new methods is a better alternative as just making the values of existing methods nullable, as e.g. #setBasicAuth(String, String, String) has two value parameters, which then would have to be checked for nullability and special handling would be required if only one value is null. This would result in a less clear API. By adding new methods following the naming #removeXY(String) no ambiguity is added and a clear API is kept.
1 parent 7a43401 commit aaa957d

File tree

8 files changed

+320
-0
lines changed
  • modules/openapi-generator/src/main/resources/dart/libraries/dio
  • samples/openapi3/client/petstore/dart-dio

8 files changed

+320
-0
lines changed

modules/openapi-generator/src/main/resources/dart/libraries/dio/api_client.mustache

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,62 @@ class {{clientName}} {
4646
}
4747
}
4848

49+
/// Removes the OAuth token associated with the given [name].
50+
///
51+
/// If no [OAuthInterceptor] is registered or no token exists for the given
52+
/// [name], this method has no effect.
53+
void removeOAuthToken(String name) {
54+
if (this.dio.interceptors.any((i) => i is OAuthInterceptor)) {
55+
(this.dio.interceptors.firstWhere((i) => i is OAuthInterceptor) as OAuthInterceptor).tokens.remove(name);
56+
}
57+
}
58+
4959
void setBearerAuth(String name, String token) {
5060
if (this.dio.interceptors.any((i) => i is BearerAuthInterceptor)) {
5161
(this.dio.interceptors.firstWhere((i) => i is BearerAuthInterceptor) as BearerAuthInterceptor).tokens[name] = token;
5262
}
5363
}
5464

65+
/// Removes the bearer authentication token associated with the given [name].
66+
///
67+
/// If no [BearerAuthInterceptor] is registered or no token exists for the
68+
/// given [name], this method has no effect.
69+
void removeBearerAuth(String name) {
70+
if (this.dio.interceptors.any((i) => i is BearerAuthInterceptor)) {
71+
(this.dio.interceptors.firstWhere((i) => i is BearerAuthInterceptor) as BearerAuthInterceptor).tokens.remove(name);
72+
}
73+
}
74+
5575
void setBasicAuth(String name, String username, String password) {
5676
if (this.dio.interceptors.any((i) => i is BasicAuthInterceptor)) {
5777
(this.dio.interceptors.firstWhere((i) => i is BasicAuthInterceptor) as BasicAuthInterceptor).authInfo[name] = BasicAuthInfo(username, password);
5878
}
5979
}
6080

81+
/// Removes the basic authentication credentials associated with the given [name].
82+
///
83+
/// If no [BasicAuthInterceptor] is registered or no credentials exist for the
84+
/// given [name], this method has no effect.
85+
void removeBasicAuth(String name) {
86+
if (this.dio.interceptors.any((i) => i is BasicAuthInterceptor)) {
87+
(this.dio.interceptors.firstWhere((i) => i is BasicAuthInterceptor) as BasicAuthInterceptor).authInfo.remove(name);
88+
}
89+
}
90+
6191
void setApiKey(String name, String apiKey) {
6292
if (this.dio.interceptors.any((i) => i is ApiKeyAuthInterceptor)) {
6393
(this.dio.interceptors.firstWhere((element) => element is ApiKeyAuthInterceptor) as ApiKeyAuthInterceptor).apiKeys[name] = apiKey;
6494
}
95+
}
96+
97+
/// Removes the API key associated with the given [name].
98+
///
99+
/// If no [ApiKeyAuthInterceptor] is registered or no API key exists for the
100+
/// given [name], this method has no effect.
101+
void removeApiKey(String name) {
102+
if (this.dio.interceptors.any((i) => i is ApiKeyAuthInterceptor)) {
103+
(this.dio.interceptors.firstWhere((element) => element is ApiKeyAuthInterceptor) as ApiKeyAuthInterceptor).apiKeys.remove(name);
104+
}
65105
}{{#apiInfo}}{{#apis}}
66106

67107
/// Get {{classname}} instance, base route and serializer can be overridden by a given but be careful,

samples/openapi3/client/petstore/dart-dio/binary_response/lib/src/api.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,64 @@ class Openapi {
4242
}
4343
}
4444

45+
/// Removes the OAuth token associated with the given [name].
46+
///
47+
/// If no [OAuthInterceptor] is registered or no token exists for the given
48+
/// [name], this method has no effect.
49+
void removeOAuthToken(String name) {
50+
if (this.dio.interceptors.any((i) => i is OAuthInterceptor)) {
51+
(this.dio.interceptors.firstWhere((i) => i is OAuthInterceptor) as OAuthInterceptor).tokens.remove(name);
52+
}
53+
}
54+
4555
void setBearerAuth(String name, String token) {
4656
if (this.dio.interceptors.any((i) => i is BearerAuthInterceptor)) {
4757
(this.dio.interceptors.firstWhere((i) => i is BearerAuthInterceptor) as BearerAuthInterceptor).tokens[name] = token;
4858
}
4959
}
5060

61+
/// Removes the bearer authentication token associated with the given [name].
62+
///
63+
/// If no [BearerAuthInterceptor] is registered or no token exists for the
64+
/// given [name], this method has no effect.
65+
void removeBearerAuth(String name) {
66+
if (this.dio.interceptors.any((i) => i is BearerAuthInterceptor)) {
67+
(this.dio.interceptors.firstWhere((i) => i is BearerAuthInterceptor) as BearerAuthInterceptor).tokens.remove(name);
68+
}
69+
}
70+
5171
void setBasicAuth(String name, String username, String password) {
5272
if (this.dio.interceptors.any((i) => i is BasicAuthInterceptor)) {
5373
(this.dio.interceptors.firstWhere((i) => i is BasicAuthInterceptor) as BasicAuthInterceptor).authInfo[name] = BasicAuthInfo(username, password);
5474
}
5575
}
5676

77+
/// Removes the basic authentication credentials associated with the given [name].
78+
///
79+
/// If no [BasicAuthInterceptor] is registered or no credentials exist for the
80+
/// given [name], this method has no effect.
81+
void removeBasicAuth(String name) {
82+
if (this.dio.interceptors.any((i) => i is BasicAuthInterceptor)) {
83+
(this.dio.interceptors.firstWhere((i) => i is BasicAuthInterceptor) as BasicAuthInterceptor).authInfo.remove(name);
84+
}
85+
}
86+
5787
void setApiKey(String name, String apiKey) {
5888
if (this.dio.interceptors.any((i) => i is ApiKeyAuthInterceptor)) {
5989
(this.dio.interceptors.firstWhere((element) => element is ApiKeyAuthInterceptor) as ApiKeyAuthInterceptor).apiKeys[name] = apiKey;
6090
}
6191
}
6292

93+
/// Removes the API key associated with the given [name].
94+
///
95+
/// If no [ApiKeyAuthInterceptor] is registered or no API key exists for the
96+
/// given [name], this method has no effect.
97+
void removeApiKey(String name) {
98+
if (this.dio.interceptors.any((i) => i is ApiKeyAuthInterceptor)) {
99+
(this.dio.interceptors.firstWhere((element) => element is ApiKeyAuthInterceptor) as ApiKeyAuthInterceptor).apiKeys.remove(name);
100+
}
101+
}
102+
63103
/// Get DefaultApi instance, base route and serializer can be overridden by a given but be careful,
64104
/// by doing that all interceptors will not be executed
65105
DefaultApi getDefaultApi() {

samples/openapi3/client/petstore/dart-dio/oneof/lib/src/api.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,64 @@ class Openapi {
4747
}
4848
}
4949

50+
/// Removes the OAuth token associated with the given [name].
51+
///
52+
/// If no [OAuthInterceptor] is registered or no token exists for the given
53+
/// [name], this method has no effect.
54+
void removeOAuthToken(String name) {
55+
if (this.dio.interceptors.any((i) => i is OAuthInterceptor)) {
56+
(this.dio.interceptors.firstWhere((i) => i is OAuthInterceptor) as OAuthInterceptor).tokens.remove(name);
57+
}
58+
}
59+
5060
void setBearerAuth(String name, String token) {
5161
if (this.dio.interceptors.any((i) => i is BearerAuthInterceptor)) {
5262
(this.dio.interceptors.firstWhere((i) => i is BearerAuthInterceptor) as BearerAuthInterceptor).tokens[name] = token;
5363
}
5464
}
5565

66+
/// Removes the bearer authentication token associated with the given [name].
67+
///
68+
/// If no [BearerAuthInterceptor] is registered or no token exists for the
69+
/// given [name], this method has no effect.
70+
void removeBearerAuth(String name) {
71+
if (this.dio.interceptors.any((i) => i is BearerAuthInterceptor)) {
72+
(this.dio.interceptors.firstWhere((i) => i is BearerAuthInterceptor) as BearerAuthInterceptor).tokens.remove(name);
73+
}
74+
}
75+
5676
void setBasicAuth(String name, String username, String password) {
5777
if (this.dio.interceptors.any((i) => i is BasicAuthInterceptor)) {
5878
(this.dio.interceptors.firstWhere((i) => i is BasicAuthInterceptor) as BasicAuthInterceptor).authInfo[name] = BasicAuthInfo(username, password);
5979
}
6080
}
6181

82+
/// Removes the basic authentication credentials associated with the given [name].
83+
///
84+
/// If no [BasicAuthInterceptor] is registered or no credentials exist for the
85+
/// given [name], this method has no effect.
86+
void removeBasicAuth(String name) {
87+
if (this.dio.interceptors.any((i) => i is BasicAuthInterceptor)) {
88+
(this.dio.interceptors.firstWhere((i) => i is BasicAuthInterceptor) as BasicAuthInterceptor).authInfo.remove(name);
89+
}
90+
}
91+
6292
void setApiKey(String name, String apiKey) {
6393
if (this.dio.interceptors.any((i) => i is ApiKeyAuthInterceptor)) {
6494
(this.dio.interceptors.firstWhere((element) => element is ApiKeyAuthInterceptor) as ApiKeyAuthInterceptor).apiKeys[name] = apiKey;
6595
}
6696
}
6797

98+
/// Removes the API key associated with the given [name].
99+
///
100+
/// If no [ApiKeyAuthInterceptor] is registered or no API key exists for the
101+
/// given [name], this method has no effect.
102+
void removeApiKey(String name) {
103+
if (this.dio.interceptors.any((i) => i is ApiKeyAuthInterceptor)) {
104+
(this.dio.interceptors.firstWhere((element) => element is ApiKeyAuthInterceptor) as ApiKeyAuthInterceptor).apiKeys.remove(name);
105+
}
106+
}
107+
68108
/// Get DefaultApi instance, base route and serializer can be overridden by a given but be careful,
69109
/// by doing that all interceptors will not be executed
70110
DefaultApi getDefaultApi() {

samples/openapi3/client/petstore/dart-dio/oneof_polymorphism_and_inheritance/lib/src/api.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,64 @@ class Openapi {
4848
}
4949
}
5050

51+
/// Removes the OAuth token associated with the given [name].
52+
///
53+
/// If no [OAuthInterceptor] is registered or no token exists for the given
54+
/// [name], this method has no effect.
55+
void removeOAuthToken(String name) {
56+
if (this.dio.interceptors.any((i) => i is OAuthInterceptor)) {
57+
(this.dio.interceptors.firstWhere((i) => i is OAuthInterceptor) as OAuthInterceptor).tokens.remove(name);
58+
}
59+
}
60+
5161
void setBearerAuth(String name, String token) {
5262
if (this.dio.interceptors.any((i) => i is BearerAuthInterceptor)) {
5363
(this.dio.interceptors.firstWhere((i) => i is BearerAuthInterceptor) as BearerAuthInterceptor).tokens[name] = token;
5464
}
5565
}
5666

67+
/// Removes the bearer authentication token associated with the given [name].
68+
///
69+
/// If no [BearerAuthInterceptor] is registered or no token exists for the
70+
/// given [name], this method has no effect.
71+
void removeBearerAuth(String name) {
72+
if (this.dio.interceptors.any((i) => i is BearerAuthInterceptor)) {
73+
(this.dio.interceptors.firstWhere((i) => i is BearerAuthInterceptor) as BearerAuthInterceptor).tokens.remove(name);
74+
}
75+
}
76+
5777
void setBasicAuth(String name, String username, String password) {
5878
if (this.dio.interceptors.any((i) => i is BasicAuthInterceptor)) {
5979
(this.dio.interceptors.firstWhere((i) => i is BasicAuthInterceptor) as BasicAuthInterceptor).authInfo[name] = BasicAuthInfo(username, password);
6080
}
6181
}
6282

83+
/// Removes the basic authentication credentials associated with the given [name].
84+
///
85+
/// If no [BasicAuthInterceptor] is registered or no credentials exist for the
86+
/// given [name], this method has no effect.
87+
void removeBasicAuth(String name) {
88+
if (this.dio.interceptors.any((i) => i is BasicAuthInterceptor)) {
89+
(this.dio.interceptors.firstWhere((i) => i is BasicAuthInterceptor) as BasicAuthInterceptor).authInfo.remove(name);
90+
}
91+
}
92+
6393
void setApiKey(String name, String apiKey) {
6494
if (this.dio.interceptors.any((i) => i is ApiKeyAuthInterceptor)) {
6595
(this.dio.interceptors.firstWhere((element) => element is ApiKeyAuthInterceptor) as ApiKeyAuthInterceptor).apiKeys[name] = apiKey;
6696
}
6797
}
6898

99+
/// Removes the API key associated with the given [name].
100+
///
101+
/// If no [ApiKeyAuthInterceptor] is registered or no API key exists for the
102+
/// given [name], this method has no effect.
103+
void removeApiKey(String name) {
104+
if (this.dio.interceptors.any((i) => i is ApiKeyAuthInterceptor)) {
105+
(this.dio.interceptors.firstWhere((element) => element is ApiKeyAuthInterceptor) as ApiKeyAuthInterceptor).apiKeys.remove(name);
106+
}
107+
}
108+
69109
/// Get BarApi instance, base route and serializer can be overridden by a given but be careful,
70110
/// by doing that all interceptors will not be executed
71111
BarApi getBarApi() {

samples/openapi3/client/petstore/dart-dio/oneof_primitive/lib/src/api.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,64 @@ class Openapi {
4747
}
4848
}
4949

50+
/// Removes the OAuth token associated with the given [name].
51+
///
52+
/// If no [OAuthInterceptor] is registered or no token exists for the given
53+
/// [name], this method has no effect.
54+
void removeOAuthToken(String name) {
55+
if (this.dio.interceptors.any((i) => i is OAuthInterceptor)) {
56+
(this.dio.interceptors.firstWhere((i) => i is OAuthInterceptor) as OAuthInterceptor).tokens.remove(name);
57+
}
58+
}
59+
5060
void setBearerAuth(String name, String token) {
5161
if (this.dio.interceptors.any((i) => i is BearerAuthInterceptor)) {
5262
(this.dio.interceptors.firstWhere((i) => i is BearerAuthInterceptor) as BearerAuthInterceptor).tokens[name] = token;
5363
}
5464
}
5565

66+
/// Removes the bearer authentication token associated with the given [name].
67+
///
68+
/// If no [BearerAuthInterceptor] is registered or no token exists for the
69+
/// given [name], this method has no effect.
70+
void removeBearerAuth(String name) {
71+
if (this.dio.interceptors.any((i) => i is BearerAuthInterceptor)) {
72+
(this.dio.interceptors.firstWhere((i) => i is BearerAuthInterceptor) as BearerAuthInterceptor).tokens.remove(name);
73+
}
74+
}
75+
5676
void setBasicAuth(String name, String username, String password) {
5777
if (this.dio.interceptors.any((i) => i is BasicAuthInterceptor)) {
5878
(this.dio.interceptors.firstWhere((i) => i is BasicAuthInterceptor) as BasicAuthInterceptor).authInfo[name] = BasicAuthInfo(username, password);
5979
}
6080
}
6181

82+
/// Removes the basic authentication credentials associated with the given [name].
83+
///
84+
/// If no [BasicAuthInterceptor] is registered or no credentials exist for the
85+
/// given [name], this method has no effect.
86+
void removeBasicAuth(String name) {
87+
if (this.dio.interceptors.any((i) => i is BasicAuthInterceptor)) {
88+
(this.dio.interceptors.firstWhere((i) => i is BasicAuthInterceptor) as BasicAuthInterceptor).authInfo.remove(name);
89+
}
90+
}
91+
6292
void setApiKey(String name, String apiKey) {
6393
if (this.dio.interceptors.any((i) => i is ApiKeyAuthInterceptor)) {
6494
(this.dio.interceptors.firstWhere((element) => element is ApiKeyAuthInterceptor) as ApiKeyAuthInterceptor).apiKeys[name] = apiKey;
6595
}
6696
}
6797

98+
/// Removes the API key associated with the given [name].
99+
///
100+
/// If no [ApiKeyAuthInterceptor] is registered or no API key exists for the
101+
/// given [name], this method has no effect.
102+
void removeApiKey(String name) {
103+
if (this.dio.interceptors.any((i) => i is ApiKeyAuthInterceptor)) {
104+
(this.dio.interceptors.firstWhere((element) => element is ApiKeyAuthInterceptor) as ApiKeyAuthInterceptor).apiKeys.remove(name);
105+
}
106+
}
107+
68108
/// Get DefaultApi instance, base route and serializer can be overridden by a given but be careful,
69109
/// by doing that all interceptors will not be executed
70110
DefaultApi getDefaultApi() {

samples/openapi3/client/petstore/dart-dio/petstore-timemachine/lib/src/api.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,64 @@ class Openapi {
4949
}
5050
}
5151

52+
/// Removes the OAuth token associated with the given [name].
53+
///
54+
/// If no [OAuthInterceptor] is registered or no token exists for the given
55+
/// [name], this method has no effect.
56+
void removeOAuthToken(String name) {
57+
if (this.dio.interceptors.any((i) => i is OAuthInterceptor)) {
58+
(this.dio.interceptors.firstWhere((i) => i is OAuthInterceptor) as OAuthInterceptor).tokens.remove(name);
59+
}
60+
}
61+
5262
void setBearerAuth(String name, String token) {
5363
if (this.dio.interceptors.any((i) => i is BearerAuthInterceptor)) {
5464
(this.dio.interceptors.firstWhere((i) => i is BearerAuthInterceptor) as BearerAuthInterceptor).tokens[name] = token;
5565
}
5666
}
5767

68+
/// Removes the bearer authentication token associated with the given [name].
69+
///
70+
/// If no [BearerAuthInterceptor] is registered or no token exists for the
71+
/// given [name], this method has no effect.
72+
void removeBearerAuth(String name) {
73+
if (this.dio.interceptors.any((i) => i is BearerAuthInterceptor)) {
74+
(this.dio.interceptors.firstWhere((i) => i is BearerAuthInterceptor) as BearerAuthInterceptor).tokens.remove(name);
75+
}
76+
}
77+
5878
void setBasicAuth(String name, String username, String password) {
5979
if (this.dio.interceptors.any((i) => i is BasicAuthInterceptor)) {
6080
(this.dio.interceptors.firstWhere((i) => i is BasicAuthInterceptor) as BasicAuthInterceptor).authInfo[name] = BasicAuthInfo(username, password);
6181
}
6282
}
6383

84+
/// Removes the basic authentication credentials associated with the given [name].
85+
///
86+
/// If no [BasicAuthInterceptor] is registered or no credentials exist for the
87+
/// given [name], this method has no effect.
88+
void removeBasicAuth(String name) {
89+
if (this.dio.interceptors.any((i) => i is BasicAuthInterceptor)) {
90+
(this.dio.interceptors.firstWhere((i) => i is BasicAuthInterceptor) as BasicAuthInterceptor).authInfo.remove(name);
91+
}
92+
}
93+
6494
void setApiKey(String name, String apiKey) {
6595
if (this.dio.interceptors.any((i) => i is ApiKeyAuthInterceptor)) {
6696
(this.dio.interceptors.firstWhere((element) => element is ApiKeyAuthInterceptor) as ApiKeyAuthInterceptor).apiKeys[name] = apiKey;
6797
}
6898
}
6999

100+
/// Removes the API key associated with the given [name].
101+
///
102+
/// If no [ApiKeyAuthInterceptor] is registered or no API key exists for the
103+
/// given [name], this method has no effect.
104+
void removeApiKey(String name) {
105+
if (this.dio.interceptors.any((i) => i is ApiKeyAuthInterceptor)) {
106+
(this.dio.interceptors.firstWhere((element) => element is ApiKeyAuthInterceptor) as ApiKeyAuthInterceptor).apiKeys.remove(name);
107+
}
108+
}
109+
70110
/// Get PetApi instance, base route and serializer can be overridden by a given but be careful,
71111
/// by doing that all interceptors will not be executed
72112
PetApi getPetApi() {

0 commit comments

Comments
 (0)