Merge branch 'hotfix_broken_likes' into 'develop'
[akkoma] / lib / pleroma / uploaders / swift / keystone.ex
1 defmodule Pleroma.Uploaders.Swift.Keystone do
2 use HTTPoison.Base
3
4 @settings Application.get_env(:pleroma, Pleroma.Uploaders.Swift)
5
6 def process_url(url) do
7 Enum.join(
8 [Keyword.fetch!(@settings, :auth_url), url],
9 "/"
10 )
11 end
12
13 def process_response_body(body) do
14 body
15 |> Poison.decode!()
16 end
17
18 def get_token() do
19 username = Keyword.fetch!(@settings, :username)
20 password = Keyword.fetch!(@settings, :password)
21 tenant_id = Keyword.fetch!(@settings, :tenant_id)
22
23 case post(
24 "/tokens",
25 make_auth_body(username, password, tenant_id),
26 ["Content-Type": "application/json"],
27 hackney: [:insecure]
28 ) do
29 {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
30 body["access"]["token"]["id"]
31
32 {:ok, %HTTPoison.Response{status_code: _}} ->
33 ""
34 end
35 end
36
37 def make_auth_body(username, password, tenant) do
38 Poison.encode!(%{
39 :auth => %{
40 :passwordCredentials => %{
41 :username => username,
42 :password => password
43 },
44 :tenantId => tenant
45 }
46 })
47 end
48 end