<commit_before>-module(mt_sso).

-export([
  main/0
]).

-include_lib("nitrogen_core/include/wf.hrl").
-include_lib("webmachine/include/webmachine.hrl").

-include_lib("metalkia_core/include/mt_records.hrl").
-include_lib("metalkia_core/include/mt_log.hrl").
-include_lib("metalkia_core/include/mt_util.hrl").


main() ->
  ?DBG("User: ~p", [wf:user()]),
  Action = wf:q(action),
  ?DBG("Action: ~p", [Action]),
  case Action of
    "auth" ->
      ReturnUrl = wf:q(return_url),
      AuthBaseUri = wf:q(auth_domain),
      case check_domain(AuthBaseUri) of
        true ->
          case wf:user() of
            undefined ->
              %% Need signin
              "";
            _UserName ->
              VerificationCode = mtc_util:rand_str(10),
              mtws_session:put_state(VerificationCode, mtws_common:get_state()),
              "<script>"
              "window.top.location = '" ++ mtc_util:uri_encode(AuthBaseUri) ++ "/sso" ++
              "?action=verify" ++
              "&code=" ++ mtc_util:uri_encode(VerificationCode) ++
              "&return_url=" ++ mtc_util:uri_encode(ReturnUrl) ++
              "'</script>"
          end;
        _ -> ""
      end;
    "verify" ->
      Code = wf:q(code),
      ReturnUrl = wf:q(return_url),
      ?DBG("Code: ~p", [Code]),
      ?DBG("ReturnUrl: ~p", [ReturnUrl]),
      mtws_common:set_state(mtws_session:get_state(Code)),
      wf:redirect(ReturnUrl)
  end.

check_domain(_Domain) -><commit_msg>Do not quote redirect url<commit_after>-module(mt_sso).

-export([
  main/0
]).

-include_lib("nitrogen_core/include/wf.hrl").
-include_lib("webmachine/include/webmachine.hrl").

-include_lib("metalkia_core/include/mt_records.hrl").
-include_lib("metalkia_core/include/mt_log.hrl").
-include_lib("metalkia_core/include/mt_util.hrl").


main() ->
  ?DBG("User: ~p", [wf:user()]),
  Action = wf:q(action),
  ?DBG("Action: ~p", [Action]),
  case Action of
    "auth" ->
      ReturnUrl = wf:q(return_url),
      AuthBaseUri = wf:q(auth_domain),
      case check_domain(AuthBaseUri) of
        true ->
          case wf:user() of
            undefined ->
              %% Need signin
              "";
            _UserName ->
              VerificationCode = mtc_util:rand_str(10),
              mtws_session:put_state(VerificationCode, mtws_common:get_state()),
              "<script>"
              "window.top.location = '" ++ AuthBaseUri ++ "/sso" ++
              "?action=verify" ++
              "&code=" ++ mtc_util:uri_encode(VerificationCode) ++
              "&return_url=" ++ mtc_util:uri_encode(ReturnUrl) ++
              "'</script>"
          end;
        _ -> ""
      end;
    "verify" ->
      Code = wf:q(code),
      ReturnUrl = wf:q(return_url),
      ?DBG("Code: ~p", [Code]),
      ?DBG("ReturnUrl: ~p", [ReturnUrl]),
      mtws_common:set_state(mtws_session:get_state(Code)),
      wf:redirect(ReturnUrl)
  end.

check_domain(_Domain) ->
{'fraction_non_alphanumeric': 0.1689996585865483, 'fraction_numerical': 0.0020484807101399795, 'mean_word_length': 2.4349355216881596, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '177225', 'n_tokens_mistral': 1021, 'n_tokens_neox': 1014, 'n_words': 138}