Remove magic strings

This commit is contained in:
Albireo
2015-07-13 18:32:55 +02:00
parent f3c6458ec8
commit fd02b65a12
4 changed files with 61 additions and 33 deletions

View File

@@ -2,6 +2,32 @@ namespace Owin.Security.Providers.Imgur
{
internal static class ImgurAuthenticationDefaults
{
public const string AuthenticationType = "Imgur";
internal const string AccessDeniedErrorMessage = "access_denied";
internal const string AccessTokenPropertyName = "access_token";
internal const string AccountIdPropertyName = "account_id";
internal const string AccountUsernamePropertyName = "account_username";
internal const string AuthenticationType = "Imgur";
internal const string AuthorizationCodeGrantType = "authorization_code";
internal const string AuthorizationUri = "https://api.imgur.com/oauth2/authorize";
internal const string CallbackPath = "/signin-imgur";
internal const string ClientIdParameter = "client_id";
internal const string ClientSecretParameter = "client_secret";
internal const string CodeParameter = "code";
internal const string CodeResponseType = "code";
internal const string CommunicationFailureMessage = ""; // TODO
internal const string DeserializationFailureMessage = ""; // TODO
internal const string ErrorParameter = "error";
internal const string ExpiresInPropertyName = "expires_in";
internal const string GrantTypeParameter = "grant_type";
internal const string Int32Format = "D";
internal const string InvalidAuthenticationTicketMessage = ""; // TODO
internal const string RefreshInPropertyName = "refresh_token";
internal const string ResponseTypeParameter = "response_type";
internal const string ScopePropertyName = "scope";
internal const string StateParameter = "state";
internal const string TokenTypePropertyName = "token_type";
internal const string TokenUri = "https://api.imgur.com/oauth2/token";
internal const string Version = "v1";
internal const string XmlSchemaString = "http://www.w3.org/2001/XMLSchema#string";
}
}

View File

@@ -63,10 +63,10 @@
var state = this.Options.StateDataFormat.Protect(challenge.Properties);
var authorizationUri = "https://api.imgur.com/oauth2/authorize";
authorizationUri = WebUtilities.AddQueryString(authorizationUri, "client_id", Uri.EscapeDataString(this.Options.ClientId));
authorizationUri = WebUtilities.AddQueryString(authorizationUri, "response_type", "code");
authorizationUri = WebUtilities.AddQueryString(authorizationUri, "state", Uri.EscapeDataString(state));
var authorizationUri = ImgurAuthenticationDefaults.AuthorizationUri;
authorizationUri = WebUtilities.AddQueryString(authorizationUri, ImgurAuthenticationDefaults.ClientIdParameter, Uri.EscapeDataString(this.Options.ClientId));
authorizationUri = WebUtilities.AddQueryString(authorizationUri, ImgurAuthenticationDefaults.ResponseTypeParameter, ImgurAuthenticationDefaults.CodeResponseType);
authorizationUri = WebUtilities.AddQueryString(authorizationUri, ImgurAuthenticationDefaults.StateParameter, Uri.EscapeDataString(state));
this.Response.Redirect(authorizationUri);
@@ -75,13 +75,13 @@
protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
{
if (this.Request.Query.Get("error") != null)
if (this.Request.Query.Get(ImgurAuthenticationDefaults.ErrorParameter) != null)
{
return new AuthenticationTicket(null, null);
}
var code = this.Request.Query.Get("code");
var state = this.Request.Query.Get("state");
var code = this.Request.Query.Get(ImgurAuthenticationDefaults.CodeParameter);
var state = this.Request.Query.Get(ImgurAuthenticationDefaults.StateParameter);
var properties = this.Options.StateDataFormat.Unprotect(state);
if (properties == null)
@@ -96,23 +96,23 @@
AuthenticationResponse authenticationResponse;
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://api.imgur.com/oauth2/token"))
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, ImgurAuthenticationDefaults.TokenUri))
{
httpRequestMessage.Content =
new FormUrlEncodedContent(
new []
{
new KeyValuePair<string, string>("client_id", this.Options.ClientId),
new KeyValuePair<string, string>("client_secret", this.Options.ClientSecret),
new KeyValuePair<string, string>("grant_type", "authorization_code"),
new KeyValuePair<string, string>("code", code)
new KeyValuePair<string, string>(ImgurAuthenticationDefaults.ClientIdParameter, this.Options.ClientId),
new KeyValuePair<string, string>(ImgurAuthenticationDefaults.ClientSecretParameter, this.Options.ClientSecret),
new KeyValuePair<string, string>(ImgurAuthenticationDefaults.GrantTypeParameter, ImgurAuthenticationDefaults.AuthorizationCodeGrantType),
new KeyValuePair<string, string>(ImgurAuthenticationDefaults.CodeParameter, code)
});
using (var httpResponseMessage = await this.httpClient.SendAsync(httpRequestMessage, this.Request.CallCancelled))
{
if (!httpResponseMessage.IsSuccessStatusCode)
{
throw new Exception(); // TODO
throw new Exception(ImgurAuthenticationDefaults.CommunicationFailureMessage);
}
using (var stream = await httpResponseMessage.Content.ReadAsStreamAsync())
@@ -132,13 +132,13 @@
if (authenticationResponse == null)
{
throw new Exception(); // TODO
throw new Exception(ImgurAuthenticationDefaults.DeserializationFailureMessage);
}
var identity = new ClaimsIdentity(this.Options.AuthenticationType, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
identity.AddClaim(new Claim(ClaimTypes.Name, authenticationResponse.AccountUsername, "http://www.w3.org/2001/XMLSchema#string", this.Options.AuthenticationType));
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, authenticationResponse.AccountId.ToString("D", CultureInfo.InvariantCulture), "http://www.w3.org/2001/XMLSchema#string", this.Options.AuthenticationType));
identity.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, authenticationResponse.AccountUsername, "http://www.w3.org/2001/XMLSchema#string", this.Options.AuthenticationType));
identity.AddClaim(new Claim(ClaimTypes.Name, authenticationResponse.AccountUsername, ImgurAuthenticationDefaults.XmlSchemaString, this.Options.AuthenticationType));
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, authenticationResponse.AccountId.ToString(ImgurAuthenticationDefaults.Int32Format, CultureInfo.InvariantCulture), ImgurAuthenticationDefaults.XmlSchemaString, this.Options.AuthenticationType));
identity.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, authenticationResponse.AccountUsername, ImgurAuthenticationDefaults.XmlSchemaString, this.Options.AuthenticationType));
var context = new ImgurAuthenticatedContext(this.Context, this.Options);
context.AccessToken = authenticationResponse.AccessToken;
@@ -172,9 +172,7 @@
if (ticket == null)
{
this.logger.WriteError("Invalid return state, unable to redirect.");
throw new Exception("Invalid return state, unable to redirect.");
throw new Exception(ImgurAuthenticationDefaults.InvalidAuthenticationTicketMessage);
}
var context = new ImgurReturnEndpointContext(this.Context, ticket);
@@ -204,7 +202,7 @@
if (context.Identity == null)
{
location = WebUtilities.AddQueryString(location, "error", "access_denied");
location = WebUtilities.AddQueryString(location, ImgurAuthenticationDefaults.ErrorParameter, ImgurAuthenticationDefaults.AccessDeniedErrorMessage);
}
this.Response.Redirect(location);
@@ -216,25 +214,25 @@
private class AuthenticationResponse
{
[JsonProperty(PropertyName = "access_token")]
[JsonProperty(PropertyName = ImgurAuthenticationDefaults.AccessTokenPropertyName)]
public string AccessToken { get; set; }
[JsonProperty(PropertyName = "account_id")]
[JsonProperty(PropertyName = ImgurAuthenticationDefaults.AccountIdPropertyName)]
public int AccountId { get; set; }
[JsonProperty(PropertyName = "account_username")]
[JsonProperty(PropertyName = ImgurAuthenticationDefaults.AccountUsernamePropertyName)]
public string AccountUsername { get; set; }
[JsonProperty(PropertyName = "expires_in")]
[JsonProperty(PropertyName = ImgurAuthenticationDefaults.ExpiresInPropertyName)]
public int ExpiresIn { get; set; }
[JsonProperty(PropertyName = "refresh_token")]
[JsonProperty(PropertyName = ImgurAuthenticationDefaults.RefreshInPropertyName)]
public string RefreshToken { get; set; }
[JsonProperty(PropertyName = "scope")]
[JsonProperty(PropertyName = ImgurAuthenticationDefaults.ScopePropertyName)]
public string Scope { get; set; }
[JsonProperty(PropertyName = "token_type")]
[JsonProperty(PropertyName = ImgurAuthenticationDefaults.TokenTypePropertyName)]
public string TokenType { get; set; }
}
}

View File

@@ -36,12 +36,16 @@
if (string.IsNullOrWhiteSpace(this.Options.ClientId))
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.Exception_OptionMustBeProvided, "ClientId"), "options");
var message = string.Format(CultureInfo.InvariantCulture, Resources.Exception_OptionMustBeProvided, "ClientId");
throw new ArgumentException(message, "options");
}
if (string.IsNullOrWhiteSpace(this.Options.ClientSecret))
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.Exception_OptionMustBeProvided, "ClientSecret"), "options");
var message = string.Format(CultureInfo.InvariantCulture, Resources.Exception_OptionMustBeProvided, "ClientSecret");
throw new ArgumentException(message, "options");
}
if (this.Options.Provider == null)
@@ -56,7 +60,7 @@
if (this.Options.StateDataFormat == null)
{
var dataProtector = appBuilder.CreateDataProtector(TypeFullName, this.Options.AuthenticationType, "v1");
var dataProtector = appBuilder.CreateDataProtector(TypeFullName, this.Options.AuthenticationType, ImgurAuthenticationDefaults.Version);
this.Options.StateDataFormat = new PropertiesDataFormat(dataProtector);
}

View File

@@ -15,7 +15,7 @@
{
this.AuthenticationMode = AuthenticationMode.Passive;
this.BackchannelTimeout = TimeSpan.FromSeconds(60);
this.CallbackPath = new PathString("/signin-imgur");
this.CallbackPath = new PathString(ImgurAuthenticationDefaults.CallbackPath);
this.Caption = ImgurAuthenticationDefaults.AuthenticationType;
}