-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://github.com/bonfire-networks/bonfire-app/issues/918
- Loading branch information
Showing
29 changed files
with
420 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
bonfire_common = "https://github.com/bonfire-networks/bonfire_common" | ||
bonfire_ui_common = "https://github.com/bonfire-networks/bonfire_ui_common" | ||
bonfire_me = "https://github.com/bonfire-networks/bonfire_me" | ||
openid_connect = "https://github.com/DockYard/openid_connect" | ||
openid_connect = "https://github.com/DockYard/openid_connect" | ||
|
||
# ex_cldr_territories = "https://github.com/elixir-cldr/cldr_territories" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
faker = "~> 0.14" # fake data generation | ||
jason = "~> 1.0" | ||
boruta = "~> 2.3.2" # openid / oauth provider | ||
boruta = "~> 2.3.3" # openid / oauth provider | ||
openid_connect = "~> 0.2.2" # openid client | ||
surface = "~> 0.11.2" | ||
untangle = "~> 0.3" | ||
surface = "~> 0.11.4" | ||
untangle = "~> 0.3" | ||
plug_crypto = "~> 2.0" | ||
# ex_cldr = "~> 2.30" # internationalisation |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
defmodule Bonfire.API.MastoCompatible.AppController do | ||
use Bonfire.UI.Common.Web, :controller | ||
alias Bonfire.Common.Config | ||
alias Bonfire.OpenID.Web.OauthView | ||
|
||
def create(conn, params) do | ||
# curl -X POST \ | ||
# -F 'client_name=Test Application' \ | ||
# -F 'redirect_uris=urn:ietf:wg:oauth:2.0:oob' \ | ||
# -F 'scopes=read write push' \ | ||
# -F 'website=https://myapp.example' \ | ||
# https://instance.example/api/v1/apps | ||
|
||
with {:ok, client} <- | ||
Bonfire.OpenID.Clients.new(%{ | ||
name: String.trim("#{params["client_name"]} #{params["website"]}"), | ||
redirect_uris: List.wrap(params["redirect_uris"]) | ||
# _: params["scopes"], # TODO | ||
}) do | ||
json(conn, %{ | ||
"id" => client.id, | ||
"name" => client.name, | ||
"website" => nil, | ||
"redirect_uri" => List.first(client.redirect_uris || []), | ||
"client_id" => client.id, | ||
"client_secret" => client.secret | ||
# "vapid_key"=> client.vapid_key # TODO? | ||
}) | ||
else | ||
other -> | ||
error(other) | ||
|
||
conn | ||
|> put_status(500) | ||
|> put_view(OauthView) | ||
|> render("error.json", | ||
error: "Could not create a client", | ||
error_description: inspect(other) | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule Bonfire.OpenID.Web.Oauth.ReadyController do | ||
use Bonfire.UI.Common.Web, :controller | ||
alias Bonfire.OpenID.Web.OauthView | ||
|
||
def ready(%Plug.Conn{} = conn, %{"code" => code}) do | ||
conn | ||
|> put_view(OauthView) | ||
|> render("error.html", | ||
error: "Ready", | ||
error_description: "Copy/paste your authentication code into the app: #{code}" | ||
) | ||
end | ||
end |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
defmodule Bonfire.OpenID.Plugs.AuthRequired do | ||
import Plug | ||
|
||
use Bonfire.UI.Common.Web, :controller | ||
|
||
def require_auth(conn, _opts) do | ||
Bonfire.OpenID.Plugs.Authorize.maybe_load_authorization(conn) || | ||
raise Bonfire.Fail, :needs_login | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
defmodule Bonfire.OpenID.Plugs.Authorize do | ||
import Plug | ||
|
||
use Bonfire.UI.Common.Web, :controller | ||
|
||
alias Boruta.Oauth.Authorization.AccessToken | ||
alias Boruta.Oauth.Scope | ||
|
||
def load_authorization(conn, _opts) do | ||
maybe_load_authorization(conn) || conn | ||
end | ||
|
||
def maybe_load_authorization(conn, _opts \\ []) do | ||
with [authorization_header] <- get_req_header(conn, "authorization"), | ||
[_authorization_header, bearer] <- Regex.run(~r/[B|b]earer (.+)/, authorization_header), | ||
{:ok, token} <- AccessToken.authorize(value: bearer), | ||
%{} = user <- Bonfire.Me.Users.get_current(token.sub) do | ||
conn | ||
# |> assign(:current_bearer_token, bearer) | ||
|> assign(:current_token, token) | ||
|> assign(:current_user, user) | ||
else | ||
other -> | ||
info(other, "Could not load authorization") | ||
nil | ||
end | ||
end | ||
|
||
def authorize(conn, [_h | _t] = required_scopes) do | ||
current_scopes = Scope.split(conn.assigns[:current_token].scope) | ||
|
||
case Enum.empty?(required_scopes -- current_scopes) do | ||
true -> | ||
conn | ||
|
||
false -> | ||
raise Bonfire.Fail, :unauthorized | ||
end | ||
end | ||
end |
Oops, something went wrong.