はじめに
ラズベリーパイをつかって、オーオースのスクラッチ実装をApacheモジュールで実装されているmod_auth_openidcと連携してみます。
オーオースって何?という人はこちらからどうぞ。
https://qiita.com/namikitakeo/items/f1bb0fa958cf87c80000
オーオースつくりたい!という人はこちらからどうぞ。
https://qiita.com/namikitakeo/items/0de598b8e43eb5b1ff94
ラズベリーパイをもっと活用したい!という人はこちらからどうぞ。
https://qiita.com/namikitakeo/items/c88f640b62d6d1535d67
前提
説明のためにFQDNはraspberry.piとしています。ラズベリーパイで動作確認していますがAWS/AZUREなどのUbuntu(x64)でも同じ手順で動作すると思います。
# uname -a
Linux ubuntu 5.3.0-1022-raspi2 #24~18.04.1-Ubuntu SMP Sat Mar 28 00:52:28 UTC 2020 aarch64 aarch64 aarch64 GNU/Linux
# dotnet --version
3.1.102
# apt install apache2
# apt install libapache2-mod-auth-openidc
# a2enmod ssl
# a2enmod proxy
# a2enmod proxy_http
# a2enmod rewrite
# a2enmod cgi
# a2ensite default-ssl
# systemctl restart apache2
- Ubuntu(arm64)ベース
OP側のインストール
OAuthサーバーのスクラッチ実装例をダウンロードします。.NET Coreを起動して動作確認します。
https://github.com/namikitakeo/oauth
# git clone https://github.com/namikitakeo/oauth myop
# cd myop
# dotnet run
OP側の設定
{
"Myop": {
"BaseUrl": "https://raspberry.pi",
"AccessTokenExpiration": 60,
"RefreshTokenExpiration": 3600
},
"ConnectionStrings": {
"DefaultConnection": "DataSource=app.db"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
ProxyPass /js http://127.0.0.1:5000/js
ProxyPass /lib http://127.0.0.1:5000/lib
ProxyPass /op http://127.0.0.1:5000/op
ProxyPass /Home http://127.0.0.1:5000/Home
ProxyPass /Clients http://127.0.0.1:5000/Clients
ProxyPass /Codes http://127.0.0.1:5000/Codes
ProxyPass /Tokens http://127.0.0.1:5000/Tokens
ProxyPass /Identity http://127.0.0.1:5000/Identity
ProxyPassReverse /Identity http://127.0.0.1:5000/Identity
RewriteEngine On
RewriteRule ^/$ /Home [R=302,L]
RP側の設定
OIDCRedirectURI https://raspberry.pi/cgi-bin/redirect_uri
OIDCCryptoPassphrase passphrase
OIDCProviderMetadataURL https://raspberry.pi/op/.well-known/openid-configuration
OIDCResponseType "token id_token"
# OIDCResponseType "code"
OIDCScope "openid"
OIDCSSLValidateServer Off
OIDCClientID client4
# OIDCClientSecret client3
OIDCProviderTokenEndpointAuth client_secret_post
<Location /cgi-bin>
AuthType openid-connect
Require valid-user
</Location>
動作確認
テスト用CGI(landing)を用意して以下にアクセスします。/cgi-bin配下はmod_auth_openidcで保護されているのでOPにリダイレクトされます。user01@raspberry.pi/Password#1でログインできれば成功です。
https://raspberry.pi/cgi-bin/landing
# !/usr/bin/perl
print "Content-type: text/html; charset=UTF-8\n\n";
print "<html>";
print "<head>";
print "</head>";
print "<body>";
$val = $ENV{$var};
$val =~ s|\n|\\n|g;
$val =~ s|"|\\"|g;
print "Hello, ";
print "$ENV{\"OIDC_CLAIM_sub\"}<p>";
print "curl -k https://raspberry.pi/op/.well-known/openid-configuration<p>";
print "curl -k https://raspberry.pi/op/keys<p>";
print "</body>";
print "</html>";
制限事項
- Client Credentials Grantは特定のユーザーに紐づかないためadminユーザーにてaccess_tokenを発行しています。
TODO
- Hybrid Flow対応
- PKCE対応
- MSSQL(冗長構成)対応
- CONSENT(利用同意)対応
- 1ユーザーあたりのaccess_token/refresh_tokenの制限撤廃
- 1クライアントあたりのredirect_urisの複数対応
- 1クライアントあたりのgrant_typesの複数対応
最後に
わかりずらい部分がありましたら追記いたします。