| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- unit UnDemo;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls, xmldom, XMLIntf, msxmldom, XMLDoc, SOAPHTTPTrans,
- IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, JITComVCTKLib_TLB, ComObj;
- type
- SHeadInfo = Record
- Version:String;
- ServiceType:String;
- AuthResult:String;
- ErrorCode:String;
- ErrorDesc:String;
- end;
- SBodyInfo = Record
- AuthMode:String;
- AuthResult:String;
- AccessControlResult:String;
- AttrList:TStringList;
- end;
-
- TMainForm = class(TForm)
- Label1: TLabel;
- Label2: TLabel;
- edtServerIP: TEdit;
- edtServerPort: TEdit;
- Label3: TLabel;
- edtAppFlag: TEdit;
- Label4: TLabel;
- edtIsuerDN: TEdit;
- memAuthResult: TMemo;
- btnAuth: TButton;
- btnClose: TButton;
- XMLDoc: TXMLDocument;
- IdHTTP1: TIdHTTP;
- Label5: TLabel;
- edtClientIP: TEdit;
- procedure btnCloseClick(Sender: TObject);
- procedure btnAuthClick(Sender: TObject);
- private
- { Private declarations }
- function GetNodeAttributeValue(xmlNode:IXMLNode; strAttribute:String):String;
- procedure ParseHeadNode(HeadNode:IXMLNode; var HeadInfo: SHeadInfo);
- procedure ParseRootNode(RootNode:IXMLNode; var HeadInfo:SHeadInfo; var BodyInfo:SBodyInfo);
- procedure ParseBodyNode(BodyNode:IXMLNode; var BodyInfo: SBodyInfo);
- function GenRandom(strAppFlag, IP, Port:String):String;
- public
- { Public declarations }
- end;
- var
- MainForm: TMainForm;
- implementation
- {$R *.dfm}
- procedure TMainForm.btnCloseClick(Sender: TObject);
- begin
- Close;
- end;
- function TMainForm.GetNodeAttributeValue(xmlNode:IXMLNode; strAttribute:String):String;
- begin
- // xmlNode.Get
- end;
- procedure TMainForm.ParseHeadNode(HeadNode:IXMLNode; var HeadInfo: SHeadInfo);
- var
- ChildNodes:IXMLNodeList;
- ChildNode:IXMLNode;
- I:Integer;
- tempValue:String;
- begin
- ChildNodes:=HeadNode.GetChildNodes();
- for I:=0 to ChildNodes.Count-1 do begin
- ChildNode:=ChildNodes.Get(i);
- tempValue:=ChildNode.Text;
- if lowercase(ChildNode.NodeName)='version' then begin
- HeadInfo.Version := tempValue;
- end else if lowercase(ChildNode.NodeName)='servicetype' then begin
- HeadInfo.ServiceType := tempValue;
- end else if lowercase(ChildNode.NodeName)='messagestate' then begin
- HeadInfo.AuthResult := tempValue;
- end else if lowercase(ChildNode.NodeName)='messagecode' then begin
- HeadInfo.ErrorCode := tempValue;
- end else if lowercase(ChildNode.NodeName)='messagedesc' then begin
- HeadInfo.ErrorDesc := tempValue;
- end;
- end;
-
- end;
- procedure TMainForm.ParseRootNode(RootNode:IXMLNode; var HeadInfo:SHeadInfo; var BodyInfo:SBodyInfo);
- var
- ChildNodes:IXMLNodeList;
- ChildNode:IXMLNode;
- I:Integer;
- begin
- ChildNodes:=RootNode.GetChildNodes();
- for I:=0 to ChildNodes.Count-1 do begin
- ChildNode:=ChildNodes.Get(I);
- if UpperCase(ChildNode.NodeName)='HEAD' then begin
- ParseHeadNode(ChildNode, HeadInfo);
- end else if UpperCase(ChildNode.NodeName)='BODY' then begin
- ParseBodyNode(ChildNode, BodyInfo);
- end;
- end;
-
- end;
- procedure TMainForm.ParseBodyNode(BodyNode:IXMLNode; var BodyInfo: SBodyInfo);
- var
- ChildNodes:IXMLNodeList;
- ChildNode, AttribChildNode:IXMLNode;
- I,J:Integer;
- AuthChildNodes:IXMLNodeList;
- AttrName, AttrNameSpace, AttrValue:String;
- begin
- ChildNodes:=BodyNode.GetChildNodes();
- for I:=0 to ChildNodes.Count-1 do begin
- ChildNode:=ChildNodes.Get(I);
- if lowercase(ChildNode.NodeName)='authresultset' then begin
- AuthChildNodes:=ChildNode.ChildNodes;
- for J:=0 to AuthChildNodes.Count-1 do begin
- AttribChildNode:=AuthChildNodes.Get(J);
- if lowercase(AttribChildNode.NodeName)='authresult' then begin
- if AttribChildNode.HasAttribute('authMode') then BodyInfo.AuthMode:=AttribChildNode.Attributes['authMode'];
- if AttribChildNode.HasAttribute('success') then BodyInfo.AuthResult:=AttribChildNode.Attributes['success'];
- end;
- end;
- end else if lowercase(ChildNode.NodeName)='accesscontrolresult' then begin
- BodyInfo.AccessControlResult:=ChildNode.Text;
- end else if lowercase(ChildNode.NodeName)='attributes' then begin
- AuthChildNodes:=ChildNode.ChildNodes;
- BodyInfo.AttrList:=TStringList.Create;
- for J:=0 to AuthChildNodes.Count-1 do begin
- AttribChildNode:=AuthChildNodes.Get(J);
- if lowercase(AttribChildNode.NodeName)='attr' then begin
- if AttribChildNode.HasAttribute('name') then AttrName:=AttribChildNode.Attributes['name'];
- if AttribChildNode.HasAttribute('namespace') then AttrNameSpace:=AttribChildNode.Attributes['namespace'];
- AttrValue:=AttribChildNode.NodeValue;
- BodyInfo.AttrList.Add('属性名称:'+ AttrName);
- BodyInfo.AttrList.Add('名字空间:' + AttrNameSpace);
- BodyInfo.AttrList.Add('属性值:' + AttrValue);
- end;
- end;
- end;
- end;
- end;
- function TMainForm.GenRandom(strAppFlag, IP, Port:String):String;
- var
- ReponseData, HostAddr, PostMsg:String;
- StrStream:TStringStream;
- RootNode:IXMLNode;
- ChildNodes, BodyChildNode:IXMLNodeList;
- BodyNode, TempNode:IXMLNode;
- I,J:Integer;
-
- PostStream:TStringStream;
- begin
- Result:= '';
- PostMsg := '<?xml version="1.0" encoding="UTF-8"?>'
- + '<message>'
- + '<head>'
- + '<version>1.0</version>'
- + '<serviceType>OriginalService</serviceType>'
- + '</head>'
- + '<body>'
- + '<appId>' + strAppFlag + '</appId>'
- + '</body>'
- + '</message>';
- PostStream:=TStringStream.Create(PostMsg);
- HostAddr := 'http://' + IP + ':'+ Port + '/MessageService';
- try
- ReponseData:=IdHTTP1.Post(HostAddr, PostStream);
- except
- Exit;
- end;
- PostStream.Free;
- StrStream:=TStringStream.Create(ReponseData);
- XMLDoc.LoadFromStream(StrStream);
- RootNode := XMLDoc.DocumentElement;
- ChildNodes:=RootNode.ChildNodes;
- For i:=0 to ChildNodes.Count -1 do begin
- BodyNode := ChildNodes.Get(i);
- if UpperCase(BodyNode.NodeName) ='BODY' then begin
- BodyChildNode:=BodyNode.ChildNodes;
- if BodyChildNode.Count<>1 then begin
- EXIT;
- end;
- TempNode := BodyChildNode.Get(0);
- Result:= TempNode.NodeValue;
- end;
- end;
- StrStream.Free();
- end;
- procedure TMainForm.btnAuthClick(Sender: TObject);
- var
- JITVCTKObj:IJITVCTK;
- StrRandom, StrSignData:String;
- lErrorCode:Integer;
- ReponseData:String;
- StrStream:TStringStream;
- PostStream:TStringStream;
- RootNode:IXMLNode;
- HeadInfo:SHeadInfo;
- BodyInfo:SBodyInfo;
- I:Integer;
- begin
- //////////////////////////////////////////////////////////////////////////
- // 1. 生成认证请求报文
- //////////////////////////////////////////////////////////////////////////
- JITVCTKObj:=CoJITVCTK.Create;
- StrRandom:=GenRandom(edtAppFlag.Text, edtServerIP.Text, edtServerPort.Text);
- if StrRandom='' then Exit;
- lErrorCode:=JITVCTKObj.SetCert('SC', '', '', '', edtIsuerDN.Text, '');
- if lErrorCode<>0 then Exit;
- StrSignData:=JITVCTKObj.AttachSignStr('', StrRandom);
- if StrSignData='' then Exit;
-
- PostStream:=TStringStream.Create('');
- PostStream.WriteString('<?xml version="1.0" encoding="UTF-8"?>');
- PostStream.WriteString('<message>');
- PostStream.WriteString('<head>');
- PostStream.WriteString('<version>1.1</version>');
- PostStream.WriteString('<serviceType>AuthenService</serviceType>');
- PostStream.WriteString('</head>');
- PostStream.WriteString('<body>');
- PostStream.WriteString('<clientInfo>');
- PostStream.WriteString('<clientIP>'+ edtClientIP.Text +'</clientIP>');
- PostStream.WriteString('</clientInfo>');
- PostStream.WriteString('<appId>' + edtAppFlag.Text + '</appId>');
- PostStream.WriteString('<authen>');
- PostStream.WriteString('<authCredential authMode="cert">');
- PostStream.WriteString('<attach>' + StrSignData + '</attach>');
- PostStream.WriteString('</authCredential>');
- PostStream.WriteString('</authen>');
- PostStream.WriteString('<accessControl>true</accessControl>');
- PostStream.WriteString('<attributes attributeType="all">');
- {
- PostStream.WriteString('<attr name="X509Certificate.SubjectDN" namespace="http://www.jit.com.cn/cinas/ias/ns/saml/saml11/X.509"></attr>');
- PostStream.WriteString('<attr name="UMS.UserID" namespace="http://www.jit.com.cn/pmi/pms/ns/role"></attr>');
- PostStream.WriteString('<attr name="' + AnsiToUtf8('性别') + '" namespace="http://www.jit.com.cn/ums/ns/user"></attr>');
- PostStream.WriteString('<attr name="' + AnsiToUtf8('职务') + '" namespace="http://www.jit.com.cn/ums/ns/user"></attr>');
- PostStream.WriteString('<attr name="' + AnsiToUtf8('身份证') + '" namespace="http://www.jit.com.cn/ums/ns/user"></attr>');
- PostStream.WriteString('<attr name="' + AnsiToUtf8('部门') + '" namespace="http://www.jit.com.cn/ums/ns/user"></attr>');
- }
- PostStream.WriteString('</attributes>');
- PostStream.WriteString('</body>');
- PostStream.WriteString('</message>');
- //////////////////////////////////////////////////////////////////////////
- // 2. 发送认证请求报文
- //////////////////////////////////////////////////////////////////////////
- try
- ReponseData:=IdHttp1.Post('http://' + edtServerIP.Text + ':' + edtServerPort.Text + '/MessageService', PostStream);
- except
- Exit;
- end;
- PostStream.Free;
- /////////////////////////////////////////////////////////////////////////
- // 3. 解析服务器响应报文
- //////////////////////////////////////////////////////////////////////////
- StrStream:=TStringStream.Create(ReponseData);
- XMLDoc.LoadFromStream(StrStream);
- RootNode := XMLDoc.DocumentElement;
- ParseRootNode(RootNode, HeadInfo, BodyInfo);
- StrStream.Free;
- memAuthResult.Lines.Add('版本:'+ HeadInfo.Version);
- memAuthResult.Lines.Add('服务类型:' + HeadInfo.ServiceType);
- memAuthResult.Lines.Add('认证结果:' + HeadInfo.AuthResult);
- memAuthResult.Lines.Add('错误码:' + HeadInfo.ErrorCode);
- memAuthResult.Lines.Add('错误信息:' + HeadInfo.ErrorDesc);
- memAuthResult.Lines.Add('');
- memAuthResult.Lines.Add('认证模式:' + BodyInfo.AuthMode);
- memAuthResult.Lines.Add('认证结果:' + BodyInfo.AuthResult);
- memAuthResult.Lines.Add('访问控制:' + BodyInfo.AccessControlResult);
- if BodyInfo.AttrList<>nil then begin
- if BodyInfo.AttrList.Count>0 then memAuthResult.Lines.AddStrings(BodyInfo.AttrList);
- BodyInfo.AttrList.Free;
- end;
- memAuthResult.Lines.Add('============================================');
- end;
- end.
|