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
+ }
0 commit comments