//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.OAuth2.ChannelElements {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.Messaging.Reflection;
///
/// Encodes or decodes a set of scopes into the OAuth 2.0 scope message part.
///
internal class ScopeEncoder : IMessagePartEncoder {
///
/// Initializes a new instance of the class.
///
public ScopeEncoder() {
}
///
/// Encodes the specified value.
///
/// The value. Guaranteed to never be null.
///
/// The in string form, ready for message transport.
///
public string Encode(object value) {
var scopes = (IEnumerable)value;
ErrorUtilities.VerifyProtocol(!scopes.Any(scope => scope.Contains(" ")), OAuthStrings.ScopesMayNotContainSpaces);
return (scopes != null && scopes.Any()) ? string.Join(" ", scopes.ToArray()) : null;
}
///
/// Decodes the specified value.
///
/// The string value carried by the transport. Guaranteed to never be null, although it may be empty.
///
/// The deserialized form of the given string.
///
/// Thrown when the string value given cannot be decoded into the required object type.
public object Decode(string value) {
return OAuthUtilities.SplitScopes(value);
}
}
}