LoginSignup
0
0

Bash と Caddy サーバで ActivityPub のインスタンス(17) ログイン機能

Posted at

Caddy サーバの basicauth 機能を使う

前回で main.html を開けば taimeline.html と notification.html が一枚で表示されるようにしたが、タイムラインの他人の投稿の公開範囲を考慮していない。なのでタイムラインは他人に見られないようにする。
単純に Caddyfile に

ホニャララ {
	(中略)
   handle_path /main {
		file_server
    	root * ./main.html
	}
}

と追加しただけだと誰でも
https://ホニャララ/main
で見ることが出来るので、ログインしないとアクセス出来ないようにする。そのために Caddy の basicauth 機能を使う。Caddy の basicauth 機能は

basicauth /secret/* {
	Bob $2a$14$Zkx19XLiW6VYouLHR5NmfOFU0z2GTNmpkT/5qqR7hx4IjWJPDhjvG
}

とすると、
https://ホニャララ/secret
以下のディレクトリが
ユーザー Bob
パスワード hiccup
と入れないとアクセス出来ないようするものだ。
Caddy には

./caddy hash-password

で入力したパスワードをハッシュ化する機能もある。

$ ./caddy hash-password
Enter password: 
Confirm password: 
$2a$14$5RPMSt.GrU3PbJa86Vmqn.RoqodGZAkd46swopLB51o3260pBa1uu

上の例では "bar" をハッシュ化してみた。なので
ユーザー foo
パスワード bar
ならば、目的のディレクトリ (ここでは例として /private) 以下をこれでログイン出来るようにするには

basicauth /private/* {
	foo $2a$14$5RPMSt.GrU3PbJa86Vmqn.RoqodGZAkd46swopLB51o3260pBa1uu
	}

と追加すれば良い。
main.html に /private/main
timeline.html を /private/timeline.html
notification.html を /private/notification.html
でアクセスするようファイルをサーブさせるなら

	basicauth /private/* {
		foo $2a$14$5RPMSt.GrU3PbJa86Vmqn.RoqodGZAkd46swopLB51o3260pBa1uu
	}
    handle_path /private/main {
		file_server
    	root * ./main.html
	}
    handle_path /private/timeline.html {
		file_server
    	root * ./timeline.html
	}
    handle_path /private/notification.html {
		file_server
    	root * ./notification.html
	}

にする。一応の全文を書く。

ホニャララ {

    handle_path /.well-known/host-meta {
		file_server
        root * ./personal/host-meta
	header {
            Content-Type: application/xml
	    }
	}

    handle_path /.well-known/webfinger {
		file_server
    	root * ./personal/webfinger
	header {
	       Content-Type: application/activity+json
	       }
	}

    handle_path /users/aktor {
		file_server
    	root * ./personal/aktor
	header {
	       Content-Type: application/activity+json
	       }
	}

    handle_path /image/* {
		file_server
    	root * ./personal/image/
	}

    handle_path /users/aktor/inbox {
    	reverse_proxy localhost:1682
#   	reverse_proxy unix//tmp/sock # Unix ドメインソケットを使うならこちら
	}

    handle_path /users/aktor/statuses/* {
		file_server
    	root * ./statuses/
	header {
	       Content-Type: application/activity+json
	       }
	}

# private/main
#  ./caddy hash-password でパスワードのハッシュ化(bcrypt)
	basicauth /private/* {
		bar $2a$14$mVUIuWu75Tw2quQbQtokx.hzHiD8mDV2CjgpYSIvSc/27AsGX5Dx2
	}
    handle_path /private/main {
		file_server
    	root * ./main.html
	}
    handle_path /private/timeline.html {
		file_server
    	root * ./timeline.html
	}
    handle_path /private/notification.html {
		file_server
    	root * ./notification.html
	}

}

これでターミナル1 で

bash ./baca_server.sh

し、ターミナル2 で

bash ./baca_client.sh

で選んでゴチャゴチャやれば
https://ホニャララ/private/main
にアクセスすると反映されているはずである。

参考記事

basicauth(Caddyfile directive)

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