matrix_sdk/authentication/oauth/auth_code_builder.rs
1// Copyright 2022 Kévin Commaille
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::borrow::Cow;
16
17use oauth2::{
18 basic::BasicClient as OAuthClient, AuthUrl, CsrfToken, PkceCodeChallenge, RedirectUrl, Scope,
19};
20use ruma::{
21 api::client::discovery::get_authorization_server_metadata::v1::Prompt, OwnedDeviceId, UserId,
22};
23use tracing::{info, instrument};
24use url::Url;
25
26use super::{ClientRegistrationData, OAuth, OAuthError};
27use crate::{authentication::oauth::AuthorizationValidationData, Result};
28
29/// Builder type used to configure optional settings for authorization with an
30/// OAuth 2.0 authorization server via the Authorization Code flow.
31///
32/// Created with [`OAuth::login()`]. Finalized with [`Self::build()`].
33#[allow(missing_debug_implementations)]
34pub struct OAuthAuthCodeUrlBuilder {
35 oauth: OAuth,
36 registration_data: Option<ClientRegistrationData>,
37 scopes: Vec<Scope>,
38 device_id: OwnedDeviceId,
39 redirect_uri: Url,
40 prompt: Option<Vec<Prompt>>,
41 login_hint: Option<String>,
42}
43
44impl OAuthAuthCodeUrlBuilder {
45 pub(super) fn new(
46 oauth: OAuth,
47 scopes: Vec<Scope>,
48 device_id: OwnedDeviceId,
49 redirect_uri: Url,
50 registration_data: Option<ClientRegistrationData>,
51 ) -> Self {
52 Self {
53 oauth,
54 registration_data,
55 scopes,
56 device_id,
57 redirect_uri,
58 prompt: None,
59 login_hint: None,
60 }
61 }
62
63 /// Set the [`Prompt`] of the authorization URL.
64 ///
65 /// If this is not set, it is assumed that the user wants to log into an
66 /// existing account.
67 ///
68 /// [`Prompt::Create`] can be used to signify that the user wants to
69 /// register a new account.
70 pub fn prompt(mut self, prompt: Vec<Prompt>) -> Self {
71 self.prompt = Some(prompt);
72 self
73 }
74
75 /// Set a generic login hint to help an identity provider pre-fill the login
76 /// form.
77 ///
78 /// Note: This is not the same as the [`Self::user_id_hint()`] method, which
79 /// is specifically designed to a) take a `UserId` and no other type of
80 /// hint and b) be used directly by MAS and not the identity provider.
81 ///
82 /// The most likely use case for this method is to pre-fill the login page
83 /// using a provisioning link provided by an external party such as
84 /// `https://app.example.com/?server_name=example.org&login_hint=alice`
85 /// In this instance it is up to the external party to make ensure that the
86 /// hint is known to work with their identity provider. For more information
87 /// see `login_hint` in <https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest>
88 ///
89 /// The following methods are mutually exclusive: [`Self::login_hint()`] and
90 /// [`Self::user_id_hint()`].
91 pub fn login_hint(mut self, login_hint: String) -> Self {
92 self.login_hint = Some(login_hint);
93 self
94 }
95
96 /// Set the hint to the Authorization Server about the Matrix user ID the
97 /// End-User might use to log in, as defined in [MSC4198].
98 ///
99 /// [MSC4198]: https://github.com/matrix-org/matrix-spec-proposals/pull/4198
100 ///
101 /// The following methods are mutually exclusive: [`Self::login_hint()`] and
102 /// [`Self::user_id_hint()`].
103 pub fn user_id_hint(mut self, user_id: &UserId) -> Self {
104 self.login_hint = Some(format!("mxid:{user_id}"));
105 self
106 }
107
108 /// Get the URL that should be presented to login via the Authorization Code
109 /// flow.
110 ///
111 /// This URL should be presented to the user and once they are redirected to
112 /// the `redirect_uri`, the login can be completed by calling
113 /// [`OAuth::finish_login()`].
114 ///
115 /// Returns an error if the client registration was not restored, or if a
116 /// request fails.
117 #[instrument(target = "matrix_sdk::client", skip_all)]
118 pub async fn build(self) -> Result<OAuthAuthorizationData, OAuthError> {
119 let Self { oauth, registration_data, scopes, device_id, redirect_uri, prompt, login_hint } =
120 self;
121
122 let server_metadata = oauth.server_metadata().await?;
123
124 oauth.use_registration_data(&server_metadata, registration_data.as_ref()).await?;
125
126 let data = oauth.data().expect("OAuth 2.0 data should be set after registration");
127 info!(
128 issuer = server_metadata.issuer.as_str(),
129 ?scopes,
130 "Authorizing scope via the OAuth 2.0 Authorization Code flow"
131 );
132
133 let auth_url = AuthUrl::from_url(server_metadata.authorization_endpoint.clone());
134
135 let (pkce_challenge, pkce_verifier) = PkceCodeChallenge::new_random_sha256();
136 let redirect_uri = RedirectUrl::from_url(redirect_uri);
137
138 let client = OAuthClient::new(data.client_id.clone()).set_auth_uri(auth_url);
139 let mut request = client
140 .authorize_url(CsrfToken::new_random)
141 .add_scopes(scopes)
142 .set_pkce_challenge(pkce_challenge)
143 .set_redirect_uri(Cow::Borrowed(&redirect_uri));
144
145 if let Some(prompt) = prompt {
146 // This should be a list of space separated values.
147 let prompt_str = prompt.iter().map(Prompt::as_str).collect::<Vec<_>>().join(" ");
148 request = request.add_extra_param("prompt", prompt_str);
149 }
150
151 if let Some(login_hint) = login_hint {
152 request = request.add_extra_param("login_hint", login_hint);
153 }
154
155 let (url, state) = request.url();
156
157 data.authorization_data.lock().await.insert(
158 state.clone(),
159 AuthorizationValidationData { server_metadata, device_id, redirect_uri, pkce_verifier },
160 );
161
162 Ok(OAuthAuthorizationData { url, state })
163 }
164}
165
166/// The data needed to perform authorization using OAuth 2.0.
167#[derive(Debug, Clone)]
168#[cfg_attr(feature = "uniffi", derive(uniffi::Object))]
169pub struct OAuthAuthorizationData {
170 /// The URL that should be presented.
171 pub url: Url,
172 /// A unique identifier for the request, used to ensure the response
173 /// originated from the authentication issuer.
174 pub state: CsrfToken,
175}
176
177#[cfg(feature = "uniffi")]
178#[matrix_sdk_ffi_macros::export]
179impl OAuthAuthorizationData {
180 /// The login URL to use for authorization.
181 pub fn login_url(&self) -> String {
182 self.url.to_string()
183 }
184}