Skip to content

Commit

Permalink
feat: OAuth flow - Register/Login/Consent (#390)
Browse files Browse the repository at this point in the history
* wip

* Login/Register flow

* Login/Register/Consent flow setup

* Update apps/identity_web/lib/hydra.ex

Co-authored-by: Thomas DA ROCHA <thomas.darocha@lenra.io>

* keep alive

* postgres 13

* fix ?

* fix

* Fix credo & tests

* format

* fix typo

---------

Co-authored-by: Louis G <louis@lenra.me>
Co-authored-by: Thomas DA ROCHA <thomas.darocha@lenra.io>
Co-authored-by: jonas-martinez <cbobbxta.fr@gmail.com>
  • Loading branch information
4 people authored Jul 4, 2023
1 parent b2a7e84 commit 0a04080
Show file tree
Hide file tree
Showing 26 changed files with 751 additions and 170 deletions.
372 changes: 372 additions & 0 deletions apps/identity_web/lib/hydra.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,372 @@
defmodule ORY.Hydra do
@moduledoc """
Temporary fix to get the new Hydra 2 API working with this library.
Can be deleted as soon as the official library is updated :
See : https://github.com/malomohq/ory-hydra-elixir/issues/51
"""

alias ORY.Hydra.{Config, Operation, Request, Response}

@type http_headers_t :: [{String.t(), String.t()}]

@type http_method_t :: :delete | :get | :post | :put

@type response_t :: {:ok, Response.t()} | {:error, Response.t() | any}

@doc """
Accept a consent request.
"""
@spec accept_consent_request(map) :: Operation.t()
def accept_consent_request(params) do
%Operation{
method: :put,
params: params,
params_in_query: [:consent_challenge],
path: "/admin/oauth2/auth/requests/consent/accept"
}
end

@doc """
Accept a login request.
"""
@spec accept_login_request(map) :: Operation.t()
def accept_login_request(params) do
%Operation{
method: :put,
params: params,
params_in_query: [:login_challenge],
path: "/admin/oauth2/auth/requests/login/accept"
}
end

@doc """
Accepts a logout request.
"""
@spec accept_logout_request(map) :: Operation.t()
def accept_logout_request(params) do
%Operation{
method: :put,
params: params,
params_in_query: [:logout_challenge],
path: "/admin/oauth2/auth/requests/logout/accept"
}
end

@doc """
Check whether an ORY Hydra service is alive.
"""
@spec check_alive :: Operation.t()
def check_alive do
%Operation{
method: :get,
path: "/health/alive"
}
end

@doc """
Create an OAuth 2.0 client.
"""
@spec create_client(map) :: Operation.t()
def create_client(params \\ %{}) do
%Operation{
method: :post,
params: params,
path: "/admin/clients"
}
end

@doc """
Delete an OAuth 2.0 client.
"""
@spec delete_client(String.t()) :: Operation.t()
def delete_client(id) do
%Operation{
method: :delete,
path: "/admin/clients/#{id}"
}
end

@doc """
Delete a JSON Web Key.
"""
@spec delete_jwk(String.t(), String.t()) :: Operation.t()
def delete_jwk(set, kid) do
%Operation{
method: :delete,
path: "/admin/keys/#{set}/#{kid}"
}
end

@doc """
Delete a JSON Web Key set.
"""
@spec delete_jwk_set(String.t()) :: Operation.t()
def delete_jwk_set(set) do
%Operation{
method: :delete,
path: "/admin/keys/#{set}"
}
end

@doc """
Delete expired OAuth2 access tokens.
"""
@spec flush_expired_access_tokens(map) :: Operation.t()
def flush_expired_access_tokens(params \\ %{}) do
%Operation{
method: :post,
params: params,
path: "/admin/oauth2/flush"
}
end

@doc """
Generate a JSON Web Key set.
"""
@spec generate_jwk_set(String.t(), map) :: Operation.t()
def generate_jwk_set(set, params) do
%Operation{
method: :post,
params: params,
path: "/admin/keys/#{set}"
}
end

@doc """
Retrieve an OAuth 2.0 client.
"""
@spec get_client(String.t()) :: Operation.t()
def get_client(id) do
%Operation{
method: :get,
path: "/admin/clients/#{id}"
}
end

@doc """
Retrieve consent request.
"""
@spec get_consent_request(map) :: Operation.t()
def get_consent_request(params) do
%Operation{
method: :get,
params: params,
params_in_query: [:consent_challenge],
path: "/admin/oauth2/auth/requests/consent"
}
end

@doc """
Retrieve a JSON Web Key.
"""
@spec get_jwk(String.t(), String.t()) :: Operation.t()
def get_jwk(set, kid) do
%Operation{
method: :get,
path: "/admin/keys/#{set}/#{kid}"
}
end

@doc """
Retrieve a JSON Web Key set.
"""
@spec get_jwk_set(String.t()) :: Operation.t()
def get_jwk_set(set) do
%Operation{
method: :get,
path: "/admin/keys/#{set}"
}
end

@doc """
Retrieve login request.
"""
@spec get_login_request(map) :: Operation.t()
def get_login_request(params) do
%Operation{
method: :get,
params: params,
path: "/admin/oauth2/auth/requests/login"
}
end

@doc """
Retrieve logout request.
"""
@spec get_logout_request(map) :: Operation.t()
def get_logout_request(params) do
%Operation{
method: :get,
params: params,
path: "/admin/oauth2/auth/requests/logout"
}
end

@doc """
Retrieve a metric snapshot from an ORY Hydra service.
"""
@spec get_metrics :: Operation.t()
def get_metrics do
%Operation{
method: :get,
path: "/admin/metrics/prometheus"
}
end

@doc """
Retrieve the version of the ORY Hydra service.
"""
@spec get_version :: Operation.t()
def get_version do
%Operation{
method: :get,
path: "/version"
}
end

@doc """
Check whether a token is active.
"""
@spec introspect(map) :: Operation.t()
def introspect(params) do
%Operation{
content_type: :form_urlencoded,
method: :post,
params: params,
path: "/admin/oauth2/introspect"
}
end

@doc """
List all OAuth 2.0 clients.
"""
@spec list_clients(map) :: Operation.t()
def list_clients(params \\ %{}) do
%Operation{
method: :get,
params: params,
path: "/admin/clients"
}
end

@doc """
List all consent sessions.
"""
@spec list_consent_sessions(map) :: Operation.t()
def list_consent_sessions(params) do
%Operation{
method: :get,
params: params,
path: "/admin/oauth2/auth/sessions/consent"
}
end

@doc """
Reject a consent request.
"""
@spec reject_consent_request(map) :: Operation.t()
def reject_consent_request(params) do
%Operation{
method: :put,
params: params,
params_in_query: [:consent_challenge],
path: "/admin/oauth2/auth/requests/consent/reject"
}
end

@doc """
Reject a login request.
"""
@spec reject_login_request(map) :: Operation.t()
def reject_login_request(params) do
%Operation{
method: :put,
params: params,
params_in_query: [:login_challenge],
path: "/admin/oauth2/auth/requests/login/reject"
}
end

@doc """
Reject a logout request.
"""
@spec reject_logout_request(map) :: Operation.t()
def reject_logout_request(params) do
%Operation{
method: :put,
params: params,
params_in_query: [:logout_challenge],
path: "/admin/oauth2/auth/requests/logout/reject"
}
end

@doc """
Issue a request to the ORY Hydra administrative API.
"""
@spec request(Operation.t(), map) :: response_t
def request(operation, config) do
Request.send(operation, Config.new(config))
end

@doc """
Revoke consent sessions.
"""
@spec revoke_consent_sessions(map) :: Operation.t()
def revoke_consent_sessions(params) do
%Operation{
method: :delete,
params: params,
params_in_query: [:all, :client, :subject],
path: "/admin/oauth2/auth/sessions/consent"
}
end

@doc """
Revoke login sessions.
"""
@spec revoke_login_sessions(map) :: Operation.t()
def revoke_login_sessions(params) do
%Operation{
method: :delete,
params: params,
params_in_query: [:subject],
path: "/admin/oauth2/auth/sessions/login"
}
end

@doc """
Update an OAuth 2.0 client.
"""
@spec update_client(String.t(), map) :: Operation.t()
def update_client(id, params) do
%Operation{
method: :put,
params: params,
path: "/admin/clients/#{id}"
}
end

@doc """
Update a JSON Web Key.
"""
@spec update_jwk(String.t(), String.t(), map) :: Operation.t()
def update_jwk(set, kid, params) do
%Operation{
method: :put,
params: params,
path: "/admin/keys/#{set}/#{kid}"
}
end

@doc """
Update a JSON Web Key set.
"""
@spec update_jwk_set(String.t(), map) :: Operation.t()
def update_jwk_set(set, params) do
%Operation{
method: :put,
params: params,
path: "/admin/keys/#{set}"
}
end
end

This file was deleted.

Loading

0 comments on commit 0a04080

Please sign in to comment.