Skip to content

Auth Application Workflow? [Docs] #153

Open
@SimonLab

Description

@SimonLab

It took me a while to understand the workflow of the app after not working on it for a while. This issue contains my notes while I'm going over the code again. I'll convert them to documentation in the Readme and hopefully this will also help with #149

The application let you authenticate with Google, Github or by email:
image
This is done with the first part of the index controller which match the / endpoint:

case get_client_id_from_query(conn) do
# no auth_client_id means the request is for auth app
0 ->
Auth.Log.info(conn, params)
render_login_buttons(conn, params)

the second part of the index is used to authenticate user for another application. The user application redirect to the auth app and contains the auth_client_id query parameter, eg: /?auth_client_id=123

client_id ->
if client_id_valid?(client_id, conn) do
msg = "request with client_id: #{client_id} (index:73)"
Auth.Log.info(conn, Map.merge(params, %{msg: msg}))
render_login_buttons(conn, params)
else
msg = "auth_client_id: #{client_id} is not valid (index:77)"
Auth.Log.error(conn, Map.merge(params, %{msg: msg}))
conn
|> put_flash(:error, msg)
|> unauthorized(msg)
end

The redirection to the auth app is done using the auth_plug library:
https://github.com/dwyl/auth_plug/blob/77963c86483c78acb3f2fe386416d67b528607e8/lib/auth_plug.ex#L32-L39

    case AuthPlug.Token.verify_jwt(jwt) do
      {:ok, values} ->
        AuthPlug.Token.put_current_token(conn, jwt, values)

      # log the JWT verify error then redirect:
      {:error, reason} ->
        Logger.error("AuthPlug: " <> Kernel.inspect(reason))
        redirect_to_auth(conn, options) # redirect to auth application
    end

We can see that a jwt is validated and if it fails the user application redirect to the auth app with the auth_client_id:

    to =
      opts.auth_url <>
        "?referer=" <>
        URI.encode(baseurl <> conn.request_path) <>
        "&auth_client_id=" <> AuthPlug.Token.client_id()

see https://github.com/dwyl/auth_plug/blob/77963c86483c78acb3f2fe386416d67b528607e8/lib/auth_plug.ex#L47-L51

Metadata

Metadata

Assignees

Labels

T4hTime Estimate 4 HoursdiscussShare your constructive thoughts on how to make progress with this issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions