Skip to content

Commit 2182f33

Browse files
authored
Merge pull request #15 from IBM/feat/auth-methods
Feat/auth methods
2 parents 5b826eb + 0f71b32 commit 2182f33

20 files changed

+1679
-1020
lines changed

Authentication/Authenticator.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
52+
/// <summary>
53+
/// Returns the authentication type associated with the Authenticator instance.
54+
/// </summary>
55+
virtual public string AuthenticationType { get; }
56+
57+
/// <summary>
58+
/// Check if authenticator has everything it needs to authenticate. Every child class overrides this method.
59+
/// </summary>
60+
virtual public bool CanAuthenticate() {
61+
return false;
62+
}
63+
64+
/// <summary>
65+
/// Perform the necessary authentication steps for the specified request.
66+
/// </summary>
67+
virtual public void Authenticate(RESTConnector connector) { }
68+
69+
/// <summary>
70+
/// Perform the necessary authentication steps for the specified request.
71+
/// </summary>
72+
virtual public void Authenticate(WSConnector connector) { }
73+
74+
/// <summary>
75+
/// Validates the current set of configuration information in the Authenticator.
76+
/// </summary>
77+
virtual public void Validate() { }
78+
}
79+
}

Authentication/BasicAuthenticator.cs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
/// BasicAuthenticator is not waiting for token data so always return true.
79+
/// </summary>
80+
/// <returns></returns>
81+
public override bool CanAuthenticate()
82+
{
83+
return true;
84+
}
85+
86+
/// <summary>
87+
/// This method is called to authenticate an outgoing REST API request.
88+
/// Here, we'll just set the Authorization header to provide the necessary authentication info.
89+
/// </summary>
90+
/// <param name="connector"></param>
91+
public override void Authenticate(RESTConnector connector)
92+
{
93+
connector.WithAuthentication(Username, Password);
94+
}
95+
96+
/// <summary>
97+
/// This method is called to authenticate an outgoing REST API request.
98+
/// Here, we'll just set the Authorization header to provide the necessary authentication info.
99+
/// </summary>
100+
/// <param name="connector"></param>
101+
public override void Authenticate(WSConnector connector)
102+
{
103+
connector.WithAuthentication(Username, Password);
104+
}
105+
106+
public override void Validate()
107+
{
108+
if (string.IsNullOrEmpty(Username))
109+
{
110+
throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "Username"));
111+
}
112+
113+
if (string.IsNullOrEmpty(Password))
114+
{
115+
throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "Password"));
116+
}
117+
118+
if (Utility.HasBadFirstOrLastCharacter(Username))
119+
{
120+
throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "Username"));
121+
}
122+
123+
if (Utility.HasBadFirstOrLastCharacter(Password))
124+
{
125+
throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "Password"));
126+
}
127+
}
128+
}
129+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
/// <summary>
66+
/// Do we have BearerToken?
67+
/// </summary>
68+
/// <returns></returns>
69+
public override bool CanAuthenticate()
70+
{
71+
return BearerToken != null;
72+
}
73+
74+
public override string AuthenticationType
75+
{
76+
get { return AuthTypeBearer; }
77+
}
78+
79+
/// <summary>
80+
/// This method is called to authenticate an outgoing REST API request.
81+
/// Here, we'll just set the Authorization header to provide the necessary authentication info.
82+
/// </summary>
83+
/// <param name="connector"></param>
84+
public override void Authenticate(RESTConnector connector)
85+
{
86+
connector.WithAuthentication(BearerToken);
87+
}
88+
89+
/// <summary>
90+
/// This method is called to authenticate an outgoing REST API request.
91+
/// Here, we'll just set the Authorization header to provide the necessary authentication info.
92+
/// </summary>
93+
/// <param name="connector"></param>
94+
public override void Authenticate(WSConnector connector)
95+
{
96+
connector.WithAuthentication(BearerToken);
97+
}
98+
99+
public override void Validate()
100+
{
101+
if (string.IsNullOrEmpty(BearerToken))
102+
{
103+
throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "BearerToken"));
104+
}
105+
106+
if (Utility.HasBadFirstOrLastCharacter(BearerToken))
107+
{
108+
throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "BearerToken"));
109+
}
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)