headless_lms_server/domain/oauth/
helpers.rs

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