/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
using Amazon.Runtime;
using Amazon.Runtime.Internal.Settings;
using Amazon.Runtime.Internal.Util;
using Amazon.Util.Internal;
using System;
using System.Collections.Generic;
using System.Globalization;
namespace Amazon.Runtime.CredentialManagement
{
///
/// Manages endpoints to be used in conjunction with SAML role profiles. The role profiles
/// reference the endpoint to obtain the actual endpoint and any customization settings
/// needed to perform authentication.
///
public class SAMLEndpointManager
{
private NamedSettingsManager settingsManager;
///
/// Construct a SAMLEndpointManager.
///
public SAMLEndpointManager()
{
settingsManager = new NamedSettingsManager(SettingsConstants.RegisteredSAMLEndpoints);
}
///
/// True if the SAMLEndpointManager is available on the current platform, false otherwise.
///
public static bool IsAvailable
{
get
{
return NamedSettingsManager.IsAvailable;
}
}
///
/// Register an endpoint, or update an existing one.
///
/// The endpoint to register.
public void RegisterEndpoint(SAMLEndpoint samlEndpoint)
{
var properties = new Dictionary()
{
{ SettingsConstants.EndpointField, samlEndpoint.EndpointUri.ToString() },
{ SettingsConstants.AuthenticationTypeField, samlEndpoint.AuthenticationType.ToString() }
};
settingsManager.RegisterObject(samlEndpoint.Name, properties);
}
///
/// Get an endpoint, if it exists and is valid.
///
/// The name of the endpoint to get.
/// The endpoint, or null if it's invalid or doesn't exist.
/// True if the endpoint exists and is valid, false otherwise.
public bool TryGetEndpoint(string endpointName, out SAMLEndpoint samlEndpoint)
{
samlEndpoint = null;
try
{
samlEndpoint = GetEndpoint(endpointName);
}
catch (AmazonClientException e)
{
Logger.GetLogger(typeof(SAMLEndpointManager)).Error(e, "Unable to load SAML Endpoint '{0}'.", endpointName);
}
return samlEndpoint != null;
}
///
/// Get an endpoint from the store.
/// Throws an exception if there's an error reading the endpoint, or if it doesn't exist.
///
/// The name of the endpoint to get.
/// The endpoint with the given name.
public SAMLEndpoint GetEndpoint(string endpointName)
{
Dictionary properties;
if (settingsManager.TryGetObject(endpointName, out properties))
{
try
{
string authenticationType;
if (properties.TryGetValue(SettingsConstants.AuthenticationTypeField, out authenticationType))
{
return new SAMLEndpoint(endpointName, properties[SettingsConstants.EndpointField], authenticationType);
}
else
{
return new SAMLEndpoint(endpointName, properties[SettingsConstants.EndpointField], null);
}
}
catch (Exception e)
{
throw new AmazonClientException(string.Format(CultureInfo.InvariantCulture, "Error reading A SAML endpoint with name {0}.", endpointName), e);
}
}
else
{
throw new AmazonClientException(string.Format(CultureInfo.InvariantCulture, "There is no SAML endpoint registered with name {0}.", endpointName));
}
}
///
/// Delete an endpoint from the store, if it exists.
///
/// The name of the endpoint to delete.
public void UnregisterEndpoint(string endpointName)
{
settingsManager.UnregisterObject(endpointName);
}
///
/// List the names of valid endpoints in the store.
///
///
public List ListEndpointNames()
{
return settingsManager.ListObjectNames();
}
///
/// List valid endopints that can be read from the store.
///
///
public List ListEndpoints()
{
var endpoints = new List();
foreach (var endpointName in settingsManager.ListObjectNames())
{
SAMLEndpoint endpoint;
if (TryGetEndpoint(endpointName, out endpoint))
{
endpoints.Add(endpoint);
}
}
return endpoints;
}
}
}