//----------------------------------------------------------------------- // // Copyright (c) Outercurve Foundation. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOpenAuth.OAuth2 { using System; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Globalization; using System.Linq; using System.Text; using DotNetOpenAuth.Messaging; /// /// Utility methods for authorization servers. /// internal static class AuthServerUtilities { /// /// Gets information about the client with a given identifier. /// /// The authorization server. /// The client identifier. /// The client information. Never null. internal static IClientDescription GetClientOrThrow(this IAuthorizationServer authorizationServer, string clientIdentifier) { Requires.NotNullOrEmpty(clientIdentifier, "clientIdentifier"); Contract.Ensures(Contract.Result() != null); try { var result = authorizationServer.GetClient(clientIdentifier); ErrorUtilities.VerifyHost(result != null, OAuthStrings.ResultShouldNotBeNull, authorizationServer.GetType().FullName, "GetClient(string)"); return result; } catch (KeyNotFoundException ex) { throw ErrorUtilities.Wrap(ex, AuthServerStrings.ClientOrTokenSecretNotFound); } catch (ArgumentException ex) { throw ErrorUtilities.Wrap(ex, AuthServerStrings.ClientOrTokenSecretNotFound); } } /// /// Verifies a condition is true or throws an exception describing the problem. /// /// The condition that evaluates to true to avoid an exception. /// A single error code from . /// A human-readable UTF-8 encoded text providing additional information, used to assist the client developer in understanding the error that occurred. /// The formatting arguments to generate the actual description. internal static void TokenEndpointVerify(bool condition, string error, string unformattedDescription = null, params object[] args) { if (!condition) { string description = unformattedDescription != null ? string.Format(CultureInfo.CurrentCulture, unformattedDescription, args) : null; throw new TokenEndpointProtocolException(error, description); } } } }