What I'd like to do
NextcloudにSAMLアカウントでログインできるようにしたい。
What is needed
Auth0
NextCloud (サーバーOSはubuntuを使用)
SetUp NextCloud
NextCloudサーバーにApacheとShbbolethをインストールし、そもそも認証済みでなければログイン画面が表示されないようにする。
Shibbolethのインストール
#リポジトリの追加
curl --fail --remote-name https://pkg.switch.ch/switchaai/ubuntu/dists/focal/main/binary-all/misc/switchaai-apt-source_1.0.0~ubuntu20.04.1_all.deb
sudo apt install ./switchaai-apt-source_1.0.0~ubuntu20.04.1_all.deb
sudo apt update
#shibbolethをインストール
sudo apt update
sudo apt install --install-recommends shibbolet
#SSL証明書取得用にLet’s Encryptも入れておく
sudo apt install python-certbot-apache
sudo certbot --apache -d {ドメイン}
Apacheをインストールしてconfファイルを設定する
sudo apt install apache2
confファイルはこんな感じ
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
<VirtualHost *:80>
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
<VirtualHost *:443>
Include /etc/letsencrypt/options-ssl-apache.conf
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ServerName サーバーのドメイン
ProxyRequests Off
<Location />
<RequireAny>
AuthType shibboleth
ShibRequestSetting requireSession true
require shibboleth
require shib-session
#ShibUseHeaders On
#クライアントアプリからのアクセスはSSOをSKIPしている。
Require expr %{HTTP_USER_AGENT} =~ /(ownCloud|Nextcloud)/
Require ip IPAddress
</RequireAny>
ShibUseHeaders On
ProxyPreserveHost On
ProxyPass http://Wireguardで割り振ったNextcloudのIP
ProxyPassReverse http://Wireguardで割り振ったNextcloudのIP
</Location>
SSLCertificateFile 証明書のパス
SSLCertificateKeyFile 証明書のパス
</VirtualHost>
Auth0のセットアップ
今回はAuth0をSAML IdPとして使う。 登録が完了したらSAML(SSO)の設定を進めていく。 メニューからあぷりけーしょんを選択して、CREATEアプリケーションを押下する。
![nextcloud-saml-auth0app](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fvdinsmmocmj0%2F1l3S6EeKOTl3DNIzGUkN4j%2F7489f48de4764ab1e3df42480a7c2397%2Fimage-10-2048x1023.png%3Ffm%3Dwebp&w=3840&q=75)
アプリケーションタイプはSimpleを選択し、いい感じの名前を設定する。 作成できたらAddonsからSAML2をONにする。
![nextcloud-saml-auth0addon](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fvdinsmmocmj0%2F6Adtkp8SSck2bPLO4r0DSW%2Fbc93f1b5051c1e1587b04dd7387b9177%2Fimage.png%3Ffm%3Dwebp&w=3840&q=75)
ONにするとポップアップが表示されるので、証明書とIdP Metadataダウンロードを選択する。メタデータにはこんな感じのデータが格納されている。
![nextcloud-saml-authmeta](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fvdinsmmocmj0%2F7kVMjuRJLfhgpJYdrc6xJj%2F30ef8d4e2a1d2c06366d2b3ad479bbd4%2Fimage-5.png%3Ffm%3Dwebp&w=3840&q=75)
ダウンロードしたメタデータの中身はこんな感じ
<EntityDescriptor entityID="IdP(Auth0)のId" xmlns="urn:oasis:names:tc:SAML:2.0:metadata">
<IDPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
<KeyDescriptor use="signing">
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<X509Data>
<X509Certificate>証明書</X509Certificate>
</X509Data>
</KeyInfo>
</KeyDescriptor>
<SingleLogoutService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="ログアウトURL"/>
<SingleLogoutService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="ログアウトURL"/>
<NameIDFormat>認証情報の格納形式</NameIDFormat>
<SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="SPからの認証要求投げ先"/>
<SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="SPからの認証要求投げ先"/>
<Attribute Name="各種ユーザー情報を格納する形式等"/>
</IDPSSODescriptor>
</EntityDescriptor>
ここで後々必要になってくるのはentityIDなのでメモっておく。 Settingsタブに移動してCallback URLを指定する。 設定値は、nextdcloudのドメイン名/apps/user_saml/saml/acs
![nextcloud-saml-auth0setting](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fvdinsmmocmj0%2F5QVMf501UXrYo2BwIJzJix%2F279b20d1ac69969a2732d804822e2e82%2Fimage-7.png%3Ffm%3Dwebp&w=3840&q=75)
次にnextcloudを Auth0に登録する
![nextcloud-saml-auth0appsetting](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fvdinsmmocmj0%2FIpIE3xSieWlsrkBo9swOb%2Fe306c132ae36a227c4f4d07a692607b0%2Fimage-13.png%3Ffm%3Dwebp&w=3840&q=75)
Application LoginURIにドメイン/login AllowedcallbackURLsにドメイン/apps/user_saml/saml/acsを指定する。
Shibboleth設定
Shibbolethの設定に戻る
cd /etc/shibboleth/
#ダウンロードしたAuth0のメタデータをこのフォルダに配置する。
#shibbolethのconfをいじる
sudo vim shibboleth2.xml
#Line 26
<SSO entityID="エンティティId"
discoveryProtocol="SAMLDS" discoveryURL="https://ds.example.org/DS/WAYF">
SAML2
</SSO>
#Line 115<MetadataProvider type="XML" validate="true" path="上で保存したメタデータのファイル名"/>
#保存してShibbolethを再起動する
sudo systemctl restart shibd
ここまで設定できれば、NextcloudのURLでAuth0のログイン画面が表示されるはず。![nextcloud-saml-auth0login](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fvdinsmmocmj0%2F2kqgzljTMCKeTVnz1uzPbr%2F0c2d83a640f48dcd74790aed94981aec%2Fimage-3-1024x866.png%3Ffm%3Dwebp&w=3840&q=75)
NextCloudのセットアップ
基本は公式ドキュメント通りに進めればOKなので今回は割愛する。 公式ドキュメント インストールが終わったら、SAMLアプリを追加して設定を進める
UIDをマッピングする属性=http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress #auth0に登録してあるメールアドレスをnextcloudのユーザーIdとして使う場合
IdPのエンティティId=Issuer
IdPの表示名は好きなものを
SPが認証要求メッセージを送る〜=IdentitityProvierLoginURL
SLOを要求する場所=IdentitityProvierLoginURL/logoutを指定
![nextcloud-saml-nextcloudsamlsetting](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fvdinsmmocmj0%2F2DjES7BGN13FIGzhImcWy1%2Fbcbde2c53a52d647576c4f613436c62a%2Fimage-9-2048x1610.png%3Ffm%3Dwebp&w=3840&q=75)
ここまで設定すると nextcloudのログイン画面にDirectログインとSSOでログインするかの選択肢が表示される。 ただ、選択肢が表示されるのはイケてないので自動的にSSOログインが実行されるようにApacheのconfに↓を追記する。
rewrite ^/index\.php/apps\/user_saml\/saml\/selectUserBackEnd?(.*)$ https://ドメイン/index.php/apps/user_saml/saml/login?originalUrl=&idp=1 break;
これで自動的にSSOが実行されれるはず。