Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Odevzdávací Systém MO
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Martin Mareš
Odevzdávací Systém MO
Commits
aa1914bf
Commit
aa1914bf
authored
4 months ago
by
Martin Mareš
Browse files
Options
Downloads
Patches
Plain Diff
DSN: Autorizace API
parent
85343b78
No related branches found
No related tags found
1 merge request
!138
Zpracování nedoručenek
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
bin/send-dsn
+22
-3
22 additions, 3 deletions
bin/send-dsn
etc/config.py.example
+4
-0
4 additions, 0 deletions
etc/config.py.example
mo/web/api_dsn.py
+13
-0
13 additions, 0 deletions
mo/web/api_dsn.py
with
39 additions
and
3 deletions
bin/send-dsn
+
22
−
3
View file @
aa1914bf
...
...
@@ -2,21 +2,40 @@
# Tento skript se volá při doručování pošty (například pomocí "execute" v Sieve)
# a předá mail webové části OSMO přes /api/email-dsn.
import
os
from
pathlib
import
Path
import
requests
from
requests.exceptions
import
RequestException
import
sys
if
len
(
sys
.
argv
)
!=
2
:
print
(
'
Arguments: <URL of OSMO root>
/
'
,
file
=
sys
.
stderr
)
print
(
'
Arguments: <URL of OSMO root>
'
,
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
osmo_url
=
sys
.
argv
[
1
]
mail
=
sys
.
stdin
.
buffer
.
read
()
key_path
=
Path
.
home
()
/
'
.config/osmo/dsn-api-key
'
try
:
reply
=
requests
.
post
(
f
'
{
osmo_url
}
api/email-dsn
'
,
data
=
mail
,
timeout
=
30
)
except
RequestException
:
with
key_path
.
open
()
as
f
:
key
=
f
.
readline
().
strip
()
if
key
==
""
:
print
(
f
'
Cannot read key from
{
key_path
}
'
,
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
except
OSError
as
e
:
print
(
f
'
Cannot read
{
key_path
}
:
{
e
}
'
,
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
try
:
reply
=
requests
.
post
(
os
.
path
.
join
(
osmo_url
,
'
api/email-dsn
'
),
data
=
mail
,
headers
=
{
'
Authorization
'
:
f
'
Bearer
{
key
}
'
},
timeout
=
30
)
except
RequestException
as
e
:
print
(
f
'
Error sending DSN:
{
e
}
'
)
sys
.
exit
(
1
)
if
reply
.
status_code
!=
200
:
print
(
f
'
Error sending DSN: HTTP status
{
reply
.
status_code
}
'
)
sys
.
exit
(
1
)
This diff is collapsed.
Click to expand it.
etc/config.py.example
+
4
−
0
View file @
aa1914bf
...
...
@@ -111,3 +111,7 @@ MAILING_LIST_EXCLUDE = {
# Maximální počet e-mailových adres, které jsme ochotni ve webovém rozhraní zobrazit
# uživatelům bez práva unrestricted_email.
EMAILS_SHOW_MAX = 500
# Klíč k API na zpracování mailových nedoručenek. Měl by být také uložen
# v ~/.config/osmo/dsn-api-key účtu, který volá bin/send-dsn. Nesmí obsahovat mezery.
# DSN_API_KEY = "..."
This diff is collapsed.
Click to expand it.
mo/web/api_dsn.py
+
13
−
0
View file @
aa1914bf
...
...
@@ -112,8 +112,21 @@ def process_dsn_reg(dsn: db.EmailDSN) -> None:
dsn
.
reg
.
dsn_id
=
dsn
.
dsn_id
def
authorize_email_dsn
()
->
bool
:
dsn_api_token
=
getattr
(
config
,
'
DSN_API_KEY
'
,
None
)
auth_header
=
request
.
headers
.
get
(
'
Authorization
'
)
if
dsn_api_token
is
None
or
auth_header
is
None
:
return
False
fields
=
auth_header
.
split
()
return
len
(
fields
)
==
2
and
fields
[
1
]
==
dsn_api_token
@app.route
(
'
/api/email-dsn
'
,
methods
=
(
'
POST
'
,))
def
api_email_dsn
()
->
Response
:
if
not
authorize_email_dsn
():
raise
werkzeug
.
exceptions
.
Forbidden
()
body
=
request
.
get_data
(
cache
=
False
)
try
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment