2018-12-23 20:04:54 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2018-12-31 15:41:47 +00:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 20:04:54 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-08-29 01:32:24 +00:00
|
|
|
defmodule Pleroma.Uploaders.Swift.Keystone do
|
|
|
|
use HTTPoison.Base
|
|
|
|
|
|
|
|
def process_url(url) do
|
|
|
|
Enum.join(
|
2018-11-06 18:34:57 +00:00
|
|
|
[Pleroma.Config.get!([Pleroma.Uploaders.Swift, :auth_url]), url],
|
2018-08-29 01:32:24 +00:00
|
|
|
"/"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_response_body(body) do
|
|
|
|
body
|
|
|
|
|> Poison.decode!()
|
|
|
|
end
|
|
|
|
|
2019-03-05 03:18:43 +00:00
|
|
|
def get_token do
|
2018-11-06 18:34:57 +00:00
|
|
|
settings = Pleroma.Config.get(Pleroma.Uploaders.Swift)
|
|
|
|
username = Keyword.fetch!(settings, :username)
|
|
|
|
password = Keyword.fetch!(settings, :password)
|
|
|
|
tenant_id = Keyword.fetch!(settings, :tenant_id)
|
2018-08-29 01:32:24 +00:00
|
|
|
|
|
|
|
case post(
|
|
|
|
"/tokens",
|
|
|
|
make_auth_body(username, password, tenant_id),
|
|
|
|
["Content-Type": "application/json"],
|
|
|
|
hackney: [:insecure]
|
|
|
|
) do
|
2018-12-02 14:08:36 +00:00
|
|
|
{:ok, %Tesla.Env{status: 200, body: body}} ->
|
2018-08-29 01:32:24 +00:00
|
|
|
body["access"]["token"]["id"]
|
|
|
|
|
2018-12-02 14:08:36 +00:00
|
|
|
{:ok, %Tesla.Env{status: _}} ->
|
2018-08-29 01:32:24 +00:00
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_auth_body(username, password, tenant) do
|
|
|
|
Poison.encode!(%{
|
|
|
|
:auth => %{
|
|
|
|
:passwordCredentials => %{
|
|
|
|
:username => username,
|
|
|
|
:password => password
|
|
|
|
},
|
|
|
|
:tenantId => tenant
|
|
|
|
}
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|