forgefed_constants.py 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import os
  2. from requests_http_signature import HTTPSignatureHeaderAuth
  3. PROTOCOL = os.environ['FORGEFED_PROTOCOL' ] if 'FORGEFED_PROTOCOL' in os.environ else 'https'
  4. HOSTNAME = os.environ['FORGEFED_HOSTNAME' ] if 'FORGEFED_HOSTNAME' in os.environ else 'example.net'
  5. TCP_PORT = os.environ['FORGEFED_TCP_PORT' ] if 'FORGEFED_TCP_PORT' in os.environ else 80
  6. PATH_PREFIX = os.environ['FORGEFED_PATH_PREFIX'] if 'FORGEFED_PATH_PREFIX' in os.environ else ''
  7. LOCAL_CONFIG = \
  8. { \
  9. "$SCHEME" : PROTOCOL , \
  10. "$HOST" : HOSTNAME , \
  11. "$PORT" : TCP_PORT , \
  12. "$PATH_PREFIX" : PATH_PREFIX , \
  13. "Person.id" : "$DOMAIN/$id" , \
  14. "Person.likes" : "$id/likes" , \
  15. "Person.following" : "$id/following" , \
  16. "Person.followers" : "$id/followers" , \
  17. "Person.liked" : "$id/liked" , \
  18. "Person.inbox" : "$id/inbox" , \
  19. "Person.outbox" : "$id/outbox" , \
  20. "Person.url" : "$id" , \
  21. #"Note.id" : "$temp_uuid" , \
  22. "Note.id" : "$DOMAIN/$temp_uuid" , \
  23. 'Note.attributedTo' : "$from_id" , \
  24. 'Note.content' : "<p>$source.content</p>" , \
  25. 'Note.published' : "$NOW" , \
  26. 'Note.url' : "$from_id/note/$temp_uuid"
  27. }
  28. URL_REGEX = r'^' + PROTOCOL + '://' + HOSTNAME + '/' + PATH_PREFIX
  29. PATH_REGEX = r'^/' + PATH_PREFIX
  30. LOCAL_ID_REGEX = r'[^0-9a-zA-Z_-]+'
  31. FOREIGN_ID_REGEX = r'[0-9a-zA-Z_-]+@[0-9a-zA-Z-][0-9a-zA-Z.-]*[0-9a-zA-Z-]\.[a-z]+'
  32. AP_NS_URLS = [ 'https://www.w3.org/ns/activitystreams' ,
  33. 'https://w3id.org/security/v1' ]
  34. VERVIS_ACTOR_ID = 'bob@forge.angeley.es'
  35. VERVIS_ACTOR_URL = 'https://forge.angeley.es/'
  36. VERVIS_INBOX_URL = 'https://forge.angeley.es/inbox'
  37. TEST_REMOTE_ACTOR_ID = VERVIS_ACTOR_ID
  38. TEST_REMOTE_ACTOR_URL = VERVIS_ACTOR_URL
  39. TEST_REMOTE_INBOX_URL = VERVIS_INBOX_URL
  40. HTTP_SIG_PUB_KEY_FILE = 'public.pem'
  41. HTTP_SIG_PVT_KEY_FILE = 'private.pem'
  42. with open(HTTP_SIG_PUB_KEY_FILE , 'rb') as public_key_file:
  43. PUBLIC_KEY = public_key_file .read()
  44. with open(HTTP_SIG_PVT_KEY_FILE , 'rb') as private_key_file:
  45. PRIVATE_KEY = private_key_file.read()
  46. AP_DUMMY_ACTOR = PROTOCOL + '://' + HOSTNAME + PATH_PREFIX + '/alice@' + HOSTNAME # vervis extension
  47. AP_POST_HEADERS = { 'Content-Type' : 'application/activity+json' ,
  48. 'Accept' : 'application/json' ,
  49. 'ActivityPub-Actor' : AP_DUMMY_ACTOR } # vervis extension
  50. AP_SIGN_HEADERS = [ '(request-target)' , 'host' , 'date' , 'Digest' , 'ActivityPub-Actor' ] # vervis extension (ActivityPub-Actor)
  51. AP_SIGN_ALGORITHM = 'rsa-sha256'
  52. HTTP_SIG_KEY_ID = AP_DUMMY_ACTOR + '#rsa-pub-key'
  53. KEYFILE_PUB_HEADER = b'-----BEGIN PUBLIC KEY-----\n'
  54. KEYFILE_PUB_FOOTER = b'-----END PUBLIC KEY-----\n'
  55. KEYFILE_PVT_HEADER = b'-----BEGIN RSA PRIVATE KEY-----\n'
  56. KEYFILE_PVT_FOOTER = b'-----END RSA PRIVATE KEY-----\n'
  57. HTTP_SIG_AUTH = HTTPSignatureHeaderAuth(headers=AP_SIGN_HEADERS , algorithm=AP_SIGN_ALGORITHM , \
  58. key_id=HTTP_SIG_KEY_ID , key=PRIVATE_KEY )
  59. STATUS_OK = '200 OK'
  60. STATUS_NOT_FOUND = '404 NOT FOUND'
  61. RESP_NOT_FOUND = [ STATUS_NOT_FOUND , '{ "message" : "resource not found" }' ]
  62. RESP_INVALID_JSON = [ STATUS_OK , '{ "message" : "invalid JSON struct" }' ]
  63. RESP_INVALID_ACTIVITY = [ STATUS_OK , '{ "message" : "invalid activity-pub activity" }' ]
  64. # DEBUG BEGIN
  65. from pprint import pprint ; print("HTTP_SIG_AUTH=") ; pprint(vars(HTTP_SIG_AUTH))
  66. DBG_COLOR_INCOMING = 'red'
  67. DBG_COLOR_OUTGOING = 'green'
  68. # DEBUG END
  69. ## validations/sanity/env check ##
  70. if PUBLIC_KEY[0:27] != KEYFILE_PUB_HEADER or \
  71. PUBLIC_KEY[-25:] != KEYFILE_PUB_FOOTER or \
  72. len(PUBLIC_KEY) != 451 :
  73. raise ValueError("invalid keyfile: '" + HTTP_SIG_PUB_KEY_FILE + "'")
  74. if PRIVATE_KEY[0:32] != KEYFILE_PVT_HEADER or \
  75. PRIVATE_KEY[-30:] != KEYFILE_PVT_FOOTER or \
  76. len(PRIVATE_KEY) != 1675 :
  77. raise ValueError("invalid keyfile: '" + HTTP_SIG_PVT_KEY_FILE + "'")