logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

keystone.ex (1273B)


      1 # Pleroma: A lightweight social networking server
      2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
      3 # SPDX-License-Identifier: AGPL-3.0-only
      4 
      5 defmodule Pleroma.Uploaders.Swift.Keystone do
      6   use HTTPoison.Base
      7 
      8   def process_url(url) do
      9     Enum.join(
     10       [Pleroma.Config.get!([Pleroma.Uploaders.Swift, :auth_url]), url],
     11       "/"
     12     )
     13   end
     14 
     15   def process_response_body(body) do
     16     body
     17     |> Jason.decode!()
     18   end
     19 
     20   def get_token do
     21     settings = Pleroma.Config.get(Pleroma.Uploaders.Swift)
     22     username = Keyword.fetch!(settings, :username)
     23     password = Keyword.fetch!(settings, :password)
     24     tenant_id = Keyword.fetch!(settings, :tenant_id)
     25 
     26     case post(
     27            "/tokens",
     28            make_auth_body(username, password, tenant_id),
     29            ["Content-Type": "application/json"],
     30            hackney: [:insecure]
     31          ) do
     32       {:ok, %Tesla.Env{status: 200, body: body}} ->
     33         body["access"]["token"]["id"]
     34 
     35       {:ok, %Tesla.Env{status: _}} ->
     36         ""
     37     end
     38   end
     39 
     40   def make_auth_body(username, password, tenant) do
     41     Jason.encode!(%{
     42       :auth => %{
     43         :passwordCredentials => %{
     44           :username => username,
     45           :password => password
     46         },
     47         :tenantId => tenant
     48       }
     49     })
     50   end
     51 end