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