Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
funkwhale
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
Zwordi
funkwhale
Commits
4522f599
Verified
Commit
4522f599
authored
7 years ago
by
Eliot Berriot
Browse files
Options
Downloads
Patches
Plain Diff
More test cases for request signing and added helpers to verify signature
parent
aa7365b7
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
api/funkwhale_api/federation/signing.py
+35
-0
35 additions, 0 deletions
api/funkwhale_api/federation/signing.py
api/tests/federation/test_signing.py
+92
-8
92 additions, 8 deletions
api/tests/federation/test_signing.py
with
127 additions
and
8 deletions
api/funkwhale_api/federation/signing.py
+
35
−
0
View file @
4522f599
import
requests
import
requests_http_signature
from
cryptography.hazmat.primitives
import
serialization
as
crypto_serialization
from
cryptography.hazmat.primitives.asymmetric
import
rsa
from
cryptography.hazmat.backends
import
default_backend
as
crypto_default_backend
...
...
@@ -19,3 +22,35 @@ def get_key_pair(size=2048):
)
return
private_key
,
public_key
def
verify
(
request
,
public_key
):
return
requests_http_signature
.
HTTPSignatureAuth
.
verify
(
request
,
key_resolver
=
lambda
**
kwargs
:
public_key
)
def
verify_django
(
django_request
,
public_key
):
"""
Given a django WSGI request, create an underlying requests.PreparedRequest
instance we can verify
"""
headers
=
django_request
.
META
.
get
(
'
headers
'
,
{}).
copy
()
for
h
,
v
in
list
(
headers
.
items
()):
# we include lower-cased version of the headers for compatibility
# with requests_http_signature
headers
[
h
.
lower
()]
=
v
try
:
signature
=
headers
[
'
authorization
'
]
except
KeyError
:
raise
exceptions
.
MissingSignature
request
=
requests
.
Request
(
method
=
django_request
.
method
,
url
=
'
http://noop
'
,
data
=
django_request
.
body
,
headers
=
headers
)
prepared_request
=
request
.
prepare
()
return
verify
(
request
,
public_key
)
This diff is collapsed.
Click to expand it.
api/tests/federation/test_signing.py
+
92
−
8
View file @
4522f599
...
...
@@ -16,10 +16,23 @@ def test_can_sign_and_verify_request(factories):
assert
'
date
'
in
prepared_request
.
headers
assert
'
authorization
'
in
prepared_request
.
headers
assert
prepared_request
.
headers
[
'
authorization
'
].
startswith
(
'
Signature
'
)
assert
requests_http_signature
.
HTTPSignatureAuth
.
verify
(
prepared_request
,
key_resolver
=
lambda
**
kwargs
:
public
)
is
None
assert
signing
.
verify
(
prepared_request
,
public
)
is
None
def
test_can_sign_and_verify_request_digest
(
factories
):
private
,
public
=
factories
[
'
federation.KeyPair
'
]()
auth
=
factories
[
'
federation.SignatureAuth
'
](
key
=
private
)
request
=
factories
[
'
federation.SignedRequest
'
](
auth
=
auth
,
method
=
'
post
'
,
data
=
b
'
hello=world
'
)
prepared_request
=
request
.
prepare
()
assert
'
date
'
in
prepared_request
.
headers
assert
'
digest
'
in
prepared_request
.
headers
assert
'
authorization
'
in
prepared_request
.
headers
assert
prepared_request
.
headers
[
'
authorization
'
].
startswith
(
'
Signature
'
)
assert
signing
.
verify
(
prepared_request
,
public
)
is
None
def
test_verify_fails_with_wrong_key
(
factories
):
...
...
@@ -28,7 +41,78 @@ def test_verify_fails_with_wrong_key(factories):
prepared_request
=
request
.
prepare
()
with
pytest
.
raises
(
cryptography
.
exceptions
.
InvalidSignature
):
requests_http_signature
.
HTTPSignatureAuth
.
verify
(
prepared_request
,
key_resolver
=
lambda
**
kwargs
:
wrong_public
)
signing
.
verify
(
prepared_request
,
wrong_public
)
def
test_can_verify_django_request
(
factories
,
api_request
):
private_key
,
public_key
=
signing
.
get_key_pair
()
signed_request
=
factories
[
'
federation.SignedRequest
'
](
auth__key
=
private_key
)
prepared
=
signed_request
.
prepare
()
django_request
=
api_request
.
get
(
'
/
'
,
headers
=
{
'
Date
'
:
prepared
.
headers
[
'
date
'
],
'
Authorization
'
:
prepared
.
headers
[
'
authorization
'
],
}
)
assert
signing
.
verify_django
(
django_request
,
public_key
)
is
None
def
test_can_verify_django_request_digest
(
factories
,
api_request
):
private_key
,
public_key
=
signing
.
get_key_pair
()
signed_request
=
factories
[
'
federation.SignedRequest
'
](
auth__key
=
private_key
,
method
=
'
post
'
,
data
=
b
'
hello=world
'
)
prepared
=
signed_request
.
prepare
()
django_request
=
api_request
.
post
(
'
/
'
,
headers
=
{
'
Date
'
:
prepared
.
headers
[
'
date
'
],
'
Digest
'
:
prepared
.
headers
[
'
digest
'
],
'
Authorization
'
:
prepared
.
headers
[
'
authorization
'
],
}
)
assert
signing
.
verify_django
(
django_request
,
public_key
)
is
None
def
test_can_verify_django_request_digest_failure
(
factories
,
api_request
):
private_key
,
public_key
=
signing
.
get_key_pair
()
signed_request
=
factories
[
'
federation.SignedRequest
'
](
auth__key
=
private_key
,
method
=
'
post
'
,
data
=
b
'
hello=world
'
)
prepared
=
signed_request
.
prepare
()
django_request
=
api_request
.
post
(
'
/
'
,
headers
=
{
'
Date
'
:
prepared
.
headers
[
'
date
'
],
'
Digest
'
:
prepared
.
headers
[
'
digest
'
]
+
'
noop
'
,
'
Authorization
'
:
prepared
.
headers
[
'
authorization
'
],
}
)
with
pytest
.
raises
(
cryptography
.
exceptions
.
InvalidSignature
):
signing
.
verify_django
(
django_request
,
public_key
)
def
test_can_verify_django_request_failure
(
factories
,
api_request
):
private_key
,
public_key
=
signing
.
get_key_pair
()
signed_request
=
factories
[
'
federation.SignedRequest
'
](
auth__key
=
private_key
)
prepared
=
signed_request
.
prepare
()
django_request
=
api_request
.
get
(
'
/
'
,
headers
=
{
'
Date
'
:
'
Wrong
'
,
'
Authorization
'
:
prepared
.
headers
[
'
authorization
'
],
}
)
with
pytest
.
raises
(
cryptography
.
exceptions
.
InvalidSignature
):
signing
.
verify_django
(
django_request
,
public_key
)
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