Revision 7964bdc9 trunk/Pithos.Client.WPF/PithosAccount.cs

b/trunk/Pithos.Client.WPF/PithosAccount.cs
1
#region
2
/* -----------------------------------------------------------------------
3
 * <copyright file="PithosAccount.cs" company="GRNet">
4
 * 
5
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or
8
 * without modification, are permitted provided that the following
9
 * conditions are met:
10
 *
11
 *   1. Redistributions of source code must retain the above
12
 *      copyright notice, this list of conditions and the following
13
 *      disclaimer.
14
 *
15
 *   2. Redistributions in binary form must reproduce the above
16
 *      copyright notice, this list of conditions and the following
17
 *      disclaimer in the documentation and/or other materials
18
 *      provided with the distribution.
19
 *
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
22
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
25
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
 * POSSIBILITY OF SUCH DAMAGE.
33
 *
34
 * The views and conclusions contained in the software and
35
 * documentation are those of the authors and should not be
36
 * interpreted as representing official policies, either expressed
37
 * or implied, of GRNET S.A.
38
 * </copyright>
39
 * -----------------------------------------------------------------------
40
 */
41
#endregion
42

  
43
// </copyright>
1
#region

2
/* -----------------------------------------------------------------------

3
 * <copyright file="PithosAccount.cs" company="GRNet">

4
 * 

5
 * Copyright 2011-2012 GRNET S.A. All rights reserved.

6
 *

7
 * Redistribution and use in source and binary forms, with or

8
 * without modification, are permitted provided that the following

9
 * conditions are met:

10
 *

11
 *   1. Redistributions of source code must retain the above

12
 *      copyright notice, this list of conditions and the following

13
 *      disclaimer.

14
 *

15
 *   2. Redistributions in binary form must reproduce the above

16
 *      copyright notice, this list of conditions and the following

17
 *      disclaimer in the documentation and/or other materials

18
 *      provided with the distribution.

19
 *

20
 *

21
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS

22
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED

23
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR

24
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR

25
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,

26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT

27
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF

28
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED

29
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT

30
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN

31
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE

32
 * POSSIBILITY OF SUCH DAMAGE.

33
 *

34
 * The views and conclusions contained in the software and

35
 * documentation are those of the authors and should not be

36
 * interpreted as representing official policies, either expressed

37
 * or implied, of GRNET S.A.

38
 * </copyright>

39
 * -----------------------------------------------------------------------

40
 */

41
#endregion

42

  
43
// </copyright>

44 44
// -----------------------------------------------------------------------
45 45

  
46
using System.Reflection;
47
using Pithos.Client.WPF.Preferences;
46
using System.Reflection;

47
using Pithos.Client.WPF.Preferences;

48 48
using Pithos.Client.WPF.Properties;
49 49
using Pithos.Interfaces;
50
using Pithos.Network;
51
using log4net;
52

  
53
namespace Pithos.Client.WPF
54
{
50
using Pithos.Network;

51
using log4net;

52

  
53
namespace Pithos.Client.WPF

54
{

55 55
    using System;
56 56
    using System.Linq;
57 57
    using System.Net;
58
    using System.Diagnostics.Contracts;
59
    using System.Diagnostics;
60
    using System.Net.Sockets;
61

  
62
    /// <summary>
63
    /// TODO: Update summary.
64
    /// </summary>
65
    /// <summary>
66
    /// Retrieves an account name and token from PITHOS
67
    /// </summary>
68
    public static class PithosAccount
69
    {
70
        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
71

  
72
        /// <summary>
73
        /// Asynchronously retrieves PITHOS credentials
74
        /// </summary>
75
        /// <param name="loginUrl">URL to retrieve the account info from PITHOS. Must end with =</param>
76
        /// <returns>The credentials wrapped in a Task</returns>
77
        public static NetworkCredential RetrieveCredentials(string loginUrl,string accountName=null)
78
        {
79
            Contract.Requires(Uri.IsWellFormedUriString(loginUrl, UriKind.Absolute));
80

  
81
            if (!Uri.IsWellFormedUriString(loginUrl, UriKind.Absolute))
82
                throw new ArgumentException("The pithos site parameter must be a valid absolute URL", "loginUrl");
83
            
84
            //int port = GetFreePort();
85
            
86
            //TODO:Force logout here to take care of existing cookies
87

  
88

  
89
            //var listenerUrl = String.Format("http://127.0.0.1:{0}", port);
90
            var listenerUrl = "pithos://127.0.0.1";
91

  
92
            
93

  
94
            //var receiveCredentials = ListenForRedirectAsync(port);
95

  
96
            //var logoutUrl = loginUrl.Replace("login", "im/logout");
97
            //var logoutProcess=Process.Start(logoutUrl);            
98
            //TaskEx.Delay(2000).Wait();
99
            var uriBuilder = new UriBuilder(loginUrl)
100
                                 {
101
                                     Query = String.Format("next={0}&force=", listenerUrl)
102
                                 };
103

  
104
            var retrieveUri = uriBuilder.Uri;
105

  
106
            var logoutUrl= (from server in Settings.Default.Servers
107
                           where server.LoginUri == loginUrl
108
                               select new Uri(server.LogoutUri)).FirstOrDefault();
109

  
110

  
111

  
112
            var browser = new LoginView(retrieveUri,logoutUrl,accountName);            
113
            if (true == browser.ShowDialog())
114
            {
115
                return new NetworkCredential(browser.Account, browser.Token);
116
            }
117
            return null;
118

  
119
/*
120

  
121
            Log.InfoFormat("[RETRIEVE] Open Browser at {0}", retrieveUri);
122
            Process.Start(retrieveUri.ToString());
123

  
124
            return receiveCredentials;
125
*/
126
        }
127

  
58
    using System.Diagnostics.Contracts;

59
    using System.Diagnostics;

60
    using System.Net.Sockets;

61

  
62
    /// <summary>

63
    /// TODO: Update summary.

64
    /// </summary>

65
    /// <summary>

66
    /// Retrieves an account name and token from PITHOS

67
    /// </summary>

68
    public static class PithosAccount

69
    {

70
        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

71

  
72
        /// <summary>

73
        /// Asynchronously retrieves PITHOS credentials

74
        /// </summary>

75
        /// <param name="loginUrl">URL to retrieve the account info from PITHOS. Must end with =</param>

76
        /// <returns>The credentials wrapped in a Task</returns>

77
        public static NetworkCredential RetrieveCredentials(string loginUrl,string accountName=null)

78
        {

79
            Contract.Requires(Uri.IsWellFormedUriString(loginUrl, UriKind.Absolute));

80

  
81
            if (!Uri.IsWellFormedUriString(loginUrl, UriKind.Absolute))

82
                throw new ArgumentException("The pithos site parameter must be a valid absolute URL", "loginUrl");

83
            
84
            //int port = GetFreePort();

85
            
86
            //TODO:Force logout here to take care of existing cookies

87

  
88

  
89
            //var listenerUrl = String.Format("http://127.0.0.1:{0}", port);

90
            var listenerUrl = "pithos://127.0.0.1";

91

  
92
            
93

  
94
            //var receiveCredentials = ListenForRedirectAsync(port);

95

  
96
            //var logoutUrl = loginUrl.Replace("login", "im/logout");

97
            //var logoutProcess=Process.Start(logoutUrl);            

98
            //TaskEx.Delay(2000).Wait();

99
            var uriBuilder = new UriBuilder(loginUrl)

100
                                 {

101
                                     Query = String.Format("next={0}&force=", listenerUrl)

102
                                 };

103

  
104
            var retrieveUri = uriBuilder.Uri;

105

  
106
            var logoutUrl= (from server in Settings.Default.Servers

107
                           where server.LoginUri == loginUrl

108
                               select new Uri(server.LogoutUri)).FirstOrDefault();

109

  
110

  
111

  
112
            var browser = new LoginView(retrieveUri,logoutUrl,accountName);            

113
            if (true == browser.ShowDialog())

114
            {

115
                return new NetworkCredential(browser.Account, browser.Token);

116
            }

117
            return null;

118

  
119
/*

120

  
121
            Log.InfoFormat("[RETRIEVE] Open Browser at {0}", retrieveUri);

122
            Process.Start(retrieveUri.ToString());

123

  
124
            return receiveCredentials;

125
*/

126
        }

127

  
128 128
        public static Uri GetLoginUri(string serverUrl)
129 129
        {
130 130
            return GetLoginUri(new Uri(serverUrl));
......
132 132

  
133 133
        public static Uri GetLoginUri(Uri serverUri)
134 134
        {
135
            var loginUri = serverUri.Combine("im/login");
135
            var loginUri = serverUri.Combine("/login");
136
            return loginUri;
136 137
        }
137 138

  
138 139

  
139
    }
140
}
140
    }
141
}

Also available in: Unified diff