Skip to content

Feat/auth methods #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions Authentication/Authenticator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright 2019 IBM Corp. 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 IBM.Cloud.SDK.Connection;

namespace IBM.Cloud.SDK.Authentication
{
public class Authenticator
{
/// <summary>
/// These are the valid authentication types.
/// </summary>
public const string AuthTypeBasic = "basic";
public const string AuthTypeNoAuth = "noAuth";
public const string AuthTypeIam = "iam";
public const string AuthTypeCp4d = "cp4d";
public const string AuthTypeBearer = "bearerToken";

/// <summary>
/// Constants which define the names of external config propreties (credential file, environment variable, etc.).
/// </summary>
public static string PropNameAuthType = "AUTH_TYPE";
public static string PropNameUsername = "USERNAME";
public static string PropNamePassword = "PASSWORD";
public static string PropNameBearerToken = "BEARER_TOKEN";
public static string PropNameUrl = "AUTH_URL";
public static string PropNameDisableSslVerification = "AUTH_DISABLE_SSL";
public static string PropNameApikey = "APIKEY";
public static string PropNameClientId = "CLIENT_ID";
public static string PropNameClientSecret = "CLIENT_SECRET";

public static string ErrorMessagePropMissing = "The {0} property is required but was not specified.";
public static string ErrorMessagePropInvalid = "The {0} property is invalid. Please remove any surrounding {{, }}, or \" characters.";
public static string ErrorMessageReqFailed = "Error while fetching access token from token service: ";

public string Url { get; set; }
public string Username { get; private set; }
public string Password { get; private set; }

/// <summary>
/// Returns the authentication type associated with the Authenticator instance.
/// </summary>
virtual public string AuthenticationType { get; }

/// <summary>
/// Check if token data is available.
/// </summary>
virtual public bool HasTokenData() {
return false;
}

/// <summary>
/// Perform the necessary authentication steps for the specified request.
/// </summary>
virtual public void Authenticate(RESTConnector connector) { }

/// <summary>
/// Perform the necessary authentication steps for the specified request.
/// </summary>
virtual public void Authenticate(WSConnector connector) { }

/// <summary>
/// Validates the current set of configuration information in the Authenticator.
/// </summary>
virtual public void Validate() { }
}
}
128 changes: 128 additions & 0 deletions Authentication/BasicAuthenticator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* Copyright 2019 IBM Corp. 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 IBM.Cloud.SDK.Connection;
using IBM.Cloud.SDK.Utilities;
using System;
using System.Collections.Generic;
using Utility = IBM.Cloud.SDK.Utilities.Utility;

namespace IBM.Cloud.SDK.Authentication.BasicAuth
{
/// <summary>
/// This class implements support for Basic Authentication. The main purpose of this authenticator is to construct the
/// Authorization header and then add it to each outgoing REST API request.
/// </summary>
public class BasicAuthenticator : Authenticator
{
/// <summary>
/// The username configured on this authenticator
/// </summary>
public string Username { get; private set; }
/// <summary>
/// The password configured on this authenticator
/// </summary>
public string Password { get; private set; }

/// <summary>
/// Construct a BasicAuthenticator instance with the specified username and password.
/// These values are used to construct an Authorization header value that will be included
/// in outgoing REST API requests.
/// </summary>
/// <param name="username">The basic auth username</param>
/// <param name="password">The basic auth password</param>
public BasicAuthenticator(string username, string password)
{
Init(username, password);
}

/// <summary>
/// Construct a BasicAuthenticator using properties retrieved from the specified Map.
/// </summary>
/// <param name="config">A map containing the username and password values</param>
public BasicAuthenticator(Dictionary<string, string> config)
{
config.TryGetValue(PropNameUsername, out string username);
config.TryGetValue(PropNamePassword, out string password);
Init(username, password);
}

private void Init(string username, string password)
{
Username = username;
Password = password;

Validate();
}

public override string AuthenticationType
{
get { return AuthTypeBasic; }
}

/// <summary>
/// This method is called to authenticate an outgoing REST API request.
/// Here, we'll just set the Authorization header to provide the necessary authentication info.
/// </summary>
/// <param name="connector"></param>
public override void Authenticate(RESTConnector connector)
{
if (connector.Headers == null)
{
connector.Headers = new Dictionary<string,string>();;
}
connector.Headers.Add("Authorization", Utility.CreateAuthorization(Username, Password));
}

/// <summary>
/// This method is called to authenticate an outgoing REST API request.
/// Here, we'll just set the Authorization header to provide the necessary authentication info.
/// </summary>
/// <param name="connector"></param>
public override void Authenticate(WSConnector connector)
{
if (connector.Headers == null)
{
connector.Headers = new Dictionary<string,string>();;
}
connector.Headers.Add("Authorization", Utility.CreateAuthorization(Username, Password));
}

public override void Validate()
{
if (string.IsNullOrEmpty(Username))
{
throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "Username"));
}

if (string.IsNullOrEmpty(Password))
{
throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "Password"));
}

if (Utility.HasBadFirstOrLastCharacter(Username))
{
throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "Username"));
}

if (Utility.HasBadFirstOrLastCharacter(Password))
{
throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "Password"));
}
}
}
}
111 changes: 111 additions & 0 deletions Authentication/BearerTokenAuthenticator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* Copyright 2019 IBM Corp. 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 IBM.Cloud.SDK.Connection;
using IBM.Cloud.SDK.Utilities;
using System;
using System.Collections.Generic;
using Utility = IBM.Cloud.SDK.Utilities.Utility;

namespace IBM.Cloud.SDK.Authentication.Bearer
{
/// <summary>
/// This class implements support for Bearer Token Authentication. The main purpose of this authenticator is to construct the
/// Authorization header and then add it to each outgoing REST API request.
/// </summary>
public class BearerTokenAuthenticator : Authenticator
{
/// <summary>
/// The access token configured for this authenticator
/// </summary>
public string BearerToken { get; set; }

/// <summary>
/// Construct a BearerTokenAuthenticator instance with the specified access token.
/// The token value will be used to construct an Authorization header that will be included
/// in outgoing REST API requests.
/// </summary>
/// <param name="bearerToken">The access token value</param>
public BearerTokenAuthenticator(string bearerToken)
{
Init(bearerToken);
}

/// <summary>
/// Construct a BearerTokenAuthenticator using properties retrieved from the specified Map.
/// </summary>
/// <param name="config">Config a map containing the access token value</param>
public BearerTokenAuthenticator(Dictionary<string, string> config)
{
config.TryGetValue(PropNameBearerToken, out string bearerToken);
Init(bearerToken);
}

private void Init(string bearerToken)
{
BearerToken = bearerToken;

Validate();
}

public override string AuthenticationType
{
get { return AuthTypeBearer; }
}

/// <summary>
/// This method is called to authenticate an outgoing REST API request.
/// Here, we'll just set the Authorization header to provide the necessary authentication info.
/// </summary>
/// <param name="connector"></param>
public override void Authenticate(RESTConnector connector)
{
if (connector.Headers == null)
{
connector.Headers = new Dictionary<string,string>();;
}
connector.Headers.Add("Authorization", string.Format("Bearer {0}", BearerToken));
}

/// <summary>
/// This method is called to authenticate an outgoing REST API request.
/// Here, we'll just set the Authorization header to provide the necessary authentication info.
/// </summary>
/// <param name="connector"></param>
public override void Authenticate(WSConnector connector)
{
if (connector.Headers == null)
{
connector.Headers = new Dictionary<string,string>();;
}
connector.Headers.Add("Authorization", string.Format("Bearer {0}", BearerToken));
}

public override void Validate()
{
if (string.IsNullOrEmpty(BearerToken))
{
throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "BearerToken"));
}

if (Utility.HasBadFirstOrLastCharacter(BearerToken))
{
throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "BearerToken"));
}
}
}
}
Loading