matrix_sdk/widget/machine/
openid.rs

1// Copyright 2023 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::time::Duration;
16
17use ruma::{
18    api::client::account::request_openid_token, authentication::TokenType, OwnedServerName,
19};
20use serde::{Deserialize, Serialize};
21
22#[derive(Clone, Debug, Deserialize, Serialize)]
23pub(crate) struct OpenIdState {
24    pub(crate) original_request_id: String,
25    pub(crate) access_token: String,
26    #[serde(with = "ruma::serde::duration::secs")]
27    pub(crate) expires_in: Duration,
28    pub(crate) matrix_server_name: OwnedServerName,
29    pub(crate) token_type: TokenType,
30}
31
32impl OpenIdState {
33    pub(crate) fn new(id: String, ruma: request_openid_token::v3::Response) -> Self {
34        Self {
35            original_request_id: id,
36            access_token: ruma.access_token,
37            expires_in: ruma.expires_in,
38            matrix_server_name: ruma.matrix_server_name,
39            token_type: ruma.token_type,
40        }
41    }
42}
43
44#[derive(Clone, Debug, Deserialize, Serialize)]
45#[serde(rename_all = "lowercase")]
46#[serde(tag = "state")]
47pub(crate) enum OpenIdResponse {
48    Allowed(OpenIdState),
49    Blocked {
50        original_request_id: String,
51    },
52    #[serde(rename = "request")]
53    Pending,
54}