Skip to content

Commit

Permalink
[auth] credential offer as qr code
Browse files Browse the repository at this point in the history
  • Loading branch information
patatoid committed Jan 4, 2024
1 parent 53c7074 commit 226e12f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule BorutaWeb.Oauth.AuthorizeController do
alias BorutaIdentity.IdentityProviders
alias BorutaIdentity.IdentityProviders.IdentityProvider
alias BorutaIdentityWeb.Router.Helpers, as: IdentityRoutes
alias BorutaWeb.OauthView

def authorize(%Plug.Conn{} = conn, _params) do
current_user = conn.assigns[:current_user]
Expand Down Expand Up @@ -370,17 +371,15 @@ defmodule BorutaWeb.Oauth.AuthorizeController do
end

def authorize_success(
%Plug.Conn{query_params: query_params} = conn,
%Plug.Conn{} = conn,
%CredentialOfferResponse{} = response
) do
# TODO CredentialOfferResponse#redirect_to_url
conn
|> delete_session(:session_chosen)
|> redirect(
external:
query_params["redirect_uri"] <>
"?#{URI.encode_query(%{credential_offer: response |> Map.from_struct() |> Jason.encode!()})}"
)
|> put_view(OauthView)
|> put_layout(false)
|> render("credential_offer.html", credential_offer_response: response)
end

@impl Boruta.Oauth.AuthorizeApplication
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Boruta · Credential offer</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css" integrity="sha512-8bHTC73gkZ7rZ7vpqUQThUDhqcNFyYi2xgDgPDHc+GXVGHXq+xPjynxIopALmOPqzo9JZj0k6OqqewdGO3EsrQ==" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.26.0/themes/prism-dark.min.css" integrity="sha512-Njdz7T/p6Ud1FiTMqH87bzDxaZBsVNebOWmacBjMdgWyeIhUSFU4V52oGwo3sT+ud+lyIE98sS291/zxBfozKw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
.credits {
position: absolute;
font-size: .8em;
bottom: .5rem;
right: 1rem;
display: flex;
}

.credits img {
height: 1.5em;
margin-left: .5em;
}
</style>
</head>
<body>

<div class="ui container">
<div class="ui center aligned segment">
<h1>Sample credential offer</h1>
<img width="350" src="data:image/svg+xml; base64, <%= qr_code_from_credential_offer(@credential_offer_response) %>" alt="QR code" />
<p><%= text_from_credential_offer(@credential_offer_response) %></p>
</div>
</div>
<div class="credits">
Powered by <img src="/accounts/images/logo-yellow.png">
</div>
</body>
</html>

13 changes: 13 additions & 0 deletions apps/boruta_web/lib/boruta_web/views/oauth_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ defmodule BorutaWeb.OauthView do
}
end

def qr_code_from_credential_offer(credential_offer_response) do
with {:ok, code} <- text_from_credential_offer(credential_offer_response)
|> QRCode.create()
|> QRCode.render(:svg)
|> QRCode.to_base64() do
code
end
end

def text_from_credential_offer(credential_offer_response) do
"openid-credential-offer://?credential_offer#{credential_offer_response |> Map.from_struct() |> Jason.encode!() |> URI.encode()}"
end

defimpl Jason.Encoder, for: Boruta.Oauth.TokenResponse do
def encode(
%Boruta.Oauth.TokenResponse{
Expand Down

0 comments on commit 226e12f

Please sign in to comment.