Skip to main content

tmdb_rs/endpoints/
authentication.rs

1use serde::Deserialize;
2
3use crate::common::StatusResponse;
4use crate::SessionId;
5
6#[derive(Debug, Clone, Deserialize)]
7pub struct RequestToken {
8    pub success: bool,
9    pub request_token: String,
10    pub expires_at: String,
11}
12
13impl RequestToken {
14    /// the url to send the user to so they can approve the token
15    pub fn redirect_url(&self) -> String {
16        format!(
17            "https://www.themoviedb.org/authenticate/{}",
18            self.request_token
19        )
20    }
21}
22
23#[derive(Debug, Clone, Deserialize)]
24pub struct Session {
25    pub success: bool,
26    pub session_id: String,
27}
28
29#[derive(Debug, Clone, Deserialize)]
30pub struct GuestSession {
31    pub success: bool,
32    pub guest_session_id: String,
33    pub expires_at: String,
34}
35
36endpoint! {
37    /// validate the credentials: the key is good when this answers
38    validate_key(): GET "/authentication" => bool [success]
39}
40
41endpoint! {
42    /// step 1 of the v3 auth flow: a request token for the user to approve
43    request_token(): GET "/authentication/token/new" => RequestToken
44}
45
46endpoint! {
47    /// step 2, app-approved variant: validate with the user's credentials
48    validate_with_login(): POST "/authentication/token/validate_with_login" => RequestToken {
49        body { username: &str, password: &str, request_token: &str }
50    }
51}
52
53endpoint! {
54    /// step 3: exchange an approved request token for a session
55    create_session(): POST "/authentication/session/new" => Session {
56        body { request_token: &str }
57    }
58}
59
60endpoint! {
61    /// a guest session, for rating without an account
62    create_guest_session(): GET "/authentication/guest_session/new" => GuestSession
63}
64
65endpoint! {
66    /// end a session
67    delete_session(): DELETE "/authentication/session" => StatusResponse {
68        body { session_id: SessionId }
69    }
70}