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
// Copyright 2024 New Vector Ltd.
// Copyright 2022-2024 Kévin Commaille.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.

//! Requests for [Token Introspection].
//!
//! [Token Introspection]: https://www.rfc-editor.org/rfc/rfc7662

use chrono::{DateTime, Utc};
use headers::{Authorization, HeaderMapExt};
use http::Request;
use mas_http::{CatchHttpCodesLayer, FormUrlencodedRequestLayer, JsonResponseLayer};
use mas_iana::oauth::OAuthTokenTypeHint;
use oauth2_types::requests::{IntrospectionRequest, IntrospectionResponse};
use rand::Rng;
use serde::Serialize;
use tower::{Layer, Service, ServiceExt};
use url::Url;

use crate::{
    error::IntrospectionError,
    http_service::HttpService,
    types::client_credentials::{ClientCredentials, RequestWithClientCredentials},
    utils::{http_all_error_status_codes, http_error_mapper},
};

/// The method used to authenticate at the introspection endpoint.
pub enum IntrospectionAuthentication<'a> {
    /// Using client authentication.
    Credentials(ClientCredentials),

    /// Using a bearer token.
    BearerToken(&'a str),
}

impl<'a> IntrospectionAuthentication<'a> {
    /// Constructs an `IntrospectionAuthentication` from the given client
    /// credentials.
    #[must_use]
    pub fn with_client_credentials(credentials: ClientCredentials) -> Self {
        Self::Credentials(credentials)
    }

    /// Constructs an `IntrospectionAuthentication` from the given bearer token.
    #[must_use]
    pub fn with_bearer_token(token: &'a str) -> Self {
        Self::BearerToken(token)
    }

    fn apply_to_request<T: Serialize>(
        self,
        request: Request<T>,
        now: DateTime<Utc>,
        rng: &mut impl Rng,
    ) -> Result<Request<RequestWithClientCredentials<T>>, IntrospectionError> {
        let res = match self {
            IntrospectionAuthentication::Credentials(client_credentials) => {
                client_credentials.apply_to_request(request, now, rng)?
            }
            IntrospectionAuthentication::BearerToken(access_token) => {
                let (mut parts, body) = request.into_parts();

                parts
                    .headers
                    .typed_insert(Authorization::bearer(access_token)?);

                let body = RequestWithClientCredentials {
                    body,
                    credentials: None,
                };

                http::Request::from_parts(parts, body)
            }
        };

        Ok(res)
    }
}

impl<'a> From<ClientCredentials> for IntrospectionAuthentication<'a> {
    fn from(credentials: ClientCredentials) -> Self {
        Self::with_client_credentials(credentials)
    }
}

/// Obtain information about a token.
///
/// # Arguments
///
/// * `http_service` - The service to use for making HTTP requests.
///
/// * `authentication` - The method used to authenticate the request.
///
/// * `revocation_endpoint` - The URL of the issuer's Revocation endpoint.
///
/// * `token` - The token to introspect.
///
/// * `token_type_hint` - Hint about the type of the token.
///
/// * `now` - The current time.
///
/// * `rng` - A random number generator.
///
/// # Errors
///
/// Returns an error if the request fails or the response is invalid.
#[tracing::instrument(skip_all, fields(introspection_endpoint))]
pub async fn introspect_token(
    http_service: &HttpService,
    authentication: IntrospectionAuthentication<'_>,
    introspection_endpoint: &Url,
    token: String,
    token_type_hint: Option<OAuthTokenTypeHint>,
    now: DateTime<Utc>,
    rng: &mut impl Rng,
) -> Result<IntrospectionResponse, IntrospectionError> {
    tracing::debug!("Introspecting token…");

    let introspection_request = IntrospectionRequest {
        token,
        token_type_hint,
    };
    let introspection_request =
        http::Request::post(introspection_endpoint.as_str()).body(introspection_request)?;

    let introspection_request = authentication.apply_to_request(introspection_request, now, rng)?;

    let service = (
        FormUrlencodedRequestLayer::default(),
        JsonResponseLayer::<IntrospectionResponse>::default(),
        CatchHttpCodesLayer::new(http_all_error_status_codes(), http_error_mapper),
    )
        .layer(http_service.clone());

    let introspection_response = service
        .ready_oneshot()
        .await?
        .call(introspection_request)
        .await?
        .into_body();

    Ok(introspection_response)
}