twilight_model/oauth/
current_authorization_information.rs

1use super::Application;
2use crate::{user::User, util::Timestamp};
3use serde::{Deserialize, Serialize};
4
5/// Information about the current OAuth2 authorization.
6///
7/// Requires authentication with a bearer token to make the request necessary to
8/// retrieve this.
9///
10/// Refer to [Discord Docs/Get Current Authorization Information][1] for more
11/// information.
12///
13/// [1]: https://discord.com/developers/docs/topics/oauth2#get-current-authorization-information
14#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
15pub struct CurrentAuthorizationInformation {
16    /// Current application.
17    pub application: Application,
18    /// When the access token expires.
19    pub expires: Timestamp,
20    /// List of [scopes] the [`user`] has authorized the [`application`] for.
21    ///
22    /// [`application`]: Self::application
23    /// [`user`]: Self::user
24    /// [scopes]: crate::oauth::scope
25    pub scopes: Vec<String>,
26    /// User who has authorized, if the user has authorized with the
27    /// [`IDENTIFY`] scope.
28    ///
29    /// [`IDENTIFY`]: crate::oauth::scope::IDENTIFY
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub user: Option<User>,
32}
33
34#[cfg(test)]
35mod tests {
36    use crate::{
37        id::Id,
38        oauth::{scope, Application},
39        test::image_hash,
40        util::{datetime::TimestampParseError, Timestamp},
41    };
42
43    use super::CurrentAuthorizationInformation;
44    use serde::{Deserialize, Serialize};
45    use serde_test::{assert_tokens, Token};
46    use static_assertions::{assert_fields, assert_impl_all};
47    use std::fmt::Debug;
48
49    assert_fields!(
50        CurrentAuthorizationInformation: application,
51        expires,
52        scopes,
53        user
54    );
55    assert_impl_all!(
56        CurrentAuthorizationInformation: Clone,
57        Debug,
58        Deserialize<'static>,
59        Eq,
60        PartialEq,
61        Send,
62        Serialize,
63        Sync
64    );
65
66    #[test]
67    fn serde() -> Result<(), TimestampParseError> {
68        const DESCRIPTION: &str =
69            "Twilight Sparkle is the central main character of My Little Pony Friendship is Magic.";
70        const NAME: &str = "Twilight Sparkle";
71
72        let value = CurrentAuthorizationInformation {
73            application: Application {
74                approximate_guild_count: Some(2),
75                approximate_user_install_count: Some(4),
76                bot: None,
77                bot_public: true,
78                bot_require_code_grant: true,
79                cover_image: None,
80                custom_install_url: None,
81                description: DESCRIPTION.to_owned(),
82                flags: None,
83                guild: None,
84                guild_id: None,
85                icon: Some(image_hash::ICON),
86                id: Id::new(100_000_000_000_000_000),
87                install_params: None,
88                integration_types_config: None,
89                interactions_endpoint_url: None,
90                name: NAME.to_owned(),
91                owner: None,
92                primary_sku_id: None,
93                privacy_policy_url: None,
94                redirect_uris: None,
95                role_connections_verification_url: None,
96                rpc_origins: Vec::new(),
97                slug: None,
98                tags: None,
99                team: None,
100                terms_of_service_url: None,
101                verify_key: "a".to_owned(),
102            },
103            expires: Timestamp::parse("2023-01-09T17:19:44.000000+00:00")?,
104            scopes: Vec::from([scope::APPLICATIONS_COMMANDS_PERMISSIONS_UPDATE.to_owned()]),
105            user: None,
106        };
107
108        assert_tokens(
109            &value,
110            &[
111                Token::Struct {
112                    name: "CurrentAuthorizationInformation",
113                    len: 3,
114                },
115                Token::Str("application"),
116                Token::Struct {
117                    name: "Application",
118                    len: 12,
119                },
120                Token::Str("approximate_guild_count"),
121                Token::Some,
122                Token::U64(2),
123                Token::Str("approximate_user_install_count"),
124                Token::Some,
125                Token::U64(4),
126                Token::Str("bot_public"),
127                Token::Bool(true),
128                Token::Str("bot_require_code_grant"),
129                Token::Bool(true),
130                Token::Str("description"),
131                Token::Str(DESCRIPTION),
132                Token::Str("flags"),
133                Token::None,
134                Token::Str("icon"),
135                Token::Some,
136                Token::Str(image_hash::ICON_INPUT),
137                Token::Str("id"),
138                Token::NewtypeStruct { name: "Id" },
139                Token::Str("100000000000000000"),
140                Token::Str("name"),
141                Token::Str(NAME),
142                Token::Str("rpc_origins"),
143                Token::Seq { len: Some(0) },
144                Token::SeqEnd,
145                Token::Str("team"),
146                Token::None,
147                Token::Str("verify_key"),
148                Token::Str("a"),
149                Token::StructEnd,
150                Token::Str("expires"),
151                Token::Str("2023-01-09T17:19:44.000000+00:00"),
152                Token::Str("scopes"),
153                Token::Seq { len: Some(1) },
154                Token::Str(scope::APPLICATIONS_COMMANDS_PERMISSIONS_UPDATE),
155                Token::SeqEnd,
156                Token::StructEnd,
157            ],
158        );
159
160        Ok(())
161    }
162}