Skip to content

Commit 750e25d

Browse files
committed
Auth gets reported in the logs using the child class name.
Fix typo 'your'
1 parent b9a01ed commit 750e25d

File tree

4 files changed

+64
-66
lines changed

4 files changed

+64
-66
lines changed

FunctionalTests/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static void Main()
3131
// Test BasicAuth
3232
if (!string.IsNullOrEmpty(Config["basicAuth:Password"]))
3333
{
34-
if (!Config["osmApiUrl"].Contains("dev")) throw new Exception("These tests modify data, and it looks like your running them in PROD, please don't");
34+
if (!Config["osmApiUrl"].Contains("dev")) throw new Exception("These tests modify data, and it looks like you're running them in PROD, please don't");
3535

3636
testsLogger.LogInformation("Testing BasicAuth client");
3737
var basicAuth = clientFactory.CreateBasicAuthClient(Config["basicAuth:User"], Config["basicAuth:Password"]);

src/BasicAuthClient.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,29 @@
55

66
namespace OsmSharp.IO.API
77
{
8-
/// <summary>
9-
/// Use of basic auth is discouraged. Use OAuth when practical.
10-
/// </summary>
11-
public class BasicAuthClient : AuthClient
12-
{
13-
private readonly string Username;
14-
private readonly string Password;
8+
/// <summary>
9+
/// Use of basic auth is discouraged. Use OAuth when practical.
10+
/// </summary>
11+
public class BasicAuthClient : AuthClient
12+
{
13+
private readonly string Username;
14+
private readonly string Password;
1515

16-
public BasicAuthClient(HttpClient httpClient,
16+
public BasicAuthClient(HttpClient httpClient,
1717
ILogger logger,
1818
string baseAddress,
1919
string username,
2020
string password)
21-
: base (baseAddress, httpClient, logger)
22-
{
23-
Username = username;
24-
Password = password;
25-
}
21+
: base (baseAddress, httpClient, logger)
22+
{
23+
Username = username;
24+
Password = password;
25+
}
2626

27-
protected override string AddAuthentication(HttpRequestMessage request, string url, string method = "GET")
28-
{
29-
var auth = Convert.ToBase64String(Encoding.ASCII.GetBytes(Username + ":" + Password));
27+
protected override void AddAuthentication(HttpRequestMessage request, string url, string method = "GET")
28+
{
29+
var auth = Convert.ToBase64String(Encoding.ASCII.GetBytes(Username + ":" + Password));
3030
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", auth);
31-
return "Basic authentication";
32-
}
33-
}
31+
}
32+
}
3433
}

src/NonAuthClient.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ public async Task<Note> CreateNote(double latitude, double longitude, string tex
659659
}
660660
#endregion
661661

662-
protected async Task<IEnumerable<T>> GetOfType<T>(string address, Func<HttpRequestMessage, string> auth = null) where T : class
662+
protected async Task<IEnumerable<T>> GetOfType<T>(string address, Action<HttpRequestMessage> auth = null) where T : class
663663
{
664664
var content = await Get(address, auth);
665665
var streamSource = new XmlOsmStreamSource(await content.ReadAsStreamAsync());
@@ -694,7 +694,7 @@ protected string ToString(double number)
694694
#region Http
695695
protected static readonly Func<string, string> Encode = HttpUtility.UrlEncode;
696696

697-
protected async Task<T> Get<T>(string address, Func<HttpRequestMessage, string> auth = null) where T : class
697+
protected async Task<T> Get<T>(string address, Action<HttpRequestMessage> auth = null) where T : class
698698
{
699699
var content = await Get(address, auth);
700700
var stream = await content.ReadAsStreamAsync();
@@ -703,13 +703,13 @@ protected async Task<T> Get<T>(string address, Func<HttpRequestMessage, string>
703703
return element;
704704
}
705705

706-
protected async Task<HttpContent> Get(string address, Func<HttpRequestMessage, string> auth = null)
706+
protected async Task<HttpContent> Get(string address, Action<HttpRequestMessage> auth = null)
707707
{
708708
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, address))
709709
{
710-
var authString = auth?.Invoke(request);
710+
auth?.Invoke(request);
711711
var response = await _httpClient.SendAsync(request);
712-
await VerifyAndLogReponse(response, $"GET: {address} {authString}");
712+
await VerifyAndLogReponse(response, $"{GetType().Name} GET: {address}");
713713
return response.Content;
714714
}
715715
}
@@ -738,16 +738,16 @@ protected async Task<T> Put<T>(string address, HttpContent requestContent = null
738738
/// <param name="message"></param>
739739
/// <param name="url"></param>
740740
/// <param name="method"></param>
741-
protected virtual string AddAuthentication(HttpRequestMessage message, string url, string method = "GET") { return "No authentication"; }
741+
protected virtual void AddAuthentication(HttpRequestMessage message, string url, string method = "GET") { }
742742

743743
protected async Task<HttpContent> SendAuthRequest(HttpMethod method, string address, HttpContent requestContent)
744744
{
745745
using (HttpRequestMessage request = new HttpRequestMessage(method, address))
746746
{
747-
var authString = AddAuthentication(request, address, method.ToString());
747+
AddAuthentication(request, address, method.ToString());
748748
request.Content = requestContent;
749749
var response = await _httpClient.SendAsync(request);
750-
await VerifyAndLogReponse(response, $"{method}: {address} {authString}");
750+
await VerifyAndLogReponse(response, $"{GetType().Name} {method}: {address}");
751751
return response.Content;
752752
}
753753
}

src/OAuthClient.cs

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,50 @@
44

55
namespace OsmSharp.IO.API
66
{
7-
public class OAuthClient : AuthClient
8-
{
9-
/// <summary>
10-
/// The OSM consumer key that was generated from OSM site
11-
/// </summary>
12-
private readonly string ConsumerKey;
13-
/// <summary>
14-
/// The OSM consumer secret that was generated from OSM site
15-
/// </summary>
16-
private readonly string ConsumerSecret;
17-
private readonly string Token;
18-
private readonly string TokenSecret;
7+
public class OAuthClient : AuthClient
8+
{
9+
/// <summary>
10+
/// The OSM consumer key that was generated from OSM site
11+
/// </summary>
12+
private readonly string ConsumerKey;
13+
/// <summary>
14+
/// The OSM consumer secret that was generated from OSM site
15+
/// </summary>
16+
private readonly string ConsumerSecret;
17+
private readonly string Token;
18+
private readonly string TokenSecret;
1919

20-
public OAuthClient(HttpClient httpClient,
20+
public OAuthClient(HttpClient httpClient,
2121
ILogger logger,
2222
string baseAddress,
2323
string consumerKey,
2424
string consumerSecret,
2525
string token,
2626
string tokenSecret)
27-
: base (baseAddress, httpClient, logger)
28-
{
29-
ConsumerKey = consumerKey;
30-
ConsumerSecret = consumerSecret;
31-
Token = token;
32-
TokenSecret = tokenSecret;
33-
}
27+
: base (baseAddress, httpClient, logger)
28+
{
29+
ConsumerKey = consumerKey;
30+
ConsumerSecret = consumerSecret;
31+
Token = token;
32+
TokenSecret = tokenSecret;
33+
}
3434

35-
protected override string AddAuthentication(HttpRequestMessage message, string url, string method = "GET")
36-
{
37-
var request = new OAuth.OAuthRequest
38-
{
39-
ConsumerKey = ConsumerKey,
40-
ConsumerSecret = ConsumerSecret,
41-
Token = Token,
42-
TokenSecret = TokenSecret,
43-
Type = OAuth.OAuthRequestType.ProtectedResource,
44-
SignatureMethod = OAuth.OAuthSignatureMethod.HmacSha1,
45-
RequestUrl = url,
46-
Version = "1.0",
47-
Method = method
48-
};
49-
var auth = request.GetAuthorizationHeader().Replace("OAuth ", "");
35+
protected override void AddAuthentication(HttpRequestMessage message, string url, string method = "GET")
36+
{
37+
var request = new OAuth.OAuthRequest
38+
{
39+
ConsumerKey = ConsumerKey,
40+
ConsumerSecret = ConsumerSecret,
41+
Token = Token,
42+
TokenSecret = TokenSecret,
43+
Type = OAuth.OAuthRequestType.ProtectedResource,
44+
SignatureMethod = OAuth.OAuthSignatureMethod.HmacSha1,
45+
RequestUrl = url,
46+
Version = "1.0",
47+
Method = method
48+
};
49+
var auth = request.GetAuthorizationHeader().Replace("OAuth ", "");
5050
message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("OAuth", auth);
51-
return "OAuth authentication";
52-
}
53-
}
51+
}
52+
}
5453
}

0 commit comments

Comments
 (0)