Compare commits
No commits in common. "550ca0aacc243d89fae56b9eb58a2eebd63e0024" and "7f116952f5f83888052fb7ba3238555f9a2c30f3" have entirely different histories.
550ca0aacc
...
7f116952f5
6 changed files with 17 additions and 175 deletions
|
@ -1,9 +1,3 @@
|
||||||
defmodule PlugIndie.Post do
|
defmodule PlugIndie.Post do
|
||||||
defstruct [
|
defstruct [:type, :title, :content]
|
||||||
:bookmark_of,
|
|
||||||
:content,
|
|
||||||
:like_of,
|
|
||||||
:title,
|
|
||||||
:type
|
|
||||||
]
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,22 +13,6 @@ defmodule PlugIndie.Properties do
|
||||||
content: content
|
content: content
|
||||||
}}
|
}}
|
||||||
|
|
||||||
:bookmark ->
|
|
||||||
{:ok,
|
|
||||||
%PlugIndie.Post{
|
|
||||||
type: type,
|
|
||||||
title: title,
|
|
||||||
content: content,
|
|
||||||
bookmark_of: get_bookmarked_url(properties)
|
|
||||||
}}
|
|
||||||
|
|
||||||
:like ->
|
|
||||||
{:ok,
|
|
||||||
%PlugIndie.Post{
|
|
||||||
type: type,
|
|
||||||
like_of: get_liked_url(properties)
|
|
||||||
}}
|
|
||||||
|
|
||||||
:unknown ->
|
:unknown ->
|
||||||
{:error, :parse_error}
|
{:error, :parse_error}
|
||||||
end
|
end
|
||||||
|
|
33
lib/token.ex
33
lib/token.ex
|
@ -11,9 +11,7 @@ defmodule PlugIndie.Token do
|
||||||
) do
|
) do
|
||||||
case do_verify_token(access_token, token_endpoint, user_agent) do
|
case do_verify_token(access_token, token_endpoint, user_agent) do
|
||||||
{:ok, %{status: 200, body: body}} ->
|
{:ok, %{status: 200, body: body}} ->
|
||||||
body
|
verify_token_response(body, required_scope, supported_scopes, own_hostname)
|
||||||
|> map_keys_to_string()
|
|
||||||
|> verify_token_response(required_scope, supported_scopes, own_hostname)
|
|
||||||
|
|
||||||
{:ok, %{status: status}} ->
|
{:ok, %{status: status}} ->
|
||||||
{:error, :request_error, status}
|
{:error, :request_error, status}
|
||||||
|
@ -47,28 +45,19 @@ defmodule PlugIndie.Token do
|
||||||
Tesla.get(client, token_endpoint)
|
Tesla.get(client, token_endpoint)
|
||||||
end
|
end
|
||||||
|
|
||||||
def verify_token_response(
|
defp verify_token_response(
|
||||||
%{
|
%{
|
||||||
"me" => host_uri,
|
me: host_uri,
|
||||||
"scope" => scope,
|
scope: scope,
|
||||||
"client_id" => client_id,
|
client_id: client_id,
|
||||||
"issued_at" => _issued_at,
|
issued_at: _issued_at,
|
||||||
"issued_by" => _issued_by,
|
issued_by: _issued_by,
|
||||||
"nonce" => _nonce
|
nonce: _nonce
|
||||||
},
|
},
|
||||||
required_scope,
|
required_scope,
|
||||||
supported_scopes,
|
supported_scopes,
|
||||||
own_hostname
|
own_hostname
|
||||||
) do
|
) do
|
||||||
# {%{
|
|
||||||
# "client_id" => "https://indiepass.app/",
|
|
||||||
# "issued_at" => 1_733_382_601,
|
|
||||||
# "issued_by" => "https://tokens.indieauth.com/token",
|
|
||||||
# "me" => "https://blog.inhji.de/",
|
|
||||||
# "nonce" => 358_618_865,
|
|
||||||
# "scope" => "create update delete media read follow channels mute block"
|
|
||||||
# }, "create", ["create", "media"], "inhji.de"}
|
|
||||||
|
|
||||||
Logger.info("Host-URI: '#{host_uri}'")
|
Logger.info("Host-URI: '#{host_uri}'")
|
||||||
Logger.info("ClientId: '#{client_id}'")
|
Logger.info("ClientId: '#{client_id}'")
|
||||||
Logger.info("Scopes: '#{scope}'")
|
Logger.info("Scopes: '#{scope}'")
|
||||||
|
@ -83,8 +72,6 @@ defmodule PlugIndie.Token do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def verify_token_response(_, _, _, _), do: {:error, "verify_token_response", "bad request"}
|
|
||||||
|
|
||||||
defp verify_hostname_match(host_uri, own_hostname) do
|
defp verify_hostname_match(host_uri, own_hostname) do
|
||||||
hostnames_match? = get_hostname(host_uri) == own_hostname
|
hostnames_match? = get_hostname(host_uri) == own_hostname
|
||||||
|
|
||||||
|
@ -120,8 +107,4 @@ defmodule PlugIndie.Token do
|
||||||
{:error, "verify_scope_support", "scope '#{required_scope}' was not requested"}
|
{:error, "verify_scope_support", "scope '#{required_scope}' was not requested"}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp map_keys_to_string(map) do
|
|
||||||
for {key, val} <- map, into: %{}, do: {to_string(key), val}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,36 +7,11 @@ defmodule ParserTest do
|
||||||
params = %{
|
params = %{
|
||||||
"type" => ["h-entry"],
|
"type" => ["h-entry"],
|
||||||
"properties" => %{
|
"properties" => %{
|
||||||
"name" => ["First Note"],
|
"content" => "Hello World!"
|
||||||
"content" => ["Hello World!"],
|
|
||||||
"category" => ["Foo", "Bar"]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert {:ok, "entry",
|
assert {:ok, "entry", %{"content" => "Hello World!"}} =
|
||||||
%{
|
|
||||||
"content" => ["Hello World!"],
|
|
||||||
"category" => ["Foo", "Bar"],
|
|
||||||
"name" => ["First Note"]
|
|
||||||
}} ==
|
|
||||||
parse_create_body("application/json", params)
|
parse_create_body("application/json", params)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "parse_create_body with content-type form" do
|
|
||||||
params = %{
|
|
||||||
"h" => "entry",
|
|
||||||
"name" => "First Note",
|
|
||||||
"content" => "Hello World!",
|
|
||||||
"category" => ["Foo", "Bar"]
|
|
||||||
}
|
|
||||||
|
|
||||||
# TODO: fix content type
|
|
||||||
assert {:ok, "entry",
|
|
||||||
%{
|
|
||||||
"content" => ["Hello World!"],
|
|
||||||
"category" => ["Foo", "Bar"],
|
|
||||||
"name" => ["First Note"]
|
|
||||||
}} ==
|
|
||||||
parse_create_body("application/form", params)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,43 +0,0 @@
|
||||||
defmodule PropertiesTest do
|
|
||||||
use ExUnit.Case
|
|
||||||
doctest PlugIndie.Properties
|
|
||||||
import PlugIndie.Properties, only: [parse: 1]
|
|
||||||
|
|
||||||
test "parse/1 identifies a note" do
|
|
||||||
props = %{
|
|
||||||
"content" => ["Hello World!"]
|
|
||||||
}
|
|
||||||
|
|
||||||
assert {:ok,
|
|
||||||
%PlugIndie.Post{
|
|
||||||
type: :note,
|
|
||||||
content: "Hello World!"
|
|
||||||
}} == parse(props)
|
|
||||||
end
|
|
||||||
|
|
||||||
test "parse/1 identifies a bookmark" do
|
|
||||||
props = %{
|
|
||||||
"content" => ["Hello World!"],
|
|
||||||
"bookmark-of" => ["https://100r.co"]
|
|
||||||
}
|
|
||||||
|
|
||||||
assert {:ok,
|
|
||||||
%PlugIndie.Post{
|
|
||||||
type: :bookmark,
|
|
||||||
content: "Hello World!",
|
|
||||||
bookmark_of: "https://100r.co"
|
|
||||||
}} == parse(props)
|
|
||||||
end
|
|
||||||
|
|
||||||
test "parse/1 identifies a like" do
|
|
||||||
props = %{
|
|
||||||
"like-of" => ["https://100r.co"]
|
|
||||||
}
|
|
||||||
|
|
||||||
assert {:ok,
|
|
||||||
%PlugIndie.Post{
|
|
||||||
type: :like,
|
|
||||||
like_of: "https://100r.co"
|
|
||||||
}} == parse(props)
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,51 +0,0 @@
|
||||||
defmodule TokenTest do
|
|
||||||
use ExUnit.Case
|
|
||||||
|
|
||||||
describe "verify_token_response/4" do
|
|
||||||
test "with atom keyed map" do
|
|
||||||
body = %{
|
|
||||||
:client_id => "https://indiepass.app/",
|
|
||||||
:issued_at => 1_733_382_601,
|
|
||||||
:issued_by => "https://tokens.indieauth.com/token",
|
|
||||||
:me => "https://blog.inhji.de/",
|
|
||||||
:nonce => 358_618_865,
|
|
||||||
:scope => "create update delete media read follow channels mute block"
|
|
||||||
}
|
|
||||||
|
|
||||||
required_scope = "create"
|
|
||||||
supported_scopes = ["create", "media"]
|
|
||||||
hostname = "blog.inhji.de"
|
|
||||||
|
|
||||||
assert {:error, _, _} =
|
|
||||||
PlugIndie.Token.verify_token_response(
|
|
||||||
body,
|
|
||||||
required_scope,
|
|
||||||
supported_scopes,
|
|
||||||
hostname
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
test "with string keyed map" do
|
|
||||||
body = %{
|
|
||||||
"client_id" => "https://indiepass.app/",
|
|
||||||
"issued_at" => 1_733_382_601,
|
|
||||||
"issued_by" => "https://tokens.indieauth.com/token",
|
|
||||||
"me" => "https://blog.inhji.de/",
|
|
||||||
"nonce" => 358_618_865,
|
|
||||||
"scope" => "create update delete media read follow channels mute block"
|
|
||||||
}
|
|
||||||
|
|
||||||
required_scope = "create"
|
|
||||||
supported_scopes = ["create", "media"]
|
|
||||||
hostname = "blog.inhji.de"
|
|
||||||
|
|
||||||
assert PlugIndie.Token.verify_token_response(
|
|
||||||
body,
|
|
||||||
required_scope,
|
|
||||||
supported_scopes,
|
|
||||||
hostname
|
|
||||||
) ==
|
|
||||||
:ok
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
Loading…
Reference in a new issue