-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathJazzFormAuthClient.cs
More file actions
218 lines (190 loc) · 7.06 KB
/
JazzFormAuthClient.cs
File metadata and controls
218 lines (190 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*******************************************************************************
* Copyright (c) 2013 IBM Corporation.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
*
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Steve Pitschke - initial API and implementation
*******************************************************************************/
using System.Net;
using System.Net.Http.Headers;
using Microsoft.Extensions.Logging;
using OSLC4Net.Client.Exceptions;
namespace OSLC4Net.Client.Oslc.Jazz;
public class JazzFormAuthClient : OslcClient
{
private String url;
private String authUrl;
private String project;
private String user;
private String password;
private const String JAZZ_AUTH_MESSAGE_HEADER = "X-com-ibm-team-repository-web-auth-msg";
private const String JAZZ_AUTH_FAILED = "authfailed";
private readonly ILogger _logger;
public JazzFormAuthClient(ILogger<OslcClient> logger) :
base(logger)
{
_logger = logger;
}
/// <summary>
/// Create a new Jazz Form Auth client for the given URL, user and password
/// </summary>
/// <param name="url">the URL of the Jazz server, including the web app context</param>
/// <param name="user"></param>
/// <param name="password"></param>
/// <param name="logger"></param>
public JazzFormAuthClient(String url, String user, String password, ILogger<OslcClient> logger) :
this(logger)
{
this.url = url;
this.authUrl = url; //default to base URL
this.user = user;
this.password = password;
}
/// <summary>
/// Create a new Jazz Form Auth client for the given URL, user and password
/// </summary>
/// <param name="url">the URL of the Jazz server, including the web app context</param>
/// <param name="authUrl">the base URL to use for authentication. This is normally the
/// application base URL for RQM and RTC and is the JTS application URL for fronting
/// applications like RRC and DM.</param>
/// <param name="user"></param>
/// <param name="password"></param>
/// <param name="logger"></param>
public JazzFormAuthClient(String url, String authUrl, String user, String password, ILogger<OslcClient> logger) :
this(url, user, password, logger)
{
this.authUrl = authUrl;
}
public String GetUrl()
{
return url;
}
public void SetUrl(String url)
{
this.url = url;
}
public String GetAuthUrl()
{
return authUrl;
}
public void SetAuthUrl(String authUrl)
{
this.authUrl = authUrl;
}
public String GetProject()
{
return project;
}
public void SetProject(String project)
{
this.project = project;
}
public String GetUser()
{
return user;
}
public void SetUser(String user)
{
this.user = user;
}
public String GetPassword()
{
return password;
}
public void SetPassword(String password)
{
this.password = password;
}
/// <summary>
/// Execute the sequence of HTTP requests to perform a form login to a Jazz server
/// </summary>
/// <returns>The HTTP status code of the final request to verify login is successful</returns>
public async Task<HttpStatusCode> FormLoginAsync()
{
HttpStatusCode statusCode = HttpStatusCode.Unused;
HttpResponseMessage resp;
try
{
resp = await GetHttpClient().GetAsync(authUrl + "/authenticated/identity").ConfigureAwait(false);
statusCode = resp.StatusCode;
string location;
if (statusCode == HttpStatusCode.Found)
{
location = resp.Headers.Location.AbsoluteUri;
resp.ConsumeContent();
statusCode = await FollowRedirectsAsync(statusCode, location).ConfigureAwait(false);
}
GetHttpClient().DefaultRequestHeaders.Clear();
GetHttpClient().DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
GetHttpClient().DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");
GetHttpClient().DefaultRequestHeaders.Add("OSLC-Core-Version", "2.0");
String securityCheckUrl = "j_username=" + this.user + "&j_password=" + this.password;
StringContent content = new StringContent(securityCheckUrl, System.Text.Encoding.UTF8);
MediaTypeHeaderValue mediaTypeValue = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
mediaTypeValue.CharSet = "utf-8";
content.Headers.ContentType = mediaTypeValue;
resp = await GetHttpClient().PostAsync(authUrl + "/j_security_check", content).ConfigureAwait(false);
statusCode = resp.StatusCode;
String jazzAuthMessage = null;
IEnumerable<string> values = new List<string>();
if (resp.Headers.TryGetValues(JAZZ_AUTH_MESSAGE_HEADER, out values))
{
jazzAuthMessage = values.Last();
}
if (jazzAuthMessage != null && String.Compare(jazzAuthMessage, JAZZ_AUTH_FAILED, StringComparison.OrdinalIgnoreCase) == 0)
{
resp.ConsumeContent();
throw new JazzAuthFailedException(this.user, this.url);
}
else if (statusCode != HttpStatusCode.OK && statusCode != HttpStatusCode.Found)
{
resp.ConsumeContent();
throw new JazzAuthErrorException(statusCode, this.url);
}
else //success
{
location = resp.Headers.Location.AbsoluteUri;
resp.ConsumeContent();
statusCode = await FollowRedirectsAsync(statusCode, location).ConfigureAwait(false);
}
}
catch (JazzAuthFailedException)
{
throw;
}
catch (JazzAuthErrorException)
{
throw;
}
catch (Exception e)
{
_logger.LogError(e, "Error during login");
}
return statusCode;
}
private async Task<HttpStatusCode> FollowRedirectsAsync(HttpStatusCode statusCode, String location)
{
while ((statusCode == HttpStatusCode.Found) && (location != null))
{
try
{
HttpResponseMessage newResp = await GetHttpClient().GetAsync(location).ConfigureAwait(false);
statusCode = newResp.StatusCode;
location = newResp.Headers.Location?.AbsoluteUri;
newResp.ConsumeContent();
}
catch (Exception e)
{
_logger.LogError(e, "Error following redirect");
}
}
return statusCode;
}
}