Skip to main content

azure_identity_helpers/device_code/
device_code_responses.rs

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4use azure_core::credentials::Secret;
5use serde::Deserialize;
6use std::fmt;
7
8/// Error response returned from the device code flow.
9#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
10pub struct DeviceCodeErrorResponse {
11    /// Name of the error.
12    pub error: String,
13    /// Description of the error.
14    pub error_description: String,
15    /// Uri to get more information on this error.
16    pub error_uri: String,
17}
18
19impl std::error::Error for DeviceCodeErrorResponse {}
20
21impl fmt::Display for DeviceCodeErrorResponse {
22    // This trait requires `fmt` with this exact signature.
23    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24        write!(f, "{}. {}", self.error, self.error_description)
25    }
26}
27
28/// A successful token response.
29#[derive(Debug, Clone, Deserialize)]
30pub struct DeviceCodeAuthorization {
31    /// Always `Bearer`.
32    pub token_type: String,
33    /// The scopes the access token is valid for.
34    /// Format: Space separated strings
35    pub scope: String,
36    /// Number of seconds the included access token is valid for.
37    pub expires_in: u64,
38    /// Issued for the scopes that were requested.
39    /// Format: Opaque string
40    access_token: Secret,
41    /// Issued if the original scope parameter included `offline_access`.
42    /// Format: JWT
43    refresh_token: Option<Secret>,
44    /// Issued if the original scope parameter included the openid scope.
45    /// Format: Opaque string
46    id_token: Option<Secret>,
47}
48
49impl DeviceCodeAuthorization {
50    /// Get the access token
51    #[must_use]
52    pub fn access_token(&self) -> &Secret {
53        &self.access_token
54    }
55    /// Get the refresh token
56    #[must_use]
57    pub fn refresh_token(&self) -> Option<&Secret> {
58        self.refresh_token.as_ref()
59    }
60    /// Get the id token
61    #[must_use]
62    pub fn id_token(&self) -> Option<&Secret> {
63        self.id_token.as_ref()
64    }
65}