wso2 - Exchange SAML Token for OAuth Token, -
i trying exchange saml token oauth token,
i using code thing,
defaultbootstrap.bootstrap(); string responsemessage = (string) request.getparameter("samlresponse"); byte[] decoded = base64.decode(responsemessage); bytearrayinputstream = 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; // saml2 assertion part response stringwriter rspwrt = new stringwriter(); xmlhelper.writenode(responseobj.getassertions().get(0).getdom(), rspwrt); string requestmessage = rspwrt.tostring(); // base64 encoded string of message // prepared send on http protocol string encodedrequestmessage = base64.encodebytes(requestmessage.getbytes(), base64.dont_break_lines); string enc_rslt = urlencoder.encode(encodedrequestmessage, "utf-8").trim(); //create connection 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 consumer-key , 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);
but when visit application page after authentication is, got error in apim console.
[2014-03-23 02:06:14,456] error - oauthcallbackmanager error while calling oauthauthorization handler. java.lang.nullpointerexception @ org.wso2.carbon.apimgt.keymgt.util.apimanageroauthcallbackhandler.handle(apimanageroauthcallbackhandler.java:71) @ org.wso2.carbon.identity.oauth.callback.oauthcallbackmanager.handlecallback(oauthcallbackmanager.java:42) @ org.wso2.carbon.identity.oauth2.token.handlers.grant.abstractauthorizationgranthandler.validatescope(abstractauthorizationgranthandler.java:309) @ org.wso2.carbon.identity.oauth2.token.accesstokenissuer.issue(accesstokenissuer.java:157) @ org.wso2.carbon.identity.oauth2.oauth2service.issueaccesstoken(oauth2service.java:172) @ org.wso2.carbon.identity.oauth.endpoint.token.oauth2tokenendpoint.getaccesstoken(oauth2tokenendpoint.java:227) @ org.wso2.carbon.identity.oauth.endpoint.token.oauth2tokenendpoint.issueaccesstoken(oauth2tokenendpoint.java:108)
can solve issue?
as reference, please see: https://docs.wso2.org/display/am160/token+api
isabelle.
Comments
Post a Comment