headless_lms_server/domain/oauth/
helpers.rs

1use crate::domain::error::{OAuthErrorCode, OAuthErrorData};
2use crate::prelude::*;
3
4pub fn oauth_invalid_request(
5    desc: &'static str,
6    redirect: Option<&str>,
7    state: Option<&str>,
8) -> ControllerError {
9    ControllerError::new(
10        ControllerErrorType::OAuthError(Box::new(OAuthErrorData {
11            error: OAuthErrorCode::InvalidRequest.as_str().into(),
12            error_description: desc.into(),
13            redirect_uri: redirect.map(str::to_string),
14            state: state.map(str::to_string),
15            nonce: None,
16        })),
17        desc,
18        None::<anyhow::Error>,
19    )
20}
21
22pub fn oauth_invalid_client(desc: &'static str) -> ControllerError {
23    ControllerError::new(
24        ControllerErrorType::OAuthError(Box::new(OAuthErrorData {
25            error: OAuthErrorCode::InvalidClient.as_str().into(),
26            error_description: desc.into(),
27            redirect_uri: None,
28            state: None,
29            nonce: None,
30        })),
31        desc,
32        None::<anyhow::Error>,
33    )
34}
35
36pub fn oauth_invalid_grant(desc: &'static str) -> ControllerError {
37    ControllerError::new(
38        ControllerErrorType::OAuthError(Box::new(OAuthErrorData {
39            error: OAuthErrorCode::InvalidGrant.as_str().into(),
40            error_description: desc.into(),
41            redirect_uri: None,
42            state: None,
43            nonce: None,
44        })),
45        desc,
46        None::<anyhow::Error>,
47    )
48}
49
50pub fn scope_has_openid(scope: &[String]) -> bool {
51    scope.iter().any(|s| s == "openid")
52}
53
54pub fn ok_json_no_cache<T: Serialize>(value: T) -> HttpResponse {
55    let mut resp = HttpResponse::Ok();
56    resp.insert_header(("Cache-Control", "no-store"));
57    resp.insert_header(("Pragma", "no-cache"));
58    resp.json(value)
59}