aboutsummaryrefslogtreecommitdiff
path: root/auth/idolcomplex.zsh
blob: 31125142f0aefcf6427c709e449db0c4ebece79a (plain)
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
# Copyright (C) 2024-2025 Maria Lisina
# Copyright (C) 2024-2025 Danil Lisin
# SPDX-License-Identifier: Apache-2.0

ib_cookies="${cookies_file}"
ib_auth_file="${cache}/${update_id}_login.html"
dump+=(${ib_auth_file##*/})

if ! curl --cookie-jar "${ib_cookies}" \
    --max-time ${external_timeout} \
    --output "${ib_auth_file}" \
    --proxy "${external_proxy}" \
    --request GET \
    --silent \
    --user-agent "${useragent}" \
    "${ib_auth}/users/login"
then
    output_text="Failed to access ${ib_name} API"
    log_text="ib_auth (${update_id}): ${output_text}"

    . "${units}/log.zsh"
    . "${units}/dump.zsh"

    return 0
fi

ib_action="$(xq -a "action" -q "form" "${ib_auth_file}")"
ib_token="$(xq -a "value" -q "form>input[name=authenticity_token]" "${ib_auth_file}")"

if [[ -z "${ib_action}" || -z "${ib_token}" ]]
then
    output_text="Failed to get authorization data"
    log_text="ib_auth (${update_id}): ${output_text}"

    . "${units}/log.zsh"
    . "${units}/dump.zsh"

    return 0
fi

ib_auth_file="${cache}/${update_id}_authenticate.html"
dump+=(${ib_auth_file##*/})

if ! curl --cookie "${ib_cookies}" \
    --cookie-jar "${ib_cookies}" \
    --data-urlencode "authenticity_token=${ib_token}" \
    --data-urlencode "user[name]=${ib_login}" \
    --data-urlencode "user[password]=${ib_key}" \
    --data-urlencode "commit=Login" \
    --header "Referer: ${ib_auth}/users/login" \
    --max-time ${external_timeout} \
    --output "${ib_auth_file}" \
    --proxy "${external_proxy}" \
    --request POST \
    --silent \
    --user-agent "${useragent}" \
    "${ib_auth}${ib_action}"
then
    output_text="Failed to access ${ib_name} API"
    log_text="ib_auth (${update_id}): ${output_text}"

    . "${units}/log.zsh"
    . "${units}/dump.zsh"

    return 0
fi

if [[ -s "${ib_auth_file}" ]]
then
    output_text="An unknown error occurred"
    log_text="ib_auth (${update_id}): ${output_text}"

    . "${units}/log.zsh"
    . "${units}/dump.zsh"

    return 0
fi

ib_auth_file="${cache}/${update_id}_home.html"
dump+=(${ib_auth_file##*/})

if ! curl --cookie "${ib_cookies}" \
    --max-time ${external_timeout} \
    --output "${ib_auth_file}" \
    --proxy "${external_proxy}" \
    --request GET \
    --silent \
    --user-agent "${useragent}" \
    "${ib_auth}/users/home"
then
    output_text="Failed to access ${ib_name} API"
    log_text="ib_auth (${update_id}): ${output_text}"

    . "${units}/log.zsh"
    . "${units}/dump.zsh"

    return 0
fi

ib_notice="$(xq -q "div[id=notice]" "${ib_auth_file}")"

if [[ -n "${ib_notice}" && "${ib_notice}" != "You are now logged in" ]]
then
    output_text="$(printf "%s" "${ib_notice}" | htmlescape)"
    log_text="ib_auth (${update_id}): ${ib_notice}"

    . "${units}/log.zsh"
    . "${units}/dump.zsh"

    return 0
fi

if [[ -z "${ib_notice}" ]]
then
    output_text="Failed to verify user authorization"
    log_text="ib_auth (${update_id}): ${output_text}"

    . "${units}/log.zsh"
    . "${units}/dump.zsh"

    return 0
fi

strftime %s > "${timestamp_file}"