aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/uploaders/swift/keystone.ex
blob: a7921431943979195a1a1619094fc3853479394d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
defmodule Pleroma.Uploaders.Swift.Keystone do
  use HTTPoison.Base

  @settings Application.get_env(:pleroma, Pleroma.Uploaders.Swift)

  def process_url(url) do
    Enum.join(
      [Keyword.fetch!(@settings, :auth_url), url],
      "/"
    )
  end

  def process_response_body(body) do
    body
    |> Poison.decode!()
  end

  def get_token() do
    username = Keyword.fetch!(@settings, :username)
    password = Keyword.fetch!(@settings, :password)
    tenant_id = Keyword.fetch!(@settings, :tenant_id)

    case post(
           "/tokens",
           make_auth_body(username, password, tenant_id),
           ["Content-Type": "application/json"],
           hackney: [:insecure]
         ) do
      {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
        body["access"]["token"]["id"]

      {:ok, %HTTPoison.Response{status_code: _}} ->
        ""
    end
  end

  def make_auth_body(username, password, tenant) do
    Poison.encode!(%{
      :auth => %{
        :passwordCredentials => %{
          :username => username,
          :password => password
        },
        :tenantId => tenant
      }
    })
  end
end