[Ubuntu20.04] KeyCloakでSAML SSOテスト

SAMLのIDPとしてKeyCloakを使用した際のメモ。

UIに少し癖はあるものの、インストールは非常に簡単でした。

基本設定

Ubuntu20.04の基本設定を行う。下記のページを参考。

Ubuntu20.04の基本設定をコピペで

Dockerのインストール

KeycloakをDocker上に構築するため、Dockerをインストールする。

参考 Install Docker on Ubuntu

上記のページを参考にインストール。

sudo apt-get install 
ca-certificates curl gnupg lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

root以外のユーザーでもdockerコマンドが使用できるように、ユーザーをdockerグループに追加。

sudo usermod -aG docker $USER

Keycloakの起動

KeycloakのDockerイメージを使って起動します。

参考 Get started with Keycloak on Docker

上記のページを参考に下記のコマンドで起動。デーモン化するために-dを追加しています。

docker run -d -p 8080:8080 -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin quay.io/keycloak/keycloak:17.0.0 start-dev

その後、http://localhost:8080/adminにアクセスするとログイン画面が現れるので、ユーザー名adminとパスワードadminでログインします。

Keycloak初期設定

テナントにあたるRealmを作成します。

続いて、テスト用のユーザーを作成します。

続いて、ユーザーの初期パスワードを設定します。

SAMLアプリケーションの登録

SAMLアプリケーションの設定をします。

https://sptest.iamshowcase.com/testsp_metadata.xmlからSP用のmetadataをダウンロードして設定に使用します。

SAMLのSPとして、RSA SAML Test Service Providerを使用しています。

参考 RSA SAML Test Service Provider

テストアプリからリクエストの署名がされないので、Client Signature RequiredをOFFにします。

続いて、KeycloakをIDPとするMetadataを下記のページからダウンロードします。

ダウンロードしたmetadataをRSAの方にアップロードするとSPから認証を行う場合のURLが発行されるので、そのURLにアクセスしてテストします。

SAMLアプリケーションのテスト

発行されたURLにアクセスすると、リダイレクトされてKeycloakのログインページへ遷移します。

その際、送られるSAMLリクエストは下記のようなものです。

<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
                    xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
                    ID="a4abdba0175f604f24bb194f13c281ac4f0cba439"
                    IssueInstant="2022-02-23T14:48:48Z"
                    Destination="http://localhost:8080/realms/home/protocol/saml"
                    AssertionConsumerServiceURL="https://sptest.iamshowcase.com/acs"
                    ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
                    Version="2.0"
                    >
    <saml:Issuer>IAMShowcase</saml:Issuer>
</samlp:AuthnRequest>

ログインに成功すると、SAMLのアサーションに含まれる内容を表示するページへ遷移します。

<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
                xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
                Destination="https://sptest.iamshowcase.com/acs"
                ID="ID_81f71839-653f-4751-80f1-57092f65714b"
                InResponseTo="a4abdba0175f604f24bb194f13c281ac4f0cba439"
                IssueInstant="2022-02-23T14:51:41.760Z"
                Version="2.0"
                >
    <saml:Issuer>http://localhost:8080/realms/home</saml:Issuer>
    <dsig:Signature xmlns:dsig="http://www.w3.org/2000/09/xmldsig#">
        <dsig:SignedInfo>
            <dsig:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
            <dsig:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
            <dsig:Reference URI="#ID_81f71839-653f-4751-80f1-57092f65714b">
                <dsig:Transforms>
                    <dsig:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                    <dsig:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                </dsig:Transforms>
                <dsig:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
                <dsig:DigestValue>omit</dsig:DigestValue>
            </dsig:Reference>
        </dsig:SignedInfo>
        <dsig:SignatureValue>omit</dsig:SignatureValue>
        <dsig:KeyInfo>
            <dsig:KeyName>omit</dsig:KeyName>
            <dsig:X509Data>
                <dsig:X509Certificate>omit</dsig:X509Certificate>
            </dsig:X509Data>
            <dsig:KeyValue>
                <dsig:RSAKeyValue>
                    <dsig:Modulus>omit</dsig:Modulus>
                    <dsig:Exponent>AQAB</dsig:Exponent>
                </dsig:RSAKeyValue>
            </dsig:KeyValue>
        </dsig:KeyInfo>
    </dsig:Signature>
    <samlp:Status>
        <samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success" />
    </samlp:Status>
    <saml:Assertion xmlns="urn:oasis:names:tc:SAML:2.0:assertion"
                    ID="ID_4b868452-1c7d-49fc-92bf-f313d47ccc08"
                    IssueInstant="2022-02-23T14:51:41.757Z"
                    Version="2.0"
                    >
        <saml:Issuer>http://localhost:8080/realms/home</saml:Issuer>
        <dsig:Signature xmlns:dsig="http://www.w3.org/2000/09/xmldsig#">
            <dsig:SignedInfo>
                <dsig:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                <dsig:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
                <dsig:Reference URI="#ID_4b868452-1c7d-49fc-92bf-f313d47ccc08">
                    <dsig:Transforms>
                        <dsig:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                        <dsig:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                    </dsig:Transforms>
                    <dsig:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
                    <dsig:DigestValue>omit</dsig:DigestValue>
                </dsig:Reference>
            </dsig:SignedInfo>
            <dsig:SignatureValue>omit</dsig:SignatureValue>
            <dsig:KeyInfo>
                <dsig:KeyName>omit</dsig:KeyName>
                <dsig:X509Data>
                    <dsig:X509Certificate>omit</dsig:X509Certificate>
                </dsig:X509Data>
                <dsig:KeyValue>
                    <dsig:RSAKeyValue>
                        <dsig:Modulus>omit</dsig:Modulus>
                        <dsig:Exponent>AQAB</dsig:Exponent>
                    </dsig:RSAKeyValue>
                </dsig:KeyValue>
            </dsig:KeyInfo>
        </dsig:Signature>
        <saml:Subject>
            <saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified">user1</saml:NameID>
            <saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
                <saml:SubjectConfirmationData InResponseTo="a4abdba0175f604f24bb194f13c281ac4f0cba439"
                                              NotOnOrAfter="2022-02-23T14:56:39.757Z"
                                              Recipient="https://sptest.iamshowcase.com/acs"
                                              />
            </saml:SubjectConfirmation>
        </saml:Subject>
        <saml:Conditions NotBefore="2022-02-23T14:51:39.757Z"
                         NotOnOrAfter="2022-02-23T14:52:39.757Z"
                         >
            <saml:AudienceRestriction>
                <saml:Audience>IAMShowcase</saml:Audience>
            </saml:AudienceRestriction>
        </saml:Conditions>
        <saml:AuthnStatement AuthnInstant="2022-02-23T14:51:41.760Z"
                             SessionIndex="0fc0e0e2-a162-42f1-824b-23b93fec68b6::72f7f459-c526-4711-bff6-0d5dc0807b99"
                             SessionNotOnOrAfter="2022-02-24T00:51:41.760Z"
                             >
            <saml:AuthnContext>
                <saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified</saml:AuthnContextClassRef>
            </saml:AuthnContext>
        </saml:AuthnStatement>
        <saml:AttributeStatement>
            <saml:Attribute Name="Role"
                            NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"
                            >
                <saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema"
                                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                     xsi:type="xs:string"
                                     >default-roles-home</saml:AttributeValue>
            </saml:Attribute>
            <saml:Attribute Name="Role"
                            NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"
                            >
                <saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema"
                                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                     xsi:type="xs:string"
                                     >manage-account</saml:AttributeValue>
            </saml:Attribute>
            <saml:Attribute Name="Role"
                            NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"
                            >
                <saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema"
                                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                     xsi:type="xs:string"
                                     >manage-account-links</saml:AttributeValue>
            </saml:Attribute>
            <saml:Attribute Name="Role"
                            NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"
                            >
                <saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema"
                                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                     xsi:type="xs:string"
                                     >offline_access</saml:AttributeValue>
            </saml:Attribute>
            <saml:Attribute Name="Role"
                            NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"
                            >
                <saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema"
                                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                     xsi:type="xs:string"
                                     >uma_authorization</saml:AttributeValue>
            </saml:Attribute>
            <saml:Attribute Name="Role"
                            NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"
                            >
                <saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema"
                                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                     xsi:type="xs:string"
                                     >view-profile</saml:AttributeValue>
            </saml:Attribute>
        </saml:AttributeStatement>
    </saml:Assertion>
</samlp:Response>

以上で、KeycloakのインストールとSAMLアプリケーションとのフェデレーションテストは終了です。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

CAPTCHA