1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2023 Ikerian AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use Client;
use ;
use serde_json;
/// Retrieves a new access token using a refresh token.
///
/// Sends a request to refresh an access token using the provided refresh token. This function constructs
/// a refresh token payload, sends it to the specified OAuth token endpoint, and handles the server's response.
///
/// # Arguments
///
/// * `client` - A reference to a `Client` object used to send HTTP requests.
/// * `url` - A `String` reference representing the base URL of the OAuth token endpoint.
/// * `refresh_token` - A `String` reference representing the refresh token used to obtain a new access token.
///
/// # Returns
///
/// A `Result` containing either the authentication response data on success (`AuthResponse`),
/// or an `reqwest::Error` on failure. The `AuthResponse` includes the new access token and other relevant information.
///
/// # Errors
///
/// Returns an `Err` of type `reqwest::Error` if the access token request fails, if the server response status
/// is not OK, or if there is an issue with the server's response format.
///
/// # Example
///
/// ```
/// use discovery_connect::auth::{access_token, AuthResponse};
/// use reqwest::Client;
///
/// async fn get_access_token_example() {
/// let client = Client::new();
/// let base_url = "https://api.example.discovery.retinai.com";
/// let refresh_token = "your_refresh_token";
/// let response = access_token(&client, &base_url, &refresh_token).await;
/// match response {
/// Ok(auth_response) => println!("Access token: {}", auth_response.access_token),
/// Err(e) => println!("Error: {}", e),
/// }
/// }
/// ```
pub async
/// Authenticates a user against a server and obtains an authentication token.
///
/// Asynchronously authenticates a user using their credentials and optionally a multi-factor authentication (MFA) code.
/// This function builds the authentication request, sends it to the server, and processes the response.
///
/// # Arguments
///
/// * `client` - A reference to a `reqwest::Client` used for making HTTP requests.
/// * `url` - A string slice representing the base URL of the authentication server.
/// * `client_id` - A string slice representing the client identifier.
/// * `client_secret` - A string slice representing the client secret.
/// * `email` - A string slice representing the user's email address.
/// * `password` - A string slice representing the user's password.
/// * `mfa_code` - An optional string slice representing the multi-factor authentication code.
///
/// # Returns
///
/// A `Result` containing either the authentication response data on success (`AuthResponse`),
/// or an `reqwest::Error` on failure.
///
/// # Errors
///
/// Returns an `Err` of type `reqwest::Error` if the authentication request fails or if the server
/// response status is not OK.
///
/// # Example
///
/// ```
/// use discovery_connect::{QueryClient};
/// use discovery_connect::auth::{login, AuthResponse};
/// use std::sync::Arc;
///
/// let qc = Arc::new(QueryClient::new(
/// "https://api.example.discovery.retinai.com",
/// "client_id",
/// "client_secret",
/// "user@example",
/// "password123",
/// std::time::Duration::from_secs(10)));
/// async {
/// let res = login(
/// &qc.client,
/// "https://api.example.discovery.retinai.com",
/// "client_id123",
/// "client_secret456",
/// "user@example.com",
/// "password123",
/// Some("123456")
/// ).await;
/// match res {
/// Ok(auth_response) => println!("Access Token: {:?}", auth_response.access_token),
/// Err(e) => println!("Error: {}", e),
/// }
/// };
/// ```
pub async