cognite/dto/iam/
session.rs

1use std::fmt::Display;
2
3use serde::{Deserialize, Serialize};
4use serde_with::skip_serializing_none;
5
6use crate::{to_query, IntoParams, SetCursor};
7
8#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
9#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
10/// Session status.
11pub enum SessionStatus {
12    /// Session is ready to be activated.
13    Ready,
14    /// Session is active.
15    Active,
16    /// Session has been cancelled.
17    Cancelled,
18    /// Session has been revoked.
19    Revoked,
20    /// Access to the IdP has been lost.
21    AccessLost,
22}
23
24impl Display for SessionStatus {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        match self {
27            Self::Ready => write!(f, "READY"),
28            Self::Active => write!(f, "ACTIVE"),
29            Self::Cancelled => write!(f, "CANCELLED"),
30            Self::Revoked => write!(f, "REVOKED"),
31            Self::AccessLost => write!(f, "ACCESS_LOST"),
32        }
33    }
34}
35
36#[derive(Clone, Debug, Default)]
37/// Query for listing sessions.
38pub struct SessionQuery {
39    /// Filter by session status.
40    pub status: Option<SessionStatus>,
41    /// Cursor for pagination.
42    pub cursor: Option<String>,
43    /// Maximum number of sessions to return, default 25, maximum 1000.
44    pub limit: Option<u32>,
45}
46
47impl SetCursor for SessionQuery {
48    fn set_cursor(&mut self, cursor: Option<String>) {
49        self.cursor = cursor;
50    }
51}
52
53impl IntoParams for SessionQuery {
54    fn into_params(self) -> Vec<(String, String)> {
55        let mut params = vec![];
56        to_query("status", &self.status, &mut params);
57        to_query("cursor", &self.cursor, &mut params);
58        to_query("limit", &self.limit, &mut params);
59        params
60    }
61}
62
63#[skip_serializing_none]
64#[derive(Serialize, Deserialize, Clone)]
65#[serde(rename_all = "camelCase")]
66/// A CDF Session.
67pub struct Session {
68    /// Session internal ID.
69    pub id: i64,
70    /// Session type.
71    pub r#type: Option<String>,
72    /// Session status.
73    pub status: SessionStatus,
74    /// Time this session was created, in milliseconds since epoch.
75    pub creation_time: Option<i64>,
76    /// Time when this session will expire, in milliseconds since epoch.
77    pub expiration_time: Option<i64>,
78    /// Session nonce, used by the CDF service to activate the session.
79    pub nonce: Option<String>,
80    /// Client ID.
81    pub client_id: Option<String>,
82}
83
84#[derive(Serialize, Deserialize, Clone)]
85#[serde(untagged, rename_all_fields = "camelCase")]
86/// Create a new session.
87pub enum AddSession {
88    /// Create a session using client credentials.
89    ClientCredentials {
90        /// Client ID.
91        client_id: String,
92        /// Client secret.
93        client_secret: String,
94    },
95    /// Create a session using token exchange.
96    TokenExchange {
97        /// Set to `true`.
98        token_exchange: bool,
99    },
100}