root/trunk/digitalme/launchers/csharp/Novell.IdentityModel.Selectors/DigitalMeSelector.cs

Revision 1900, 7.0 kB (checked in by jnorman, 2 years ago)

Code cleanup on c# launcher

Line 
1using System;
2using System.Text;
3
4using System.IO;
5using System.Diagnostics;
6
7
8namespace Novell.IdentityModel.Selectors
9{
10    public class DigitalMeSelector
11    {
12        private static string DM_TEST_ARG_STR = "--test";
13        private static string DM_GET_TOKEN_ARG_STR = "--gettoken ";
14        private static string DM_TRUSTED_ISSUER_URIS_ARG_STR = "--issuers";
15        private static string DM_RECIPIENT_ARG_STR = "--recipient";
16        private static string DM_REQUIRED_CLAIMS_ARG_STR = "--reqclaims";
17        private static string DM_OPTIONAL_CLAIMS_ARG_STR = "--optclaims";
18        private static string DM_TOKEN_TYPE_ARG_STR = "--tokentype";
19        private static string DM_PRIVACY_POLICY_FILE_ARG_STR = "--privfile";
20        private static string DM_CERT_FILE_ARG_STR = "--certfile";
21        private static string DM_TOKEN_OUTPUT_FILE_ARG_STR = "--tokenfile";
22        private static string DM_NON_SECURE_DESKTOP_ARG_STR = "--nonsecure ";
23        private static string DM_VERSION_ARG_STR = "--version";
24        private static string DM_ISSUER_POLICY_ARG_STR = "--issuerpolicy";
25
26        // this is Microsofts API for GetToken
27        // Does mono support classes such as SecurityTokenSerializer?
28        /*
29        public static void GetToken(XmlElement endpoint,
30                                     IEnumerable<XmlElement> policy,
31                                     XmlElment requiredRemoteTokenElement,
32                                     SecurityTokenSerializer tokenSerializer)
33        {
34        }
35        */
36
37        public static void Manage()
38        {
39            String sExePath = GetExePath();
40            if (null != sExePath)
41            {
42                LaunchSelector(sExePath, null);
43            }
44        }
45
46        public static void Import(String sFilePath)
47        {
48            String sExePath = GetExePath();
49            StringBuilder sbArgs = new StringBuilder();
50
51            sbArgs.Append("--import ");
52            sbArgs.Append(sFilePath);
53
54            if (null != sExePath)
55            {
56                LaunchSelector(sExePath, sbArgs);
57            }
58        }
59
60        public static byte[] GetToken(String sRelyingParty,
61                                    String[] saRequiredClaims,
62                                    String[] saOptionalClaims,
63                                    String sTokenType)
64        {
65            String sExePath = GetExePath();
66            if (null != sExePath)
67            {
68
69                StringBuilder sbArgs = new StringBuilder();
70                String sTempFileName = System.IO.Path.GetTempFileName();
71
72                // build argument string
73                sbArgs.Append(DM_GET_TOKEN_ARG_STR);
74                sbArgs.AppendFormat("{0}=\"{1}\" ", DM_TOKEN_OUTPUT_FILE_ARG_STR, sTempFileName);
75                sbArgs.AppendFormat("{0}=\"{1}\" ", DM_RECIPIENT_ARG_STR, sRelyingParty);
76
77                // required claims
78                if (saRequiredClaims != null)
79                {
80                    sbArgs.AppendFormat(" {0}=\"", DM_REQUIRED_CLAIMS_ARG_STR);
81                    foreach (string sClaim in saRequiredClaims)
82                    {
83                        sbArgs.Append(sClaim);
84                        sbArgs.Append(" ");
85                    }
86                    sbArgs.Append("\"");
87                }
88
89                // optional claims
90                if (saOptionalClaims != null)
91                {
92                    sbArgs.AppendFormat(" {0}=\"", DM_OPTIONAL_CLAIMS_ARG_STR);
93                    foreach (string sClaim in saOptionalClaims)
94                    {
95                        sbArgs.Append(sClaim);
96                        sbArgs.Append(" ");
97                    }
98                    sbArgs.Append("\"");
99                }
100
101                // tokentype
102                if (sTokenType != null)
103                {
104                    sbArgs.AppendFormat(" {0}=\"{1}\"", DM_TOKEN_TYPE_ARG_STR, sTokenType);
105                }
106
107                LaunchSelector(sExePath, sbArgs);
108
109                // read token file
110                FileStream fs = new FileStream(sTempFileName, FileMode.Open);
111                byte[] baToken = new byte[fs.Length];
112                fs.Read(baToken, 0, (int)fs.Length);
113
114                File.Delete(sTempFileName);
115
116                DbgLog("Token: " + System.Text.Encoding.ASCII.GetString(baToken));
117                return baToken;
118            }
119            else
120            {
121                DbgLog("DigitalMe application not found");
122            }
123            return null;
124        }
125
126        private static void LaunchSelector(String sExePath, StringBuilder sbArgs)
127        {
128            try
129            {
130                // launch selector
131                Process myProcess = null;
132                StreamReader myStreamReader = null;
133
134                myProcess = new Process();
135                ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(sExePath);
136
137                if (sbArgs != null)
138                {
139                    myProcessStartInfo.Arguments = sbArgs.ToString();
140                    DbgLog("Args:" + myProcessStartInfo.Arguments);
141                }
142
143                myProcessStartInfo.UseShellExecute = false;
144                myProcessStartInfo.RedirectStandardOutput = true;
145
146                myProcess.StartInfo = myProcessStartInfo;
147                myProcess.Start();
148                myProcess.WaitForExit();
149
150                myStreamReader = myProcess.StandardOutput;
151            }
152            catch (Exception e)
153            {
154                Console.WriteLine(e.ToString());
155                throw e;
156            }
157        }
158
159
160        private static String GetExePath()
161        {
162
163            int platform = (int)Environment.OSVersion.Platform;
164            String sUserhome = Environment.GetEnvironmentVariable("HOME");
165
166            String[] paths = {"/usr/local/lib/digitalme/bin/digitalme",
167                                "/usr/lib/digitalme/bin/digitalme",
168                                sUserhome + "/Desktop/DigitalMe.app/Contents/MacOS/DigitalMe",
169                                "/Applications/DigitalMe.app/Contents/MacOS/DigitalMe",
170                                "/Applications/Utilities/DigitalMe.app/Contents/MacOS"};
171
172
173            DbgLog("Platform: " + platform.ToString());
174            DbgLog("OSVersion: " + Environment.OSVersion.Version.ToString());
175
176            foreach (string path in paths)
177            {
178                if (File.Exists(path))
179                {
180                    DbgLog(path);
181                    return path;
182                }
183            }
184
185
186            if ((128 == platform) || (4 == platform))
187            {
188                return "/usr/local/lib/digitalme/bin/digitalme";
189            }
190            else
191            {
192                DbgLog("Platform: " + platform.ToString());
193            }
194
195            return null;
196        }
197
198        private static void DbgLog(String sMessage)
199        {
200#if DEBUG
201            Console.WriteLine(sMessage);
202#endif
203        }
204    }
205}
Note: See TracBrowser for help on using the browser.