Introduce authorizations with Rajska

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2023-03-17 18:10:59 +01:00
parent b6875f6a4b
commit 8984bd7636
95 changed files with 4560 additions and 1505 deletions

View File

@@ -18,7 +18,6 @@ defmodule Mobilizon.Web.ConnCase do
alias Ecto.Adapters.SQL.Sandbox, as: Adapter
alias Mobilizon.Storage.Repo
alias Mobilizon.Users.User
alias Mobilizon.Web.Auth
@@ -33,13 +32,20 @@ defmodule Mobilizon.Web.ConnCase do
# The default endpoint for testing
@endpoint Mobilizon.Web.Endpoint
def auth_conn(%Plug.Conn{} = conn, %User{} = user) do
def auth_conn(%Plug.Conn{} = conn, user) do
{:ok, token, _claims} = Auth.Guardian.encode_and_sign(user)
conn
|> Plug.Conn.put_req_header("authorization", "Bearer #{token}")
|> Plug.Conn.put_req_header("accept", "application/json")
end
@spec set_token(Plug.Conn.t(), String.t()) :: Plug.Conn.t()
def set_token(%Plug.Conn{} = conn, token) do
conn
|> Plug.Conn.put_req_header("authorization", "Bearer #{token}")
|> Plug.Conn.put_req_header("accept", "application/json")
end
end
end

View File

@@ -503,4 +503,37 @@ defmodule Mobilizon.Factory do
type: :string
}
end
def auth_application_factory do
%Mobilizon.Applications.Application{
name: sequence("My app"),
client_id: :crypto.strong_rand_bytes(42) |> Base.encode64() |> binary_part(0, 42),
client_secret: :crypto.strong_rand_bytes(42) |> Base.encode64() |> binary_part(0, 42),
redirect_uris: [sequence("https://someredir.uri")],
scope: "read write",
website: "https://somewebsite.com"
}
end
def auth_application_token_factory do
%Mobilizon.Applications.ApplicationToken{
authorization_code: sequence("some code"),
status: "pending",
scope: "read write",
user: build(:user),
application: build(:auth_application)
}
end
def auth_application_device_activation_factory do
%Mobilizon.Applications.ApplicationDeviceActivation{
user_code: :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8),
device_code: :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8),
status: "pending",
scope: "read write",
expires_in: 600,
user: build(:user),
application: build(:auth_application)
}
end
end

View File

@@ -16,7 +16,8 @@ defmodule Mobilizon.ApplicationsFixtures do
name: "some name",
client_id: "hello",
client_secret: "secret",
redirect_uris: "somewhere\nelse"
redirect_uris: ["somewhere", "else"],
scope: "read"
})
|> Mobilizon.Applications.create_application()
@@ -34,7 +35,9 @@ defmodule Mobilizon.ApplicationsFixtures do
|> Enum.into(%{
application_id: application_fixture().id,
user_id: user.id,
authorization_code: "some code"
authorization_code: "some code",
scope: "read",
status: :pending
})
|> Mobilizon.Applications.create_application_token()
@@ -47,7 +50,13 @@ defmodule Mobilizon.ApplicationsFixtures do
def application_device_activation_fixture(attrs \\ %{}) do
{:ok, application_device_activation} =
attrs
|> Enum.into(%{})
|> Enum.into(%{
user_code: "hello",
device_code: "computers",
expires_in: 600,
application_id: application_fixture().id,
scope: "read"
})
|> Mobilizon.Applications.create_application_device_activation()
application_device_activation