2016-11-21 45 views
2

我一直在尋找解決方案,但無法找到它。如何在PHP中檢索SAML2令牌以從WSO2獲取OAuth令牌APIM

這個問題直接關係到:

這是我想要執行的情景:

  1. 身份驗證使用SAML2的SP。用SimpleSamlPhp在PHP中編碼。
  2. 使用SAML2斷言從API mgr端點獲取OAuth令牌。 (使用URL http://api.gateway/token
  3. 使用OAuth令牌在API管理器的網關上調用API。

我被困在指令:拿到SAML2斷言令牌。 我在哪裏可以找到這個令牌? 在SimpleSamlPhp中,我可以獲取用戶的屬性或他們的ID,但是我找不到任何斷言。

我砍死SSP得到最後斷言,但不知道該怎麼辦。我期待一個單一的值(如一個令牌),但它是一個複雜的結構。上面的鏈接之一表示我不應該訪問它!

什麼我發送,編碼到令牌URL?


編輯:添加一些樣本。 我的斷言XML(編輯):

<?xml version="1.0" encoding="UTF-8"?> 
<saml2:Assertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol" ID="okdjgbm...jdbh" IssueInstant="2016-11-28T09:49:45.808Z" Version="2.0"> 
    <saml2:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity">https://g...p.com:9443/samlsso</saml2:Issuer> 
    <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> 
     <ds:SignedInfo> 
     <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" /> 
     <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /> 
     <ds:Reference URI="#okd...kejdbh"> 
      <ds:Transforms> 
       <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /> 
       <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" /> 
      </ds:Transforms> 
      <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /> 
      <ds:DigestValue>RrGcR...cktFuH0=</ds:DigestValue> 
     </ds:Reference> 
     </ds:SignedInfo> 
     <ds:SignatureValue>b91j/k...Z7d4=</ds:SignatureValue> 
     <ds:KeyInfo> 
     <ds:X509Data> 
      <ds:X509Certificate>MIICNT...Wq8uHSCo=</ds:X509Certificate> 
     </ds:X509Data> 
     </ds:KeyInfo> 
    </ds:Signature> 
    <saml2:Subject> 
     <saml2:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress">[email protected]</saml2:NameID> 
     <saml2:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"> 
     <saml2:SubjectConfirmationData InResponseTo="_80258326a1...cd4bbd" NotOnOrAfter="2016-11-28T09:54:45.807Z" Recipient="http://1.2.3.4/simplesamlphp/www/module.php/saml/sp/saml2-acs.php/wso2-sp" /> 
     </saml2:SubjectConfirmation> 
     <saml2:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"> 
     <saml2:SubjectConfirmationData InResponseTo="_8025832...d4bbd" NotOnOrAfter="2016-11-28T09:54:45.807Z" Recipient="https://my.wso2.apim:8243/token" /> 
     </saml2:SubjectConfirmation> 
    </saml2:Subject> 
    <saml2:Conditions NotBefore="2016-11-28T09:49:45.808Z" NotOnOrAfter="2016-11-28T09:54:45.807Z"> 
     <saml2:AudienceRestriction> 
     <saml2:Audience>mytestapp</saml2:Audience> 
     <saml2:Audience>https://my.wso2.apim:8243/token</saml2:Audience> 
     </saml2:AudienceRestriction> 
    </saml2:Conditions> 
    <saml2:AuthnStatement AuthnInstant="2016-11-28T09:49:45.815Z" SessionIndex="1f5192...b591"> 
     <saml2:AuthnContext> 
     <saml2:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:Password</saml2:AuthnContextClassRef> 
     </saml2:AuthnContext> 
    </saml2:AuthnStatement> 
    <saml2:AttributeStatement /> 
</saml2:Assertion> 

編碼爲base64-URL編碼的XML:

PHNhbWwyO...dGlvbj4,

使用來自另一複製的方法SO張貼

function base64_url_encode($input) { 
return strtr(base64_encode($input), '+/=', '-_,'); 
} 

這是關於長20行(是的,最後有一個逗號)。

它不工作,我得到的JSON結果

{"error":"invalid_grant","error_description":"Provided Authorization Grant is invalid."} 

在痕跡:

TID: [0] [AM] [2016-11-29 11:04:50,297] DEBUG {org.wso2.carbon.identity.oauth2.token.handlers.grant.saml.SAML2BearerGrantHandler} - SAML Token Issuer verification failed or Issuer not registered {org.wso2.carbon.identity.oauth2.token.handlers.grant.saml.SAML2BearerGrantHandler} 
TID: [0] [AM] [2016-11-29 11:04:50,298] DEBUG {org.wso2.carbon.identity.oauth2.token.AccessTokenIssuer} - Invalid Grant provided by the client, id=fkJa...Ohoa, user-name=null to application=myapp-subscriber_test_PRODUCTION {org.wso2.carbon.identity.oauth2.token.AccessTokenIssuer} 
TID: [0] [AM] [2016-11-29 11:04:50,300] DEBUG {org.wso2.carbon.identity.oauth2.token.AccessTokenIssuer} - OAuth-Error-Code=invalid_grant client-id=fkJa...hoa grant-type=urn:ietf:params:oauth:grant-type:saml2-bearer scope=PRODUCTION {org.wso2.carbon.identity.oauth2.token.AccessTokenIssuer} 

我問周圍的系統配置程序。對我來說,IDS,APIM和OAuth之間的鏈接缺少某些聲明。 (我用google搜索了這個,並提出了一個stackoverflow交換來檢查發佈者id在斷言,我做了,但我不能在那裏發現任何錯誤)

感謝您的幫助,我會回來如果我有什麼新東西。當然,如果我忽略了一些明顯的東西!

+0

可能與http://stackoverflow.com/questions/19866021/how-to-get-saml2-bearer-assertion-profile-for-oauth-within-wso2-api-manager-to-w?rq = 1 – JRobinss

+0

可能與http://stackoverflow.com/questions/34738716/wso2-saml2bearergranthandler-saml-token-issuer-verification-failed-or-issuer-not – JRobinss

回答

1

我已經成功了,所以這裏有一些跡象表明可能有同樣問題的其他人。

  1. 我砍死simpleSamlPhp,因爲它們不提供SAML2斷言(見https://github.com/simplesamlphp/simplesamlphp/issues/220)。我沒有以很好的方式去做,因爲我只是想讓它工作。這是醜陋的,但它的工作原理。這是我砍,我把它放在SAML2-acs.php 125線:

    // ADDED to get SAML2 assertion in SP 
    if (array_key_exists('SAMLResponse', $_POST)) { 
        $attributes["samlresp"] = $_POST['SAMLResponse']; 
    } 
    
  2. 在我的PHP應用程序,然後我用這個代碼:

    $authdata_array= $as->getAuthDataArray(); 
    $postSaml2Assert = $authdata_array["Attributes"]["samlresp"]; 
    $msg = base64_decode($postSaml2Assert); 
    $document = new DOMDocument(); 
    $document->loadXML($msg); 
    $assertion = $document->getElementsByTagNameNS ("urn:oasis:names:tc:SAML:2.0:assertion", "Assertion")->item(0); 
    $saml2AssertionXml = $document->saveXML($assertion); 
    
  3. 我編碼斷言Base64和URL編碼。爲此,我使用代碼Passing base64 encoded strings in URL(感謝joeshmo!)

  4. APIM /實體提供商/列表/ myIdS /編輯/聯合驗證器/ SAM2的Web SSO ... /身份提供實體ID我將值設置爲預期的IDS端點,https://myids:9443/samlsso。 這解決了我的初始發行

  5. 在SP配置

    (在IDS)我加OAuth令牌既是觀衆和收件人,看起來像https://myAPIM:9443/oauth2/token/。注意,既作爲觀衆收件人!

  6. 因爲我在調試,我已經格式化了XML斷言,所以它沒有工作,我遇到了這裏詳述的問題WSO2 Identity Server OAuth2 Bearer SAML Assertion,所以我只是刪除格式並按原樣使用XML字符串。 (我當然從上面刪除這個錯誤)

  7. 我有一個最後的錯誤很容易解決:在NotOnOrAfter國旗製造麻煩,因爲當時獲得SAML2斷言,並用它來生成之間的延遲令牌。獲得的經驗:SAML2斷言應該在登錄後立即創建,而不是在調用API之前創建。

希望這可以幫助別人。感謝@Bhathiya的幫助!

2

當您使用SAML SSO登錄,你得到它的Assertion一個SAML響應。您可以看到一個示例回覆/斷言here。您需要此斷言才能獲得OAuth2令牌。但是,它看起來像你的PHP框架沒有直接向你提供SAML響應。這太糟糕了,因爲你在這裏需要它。

無論如何,既然你已經設法獲得了,那麼你現在要做的就是base64-URL對整個<Assertion>....</Assertion>標籤進行編碼並用OAuth2令牌請求發送。 Here is a nice online tool如果您需要手動嘗試。

+0

感謝您的快速響應。我會嘗試將我添加到SSP的攻擊與我得到的斷言結合起來。 – JRobinss

+0

我遵循你的建議:我不是在尋找一個單值標記,而是在XML中使用了整個斷言,並將其編碼爲base64-url-encode,並將其作爲'assertion = ...'傳遞給它。「沒有工作,有沒有我做錯了,或者我只是在APIM中有一些不好的配置?(請參閱原始問題中的示例) – JRobinss

+0

您是否在日誌中獲得任何內容? – Bee