-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working implementation of surf research drive storage
Formatted code added the possibility to encrypt donations Formatted code made field optional
- Loading branch information
Showing
19 changed files
with
275 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
defmodule Systems.Storage.SurfResearchDrive.Backend do | ||
@behaviour Systems.Storage.Backend | ||
|
||
alias Systems.Storage.SurfResearchDrive.Encryption | ||
|
||
require Logger | ||
|
||
def store( | ||
%{ | ||
"user" => username, | ||
"password" => password, | ||
"url" => url, | ||
"folder" => folder, | ||
"passphrase" => passphrase | ||
} = _endpoint, | ||
data, | ||
meta_data | ||
) do | ||
filename = filename(meta_data) | ||
file_url = url([url, folder, filename]) | ||
|
||
credentials = Base.encode64("#{username}:#{password}") | ||
|
||
headers = [ | ||
{"Authorization", "Basic #{credentials}"} | ||
] | ||
|
||
data = | ||
if passphrase != nil do | ||
Encryption.encrypt(data, passphrase) | ||
else | ||
data | ||
end | ||
|
||
case HTTPoison.put(file_url, data, headers) do | ||
{:ok, %{status_code: 201}} -> | ||
:ok | ||
|
||
{_, %{status_code: status_code, body: body}} -> | ||
{:error, "status_code=#{status_code},message=#{body}"} | ||
|
||
{:error, error} -> | ||
IO.inspect(error) | ||
{:error, error} | ||
end | ||
end | ||
|
||
def list_files(_endpoint) do | ||
Logger.error("Not yet implemented: list_files/1") | ||
{:error, :not_implemented} | ||
end | ||
|
||
def delete_files(_endpoint) do | ||
Logger.error("Not yet implemented: delete_files/1") | ||
{:error, :not_implemented} | ||
end | ||
|
||
def connected?(_), do: false | ||
|
||
defp filename(%{"identifier" => identifier}) do | ||
identifier | ||
|> Enum.map_join("_", fn [key, value] -> "#{key}-#{value}" end) | ||
|> then(&"#{&1}.json") | ||
end | ||
|
||
defp url(components) do | ||
URI.encode(Enum.join(components, "/")) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
defmodule Systems.Storage.SurfResearchDrive.Encryption do | ||
@moduledoc """ | ||
Documentation for `Systems.Storage.SurfResearchDrive.Encryption`. | ||
""" | ||
|
||
@doc """ | ||
Encrypt file with AES-256-CBC | ||
The hashed passphrase is the encryption key | ||
The hash function is there to guarantee the key consists of 256 bits | ||
The hasing function does not offer protection. | ||
If you know the hash or the passphrase you can decrypt the data | ||
Decryption can be done with: | ||
openssl enc -d -aes-256-cbc -in <(tail -c +17 file.enc) \ | ||
-out file.txt \ | ||
-K <sha256 of passphrase> \ | ||
-iv <base16 encode of first 16 bytes of file.enc> | ||
""" | ||
def encrypt(content, passphrase) do | ||
# generate key and iv | ||
key = :crypto.hash(:sha256, passphrase) | ||
iv = :crypto.strong_rand_bytes(16) | ||
|
||
padded_content = pad(content, 16) | ||
|
||
# Encrypt content | ||
cipher_text = :crypto.crypto_one_time(:aes_256_cbc, key, iv, padded_content, true) | ||
|
||
# Prepend IV and write the result | ||
iv <> cipher_text | ||
end | ||
|
||
@doc """ | ||
Applies PKCS7 padding to the given binary. | ||
""" | ||
def pad(data, block_size) do | ||
padding_size = block_size - rem(byte_size(data), block_size) | ||
data <> :binary.copy(<<padding_size>>, padding_size) | ||
end | ||
end |
Oops, something went wrong.