PicketLinkを見てみる(6) WS-Trust - Secure Token Service

なかなかPicketLink Federationを見る時間が取れない。というか前提知識が足りない。それと忙しい。忙しい原因の一つがWS-Trustを見ててたんだが、WS-TrustのSecure Token Service(STS)を調べていて、なぜかまたPicketLinkに行き着いたw しかしこれはFederationではなくてPicketLink STSとなっている。Federationの一部のような気もするが一応、別扱いしておく。


STSとは
まだWS-**1をよく知らないので、分かった事を簡単に書くと、複数のサイト間で通信を行うにあたって、その通信内容に含めるトークンを発行するサービス。
使い方としては、下図のような感じだと思う。細かいフローは異なるかもしれない。

ClientはWeb serviceのクライアント。SPはWeb Serviceだと呼び方が違うかも知れないが、クライアントが使いたいサービスを提供しているプロバイダ。そしてSTS
SAMLと同じく、STSはクライアント、SP両者から既知であり、信頼していることが前提。OpenIDとは大きく異なるところ。
トークンとしてSAMLを使用できるので、後で仕様の重なりを調査しなくては。


PicketLink STS
使い方は以下のページに詳しく書いてあるので、またもこのページを読めば分かることを書く。なお、コメントでも書かれてるようだが、JBoss AS 6では簡単に動いたが、5系ではエラーが出た。
PicketLink Security Token Service
手順については上に詳しいので省略*2

実行結果
上のページに書いてあるクライアントコードをコンパイルして実行した。
コンソール出力*3


1.SAMLアサーションが取れた。これはクライアントがSTSトークンを要求して取れたということ。

Successfully issued a standard SAMLV2.0 Assertion!
<?xml version="1.0" encoding="UTF-8"?>
<Assertion xmlns="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:ns2="http://www.w3.org/2001/04/xmlenc#" xmlns:ns3="http://www.w3.org/2000/09/xmldsig#" ID="ID_3fed36ee-d80a-4d53-8057-d35986472b7e" IssueInstant="2011-06-18T05:12:36.316Z" Version="2.0">
 <Issuer>PicketLinkSTS</Issuer>
 <Subject>
  <NameID NameQualifier="urn:picketlink:identity-federation">admin</NameID>
  <SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"/>
 </Subject>
 <Conditions NotBefore="2011-06-18T05:12:36.316Z" NotOnOrAfter="2011-06-18T07:12:36.316Z"/>
 <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#WithComments"/>
   <dsig:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
   <dsig:Reference URI="#ID_3fed36ee-d80a-4d53-8057-d35986472b7e">
    <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/2000/09/xmldsig#sha1"/>
    <dsig:DigestValue>8LFDthG1XMOW1s3kDwn9FTkekUI=</dsig:DigestValue>
   </dsig:Reference>
  </dsig:SignedInfo>
  <dsig:SignatureValue>ZQQljf54zZLvXqz6mmQXU6yIiLSgn4LPFDxL6bCDm38j7wrxl2ukuJkxfPZTjrvGPSb/tFt1u7kjWtgtVMjzUGq0dq6LhkSY2VRkK6mHO7U1j9It9OIBoLrC5tW3slc6UkVq+icDNtjolCP38ccJGPjdYdBG6g5J984FH9la7KM=</dsig:SignatureValue>
  <dsig:KeyInfo>
   <dsig:KeyValue>
    <dsig:RSAKeyValue>
     <dsig:Modulus>suGIyhVTbFvDwZdx8Av62zmP+aGOlsBN8WUE3eEEcDtOIZgO78SImMQGwB2C0eIVMhiLRzVPqoW1dCPAveTm653zHOmubaps1fY0lLJDSZbTbhjeYhoQmmaBro/tDpVw5lKJns2qVnMuRK19ju2dxpKwlYGGtrP5VQv00dfNPbs=</dsig:Modulus>
     <dsig:Exponent>AQAB</dsig:Exponent>
    </dsig:RSAKeyValue>
   </dsig:KeyValue>
  </dsig:KeyInfo>
 </dsig:Signature>
</Assertion>


2.アサーションの検証を行い、成功している。これはSPが行うべきこと。

Is assertion valid? true


パケットキャプチャ結果*4


コンソール出力の1に該当するrequest/response

POST /picketlink-sts/PicketLinkSTS HTTP/1.1
Content-type: text/xml;charset="utf-8"
Authorization: Basic YWRtaW46YWRtaW4=
Soapaction: ""
Accept: text/xml, multipart/related, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
User-Agent: JAX-WS RI 2.1.6
Host: localhost:8080
Connection: keep-alive
Content-Length: 778
<?xml version="1.0"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
 <S:Body>
  <ns4:RequestSecurityToken xmlns="http://www.w3.org/2005/08/addressing" xmlns:ns2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:ns3="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:ns4="http://docs.oasis-open.org/ws-sx/ws-trust/200512" xmlns:ns5="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:ns6="http://www.w3.org/2000/09/xmldsig#" Context="context">
   <ns4:TokenType>http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0</ns4:TokenType>
   <ns4:RequestType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue</ns4:RequestType>
  </ns4:RequestSecurityToken>
 </S:Body>
</S:Envelope>
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Powered-By: Servlet/3.0; JBossAS-6
Content-Type: text/xml;charset=UTF-8
Content-Length: 3331
Date: Sat, 18 Jun 2011 05:12:36 GMT
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 <soap:Body>
  <ns4:RequestSecurityTokenResponseCollection xmlns="http://www.w3.org/2005/08/addressing" xmlns:ns2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:ns3="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:ns4="http://docs.oasis-open.org/ws-sx/ws-trust/200512" xmlns:ns5="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:ns6="http://www.w3.org/2000/09/xmldsig#">
   <ns4:RequestSecurityTokenResponse Context="context">
    <ns4:TokenType>http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0</ns4:TokenType>
    <ns4:Lifetime>
     <ns3:Created>2011-06-18T05:12:36.316Z</ns3:Created>
     <ns3:Expires>2011-06-18T07:12:36.316Z</ns3:Expires>
    </ns4:Lifetime>
    <ns4:KeySize>128</ns4:KeySize>
    <ns4:KeyType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/Bearer</ns4:KeyType>
    <ns4:RequestedSecurityToken>
     <Assertion xmlns="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:ns2="http://www.w3.org/2001/04/xmlenc#" xmlns:ns3="http://www.w3.org/2000/09/xmldsig#" ID="ID_3fed36ee-d80a-4d53-8057-d35986472b7e" IssueInstant="2011-06-18T05:12:36.316Z" Version="2.0">
      <Issuer>PicketLinkSTS</Issuer>
      <Subject>
       <NameID NameQualifier="urn:picketlink:identity-federation">admin</NameID>
       <SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"/>
      </Subject>
      <Conditions NotBefore="2011-06-18T05:12:36.316Z" NotOnOrAfter="2011-06-18T07:12:36.316Z"/>
      <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#WithComments"/>
        <dsig:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
        <dsig:Reference URI="#ID_3fed36ee-d80a-4d53-8057-d35986472b7e">
         <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/2000/09/xmldsig#sha1"/>
         <dsig:DigestValue>8LFDthG1XMOW1s3kDwn9FTkekUI=</dsig:DigestValue>
        </dsig:Reference>
       </dsig:SignedInfo>
       <dsig:SignatureValue>ZQQljf54zZLvXqz6mmQXU6yIiLSgn4LPFDxL6bCDm38j7wrxl2ukuJkxfPZTjrvGPSb/tFt1u7kj
WtgtVMjzUGq0dq6LhkSY2VRkK6mHO7U1j9It9OIBoLrC5tW3slc6UkVq+icDNtjolCP38ccJGPjd
YdBG6g5J984FH9la7KM=</dsig:SignatureValue>
       <dsig:KeyInfo>
        <dsig:KeyValue>
         <dsig:RSAKeyValue>
          <dsig:Modulus>suGIyhVTbFvDwZdx8Av62zmP+aGOlsBN8WUE3eEEcDtOIZgO78SImMQGwB2C0eIVMhiLRzVPqoW1
dCPAveTm653zHOmubaps1fY0lLJDSZbTbhjeYhoQmmaBro/tDpVw5lKJns2qVnMuRK19ju2dxpKw
lYGGtrP5VQv00dfNPbs=</dsig:Modulus>
          <dsig:Exponent>AQAB</dsig:Exponent>
         </dsig:RSAKeyValue>
        </dsig:KeyValue>
       </dsig:KeyInfo>
      </dsig:Signature>
     </Assertion>
    </ns4:RequestedSecurityToken>
    <ns4:RequestedAttachedReference>
     <ns5:SecurityTokenReference xmlns:ns7="http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd" ns7:TokenType="http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0">
      <ns5:KeyIdentifier ValueType="http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLID">#ID_3fed36ee-d80a-4d53-8057-d35986472b7e</ns5:KeyIdentifier>
     </ns5:SecurityTokenReference>
    </ns4:RequestedAttachedReference>
   </ns4:RequestSecurityTokenResponse>
  </ns4:RequestSecurityTokenResponseCollection>
 </soap:Body>
</soap:Envelope>


コンソール出力の2に該当するrequest/response

POST /picketlink-sts/PicketLinkSTS HTTP/1.1
Content-type: text/xml;charset="utf-8"
Authorization: Basic YWRtaW46YWRtaW4=
Soapaction: ""
Accept: text/xml, multipart/related, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
User-Agent: JAX-WS RI 2.1.6
Host: localhost:8080
Connection: keep-alive
Content-Length: 2730
<?xml version="1.0"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
 <S:Body>
  <ns4:RequestSecurityToken xmlns="http://www.w3.org/2005/08/addressing" xmlns:ns2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:ns3="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:ns4="http://docs.oasis-open.org/ws-sx/ws-trust/200512" xmlns:ns5="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:ns6="http://www.w3.org/2000/09/xmldsig#" Context="context">
   <ns4:TokenType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/RSTR/Status</ns4:TokenType>
   <ns4:RequestType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/Validate</ns4:RequestType>
   <ns4:ValidateTarget>
    <Assertion xmlns="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:ns2="http://www.w3.org/2001/04/xmlenc#" xmlns:ns3="http://www.w3.org/2000/09/xmldsig#" ID="ID_3fed36ee-d80a-4d53-8057-d35986472b7e" IssueInstant="2011-06-18T05:12:36.316Z" Version="2.0">
     <Issuer>PicketLinkSTS</Issuer>
     <Subject>
      <NameID NameQualifier="urn:picketlink:identity-federation">admin</NameID>
      <SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"/>
     </Subject>
     <Conditions NotBefore="2011-06-18T05:12:36.316Z" NotOnOrAfter="2011-06-18T07:12:36.316Z"/>
     <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#WithComments"/>
       <dsig:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
       <dsig:Reference URI="#ID_3fed36ee-d80a-4d53-8057-d35986472b7e">
        <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/2000/09/xmldsig#sha1"/>
        <dsig:DigestValue>8LFDthG1XMOW1s3kDwn9FTkekUI=</dsig:DigestValue>
       </dsig:Reference>
      </dsig:SignedInfo>
      <dsig:SignatureValue>ZQQljf54zZLvXqz6mmQXU6yIiLSgn4LPFDxL6bCDm38j7wrxl2ukuJkxfPZTjrvGPSb/tFt1u7kj
WtgtVMjzUGq0dq6LhkSY2VRkK6mHO7U1j9It9OIBoLrC5tW3slc6UkVq+icDNtjolCP38ccJGPjd
YdBG6g5J984FH9la7KM=</dsig:SignatureValue>
      <dsig:KeyInfo>
       <dsig:KeyValue>
        <dsig:RSAKeyValue>
         <dsig:Modulus>suGIyhVTbFvDwZdx8Av62zmP+aGOlsBN8WUE3eEEcDtOIZgO78SImMQGwB2C0eIVMhiLRzVPqoW1
dCPAveTm653zHOmubaps1fY0lLJDSZbTbhjeYhoQmmaBro/tDpVw5lKJns2qVnMuRK19ju2dxpKw
lYGGtrP5VQv00dfNPbs=</dsig:Modulus>
         <dsig:Exponent>AQAB</dsig:Exponent>
        </dsig:RSAKeyValue>
       </dsig:KeyValue>
      </dsig:KeyInfo>
     </dsig:Signature>
    </Assertion>
   </ns4:ValidateTarget>
  </ns4:RequestSecurityToken>
 </S:Body>
</S:Envelope>
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Powered-By: Servlet/3.0; JBossAS-6
Content-Type: text/xml;charset=UTF-8
Content-Length: 947
Date: Sat, 18 Jun 2011 05:12:36 GMT
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 <soap:Body>
  <ns4:RequestSecurityTokenResponseCollection xmlns="http://www.w3.org/2005/08/addressing" xmlns:ns2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:ns3="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:ns4="http://docs.oasis-open.org/ws-sx/ws-trust/200512" xmlns:ns5="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:ns6="http://www.w3.org/2000/09/xmldsig#">
   <ns4:RequestSecurityTokenResponse Context="context">
    <ns4:TokenType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/RSTR/Status</ns4:TokenType>
    <ns4:Status>
     <ns4:Code>http://docs.oasis-open.org/ws-sx/ws-trust/200512/status/valid</ns4:Code>
     <ns4:Reason>SAMLV2.0 Assertion successfuly validated</ns4:Reason>
    </ns4:Status>
   </ns4:RequestSecurityTokenResponse>
  </ns4:RequestSecurityTokenResponseCollection>
 </soap:Body>
</soap:Envelope>


長いXMLだな…。


まとめ
このサンプルではほとんどなにも手を加える事なくWS-Trustの確認ができた。とはいえよく中身を分かっていないので、ちゃんと見ていこう。


参考
WS-Trust 1.4
Java Web サービス: WS-Trust と WS-SecureConversation

*1:いまさらかと思わないでもないのだが、開発してるってことはどこかに需要があるんだよなー。

*2:後で追記するかも

*3:XMLは後で整形した

*4:同じくXMLは後で整形した