Skip to content

Commit

Permalink
feat(Authentication): Update auth methods
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Auth methods were updated and old authentication methods were removed
  • Loading branch information
mediumTaj committed Aug 17, 2019
1 parent f836045 commit e9095e2
Show file tree
Hide file tree
Showing 35 changed files with 2,144 additions and 1,415 deletions.
23 changes: 22 additions & 1 deletion src/IBM.Cloud.SDK.Core/Authentication/Authenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,31 @@ namespace IBM.Cloud.SDK.Core.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 AuthtypeIcp4d = "icp4d";
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 PropnameDisableSsl = "AUTH_DISABLE_SSL";
public static string PropnameApikey = "APIKEY";
public static string PropnameClientId = "CLIENT_ID";
public static string PropnameClientSecret = "CLIENT_SECRET";

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

/// <summary>
/// Returns the authentication type associated with the AuthenticatorConfig instance.
Expand Down
50 changes: 0 additions & 50 deletions src/IBM.Cloud.SDK.Core/Authentication/AuthenticatorFactory.cs

This file was deleted.

53 changes: 0 additions & 53 deletions src/IBM.Cloud.SDK.Core/Authentication/BasicAuth/BasicAuthConfig.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,56 @@
*/

using IBM.Cloud.SDK.Core.Http;
using IBM.Cloud.SDK.Core.Util;
using System;
using System.Collections.Generic;

namespace IBM.Cloud.SDK.Core.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
public class BasicAuthenticator : Authenticator, IAuthenticator
{
private string Username;
private string Password;
/// <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>
/// Initialize our Authorization header value using the information in the BasicAuthConfig instance.
/// This ctor assumes that the config object has already passed validation.
/// Construct a BasicAuthenticator using properties retrieved from the specified Map.
/// </summary>
/// <param name="config">the BasicAuthConfig instance that holds the username and password values from which to build the
/// Authorization header.</param>
public BasicAuthenticator(BasicAuthConfig config)
/// <param name="config">A map containing the username and password values</param>
public BasicAuthenticator(Dictionary<string, string> config)
{
Username = config.Username;
Password = config.Password;
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
Expand All @@ -54,5 +82,28 @@ public override void Authenticate(IClient client)
{
client.WithAuthentication(Username, Password);
}

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

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

if (CredentialUtils.HasBadStartOrEndChar(Username))
{
throw new ArgumentException(string.Format(ErrormsgPropInvalid, "Username"));
}

if (CredentialUtils.HasBadStartOrEndChar(Password))
{
throw new ArgumentException(string.Format(ErrormsgPropInvalid, "Password"));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* 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.Core.Http;
using IBM.Cloud.SDK.Core.Util;
using System;
using System.Collections.Generic;

namespace IBM.Cloud.SDK.Core.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="client"></param>
public override void Authenticate(IClient client)
{
client.WithAuthentication(BearerToken);
}

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

if (CredentialUtils.HasBadStartOrEndChar(BearerToken))
{
throw new ArgumentException(string.Format(ErrormsgPropInvalid, "BearerToken"));
}
}
}
}

0 comments on commit e9095e2

Please sign in to comment.