Skip to content

Commit 6716d05

Browse files
committed
feat(auth): add new authenticator methods
1 parent 5b826eb commit 6716d05

11 files changed

+1185
-0
lines changed

Authentication/Authenticator.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Copyright 2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using IBM.Cloud.SDK.Connection;
19+
20+
namespace IBM.Cloud.SDK.Authentication
21+
{
22+
public class Authenticator
23+
{
24+
/// <summary>
25+
/// These are the valid authentication types.
26+
/// </summary>
27+
public const string AuthTypeBasic = "basic";
28+
public const string AuthTypeNoAuth = "noAuth";
29+
public const string AuthTypeIam = "iam";
30+
public const string AuthTypeCp4d = "cp4d";
31+
public const string AuthTypeBearer = "bearerToken";
32+
33+
/// <summary>
34+
/// Constants which define the names of external config propreties (credential file, environment variable, etc.).
35+
/// </summary>
36+
public static string PropNameAuthType = "AUTH_TYPE";
37+
public static string PropNameUsername = "USERNAME";
38+
public static string PropNamePassword = "PASSWORD";
39+
public static string PropNameBearerToken = "BEARER_TOKEN";
40+
public static string PropNameUrl = "AUTH_URL";
41+
public static string PropNameDisableSslVerification = "AUTH_DISABLE_SSL";
42+
public static string PropNameApikey = "APIKEY";
43+
public static string PropNameClientId = "CLIENT_ID";
44+
public static string PropNameClientSecret = "CLIENT_SECRET";
45+
46+
public static string ErrorMessagePropMissing = "The {0} property is required but was not specified.";
47+
public static string ErrorMessagePropInvalid = "The {0} property is invalid. Please remove any surrounding {{, }}, or \" characters.";
48+
public static string ErrorMessageReqFailed = "Error while fetching access token from token service: ";
49+
50+
public string Url { get; set; }
51+
public string Username { get; private set; }
52+
public string Password { get; private set; }
53+
54+
/// <summary>
55+
/// Returns the authentication type associated with the Authenticator instance.
56+
/// </summary>
57+
virtual public string AuthenticationType { get; }
58+
59+
/// <summary>
60+
/// Check if token data is available.
61+
/// </summary>
62+
virtual public bool HasTokenData() {
63+
return false;
64+
}
65+
66+
/// <summary>
67+
/// Perform the necessary authentication steps for the specified request.
68+
/// </summary>
69+
virtual public void Authenticate(RESTConnector connector) { }
70+
71+
/// <summary>
72+
/// Perform the necessary authentication steps for the specified request.
73+
/// </summary>
74+
virtual public void Authenticate(WSConnector connector) { }
75+
76+
/// <summary>
77+
/// Validates the current set of configuration information in the Authenticator.
78+
/// </summary>
79+
virtual public void Validate() { }
80+
}
81+
}

Authentication/BasicAuthenticator.cs

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* Copyright 2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using IBM.Cloud.SDK.Connection;
19+
using IBM.Cloud.SDK.Utilities;
20+
using System;
21+
using System.Collections.Generic;
22+
using Utility = IBM.Cloud.SDK.Utilities.Utility;
23+
24+
namespace IBM.Cloud.SDK.Authentication.BasicAuth
25+
{
26+
/// <summary>
27+
/// This class implements support for Basic Authentication. The main purpose of this authenticator is to construct the
28+
/// Authorization header and then add it to each outgoing REST API request.
29+
/// </summary>
30+
public class BasicAuthenticator : Authenticator
31+
{
32+
/// <summary>
33+
/// The username configured on this authenticator
34+
/// </summary>
35+
public string Username { get; private set; }
36+
/// <summary>
37+
/// The password configured on this authenticator
38+
/// </summary>
39+
public string Password { get; private set; }
40+
41+
/// <summary>
42+
/// Construct a BasicAuthenticator instance with the specified username and password.
43+
/// These values are used to construct an Authorization header value that will be included
44+
/// in outgoing REST API requests.
45+
/// </summary>
46+
/// <param name="username">The basic auth username</param>
47+
/// <param name="password">The basic auth password</param>
48+
public BasicAuthenticator(string username, string password)
49+
{
50+
Init(username, password);
51+
}
52+
53+
/// <summary>
54+
/// Construct a BasicAuthenticator using properties retrieved from the specified Map.
55+
/// </summary>
56+
/// <param name="config">A map containing the username and password values</param>
57+
public BasicAuthenticator(Dictionary<string, string> config)
58+
{
59+
config.TryGetValue(PropNameUsername, out string username);
60+
config.TryGetValue(PropNamePassword, out string password);
61+
Init(username, password);
62+
}
63+
64+
private void Init(string username, string password)
65+
{
66+
Username = username;
67+
Password = password;
68+
69+
Validate();
70+
}
71+
72+
public override string AuthenticationType
73+
{
74+
get { return AuthTypeBasic; }
75+
}
76+
77+
/// <summary>
78+
/// This method is called to authenticate an outgoing REST API request.
79+
/// Here, we'll just set the Authorization header to provide the necessary authentication info.
80+
/// </summary>
81+
/// <param name="connector"></param>
82+
public override void Authenticate(RESTConnector connector)
83+
{
84+
connector.Headers.Add("Authorization", Utility.CreateAuthorization(Username, Password));
85+
}
86+
87+
/// <summary>
88+
/// This method is called to authenticate an outgoing REST API request.
89+
/// Here, we'll just set the Authorization header to provide the necessary authentication info.
90+
/// </summary>
91+
/// <param name="connector"></param>
92+
public override void Authenticate(WSConnector connector)
93+
{
94+
if (connector.Headers == null)
95+
{
96+
connector.Headers = new Dictionary<string,string>();;
97+
}
98+
connector.Headers.Add("Authorization", Utility.CreateAuthorization(Username, Password));
99+
}
100+
101+
public override void Validate()
102+
{
103+
if (string.IsNullOrEmpty(Username))
104+
{
105+
throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "Username"));
106+
}
107+
108+
if (string.IsNullOrEmpty(Password))
109+
{
110+
throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "Password"));
111+
}
112+
113+
if (Utility.HasBadFirstOrLastCharacter(Username))
114+
{
115+
throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "Username"));
116+
}
117+
118+
if (Utility.HasBadFirstOrLastCharacter(Password))
119+
{
120+
throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "Password"));
121+
}
122+
}
123+
}
124+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Copyright 2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using IBM.Cloud.SDK.Connection;
19+
using IBM.Cloud.SDK.Utilities;
20+
using System;
21+
using System.Collections.Generic;
22+
using Utility = IBM.Cloud.SDK.Utilities.Utility;
23+
24+
namespace IBM.Cloud.SDK.Authentication.Bearer
25+
{
26+
/// <summary>
27+
/// This class implements support for Bearer Token Authentication. The main purpose of this authenticator is to construct the
28+
/// Authorization header and then add it to each outgoing REST API request.
29+
/// </summary>
30+
public class BearerTokenAuthenticator : Authenticator
31+
{
32+
/// <summary>
33+
/// The access token configured for this authenticator
34+
/// </summary>
35+
public string BearerToken { get; set; }
36+
37+
/// <summary>
38+
/// Construct a BearerTokenAuthenticator instance with the specified access token.
39+
/// The token value will be used to construct an Authorization header that will be included
40+
/// in outgoing REST API requests.
41+
/// </summary>
42+
/// <param name="bearerToken">The access token value</param>
43+
public BearerTokenAuthenticator(string bearerToken)
44+
{
45+
Init(bearerToken);
46+
}
47+
48+
/// <summary>
49+
/// Construct a BearerTokenAuthenticator using properties retrieved from the specified Map.
50+
/// </summary>
51+
/// <param name="config">Config a map containing the access token value</param>
52+
public BearerTokenAuthenticator(Dictionary<string, string> config)
53+
{
54+
config.TryGetValue(PropNameBearerToken, out string bearerToken);
55+
Init(bearerToken);
56+
}
57+
58+
private void Init(string bearerToken)
59+
{
60+
BearerToken = bearerToken;
61+
62+
Validate();
63+
}
64+
65+
public override string AuthenticationType
66+
{
67+
get { return AuthTypeBearer; }
68+
}
69+
70+
/// <summary>
71+
/// This method is called to authenticate an outgoing REST API request.
72+
/// Here, we'll just set the Authorization header to provide the necessary authentication info.
73+
/// </summary>
74+
/// <param name="connector"></param>
75+
public override void Authenticate(RESTConnector connector)
76+
{
77+
connector.Headers.Add("Authorization", string.Format("Bearer {0}", BearerToken));
78+
}
79+
80+
public override void Validate()
81+
{
82+
if (string.IsNullOrEmpty(BearerToken))
83+
{
84+
throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "BearerToken"));
85+
}
86+
87+
if (Utility.HasBadFirstOrLastCharacter(BearerToken))
88+
{
89+
throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "BearerToken"));
90+
}
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)