aboutsummaryrefslogtreecommitdiff
path: root/src/model.rs
blob: 86e30051afeb5a80ffbb5572df1d35a957a75e41 (plain) (blame)
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
193
194
195
196
//! A collection of models that can be deserialized from response bodies and
//! serialized into request bodies.

use serenity_model::{PartialGuild, Webhook};

/// Structure of data used as the body of a request to exchange the [`code`] for
/// an access token.
///
/// [`code`]: #structfield.code
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct AccessTokenExchangeRequest {
    /// Your application's client ID.
    pub client_id: u64,
    /// Your application's client secret.
    pub client_secret: String,
    /// The code in the query parameters to your redirect URI.
    pub code: String,
    /// The type of grant.
    ///
    /// Must be set to `authorization_code`.
    ///
    /// If using [`AccessTokenExchangeRequest::new`], this will automatically be
    /// set for you.
    pub grant_type: String,
    /// Your redirect URI.
    pub redirect_uri: String,
}

impl AccessTokenExchangeRequest {
    /// Creates a new request body for exchanging a code for an access token.
    ///
    /// # Examples
    ///
    /// Create a new request and assert that the grant type is correct:
    ///
    /// ```rust
    /// use serenity_oauth::model::AccessTokenExchangeRequest;
    ///
    /// let request = AccessTokenExchangeRequest::new(
    ///     249608697955745802,
    ///     "dd99opUAgs7SQEtk2kdRrTMU5zagR2a4",
    ///     "user code here",
    ///     "https://myapplication.website",
    /// );
    ///
    /// assert_eq!(request.grant_type, "authorization_code");
    /// ```
    pub fn new<S, T, U>(
        client_id: u64,
        client_secret: S,
        code: T,
        redirect_uri: U,
    ) -> Self where S: Into<String>, T: Into<String>, U: Into<String> {
        Self {
            client_secret: client_secret.into(),
            code: code.into(),
            grant_type: "authorization_code".to_owned(),
            redirect_uri: redirect_uri.into(),
            client_id,
        }
    }
}

/// Response data containing a new access token and refresh token.
///
/// This can be received when either:
///
/// 1. exchanging a code for an access token;
/// 2. exchanging a refresh token for a fresh access token.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct AccessTokenResponse {
    /// The user's access token.
    pub access_token: String,
    /// The number of seconds until the access token expires.
    pub expires_in: u64,
    /// The refresh token to use when the access token expires.
    pub refresh_token: String,
    /// The scope that is granted.
    pub scope: String,
    /// The type of token received.
    pub token_type: String,
}

/// Response data containing an access token, but without a refresh token.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ClientCredentialsAccessTokenResponse {
    /// The user's access token.
    pub access_token: String,
    /// The number of seconds until the access token expires.
    pub expires_in: u64,
    /// The scope that is granted.
    pub scope: String,
    /// The type of token received.
    pub token_type: String,
}

/// An extended [`Scope::Bot`] authorization flow.
///
/// This will authorize the application as a bot into a user's selected guild,
/// as well as granting additional scopes.
///
/// [`Scope::Bot`]: ../enum.Scope.html#variant.Bot
#[derive(Clone, Debug, Deserialize)]
pub struct ExtendedBotAuthorizationResponse {
    /// The user's access token.
    pub access_token: String,
    /// The number of seconds until the access token expires.
    pub expires_in: u64,
    /// Partial guild data that the application was authorized into.
    pub guild: PartialGuild,
    /// The refresh token to use when the access token expires.
    pub refresh_token: String,
    /// The scope that is granted.
    pub scope: String,
    /// The type of token received.
    pub token_type: String,
}

/// Request for exchanging a refresh token for a new access token.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RefreshTokenRequest {
    /// Your application's client ID.
    pub client_id: u64,
    /// Your application's client secret.
    pub client_secret: String,
    /// The type of grant.
    ///
    /// Must be set to `refresh_token`.
    ///
    /// If using [`RefreshTokenRequest::new`], this will automatically be
    /// set for you.
    pub grant_type: String,
    /// Your redirect URI.
    pub redirect_uri: String,
    /// The user's refresh token.
    pub refresh_token: String,
}

impl RefreshTokenRequest {
    /// Creates a new request body for refreshing an access token using a
    /// refresh token.
    ///
    /// # Examples
    ///
    /// Create a new request and assert that the grant type is correct:
    ///
    /// ```rust
    /// use serenity_oauth::model::RefreshTokenRequest;
    ///
    /// let request = RefreshTokenRequest::new(
    ///     249608697955745802,
    ///     "dd99opUAgs7SQEtk2kdRrTMU5zagR2a4",
    ///     "user code here",
    ///     "https://myapplication.website",
    /// );
    ///
    /// assert_eq!(request.grant_type, "refresh_token");
    /// ```
    pub fn new<S, T, U>(
        client_id: u64,
        client_secret: S,
        redirect_uri: T,
        refresh_token: U,
    ) -> Self where S: Into<String>, T: Into<String>, U: Into<String> {
        Self {
            client_secret: client_secret.into(),
            grant_type: "refresh_token".to_owned(),
            redirect_uri: redirect_uri.into(),
            refresh_token: refresh_token.into(),
            client_id,
        }
    }
}

/// The response data from a successful trading of a code for an access token
/// after authorization of [`Scope::WebhookIncoming`].
///
/// You should store [`webhook`]'s `id` and `token` structfields.
///
/// [`Scope::WebhookIncoming`]: ../enum.Scope.html#variant.WebhookIncoming
/// [`webhook`]: #structfield.webhook
#[derive(Clone, Debug, Deserialize)]
pub struct WebhookTokenResponse {
    /// The user's access token.
    pub access_token: String,
    /// The number of seconds until the access token expires.
    pub expires_in: u64,
    /// The refresh token to use when the access token expires.
    pub refresh_token: String,
    /// The scope that is granted.
    pub scope: String,
    /// The type of token received.
    pub token_type: String,
    /// Information about the webhook created.
    pub webhook: Webhook,
}