【小ネタ】mod_auth_openidc でシングルサインオンしたユーザー名をアクセスログに出力する

2017.02.20

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

こんにちは植木和樹@上越妙高オフィスです。

引き続き Apacheの mod_auth_openidc モジュールを使って、Auth0を利用したシングルサインオン環境を検証しています。

無事Auth0を使って認証できるようになったわけですが、RPMによってデフォルトで配置される設定ファイル(/etc/httpd/conf.d/auth_openidc.conf)だと、誰がアクセスしているのかログに出力されませんでした。

今回はシングルサインオンしたユーザー名をログに出力するための設定を行います。

設定内容

Apacheのhttpd.confで定義されているcombined形式では、3項目にREMOTE_USER情報を出力するようになっています。

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

そのためOpenID Connectで認証されたユーザー名を、REMOTE_USERにマッピングしてあげれば良さそうです。このマッピングは /etc/httpd/conf.d/auth_openidc.conf の OIDCRemoteUserClaim で行います。

Using the OIDCRemoteUserClaim parameter in httpd.conf configuration, the claim value is set it as REMOTE_USER header variable.

具体的には OIDCRemoteUserClaim の第1引数で、OpenID Connectのclaim(認証したユーザーの情報)からどの項目をREMOTE_USERにマッピングするかを指定します。第2引数は正規表現を指定することで、第1引数で得た値の一部のみをユーザー名として切り出すことができます。例えば下の例ではemailアドレスの @ より前をREMOTE_USERにマッピングしています。

# (Optional)
# The claim that is used when setting the REMOTE_USER variable on OpenID Connect protected paths.
# If the claim name is postfixed with a \"@\", the claim value will be post-fixed with the
# \"iss\" value value (with leading "https://" stripped) to make this value unique across different OPs.
# When not defined the default "sub@" is used.
#
# An optional regular expression can be added as a 2nd parameter that will be applied to the
# resulting value from the 1st parameter and the first match returned from that expression will
# be set as the REMOTE_USER. E.g. to strip a domain from an e-mail style address you'd use ^(.*)@
OIDCRemoteUserClaim email ^(.*)@

設定を保存したら、httpdをリスタートしてください。

まとめ

ユーザー名がログに出力されることで、認証されたユーザーのみがアクセスできていることが確認できるようになりました。

Auth0で設定していたつもりが実は設定が間違っていて認証が有効になってなかった、ということがないようにログで確認するようにしましょう。

参考ページ