Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tachyon oauth #335

Merged
merged 27 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
96c9ec2
Basic setup for applications
geekingfrog Jun 8, 2024
28a5438
Authorization code logic and table
geekingfrog Jun 9, 2024
28d0350
Basic logic for refresh tokens
geekingfrog Jun 10, 2024
f975dc9
Refactor: composable joins
geekingfrog Jun 12, 2024
a8a7714
Exchange code for token logic
geekingfrog Jun 12, 2024
554b140
Implement PKCE
geekingfrog Jun 13, 2024
2654f67
Nicer redirect uri on login
geekingfrog Jun 14, 2024
7241336
Validate redirection uris
geekingfrog Jun 15, 2024
141eafe
Http interface for authorization code
geekingfrog Jun 15, 2024
793a5e3
Http interface to get token
geekingfrog Jun 15, 2024
8a70a37
Refactor: utilities to generate authorization code
geekingfrog Jun 15, 2024
356b629
Add refresh token to the token endpoint
geekingfrog Jun 15, 2024
2005c11
Skeleton autohost table
geekingfrog Jun 16, 2024
161eea2
Add basic operations for client credentials
geekingfrog Jun 16, 2024
7c5b4ed
Add constraint for token owner
geekingfrog Jun 16, 2024
0251867
Can get a token from client credentials
geekingfrog Jun 16, 2024
4989eae
Http interface to get token from client credentials
geekingfrog Jun 16, 2024
4bd1e6b
Add oauth metadata endpoint
geekingfrog Jun 16, 2024
aa1ff94
Add support for basic auth in OAuth endpoint
geekingfrog Jun 27, 2024
5a9385f
Fix dialyzer errors
geekingfrog Jun 27, 2024
5ddcc77
Add unique constraint on app uid
geekingfrog Jul 28, 2024
c7293b1
Improve and surface error messages
geekingfrog Aug 5, 2024
802ab08
Only include state in redirection when provided
geekingfrog Aug 5, 2024
a13c76f
Do not create refresh token when using client credentials
geekingfrog Aug 5, 2024
c50142d
Allow basic auth to pass client_id for code flow
geekingfrog Aug 5, 2024
a97dc99
Forbid basic auth and query params at the same time
geekingfrog Aug 5, 2024
a479a14
Basic check for scopes when refreshing
geekingfrog Aug 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ if Teiserver.ConfigHelpers.get_env("PHX_SERVER", nil) do
config :teiserver, TeiserverWeb.Endpoint, server: true
end

# used for mailing, checking origins, finding tls certs…
domain_name = Teiserver.ConfigHelpers.get_env("TEI_DOMAIN_NAME", "beyondallreason.info")
L-e-x-o-n marked this conversation as resolved.
Show resolved Hide resolved

# Only do some runtime configuration in production since in dev and tests the
# files are automatically recompiled on the fly and thus, config/{dev,test}.exs
# are just fine
if config_env() == :prod do
# used for mailing, checking origins, finding tls certs…
domain_name = Teiserver.ConfigHelpers.get_env("TEI_DOMAIN_NAME", "beyondallreason.info")

certificates = [
keyfile: Teiserver.ConfigHelpers.get_env("TEI_TLS_PRIVATE_KEY_PATH"),
certfile: Teiserver.ConfigHelpers.get_env("TEI_TLS_CERT_PATH"),
Expand Down Expand Up @@ -190,3 +190,5 @@ if config_env() == :prod do
bot_name: Teiserver.ConfigHelpers.get_env("TEI_DISCORD_BOT_NAME")
end
end

config :teiserver, Teiserver.OAuth, issuer: "https://#{domain_name}"
2 changes: 2 additions & 0 deletions lib/teiserver/account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ defmodule Teiserver.Account do

alias Teiserver.Account.UserLib

@type t :: UserLib.t()

@spec icon :: String.t()
def icon, do: "fa-solid fa-user-alt"

Expand Down
7 changes: 6 additions & 1 deletion lib/teiserver/account/error_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ defmodule Teiserver.Account.ErrorHandler do
@impl Guardian.Plug.ErrorHandler

def auth_error(conn, {:unauthenticated, _reason}, _opts) do
redirect_to = "#{conn.request_path}?#{conn.query_string}"
redirect_to =
if conn.query_string != nil && conn.query_string != "" do
"#{conn.request_path}?#{conn.query_string}"
else
"#{conn.request_path}"
end

conn
|> put_resp_cookie("_redirect_to", redirect_to, sign: true, max_age: 60 * 5)
Expand Down
3 changes: 3 additions & 0 deletions lib/teiserver/account/schemas/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ defmodule Teiserver.Account.User do

alias Argon2

# TODO: this is where a user should be defined. This is only a placeholder for now
@type t :: term()

schema "account_users" do
field :name, :string
field :email, :string
Expand Down
12 changes: 12 additions & 0 deletions lib/teiserver/autohost.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Teiserver.Autohost do
alias Teiserver.Autohost.Autohost
alias Teiserver.Repo

def create_autohost(attrs \\ %{}) do
%Autohost{}
|> Autohost.changeset(attrs)
|> Repo.insert()
end

defdelegate get_autohost(id), to: Teiserver.AutohostQueries
end
20 changes: 20 additions & 0 deletions lib/teiserver/autohost/queries/autohost_query.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Teiserver.AutohostQueries do
use TeiserverWeb, :queries
alias Teiserver.Autohost.Autohost

@spec get_autohost(Autohost.id()) :: Autohost.t() | nil
def get_autohost(nil), do: nil

def get_autohost(id) do
base_query() |> where_id(id) |> Repo.one()
end

def base_query() do
from autohost in Autohost, as: :autohost
end

def where_id(query, id) do
from autohost in query,
where: autohost.id == ^id
end
end
21 changes: 21 additions & 0 deletions lib/teiserver/autohost/schemas/autohost.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Teiserver.Autohost.Autohost do
@moduledoc false
use TeiserverWeb, :schema

@type id :: integer()
@type t :: %__MODULE__{
id: id(),
name: String.t()
}

schema "teiserver_autohosts" do
field :name, :string

timestamps(type: :utc_datetime)
end

def changeset(autohost, attrs) do
autohost
|> cast(attrs, [:name])
end
end
Loading
Loading