0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Bash と Caddy サーバで ActivityPub のインスタンス(9) フォローリクエストを承認する

Last updated at Posted at 2024-01-18

まずフォローしてもらう

自分に。
他のインスタンスにアカウントがあるならば、その検索に
@aktor@ホニャララ
と入れると今作っているインスタンスの自分が表示されるのは (1) の通りである。
あとはそれをフォローしてみる。大丈夫大丈夫、あとでアンフォローしとけば何もないと思う、多分。
すると (2) でやったように 202 ディレクトリに他のインスタンスの自分からのフォローリクエストが届いているはずだ。そのボディは大体以下の様だろう。

{
  "@context": "https://www.w3.org/ns/activitystreams",
  "id": "https://他のインスタンス/1qaz2wsx3edc4rfv5tgb",
  "type": "Follow",
  "actor": "https://他のインスタンス/users/他のインスタンスの自分",
  "object": "https://ホニャララ/users/aktor"
}

この json オブジェクトを

{
  "@context": "https://www.w3.org/ns/activitystreams",
  "id": "https://ホニャララ/users/aktor/accept/1705518720",
  "type": "Accept",
  "actor": "https://ホニャララ/users/aktor",
  "object": {
	  "@context": "https://www.w3.org/ns/activitystreams",
	  "id": "https://他のインスタンス/1qaz2wsx3edc4rfv5tgb",
	  "type": "Follow",
	  "actor": "https://他のインスタンス/users/他のインスタンスの自分",
	  "object": "https://ホニャララ/users/aktor"}
}

と、Accept オブジェクトで包んで他のインスタンスの自分の inbox へ投げつけると相手(他のインスタンスの自分)は自分をフォローできる。

例によってまず json_templates ディレクトリに accept.json を作る。

accept.json
{
  "@context": "https://www.w3.org/ns/activitystreams",
  "id": "https://{my_instance}/users/{myself}/accept/{id}",
  "type": "Accept",
  "actor": "https://{my_instance}/users/{myself}",
  "object": {follow_request}
}

あとで自分宛に着た POST は 202 から移動させるようにする(予定)のだが、今のところは 202 へ着たフォローリクエストのファイルをそのままカレントディレクトリへコピーしてほしい。
accept.sh はそのファイルのボディの json を {follow_request} に突っ込んで送ってきたアクターの inbox へ curl で POST するだけである。
What request do you accept?:
には、フォローリクエストファイルの json の "id" タグの値(https://他のインスタンス/1qaz2wsx3edc4rfv5tgb)をコピー&ペーストなどで入力すること。

accept.sh
#!/bin/bash


read -p "@myself@my_instance: " MY
MY_arry=(${MY//@/ })
myself=${MY_arry[0]}
my_instance=${MY_arry[1]}
echo

read -p "@to@to_instance: " TO
TO_arry=(${TO//@/ })
to=${TO_arry[0]}
to_instance=${TO_arry[1]}
echo

read -p "What request do you accept?: " id_of_request
echo

# grep でカレントディレクトリから accept したいリクエストのファイルの json 行を取得
follow_request_json=$(grep -rh "$id_of_request" ./)

unixtime=$(date "+%s")
date_now=$(date -uR  | head -c -6 | sed -e 's/$/GMT/')

sed -e "s/{id}/$unixtime/g" ./json_templates/accept.json > live
sed -i "s/{myself}/$myself/g" live
sed -i "s/{my_instance}/$my_instance/g" live

sed -i "s|{follow_request}|$follow_request_json|g" live

body=$(cat live | jq -c .)
digest_val=$(echo -n "$body" | sha256sum | xxd -r -p | base64)

URL=$(curl https://$to_instance/.well-known/webfinger?resource=acct:$to@$to_instance | jq -r '.links[] | select (.rel == "self")'.href | tr -d "\"")
INBOX=$(curl -H "Accept: application/activity+json" $URL | jq .inbox | tr -d "\"")
REQUEST_TARGET=$(echo  "/"$(sed -E 's/^.*(https):\/\/([^/]+).([^/]+)/\3/g' <<< $INBOX))

signature_headers="(request-target): post ${REQUEST_TARGET}\ndate: ${date_now}\nhost: ${to_instance}\ndigest: SHA-256=${digest_val}"
echo -e "$signature_headers" > 4_sig_headers # 後に見返すためにファイルに
sig_val=$(echo -en $signature_headers | openssl dgst -binary -sign ./personal/private -sha256 | openssl enc -A -base64)
signature="Signature: keyId=\"https://${my_instance}/users/${myself}#main-key\",algorithm=\"rsa-sha256\",headers=\"(request-target) date host digest\",signature=\"$sig_val\""

curl -v -H "Date: $date_now" -H "Digest: SHA-256=$digest_val" -H "$signature" $INBOX -d "$body"

echo $body  > ./post/$unixtime

echo $INBOX >> followers
bash ./script/accept.sh

で、適切に入力すれば他のインスタンスの自分の「フォロー」に Baca の自分が追加されているはずである。
また、最後に

echo $INBOX >> followers

という行を追加しているが、これは新しく他のインスタンスの自分(の inbox エンドポイント)をフォロワーとして followers ファイルに追加するものだ。

参考記事

Fediverse入門―非中央集権型SNSサーバを作ろう!(1)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?