Skip to main content

openstack_keystone_core/api/v4/
token_restriction.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12//
13// SPDX-License-Identifier: Apache-2.0
14//! Token restriction types.
15use axum::{
16    Json,
17    http::StatusCode,
18    response::{IntoResponse, Response},
19};
20
21pub use openstack_keystone_api_types::v4::token_restriction::*;
22
23use crate::token::types::{
24    self as types, TokenRestriction as ProviderTokenRestriction,
25    TokenRestrictionCreate as ProviderTokenRestrictionCreate,
26    TokenRestrictionUpdate as ProviderTokenRestrictionUpdate,
27};
28
29impl From<TokenRestrictionListParameters> for types::TokenRestrictionListParameters {
30    fn from(value: TokenRestrictionListParameters) -> Self {
31        Self {
32            domain_id: value.domain_id,
33            user_id: value.user_id,
34            project_id: value.project_id,
35        }
36    }
37}
38
39impl From<ProviderTokenRestriction> for TokenRestriction {
40    fn from(value: ProviderTokenRestriction) -> Self {
41        Self {
42            allow_rescope: value.allow_rescope,
43            allow_renew: value.allow_renew,
44            id: value.id,
45            domain_id: value.domain_id,
46            project_id: value.project_id,
47            user_id: value.user_id,
48            roles: value
49                .roles
50                .map(|roles| roles.into_iter().map(Into::into).collect())
51                .unwrap_or_default(),
52        }
53    }
54}
55
56impl From<TokenRestrictionCreateRequest> for ProviderTokenRestrictionCreate {
57    fn from(value: TokenRestrictionCreateRequest) -> Self {
58        Self {
59            allow_rescope: value.restriction.allow_rescope,
60            allow_renew: value.restriction.allow_renew,
61            id: String::new(),
62            domain_id: value.restriction.domain_id,
63            project_id: value.restriction.project_id,
64            user_id: value.restriction.user_id,
65            role_ids: value
66                .restriction
67                .roles
68                .into_iter()
69                .map(|role| role.id)
70                .collect(),
71        }
72    }
73}
74
75impl From<TokenRestrictionUpdateRequest> for ProviderTokenRestrictionUpdate {
76    fn from(value: TokenRestrictionUpdateRequest) -> Self {
77        Self {
78            allow_rescope: value.restriction.allow_rescope,
79            allow_renew: value.restriction.allow_renew,
80            project_id: value.restriction.project_id,
81            user_id: value.restriction.user_id,
82            role_ids: value
83                .restriction
84                .roles
85                .map(|roles| roles.into_iter().map(|role| role.id).collect()),
86        }
87    }
88}
89
90//impl From<crate::role::types::RoleRef> for RoleRef {
91//    fn from(value: crate::role::types::RoleRef) -> Self {
92//        Self {
93//            id: value.id,
94//            name: value.name.unwrap_or_default(),
95//            domain_id: value.domain_id,
96//        }
97//    }
98//}
99
100impl IntoResponse for ProviderTokenRestriction {
101    fn into_response(self) -> Response {
102        (
103            StatusCode::OK,
104            Json(TokenRestrictionResponse {
105                restriction: TokenRestriction::from(self),
106            }),
107        )
108            .into_response()
109    }
110}