Skip to content

Commit

Permalink
Refactoring & cleanup around AuthorizationHeader.
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuTurcotte committed Feb 22, 2012
1 parent 62f1ad9 commit b9d6172
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 29 deletions.
1 change: 1 addition & 0 deletions OAuth/Authenticator/RequestAuthenticator.cs
Expand Up @@ -36,3 +36,4 @@ public interface RequestAuthenticator
void SignRequest(WebRequest request);
}
}

28 changes: 14 additions & 14 deletions OAuth/Base/AuthorizationHeader.cs
Expand Up @@ -29,10 +29,10 @@ namespace OAuth.Base
internal class AuthorizationHeader
{
private readonly ClientCredentials credentials;
private readonly string version = "1.0";
private readonly TimeStamp timestamp;
private readonly Nonce nonce;
private readonly Signature signature;

private Token token;
private string callback;
private string verifier;
Expand All @@ -51,34 +51,34 @@ public AuthorizationHeader(ClientCredentials credentials, Nonce nonce, TimeStamp

public override string ToString()
{
StringBuilder builder = new StringBuilder();
AuthorizationHeaderBuilder header = new AuthorizationHeaderBuilder();

builder.Append("OAuth ");
builder.HeaderField(AuthorizationHeaderFields.REALM, "").Comma();
builder.HeaderField(AuthorizationHeaderFields.VERSION, version).Comma();
builder.HeaderField(AuthorizationHeaderFields.CONSUMER_KEY, credentials.Identifier).Comma();
builder.HeaderField(AuthorizationHeaderFields.SIGNATURE_METHOD, signature.Method).Comma();
header.Append("OAuth ");
header.AppendField(AuthorizationHeaderFields.REALM).AppendComma();
header.AppendField(AuthorizationHeaderFields.VERSION, OAuthVersion.VERSION).AppendComma();
header.AppendField(AuthorizationHeaderFields.CONSUMER_KEY, credentials.Identifier).AppendComma();
header.AppendField(AuthorizationHeaderFields.SIGNATURE_METHOD, signature.Method).AppendComma();

if (token != null)
{
builder.HeaderField(AuthorizationHeaderFields.TOKEN, token.Value).Comma();
header.AppendField(AuthorizationHeaderFields.TOKEN, token.Value).AppendComma();
}

if (!String.IsNullOrEmpty(verifier))
{
builder.HeaderField(AuthorizationHeaderFields.VERIFIER, verifier).Comma();
header.AppendField(AuthorizationHeaderFields.VERIFIER, verifier).AppendComma();
}

if (!String.IsNullOrEmpty(callback))
{
builder.HeaderField(AuthorizationHeaderFields.CALLBACK, callback).Comma();
header.AppendField(AuthorizationHeaderFields.CALLBACK, callback).AppendComma();
}

builder.HeaderField(AuthorizationHeaderFields.NONCE, nonce.ToString()).Comma();
builder.HeaderField(AuthorizationHeaderFields.TIMESTAMP, timestamp.ToString()).Comma();
builder.HeaderField(AuthorizationHeaderFields.SIGNATURE, signature.Value);
header.AppendField(AuthorizationHeaderFields.NONCE, nonce.ToString()).AppendComma();
header.AppendField(AuthorizationHeaderFields.TIMESTAMP, timestamp.ToString()).AppendComma();
header.AppendField(AuthorizationHeaderFields.SIGNATURE, signature.Value);

return builder.ToString();
return header.ToString();
}
}
}
@@ -1,16 +1,16 @@
#region Copyright
// Copyright (C) 2012 Mathieu Turcotte
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -20,20 +20,42 @@
// SOFTWARE.
#endregion

using System;
using System.Text;
using OAuth.Utils;

namespace OAuth.Utils
namespace OAuth.Base
{
static class StringBuilderExtensions
internal class AuthorizationHeaderBuilder
{
public static StringBuilder HeaderField(this StringBuilder builder, string name, string value)
StringBuilder header = new StringBuilder();

public AuthorizationHeaderBuilder Append(string value)
{
header.Append(value);
return this;
}

public AuthorizationHeaderBuilder AppendField(string name, string value)
{
header.Append(name).Append("=\"").Append(value).Append("\"");
return this;
}

public AuthorizationHeaderBuilder AppendField(string name)
{
return AppendField(name, "");
}

public AuthorizationHeaderBuilder AppendComma()
{
return builder.Append(name).Append("=\"").Append(value).Append("\"");
header.Append(',');
return this;
}

public static StringBuilder Comma(this StringBuilder builder)
public override string ToString()
{
return builder.Append(',');
return header.ToString();
}
}
}
5 changes: 1 addition & 4 deletions OAuth/Base/BaseString.cs
Expand Up @@ -28,8 +28,6 @@

namespace OAuth.Base
{
// Don't handle entity-body.

internal class BaseString
{
private readonly Uri uri;
Expand All @@ -38,7 +36,6 @@ internal class BaseString
private readonly TimeStamp timestamp;
private readonly ClientCredentials credentials;
private readonly string signatureType;
private readonly string version = "1.0";

private Token token;
private string callback;
Expand Down Expand Up @@ -125,7 +122,7 @@ private List<BaseStringParameter> GetBaseStringParameters()

List<BaseStringParameter> parameters = new List<BaseStringParameter>();

parameters.Add(new BaseStringParameter(AuthorizationHeaderFields.VERSION, version));
parameters.Add(new BaseStringParameter(AuthorizationHeaderFields.VERSION, OAuthVersion.VERSION));
parameters.Add(new BaseStringParameter(AuthorizationHeaderFields.NONCE, nonce.ToString()));
parameters.Add(new BaseStringParameter(AuthorizationHeaderFields.TIMESTAMP, timestamp.ToString()));
parameters.Add(new BaseStringParameter(AuthorizationHeaderFields.CONSUMER_KEY, credentials.Identifier));
Expand Down
12 changes: 12 additions & 0 deletions OAuth/Base/OAuthVersion.cs
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OAuth.Base
{
internal class OAuthVersion
{
public const string VERSION = "1.0";
}
}
3 changes: 2 additions & 1 deletion OAuth/OAuth.csproj
Expand Up @@ -48,6 +48,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Authenticator\RsaSha1RequestAuthenticator.cs" />
<Compile Include="Base\AuthorizationHeaderBuilder.cs" />
<Compile Include="Base\OAuthVersion.cs" />
<Compile Include="Base\RsaSha1Signature.cs" />
<Compile Include="Utils\QueryStringParser.cs" />
<Compile Include="Authenticator\RequestAuthenticator.cs" />
Expand All @@ -67,7 +69,6 @@
<Compile Include="Base\BaseStringParameterComparer.cs" />
<Compile Include="Base\TimeStamp.cs" />
<Compile Include="Base\Token.cs" />
<Compile Include="Utils\StringBuilderExtensions.cs" />
<Compile Include="Utils\UrlEncoder.cs" />
<Compile Include="Base\NegotiationToken.cs" />
<Compile Include="AccessTokenRequest.cs" />
Expand Down
2 changes: 1 addition & 1 deletion OAuth/Utils/UrlEncoder.cs
Expand Up @@ -29,7 +29,7 @@ internal class UrlEncoder
{
private const string UNRESERVED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";

public virtual string Encode(string content)
public string Encode(string content)
{
StringBuilder builder = new StringBuilder();

Expand Down

0 comments on commit b9d6172

Please sign in to comment.