@@ -72,20 +72,20 @@ def run
72
72
unless res
73
73
fail_with ( Failure ::Unreachable , 'Failed to receive a reply from the server.' )
74
74
end
75
- if res . code == 200
76
- json = res . get_json_document
77
- if json . key? ( 'passwordRandom' ) && json . key? ( 'serial' )
78
- password_random = json [ 'passwordRandom' ]
79
- serial = json [ 'serial' ]
80
- print_good ( 'Retrieved passwordRandom: ' + password_random )
81
- print_good ( 'Retrieved serial: ' + serial )
82
- else
83
- fail_with ( Failure ::UnexpectedReply , 'Unable to retrieve passwordRandom and serial' )
84
- end
85
- else
75
+ unless res . code == 200
86
76
fail_with ( Failure ::UnexpectedReply , res . to_s )
87
77
end
88
78
79
+ json = res . get_json_document
80
+ unless json . key? ( 'passwordRandom' ) && json . key? ( 'serial' )
81
+ fail_with ( Failure ::UnexpectedReply , 'Unable to retrieve passwordRandom and serial' )
82
+ end
83
+
84
+ password_random = json [ 'passwordRandom' ]
85
+ serial = json [ 'serial' ]
86
+ print_good ( 'Retrieved passwordRandom: ' + password_random )
87
+ print_good ( 'Retrieved serial: ' + serial )
88
+
89
89
# 2) Create passwordCustom
90
90
sha1_hash = Digest ::SHA1 . hexdigest ( serial )
91
91
combined_string = sha1_hash + password_random + 'cid2016'
@@ -107,24 +107,26 @@ def run
107
107
unless res
108
108
fail_with ( Failure ::Unreachable , 'Failed to receive a reply from the server.' )
109
109
end
110
- if res . code == 200
111
- json = res . get_json_document
112
- if json . key? ( 'accessToken' )
113
- access_token = json [ 'accessToken' ]
114
- print_good ( 'Retrieved JWT: ' + access_token )
115
- else
116
- fail_with ( Failure ::UnexpectedReply , 'Did not receive JWT' )
117
- end
118
- else
110
+ unless res . code == 200
119
111
fail_with ( Failure ::UnexpectedReply , res . to_s )
120
112
end
121
113
114
+ json = res . get_json_document
115
+ unless json . key? ( 'accessToken' )
116
+ fail_with ( Failure ::UnexpectedReply , 'Did not receive JWT' )
117
+ end
118
+
119
+ access_token = json [ 'accessToken' ]
120
+ print_good ( 'Retrieved JWT: ' + access_token )
121
+
122
122
# 4) Add a new administrative user
123
- body = '{"idType": "1", ' \
124
- "\" name\" : \" #{ datastore [ 'NEW_USER' ] } \" , " \
125
- "\" user\" : \" #{ datastore [ 'NEW_USER' ] } \" , " \
126
- "\" newPassword\" : \" #{ datastore [ 'NEW_PASSWORD' ] } \" , " \
127
- "\" password_confirmation\" : \" #{ datastore [ 'NEW_PASSWORD' ] } \" }"
123
+ body = {
124
+ idType : '1' ,
125
+ name : datastore [ 'NEW_USER' ] ,
126
+ user : datastore [ 'NEW_USER' ] ,
127
+ newPassword : datastore [ 'NEW_PASSWORD' ] ,
128
+ password_confirmation : datastore [ 'NEW_PASSWORD' ]
129
+ } . to_json
128
130
129
131
res = send_request_cgi ( {
130
132
'method' => 'POST' ,
@@ -140,17 +142,44 @@ def run
140
142
fail_with ( Failure ::Unreachable , 'Failed to receive a reply from the server.' )
141
143
end
142
144
143
- if res . code == 200
144
- json = res . get_json_document
145
- if json . key? ( 'code' ) && json [ 'code' ] == 200 && json . key? ( 'error' ) && json [ 'error' ] == 'OK'
146
- store_valid_credential ( user : datastore [ 'NEW_USER' ] , private : datastore [ 'NEW_PASSWORD' ] , proof : json )
147
- print_good ( "New user '#{ datastore [ 'NEW_USER' ] } :#{ datastore [ 'NEW_PASSWORD' ] } ' was successfully added." )
148
- print_good ( "Login at: https://#{ datastore [ 'RHOSTS' ] } :#{ datastore [ 'RPORT' ] } /#/login" )
149
- else
150
- fail_with ( Failure ::UnexpectedReply , 'Received unexpected value for code and/or error:\n' + json . to_s )
151
- end
152
- else
145
+ unless res . code == 200
146
+ fail_with ( Failure ::UnexpectedReply , res . to_s )
147
+ end
148
+
149
+ json = res . get_json_document
150
+ unless json . key? ( 'code' ) && json [ 'code' ] == 200 && json . key? ( 'error' ) && json [ 'error' ] == 'OK'
151
+ fail_with ( Failure ::UnexpectedReply , 'Received unexpected value for code and/or error:\n' + json . to_s )
152
+ end
153
+
154
+ # 5) Confirm credentials work
155
+ body = {
156
+ username : datastore [ 'NEW_USER' ] ,
157
+ password : datastore [ 'NEW_PASSWORD' ] ,
158
+ passwordCustom : nil
159
+ } . to_json
160
+
161
+ res = send_request_cgi ( {
162
+ 'method' => 'POST' ,
163
+ 'ctype' => 'application/json' ,
164
+ 'uri' => normalize_uri ( target_uri . path , 'api/login/' ) ,
165
+ 'data' => body
166
+ } )
167
+
168
+ unless res
169
+ fail_with ( Failure ::Unreachable , 'Failed to receive a reply from the server.' )
170
+ end
171
+
172
+ unless res . code == 200
153
173
fail_with ( Failure ::UnexpectedReply , res . to_s )
154
174
end
175
+
176
+ json = res . get_json_document
177
+ unless json . key? ( 'accessToken' ) && json . key? ( 'unlock' )
178
+ fail_with ( Failure ::UnexpectedReply , 'Received unexpected reply:\n' + json . to_s )
179
+ end
180
+
181
+ store_valid_credential ( user : datastore [ 'NEW_USER' ] , private : datastore [ 'NEW_PASSWORD' ] , proof : json . to_s )
182
+ print_good ( "New user '#{ datastore [ 'NEW_USER' ] } :#{ datastore [ 'NEW_PASSWORD' ] } ' was successfully added." )
183
+ print_good ( "Login at: #{ full_uri ( normalize_uri ( target_uri , '#/login' ) ) } " )
155
184
end
156
185
end
0 commit comments