UnDemo.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. unit UnDemo;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, StdCtrls, xmldom, XMLIntf, msxmldom, XMLDoc, SOAPHTTPTrans,
  6. IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, JITComVCTKLib_TLB, ComObj;
  7. type
  8. SHeadInfo = Record
  9. Version:String;
  10. ServiceType:String;
  11. AuthResult:String;
  12. ErrorCode:String;
  13. ErrorDesc:String;
  14. end;
  15. SBodyInfo = Record
  16. AuthMode:String;
  17. AuthResult:String;
  18. AccessControlResult:String;
  19. AttrList:TStringList;
  20. end;
  21. TMainForm = class(TForm)
  22. Label1: TLabel;
  23. Label2: TLabel;
  24. edtServerIP: TEdit;
  25. edtServerPort: TEdit;
  26. Label3: TLabel;
  27. edtAppFlag: TEdit;
  28. Label4: TLabel;
  29. edtIsuerDN: TEdit;
  30. memAuthResult: TMemo;
  31. btnAuth: TButton;
  32. btnClose: TButton;
  33. XMLDoc: TXMLDocument;
  34. IdHTTP1: TIdHTTP;
  35. Label5: TLabel;
  36. edtClientIP: TEdit;
  37. procedure btnCloseClick(Sender: TObject);
  38. procedure btnAuthClick(Sender: TObject);
  39. private
  40. { Private declarations }
  41. function GetNodeAttributeValue(xmlNode:IXMLNode; strAttribute:String):String;
  42. procedure ParseHeadNode(HeadNode:IXMLNode; var HeadInfo: SHeadInfo);
  43. procedure ParseRootNode(RootNode:IXMLNode; var HeadInfo:SHeadInfo; var BodyInfo:SBodyInfo);
  44. procedure ParseBodyNode(BodyNode:IXMLNode; var BodyInfo: SBodyInfo);
  45. function GenRandom(strAppFlag, IP, Port:String):String;
  46. public
  47. { Public declarations }
  48. end;
  49. var
  50. MainForm: TMainForm;
  51. implementation
  52. {$R *.dfm}
  53. procedure TMainForm.btnCloseClick(Sender: TObject);
  54. begin
  55. Close;
  56. end;
  57. function TMainForm.GetNodeAttributeValue(xmlNode:IXMLNode; strAttribute:String):String;
  58. begin
  59. // xmlNode.Get
  60. end;
  61. procedure TMainForm.ParseHeadNode(HeadNode:IXMLNode; var HeadInfo: SHeadInfo);
  62. var
  63. ChildNodes:IXMLNodeList;
  64. ChildNode:IXMLNode;
  65. I:Integer;
  66. tempValue:String;
  67. begin
  68. ChildNodes:=HeadNode.GetChildNodes();
  69. for I:=0 to ChildNodes.Count-1 do begin
  70. ChildNode:=ChildNodes.Get(i);
  71. tempValue:=ChildNode.Text;
  72. if lowercase(ChildNode.NodeName)='version' then begin
  73. HeadInfo.Version := tempValue;
  74. end else if lowercase(ChildNode.NodeName)='servicetype' then begin
  75. HeadInfo.ServiceType := tempValue;
  76. end else if lowercase(ChildNode.NodeName)='messagestate' then begin
  77. HeadInfo.AuthResult := tempValue;
  78. end else if lowercase(ChildNode.NodeName)='messagecode' then begin
  79. HeadInfo.ErrorCode := tempValue;
  80. end else if lowercase(ChildNode.NodeName)='messagedesc' then begin
  81. HeadInfo.ErrorDesc := tempValue;
  82. end;
  83. end;
  84. end;
  85. procedure TMainForm.ParseRootNode(RootNode:IXMLNode; var HeadInfo:SHeadInfo; var BodyInfo:SBodyInfo);
  86. var
  87. ChildNodes:IXMLNodeList;
  88. ChildNode:IXMLNode;
  89. I:Integer;
  90. begin
  91. ChildNodes:=RootNode.GetChildNodes();
  92. for I:=0 to ChildNodes.Count-1 do begin
  93. ChildNode:=ChildNodes.Get(I);
  94. if UpperCase(ChildNode.NodeName)='HEAD' then begin
  95. ParseHeadNode(ChildNode, HeadInfo);
  96. end else if UpperCase(ChildNode.NodeName)='BODY' then begin
  97. ParseBodyNode(ChildNode, BodyInfo);
  98. end;
  99. end;
  100. end;
  101. procedure TMainForm.ParseBodyNode(BodyNode:IXMLNode; var BodyInfo: SBodyInfo);
  102. var
  103. ChildNodes:IXMLNodeList;
  104. ChildNode, AttribChildNode:IXMLNode;
  105. I,J:Integer;
  106. AuthChildNodes:IXMLNodeList;
  107. AttrName, AttrNameSpace, AttrValue:String;
  108. begin
  109. ChildNodes:=BodyNode.GetChildNodes();
  110. for I:=0 to ChildNodes.Count-1 do begin
  111. ChildNode:=ChildNodes.Get(I);
  112. if lowercase(ChildNode.NodeName)='authresultset' then begin
  113. AuthChildNodes:=ChildNode.ChildNodes;
  114. for J:=0 to AuthChildNodes.Count-1 do begin
  115. AttribChildNode:=AuthChildNodes.Get(J);
  116. if lowercase(AttribChildNode.NodeName)='authresult' then begin
  117. if AttribChildNode.HasAttribute('authMode') then BodyInfo.AuthMode:=AttribChildNode.Attributes['authMode'];
  118. if AttribChildNode.HasAttribute('success') then BodyInfo.AuthResult:=AttribChildNode.Attributes['success'];
  119. end;
  120. end;
  121. end else if lowercase(ChildNode.NodeName)='accesscontrolresult' then begin
  122. BodyInfo.AccessControlResult:=ChildNode.Text;
  123. end else if lowercase(ChildNode.NodeName)='attributes' then begin
  124. AuthChildNodes:=ChildNode.ChildNodes;
  125. BodyInfo.AttrList:=TStringList.Create;
  126. for J:=0 to AuthChildNodes.Count-1 do begin
  127. AttribChildNode:=AuthChildNodes.Get(J);
  128. if lowercase(AttribChildNode.NodeName)='attr' then begin
  129. if AttribChildNode.HasAttribute('name') then AttrName:=AttribChildNode.Attributes['name'];
  130. if AttribChildNode.HasAttribute('namespace') then AttrNameSpace:=AttribChildNode.Attributes['namespace'];
  131. AttrValue:=AttribChildNode.NodeValue;
  132. BodyInfo.AttrList.Add('属性名称:'+ AttrName);
  133. BodyInfo.AttrList.Add('名字空间:' + AttrNameSpace);
  134. BodyInfo.AttrList.Add('属性值:' + AttrValue);
  135. end;
  136. end;
  137. end;
  138. end;
  139. end;
  140. function TMainForm.GenRandom(strAppFlag, IP, Port:String):String;
  141. var
  142. ReponseData, HostAddr, PostMsg:String;
  143. StrStream:TStringStream;
  144. RootNode:IXMLNode;
  145. ChildNodes, BodyChildNode:IXMLNodeList;
  146. BodyNode, TempNode:IXMLNode;
  147. I,J:Integer;
  148. PostStream:TStringStream;
  149. begin
  150. Result:= '';
  151. PostMsg := '<?xml version="1.0" encoding="UTF-8"?>'
  152. + '<message>'
  153. + '<head>'
  154. + '<version>1.0</version>'
  155. + '<serviceType>OriginalService</serviceType>'
  156. + '</head>'
  157. + '<body>'
  158. + '<appId>' + strAppFlag + '</appId>'
  159. + '</body>'
  160. + '</message>';
  161. PostStream:=TStringStream.Create(PostMsg);
  162. HostAddr := 'http://' + IP + ':'+ Port + '/MessageService';
  163. try
  164. ReponseData:=IdHTTP1.Post(HostAddr, PostStream);
  165. except
  166. Exit;
  167. end;
  168. PostStream.Free;
  169. StrStream:=TStringStream.Create(ReponseData);
  170. XMLDoc.LoadFromStream(StrStream);
  171. RootNode := XMLDoc.DocumentElement;
  172. ChildNodes:=RootNode.ChildNodes;
  173. For i:=0 to ChildNodes.Count -1 do begin
  174. BodyNode := ChildNodes.Get(i);
  175. if UpperCase(BodyNode.NodeName) ='BODY' then begin
  176. BodyChildNode:=BodyNode.ChildNodes;
  177. if BodyChildNode.Count<>1 then begin
  178. EXIT;
  179. end;
  180. TempNode := BodyChildNode.Get(0);
  181. Result:= TempNode.NodeValue;
  182. end;
  183. end;
  184. StrStream.Free();
  185. end;
  186. procedure TMainForm.btnAuthClick(Sender: TObject);
  187. var
  188. JITVCTKObj:IJITVCTK;
  189. StrRandom, StrSignData:String;
  190. lErrorCode:Integer;
  191. ReponseData:String;
  192. StrStream:TStringStream;
  193. PostStream:TStringStream;
  194. RootNode:IXMLNode;
  195. HeadInfo:SHeadInfo;
  196. BodyInfo:SBodyInfo;
  197. I:Integer;
  198. begin
  199. //////////////////////////////////////////////////////////////////////////
  200. // 1. 生成认证请求报文
  201. //////////////////////////////////////////////////////////////////////////
  202. JITVCTKObj:=CoJITVCTK.Create;
  203. StrRandom:=GenRandom(edtAppFlag.Text, edtServerIP.Text, edtServerPort.Text);
  204. if StrRandom='' then Exit;
  205. lErrorCode:=JITVCTKObj.SetCert('SC', '', '', '', edtIsuerDN.Text, '');
  206. if lErrorCode<>0 then Exit;
  207. StrSignData:=JITVCTKObj.AttachSignStr('', StrRandom);
  208. if StrSignData='' then Exit;
  209. PostStream:=TStringStream.Create('');
  210. PostStream.WriteString('<?xml version="1.0" encoding="UTF-8"?>');
  211. PostStream.WriteString('<message>');
  212. PostStream.WriteString('<head>');
  213. PostStream.WriteString('<version>1.1</version>');
  214. PostStream.WriteString('<serviceType>AuthenService</serviceType>');
  215. PostStream.WriteString('</head>');
  216. PostStream.WriteString('<body>');
  217. PostStream.WriteString('<clientInfo>');
  218. PostStream.WriteString('<clientIP>'+ edtClientIP.Text +'</clientIP>');
  219. PostStream.WriteString('</clientInfo>');
  220. PostStream.WriteString('<appId>' + edtAppFlag.Text + '</appId>');
  221. PostStream.WriteString('<authen>');
  222. PostStream.WriteString('<authCredential authMode="cert">');
  223. PostStream.WriteString('<attach>' + StrSignData + '</attach>');
  224. PostStream.WriteString('</authCredential>');
  225. PostStream.WriteString('</authen>');
  226. PostStream.WriteString('<accessControl>true</accessControl>');
  227. PostStream.WriteString('<attributes attributeType="all">');
  228. {
  229. PostStream.WriteString('<attr name="X509Certificate.SubjectDN" namespace="http://www.jit.com.cn/cinas/ias/ns/saml/saml11/X.509"></attr>');
  230. PostStream.WriteString('<attr name="UMS.UserID" namespace="http://www.jit.com.cn/pmi/pms/ns/role"></attr>');
  231. PostStream.WriteString('<attr name="' + AnsiToUtf8('性别') + '" namespace="http://www.jit.com.cn/ums/ns/user"></attr>');
  232. PostStream.WriteString('<attr name="' + AnsiToUtf8('职务') + '" namespace="http://www.jit.com.cn/ums/ns/user"></attr>');
  233. PostStream.WriteString('<attr name="' + AnsiToUtf8('身份证') + '" namespace="http://www.jit.com.cn/ums/ns/user"></attr>');
  234. PostStream.WriteString('<attr name="' + AnsiToUtf8('部门') + '" namespace="http://www.jit.com.cn/ums/ns/user"></attr>');
  235. }
  236. PostStream.WriteString('</attributes>');
  237. PostStream.WriteString('</body>');
  238. PostStream.WriteString('</message>');
  239. //////////////////////////////////////////////////////////////////////////
  240. // 2. 发送认证请求报文
  241. //////////////////////////////////////////////////////////////////////////
  242. try
  243. ReponseData:=IdHttp1.Post('http://' + edtServerIP.Text + ':' + edtServerPort.Text + '/MessageService', PostStream);
  244. except
  245. Exit;
  246. end;
  247. PostStream.Free;
  248. /////////////////////////////////////////////////////////////////////////
  249. // 3. 解析服务器响应报文
  250. //////////////////////////////////////////////////////////////////////////
  251. StrStream:=TStringStream.Create(ReponseData);
  252. XMLDoc.LoadFromStream(StrStream);
  253. RootNode := XMLDoc.DocumentElement;
  254. ParseRootNode(RootNode, HeadInfo, BodyInfo);
  255. StrStream.Free;
  256. memAuthResult.Lines.Add('版本:'+ HeadInfo.Version);
  257. memAuthResult.Lines.Add('服务类型:' + HeadInfo.ServiceType);
  258. memAuthResult.Lines.Add('认证结果:' + HeadInfo.AuthResult);
  259. memAuthResult.Lines.Add('错误码:' + HeadInfo.ErrorCode);
  260. memAuthResult.Lines.Add('错误信息:' + HeadInfo.ErrorDesc);
  261. memAuthResult.Lines.Add('');
  262. memAuthResult.Lines.Add('认证模式:' + BodyInfo.AuthMode);
  263. memAuthResult.Lines.Add('认证结果:' + BodyInfo.AuthResult);
  264. memAuthResult.Lines.Add('访问控制:' + BodyInfo.AccessControlResult);
  265. if BodyInfo.AttrList<>nil then begin
  266. if BodyInfo.AttrList.Count>0 then memAuthResult.Lines.AddStrings(BodyInfo.AttrList);
  267. BodyInfo.AttrList.Free;
  268. end;
  269. memAuthResult.Lines.Add('============================================');
  270. end;
  271. end.