为OAuth令牌交换SAML令牌,

时间:2014-03-22 20:49:15

标签: oauth wso2 saml wso2is

我正在尝试为OAuth令牌交换SAML令牌,

SAML SSO Config

Trusted IDP Config

OAuth Config

我正在使用此代码来做这件事,

DefaultBootstrap.bootstrap();

        String responseMessage = (String) request.getParameter("SAMLResponse");
        byte[] decoded = Base64.decode(responseMessage);

        ByteArrayInputStream is = new ByteArrayInputStream(decoded);

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();

        Document document = docBuilder.parse(is);
        Element element = document.getDocumentElement();

        UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory();
        Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(element);
        XMLObject responseXmlObj = unmarshaller.unmarshall(element);

        Response responseObj = (Response) responseXmlObj;


        // Get the SAML2 Assertion part from the response
        StringWriter rspWrt = new StringWriter();
        XMLHelper.writeNode(responseObj.getAssertions().get(0).getDOM(), rspWrt);
        String requestMessage = rspWrt.toString();

        // Get the Base64 encoded string of the message
        // Then Get it prepared to send it over HTTP protocol
        String encodedRequestMessage = Base64.encodeBytes(requestMessage.getBytes(), Base64.DONT_BREAK_LINES);
        String enc_rslt = URLEncoder.encode(encodedRequestMessage, "UTF-8").trim();

        //Create connection to the Token endpoint of API manger
        URL url = new URL("https://localhost:9444/oauth2/token");

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

        String userCredentials = "xO6qReKiAaqCCfjmxIRLzh2ilAAa:oLrwuUnkR8xueDmZr1KYprYV1B4a";
        String basicAuth = "Basic " + new String(Base64.encodeBytes(userCredentials.getBytes()));
        basicAuth = basicAuth.replaceAll("\\r|\\n", "");

        // Set the consumer-key and Consumer-secret
        connection.setRequestProperty("Authorization", basicAuth);
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //Send request
        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.writeBytes("grant_type=urn:ietf:params:oauth:grant-type:saml2-bearer&assertion="+enc_rslt);
        wr.flush();
        wr.close();

        //Get Response
        InputStream iss = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(iss));

        String line;
        StringBuffer responseString = new StringBuffer();
        while((line = rd.readLine()) != null) {
            responseString.append(line);
            responseString.append('\r');
        }

        rd.close();
        System.out.println(responseString);

但是当我从IS身份验证后访问应用程序页面时,我在APIM控制台中收到此错误。

    [2014-03-23 02:06:14,456] ERROR - OAuthCallbackManager Error while calling OAuthAuthorization Handler. 
java.lang.NullPointerException
    at org.wso2.carbon.apimgt.keymgt.util.APIManagerOAuthCallbackHandler.handle(APIManagerOAuthCallbackHandler.java:71)
    at org.wso2.carbon.identity.oauth.callback.OAuthCallbackManager.handleCallback(OAuthCallbackManager.java:42)
    at org.wso2.carbon.identity.oauth2.token.handlers.grant.AbstractAuthorizationGrantHandler.validateScope(AbstractAuthorizationGrantHandler.java:309)
    at org.wso2.carbon.identity.oauth2.token.AccessTokenIssuer.issue(AccessTokenIssuer.java:157)
    at org.wso2.carbon.identity.oauth2.OAuth2Service.issueAccessToken(OAuth2Service.java:172)
    at org.wso2.carbon.identity.oauth.endpoint.token.OAuth2TokenEndpoint.getAccessToken(OAuth2TokenEndpoint.java:227)
    at org.wso2.carbon.identity.oauth.endpoint.token.OAuth2TokenEndpoint.issueAccessToken(OAuth2TokenEndpoint.java:108)

你能帮忙解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

作为参考,请参阅:https://docs.wso2.org/display/AM160/Token+API

伊莎贝尔。