summaryrefslogtreecommitdiffstats
path: root/netwerk/protocol/http/nsHttpChannelAuthProvider.cpp
blob: 8c4f8342397925cbac42abbd75dbc55eb2ca51be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set expandtab ts=4 sw=2 sts=2 cin: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// HttpLog.h should generally be included first
#include "HttpLog.h"

#include "mozilla/BasePrincipal.h"
#include "mozilla/StoragePrincipalHelper.h"
#include "mozilla/Tokenizer.h"
#include "MockHttpAuth.h"
#include "nsHttpChannelAuthProvider.h"
#include "nsCRT.h"
#include "nsNetUtil.h"
#include "nsHttpHandler.h"
#include "nsIHttpAuthenticator.h"
#include "nsIHttpChannelInternal.h"
#include "nsIAuthPrompt2.h"
#include "nsIAuthPromptProvider.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsEscape.h"
#include "nsAuthInformationHolder.h"
#include "nsIStringBundle.h"
#include "nsIPromptService.h"
#include "netCore.h"
#include "nsIHttpAuthenticableChannel.h"
#include "nsIURI.h"
#include "nsContentUtils.h"
#include "nsHttp.h"
#include "nsHttpBasicAuth.h"
#include "nsHttpDigestAuth.h"
#ifdef MOZ_AUTH_EXTENSION
#  include "nsHttpNegotiateAuth.h"
#endif
#include "nsHttpNTLMAuth.h"
#include "nsServiceManagerUtils.h"
#include "nsIURL.h"
#include "mozilla/StaticPrefs_network.h"
#include "mozilla/StaticPrefs_prompts.h"
#include "mozilla/Telemetry.h"
#include "nsIProxiedChannel.h"
#include "nsIProxyInfo.h"

namespace mozilla::net {

#define SUBRESOURCE_AUTH_DIALOG_DISALLOW_ALL 0
#define SUBRESOURCE_AUTH_DIALOG_DISALLOW_CROSS_ORIGIN 1
#define SUBRESOURCE_AUTH_DIALOG_ALLOW_ALL 2

#define HTTP_AUTH_DIALOG_TOP_LEVEL_DOC 29
#define HTTP_AUTH_DIALOG_SAME_ORIGIN_SUBRESOURCE 30
#define HTTP_AUTH_DIALOG_SAME_ORIGIN_XHR 31
#define HTTP_AUTH_DIALOG_NON_WEB_CONTENT 32

#define HTTP_AUTH_BASIC_INSECURE 0
#define HTTP_AUTH_BASIC_SECURE 1
#define HTTP_AUTH_DIGEST_INSECURE 2
#define HTTP_AUTH_DIGEST_SECURE 3
#define HTTP_AUTH_NTLM_INSECURE 4
#define HTTP_AUTH_NTLM_SECURE 5
#define HTTP_AUTH_NEGOTIATE_INSECURE 6
#define HTTP_AUTH_NEGOTIATE_SECURE 7

#define MAX_DISPLAYED_USER_LENGTH 64
#define MAX_DISPLAYED_HOST_LENGTH 64

static void GetOriginAttributesSuffix(nsIChannel* aChan, nsACString& aSuffix) {
  OriginAttributes oa;

  // Deliberately ignoring the result and going with defaults
  if (aChan) {
    StoragePrincipalHelper::GetOriginAttributesForNetworkState(aChan, oa);
  }

  oa.CreateSuffix(aSuffix);
}

nsHttpChannelAuthProvider::nsHttpChannelAuthProvider()
    : mProxyAuth(false),
      mTriedProxyAuth(false),
      mTriedHostAuth(false),
      mSuppressDefensiveAuth(false),
      mCrossOrigin(false),
      mConnectionBased(false),
      mHttpHandler(gHttpHandler) {}

nsHttpChannelAuthProvider::~nsHttpChannelAuthProvider() {
  MOZ_RELEASE_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(!mAuthChannel, "Disconnect wasn't called");
}

NS_IMETHODIMP
nsHttpChannelAuthProvider::Init(nsIHttpAuthenticableChannel* channel) {
  MOZ_ASSERT(channel, "channel expected!");

  mAuthChannel = channel;

  nsresult rv = mAuthChannel->GetURI(getter_AddRefs(mURI));
  if (NS_FAILED(rv)) return rv;

  rv = mAuthChannel->GetIsSSL(&mUsingSSL);
  if (NS_FAILED(rv)) return rv;

  nsCOMPtr<nsIProxiedChannel> proxied(channel);
  if (proxied) {
    nsCOMPtr<nsIProxyInfo> pi;
    rv = proxied->GetProxyInfo(getter_AddRefs(pi));
    if (NS_FAILED(rv)) return rv;

    if (pi) {
      nsAutoCString proxyType;
      rv = pi->GetType(proxyType);
      if (NS_FAILED(rv)) return rv;

      mProxyUsingSSL = proxyType.EqualsLiteral("https");
    }
  }

  rv = mURI->GetAsciiHost(mHost);
  if (NS_FAILED(rv)) return rv;

  // reject the URL if it doesn't specify a host
  if (mHost.IsEmpty()) return NS_ERROR_MALFORMED_URI;

  rv = mURI->GetPort(&mPort);
  if (NS_FAILED(rv)) return rv;

  nsCOMPtr<nsIChannel> bareChannel = do_QueryInterface(channel);
  mIsPrivate = NS_UsePrivateBrowsing(bareChannel);

  return NS_OK;
}

NS_IMETHODIMP
nsHttpChannelAuthProvider::ProcessAuthentication(uint32_t httpStatus,
                                                 bool SSLConnectFailed) {
  LOG(
      ("nsHttpChannelAuthProvider::ProcessAuthentication "
       "[this=%p channel=%p code=%u SSLConnectFailed=%d]\n",
       this, mAuthChannel, httpStatus, SSLConnectFailed));

  MOZ_ASSERT(mAuthChannel, "Channel not initialized");

  nsCOMPtr<nsIProxyInfo> proxyInfo;
  nsresult rv = mAuthChannel->GetProxyInfo(getter_AddRefs(proxyInfo));
  if (NS_FAILED(rv)) return rv;
  if (proxyInfo) {
    mProxyInfo = do_QueryInterface(proxyInfo);
    if (!mProxyInfo) return NS_ERROR_NO_INTERFACE;
  }

  nsAutoCString challenges;
  mProxyAuth = (httpStatus == 407);

  rv = PrepareForAuthentication(mProxyAuth);
  if (NS_FAILED(rv)) return rv;

  if (mProxyAuth) {
    // only allow a proxy challenge if we have a proxy server configured.
    // otherwise, we could inadvertently expose the user's proxy
    // credentials to an origin server.  We could attempt to proceed as
    // if we had received a 401 from the server, but why risk flirting
    // with trouble?  IE similarly rejects 407s when a proxy server is
    // not configured, so there's no reason not to do the same.
    if (!UsingHttpProxy()) {
      LOG(("rejecting 407 when proxy server not configured!\n"));
      return NS_ERROR_UNEXPECTED;
    }
    if (UsingSSL() && !SSLConnectFailed) {
      // we need to verify that this challenge came from the proxy
      // server itself, and not some server on the other side of the
      // SSL tunnel.
      LOG(("rejecting 407 from origin server!\n"));
      return NS_ERROR_UNEXPECTED;
    }
    rv = mAuthChannel->GetProxyChallenges(challenges);
  } else {
    rv = mAuthChannel->GetWWWChallenges(challenges);
  }
  if (NS_FAILED(rv)) return rv;

  nsAutoCString creds;
  rv = GetCredentials(challenges, mProxyAuth, creds);
  if (rv == NS_ERROR_IN_PROGRESS) return rv;
  if (NS_FAILED(rv)) {
    LOG(("unable to authenticate\n"));
  } else {
    // set the authentication credentials
    if (mProxyAuth) {
      rv = mAuthChannel->SetProxyCredentials(creds);
    } else {
      rv = mAuthChannel->SetWWWCredentials(creds);
    }
  }
  return rv;
}

NS_IMETHODIMP
nsHttpChannelAuthProvider::AddAuthorizationHeaders(
    bool aDontUseCachedWWWCreds) {
  LOG(
      ("nsHttpChannelAuthProvider::AddAuthorizationHeaders? "
       "[this=%p channel=%p]\n",
       this, mAuthChannel));

  MOZ_ASSERT(mAuthChannel, "Channel not initialized");

  nsCOMPtr<nsIProxyInfo> proxyInfo;
  nsresult rv = mAuthChannel->GetProxyInfo(getter_AddRefs(proxyInfo));
  if (NS_FAILED(rv)) return rv;
  if (proxyInfo) {
    mProxyInfo = do_QueryInterface(proxyInfo);
    if (!mProxyInfo) return NS_ERROR_NO_INTERFACE;
  }

  uint32_t loadFlags;
  rv = mAuthChannel->GetLoadFlags(&loadFlags);
  if (NS_FAILED(rv)) return rv;

  // this getter never fails
  nsHttpAuthCache* authCache = gHttpHandler->AuthCache(mIsPrivate);

  // check if proxy credentials should be sent
  if (!ProxyHost().IsEmpty() && UsingHttpProxy()) {
    SetAuthorizationHeader(authCache, nsHttp::Proxy_Authorization, "http"_ns,
                           ProxyHost(), ProxyPort(),
                           ""_ns,  // proxy has no path
                           mProxyIdent);
  }

  if (loadFlags & nsIRequest::LOAD_ANONYMOUS) {
    LOG(("Skipping Authorization header for anonymous load\n"));
    return NS_OK;
  }

  if (aDontUseCachedWWWCreds) {
    LOG(
        ("Authorization header already present:"
         " skipping adding auth header from cache\n"));
    return NS_OK;
  }

  // check if server credentials should be sent
  nsAutoCString path, scheme;
  if (NS_SUCCEEDED(GetCurrentPath(path)) &&
      NS_SUCCEEDED(mURI->GetScheme(scheme))) {
    SetAuthorizationHeader(authCache, nsHttp::Authorization, scheme, Host(),
                           Port(), path, mIdent);
  }

  return NS_OK;
}

NS_IMETHODIMP
nsHttpChannelAuthProvider::CheckForSuperfluousAuth() {
  LOG(
      ("nsHttpChannelAuthProvider::CheckForSuperfluousAuth? "
       "[this=%p channel=%p]\n",
       this, mAuthChannel));

  MOZ_ASSERT(mAuthChannel, "Channel not initialized");

  // we've been called because it has been determined that this channel is
  // getting loaded without taking the userpass from the URL.  if the URL
  // contained a userpass, then (provided some other conditions are true),
  // we'll give the user an opportunity to abort the channel as this might be
  // an attempt to spoof a different site (see bug 232567).
  if (!ConfirmAuth("SuperfluousAuth", true)) {
    // calling cancel here sets our mStatus and aborts the HTTP
    // transaction, which prevents OnDataAvailable events.
    Unused << mAuthChannel->Cancel(NS_ERROR_ABORT);
    return NS_ERROR_ABORT;
  }
  return NS_OK;
}

NS_IMETHODIMP
nsHttpChannelAuthProvider::Cancel(nsresult status) {
  MOZ_ASSERT(mAuthChannel, "Channel not initialized");

  if (mAsyncPromptAuthCancelable) {
    mAsyncPromptAuthCancelable->Cancel(status);
    mAsyncPromptAuthCancelable = nullptr;
  }

  if (mGenerateCredentialsCancelable) {
    mGenerateCredentialsCancelable->Cancel(status);
    mGenerateCredentialsCancelable = nullptr;
  }
  return NS_OK;
}

NS_IMETHODIMP
nsHttpChannelAuthProvider::Disconnect(nsresult status) {
  mAuthChannel = nullptr;

  if (mAsyncPromptAuthCancelable) {
    mAsyncPromptAuthCancelable->Cancel(status);
    mAsyncPromptAuthCancelable = nullptr;
  }

  if (mGenerateCredentialsCancelable) {
    mGenerateCredentialsCancelable->Cancel(status);
    mGenerateCredentialsCancelable = nullptr;
  }

  NS_IF_RELEASE(mProxyAuthContinuationState);
  NS_IF_RELEASE(mAuthContinuationState);

  return NS_OK;
}

// helper function for getting an auth prompt from an interface requestor
static void GetAuthPrompt(nsIInterfaceRequestor* ifreq, bool proxyAuth,
                          nsIAuthPrompt2** result) {
  if (!ifreq) return;

  uint32_t promptReason;
  if (proxyAuth) {
    promptReason = nsIAuthPromptProvider::PROMPT_PROXY;
  } else {
    promptReason = nsIAuthPromptProvider::PROMPT_NORMAL;
  }

  nsCOMPtr<nsIAuthPromptProvider> promptProvider = do_GetInterface(ifreq);
  if (promptProvider) {
    promptProvider->GetAuthPrompt(promptReason, NS_GET_IID(nsIAuthPrompt2),
                                  reinterpret_cast<void**>(result));
  } else {
    NS_QueryAuthPrompt2(ifreq, result);
  }
}

// generate credentials for the given challenge, and update the auth cache.
nsresult nsHttpChannelAuthProvider::GenCredsAndSetEntry(
    nsIHttpAuthenticator* auth, bool proxyAuth, const nsACString& scheme,
    const nsACString& host, int32_t port, const nsACString& directory,
    const nsACString& realm, const nsACString& challenge,
    const nsHttpAuthIdentity& ident, nsCOMPtr<nsISupports>& sessionState,
    nsACString& result) {
  nsresult rv;
  nsISupports* ss = sessionState;

  // set informations that depend on whether
  // we're authenticating against a proxy
  // or a webserver
  nsISupports** continuationState;

  if (proxyAuth) {
    continuationState = &mProxyAuthContinuationState;
  } else {
    continuationState = &mAuthContinuationState;
  }

  rv = auth->GenerateCredentialsAsync(
      mAuthChannel, this, challenge, proxyAuth, ident.Domain(), ident.User(),
      ident.Password(), ss, *continuationState,
      getter_AddRefs(mGenerateCredentialsCancelable));
  if (NS_SUCCEEDED(rv)) {
    // Calling generate credentials async, results will be dispatched to the
    // main thread by calling OnCredsGenerated method
    return NS_ERROR_IN_PROGRESS;
  }

  uint32_t generateFlags;
  rv = auth->GenerateCredentials(
      mAuthChannel, challenge, proxyAuth, ident.Domain(), ident.User(),
      ident.Password(), &ss, &*continuationState, &generateFlags, result);

  sessionState.swap(ss);
  if (NS_FAILED(rv)) return rv;

    // don't log this in release build since it could contain sensitive info.
#ifdef DEBUG
  LOG(("generated creds: %s\n", result.BeginReading()));
#endif

  return UpdateCache(auth, scheme, host, port, directory, realm, challenge,
                     ident, result, generateFlags, sessionState, proxyAuth);
}

nsresult nsHttpChannelAuthProvider::UpdateCache(
    nsIHttpAuthenticator* auth, const nsACString& scheme,
    const nsACString& host, int32_t port, const nsACString& directory,
    const nsACString& realm, const nsACString& challenge,
    const nsHttpAuthIdentity& ident, const nsACString& creds,
    uint32_t generateFlags, nsISupports* sessionState, bool aProxyAuth) {
  nsresult rv;

  uint32_t authFlags;
  rv = auth->GetAuthFlags(&authFlags);
  if (NS_FAILED(rv)) return rv;

  // find out if this authenticator allows reuse of credentials and/or
  // challenge.
  bool saveCreds =
      0 != (authFlags & nsIHttpAuthenticator::REUSABLE_CREDENTIALS);
  bool saveChallenge =
      0 != (authFlags & nsIHttpAuthenticator::REUSABLE_CHALLENGE);

  bool saveIdentity =
      0 == (generateFlags & nsIHttpAuthenticator::USING_INTERNAL_IDENTITY);

  // this getter never fails
  nsHttpAuthCache* authCache = gHttpHandler->AuthCache(mIsPrivate);

  nsAutoCString suffix;
  if (!aProxyAuth) {
    // We don't isolate proxy credentials cache entries with the origin suffix
    // as it would only annoy users with authentication dialogs popping up.
    nsCOMPtr<nsIChannel> chan = do_QueryInterface(mAuthChannel);
    GetOriginAttributesSuffix(chan, suffix);
  }

  // create a cache entry.  we do this even though we don't yet know that
  // these credentials are valid b/c we need to avoid prompting the user
  // more than once in case the credentials are valid.
  //
  // if the credentials are not reusable, then we don't bother sticking
  // them in the auth cache.
  rv = authCache->SetAuthEntry(scheme, host, port, directory, realm,
                               saveCreds ? creds : ""_ns,
                               saveChallenge ? challenge : ""_ns, suffix,
                               saveIdentity ? &ident : nullptr, sessionState);
  return rv;
}

NS_IMETHODIMP nsHttpChannelAuthProvider::ClearProxyIdent() {
  LOG(("nsHttpChannelAuthProvider::ClearProxyIdent [this=%p]\n", this));

  mProxyIdent.Clear();
  return NS_OK;
}

nsresult nsHttpChannelAuthProvider::PrepareForAuthentication(bool proxyAuth) {
  LOG(
      ("nsHttpChannelAuthProvider::PrepareForAuthentication "
       "[this=%p channel=%p]\n",
       this, mAuthChannel));

  if (!proxyAuth) {
    // reset the current proxy continuation state because our last
    // authentication attempt was completed successfully.
    NS_IF_RELEASE(mProxyAuthContinuationState);
    LOG(("  proxy continuation state has been reset"));
  }

  if (!UsingHttpProxy() || mProxyAuthType.IsEmpty()) return NS_OK;

  // We need to remove any Proxy_Authorization header left over from a
  // non-request based authentication handshake (e.g., for NTLM auth).

  nsresult rv;
  nsCOMPtr<nsIHttpAuthenticator> precedingAuth;
  nsCString proxyAuthType;
  rv = GetAuthenticator(mProxyAuthType, proxyAuthType,
                        getter_AddRefs(precedingAuth));
  if (NS_FAILED(rv)) return rv;

  uint32_t precedingAuthFlags;
  rv = precedingAuth->GetAuthFlags(&precedingAuthFlags);
  if (NS_FAILED(rv)) return rv;

  if (!(precedingAuthFlags & nsIHttpAuthenticator::REQUEST_BASED)) {
    nsAutoCString challenges;
    rv = mAuthChannel->GetProxyChallenges(challenges);
    if (NS_FAILED(rv)) {
      // delete the proxy authorization header because we weren't
      // asked to authenticate
      rv = mAuthChannel->SetProxyCredentials(""_ns);
      if (NS_FAILED(rv)) return rv;
      LOG(("  cleared proxy authorization header"));
    }
  }

  return NS_OK;
}

class MOZ_STACK_CLASS ChallengeParser final : Tokenizer {
 public:
  explicit ChallengeParser(const nsACString& aChallenges)
      : Tokenizer(aChallenges, nullptr, "") {
    Record();
  }

  Maybe<nsDependentCSubstring> GetNext() {
    Token t;
    nsDependentCSubstring result;

    bool inQuote = false;

    while (Next(t)) {
      if (t.Type() == TOKEN_EOL) {
        Claim(result, ClaimInclusion::EXCLUDE_LAST);
        SkipWhites(WhiteSkipping::INCLUDE_NEW_LINE);
        Record();
        inQuote = false;
        if (!result.IsEmpty()) {
          return Some(result);
        }
      } else if (t.Equals(Token::Char(',')) && !inQuote &&
                 StaticPrefs::
                     network_auth_allow_multiple_challenges_same_line()) {
        // Sometimes we get multiple challenges separated by a comma.
        // This is not great, as it's slightly ambiguous. We check if something
        // is a new challenge by matching agains <param_name> =
        // If the , isn't followed by a word and = then most likely
        // it is the name of an authType.

        const char* prevCursorPos = mCursor;
        const char* prevRollbackPos = mRollback;

        auto hasWordAndEqual = [&]() {
          SkipWhites();
          nsDependentCSubstring word;
          if (!ReadWord(word)) {
            return false;
          }
          SkipWhites();
          return Check(Token::Char('='));
        };
        if (!hasWordAndEqual()) {
          // This is not a parameter. It means the `,` character starts a
          // different challenge.
          // We'll revert the cursor and return the contents so far.
          mCursor = prevCursorPos;
          mRollback = prevRollbackPos;
          Claim(result, ClaimInclusion::EXCLUDE_LAST);
          SkipWhites();
          Record();
          if (!result.IsEmpty()) {
            return Some(result);
          }
        }
      } else if (t.Equals(Token::Char('"'))) {
        inQuote = !inQuote;
      }
    }

    Claim(result, Tokenizer::ClaimInclusion::INCLUDE_LAST);
    SkipWhites();
    Record();
    if (!result.IsEmpty()) {
      return Some(result);
    }
    return Nothing{};
  }
};

enum ChallengeRank {
  Unknown = 0,
  Basic = 1,
  Digest = 2,
  NTLM = 3,
  Negotiate = 4,
};

ChallengeRank Rank(const nsACString& aChallenge) {
  if (StringBeginsWith(aChallenge, "Negotiate"_ns,
                       nsCaseInsensitiveCStringComparator)) {
    return ChallengeRank::Negotiate;
  }

  if (StringBeginsWith(aChallenge, "NTLM"_ns,
                       nsCaseInsensitiveCStringComparator)) {
    return ChallengeRank::NTLM;
  }

  if (StringBeginsWith(aChallenge, "Digest"_ns,
                       nsCaseInsensitiveCStringComparator)) {
    return ChallengeRank::Digest;
  }

  if (StringBeginsWith(aChallenge, "Basic"_ns,
                       nsCaseInsensitiveCStringComparator)) {
    return ChallengeRank::Basic;
  }

  return ChallengeRank::Unknown;
}

nsresult nsHttpChannelAuthProvider::GetCredentials(
    const nsACString& aChallenges, bool proxyAuth, nsCString& creds) {
  LOG(("nsHttpChannelAuthProvider::GetCredentials"));
  nsAutoCString challenges(aChallenges);

  using AuthChallenge = struct AuthChallenge {
    nsDependentCSubstring challenge;
    uint16_t algorithm = 0;
    ChallengeRank rank = ChallengeRank::Unknown;

    void operator=(const AuthChallenge& aOther) {
      challenge.Rebind(aOther.challenge, 0);
      algorithm = aOther.algorithm;
      rank = aOther.rank;
    }
  };

  nsTArray<AuthChallenge> cc;

  ChallengeParser p(challenges);
  while (true) {
    auto next = p.GetNext();
    if (next.isNothing()) {
      break;
    }
    AuthChallenge ac{next.ref(), 0};
    nsAutoCString realm, domain, nonce, opaque;
    bool stale = false;
    uint16_t qop = 0;
    ac.rank = Rank(ac.challenge);
    if (StringBeginsWith(ac.challenge, "Digest"_ns,
                         nsCaseInsensitiveCStringComparator)) {
      Unused << nsHttpDigestAuth::ParseChallenge(ac.challenge, realm, domain,
                                                 nonce, opaque, &stale,
                                                 &ac.algorithm, &qop);
    }
    cc.AppendElement(ac);
  }

  cc.StableSort([](const AuthChallenge& lhs, const AuthChallenge& rhs) {
    if (StaticPrefs::network_auth_choose_most_secure_challenge()) {
      // Different auth types
      if (lhs.rank != rhs.rank) {
        return lhs.rank < rhs.rank ? 1 : -1;
      }

      // If they're the same auth type, and not a Digest, then we treat them
      // as equal (don't reorder them).
      if (lhs.rank != ChallengeRank::Digest) {
        return 0;
      }
    } else {
      // Non-digest challenges should not be reordered when the pref is off.
      if (lhs.algorithm == 0 || rhs.algorithm == 0) {
        return 0;
      }
    }

    // Same algorithm.
    if (lhs.algorithm == rhs.algorithm) {
      return 0;
    }
    return lhs.algorithm < rhs.algorithm ? 1 : -1;
  });

  nsCOMPtr<nsIHttpAuthenticator> auth;
  nsCString authType;  // force heap allocation to enable string sharing since
                       // we'll be assigning this value into mAuthType.

  // set informations that depend on whether we're authenticating against a
  // proxy or a webserver
  nsISupports** currentContinuationState;
  nsCString* currentAuthType;

  if (proxyAuth) {
    currentContinuationState = &mProxyAuthContinuationState;
    currentAuthType = &mProxyAuthType;
  } else {
    currentContinuationState = &mAuthContinuationState;
    currentAuthType = &mAuthType;
  }

  nsresult rv = NS_ERROR_NOT_AVAILABLE;
  bool gotCreds = false;

  // figure out which challenge we can handle and which authenticator to use.
  for (size_t i = 0; i < cc.Length(); i++) {
    rv = GetAuthenticator(cc[i].challenge, authType, getter_AddRefs(auth));
    LOG(("trying auth for %s", authType.get()));
    if (NS_SUCCEEDED(rv)) {
      //
      // if we've already selected an auth type from a previous challenge
      // received while processing this channel, then skip others until
      // we find a challenge corresponding to the previously tried auth
      // type.
      //
      if (!currentAuthType->IsEmpty() && authType != *currentAuthType) continue;

      //
      // we allow the routines to run all the way through before we
      // decide if they are valid.
      //
      // we don't worry about the auth cache being altered because that
      // would have been the last step, and if the error is from updating
      // the authcache it wasn't really altered anyway. -CTN
      //
      // at this point the code is really only useful for client side
      // errors (it will not automatically fail over to do a different
      // auth type if the server keeps rejecting what is being sent, even
      // if a particular auth method only knows 1 thing, like a
      // non-identity based authentication method)
      //
      rv = GetCredentialsForChallenge(cc[i].challenge, authType, proxyAuth,
                                      auth, creds);
      if (NS_SUCCEEDED(rv)) {
        gotCreds = true;
        *currentAuthType = authType;

        break;
      }
      if (rv == NS_ERROR_IN_PROGRESS) {
        // authentication prompt has been invoked and result is
        // expected asynchronously, save current challenge being
        // processed and all remaining challenges to use later in
        // OnAuthAvailable and now immediately return
        mCurrentChallenge = cc[i].challenge;
        // imperfect; does not save server-side preference ordering.
        // instead, continues with remaining string as provided by client
        mRemainingChallenges.Truncate();
        while (i + 1 < cc.Length()) {
          i++;
          mRemainingChallenges.Append(cc[i].challenge);
          mRemainingChallenges.Append("\n"_ns);
        }
        return rv;
      }

      // reset the auth type and continuation state
      NS_IF_RELEASE(*currentContinuationState);
      currentAuthType->Truncate();
    }
  }

  if (!gotCreds && !currentAuthType->IsEmpty()) {
    // looks like we never found the auth type we were looking for.
    // reset the auth type and continuation state, and try again.
    currentAuthType->Truncate();
    NS_IF_RELEASE(*currentContinuationState);

    rv = GetCredentials(challenges, proxyAuth, creds);
  }

  return rv;
}

nsresult nsHttpChannelAuthProvider::GetAuthorizationMembers(
    bool proxyAuth, nsACString& scheme, nsCString& host, int32_t& port,
    nsACString& path, nsHttpAuthIdentity*& ident,
    nsISupports**& continuationState) {
  if (proxyAuth) {
    MOZ_ASSERT(UsingHttpProxy(),
               "proxyAuth is true, but no HTTP proxy is configured!");

    host = ProxyHost();
    port = ProxyPort();
    ident = &mProxyIdent;
    scheme.AssignLiteral("http");

    continuationState = &mProxyAuthContinuationState;
  } else {
    host = Host();
    port = Port();
    ident = &mIdent;

    nsresult rv;
    rv = GetCurrentPath(path);
    if (NS_FAILED(rv)) return rv;

    rv = mURI->GetScheme(scheme);
    if (NS_FAILED(rv)) return rv;

    continuationState = &mAuthContinuationState;
  }

  return NS_OK;
}

nsresult nsHttpChannelAuthProvider::GetCredentialsForChallenge(
    const nsACString& aChallenge, const nsACString& aAuthType, bool proxyAuth,
    nsIHttpAuthenticator* auth, nsCString& creds) {
  LOG(
      ("nsHttpChannelAuthProvider::GetCredentialsForChallenge "
       "[this=%p channel=%p proxyAuth=%d challenges=%s]\n",
       this, mAuthChannel, proxyAuth, nsCString(aChallenge).get()));

  // this getter never fails
  nsHttpAuthCache* authCache = gHttpHandler->AuthCache(mIsPrivate);

  uint32_t authFlags;
  nsresult rv = auth->GetAuthFlags(&authFlags);
  if (NS_FAILED(rv)) return rv;

  nsAutoCString realm;
  ParseRealm(aChallenge, realm);

  // if no realm, then use the auth type as the realm.  ToUpperCase so the
  // ficticious realm stands out a bit more.
  // XXX this will cause some single signon misses!
  // XXX this was meant to be used with NTLM, which supplies no realm.
  /*
  if (realm.IsEmpty()) {
      realm = authType;
      ToUpperCase(realm);
  }
  */

  // set informations that depend on whether
  // we're authenticating against a proxy
  // or a webserver
  nsAutoCString host;
  int32_t port;
  nsHttpAuthIdentity* ident;
  nsAutoCString path, scheme;
  bool identFromURI = false;
  nsISupports** continuationState;

  rv = GetAuthorizationMembers(proxyAuth, scheme, host, port, path, ident,
                               continuationState);
  if (NS_FAILED(rv)) return rv;

  uint32_t loadFlags;
  rv = mAuthChannel->GetLoadFlags(&loadFlags);
  if (NS_FAILED(rv)) return rv;

  // Fill only for non-proxy auth, proxy credentials are not OA-isolated.
  nsAutoCString suffix;

  if (!proxyAuth) {
    nsCOMPtr<nsIChannel> chan = do_QueryInterface(mAuthChannel);
    GetOriginAttributesSuffix(chan, suffix);

    // if this is the first challenge, then try using the identity
    // specified in the URL.
    if (mIdent.IsEmpty()) {
      GetIdentityFromURI(authFlags, mIdent);
      identFromURI = !mIdent.IsEmpty();
    }

    if ((loadFlags & nsIRequest::LOAD_ANONYMOUS) && !identFromURI) {
      LOG(("Skipping authentication for anonymous non-proxy request\n"));
      return NS_ERROR_NOT_AVAILABLE;
    }

    // Let explicit URL credentials pass
    // regardless of the LOAD_ANONYMOUS flag
  } else if ((loadFlags & nsIRequest::LOAD_ANONYMOUS) && !UsingHttpProxy()) {
    LOG(("Skipping authentication for anonymous non-proxy request\n"));
    return NS_ERROR_NOT_AVAILABLE;
  }

  //
  // if we already tried some credentials for this transaction, then
  // we need to possibly clear them from the cache, unless the credentials
  // in the cache have changed, in which case we'd want to give them a
  // try instead.
  //
  nsHttpAuthEntry* entry = nullptr;
  Unused << authCache->GetAuthEntryForDomain(scheme, host, port, realm, suffix,
                                             &entry);

  // hold reference to the auth session state (in case we clear our
  // reference to the entry).
  nsCOMPtr<nsISupports> sessionStateGrip;
  if (entry) sessionStateGrip = entry->mMetaData;

  // remember if we already had the continuation state.  it means we are in
  // the middle of the authentication exchange and the connection must be
  // kept sticky then (and only then).
  bool authAtProgress = !!*continuationState;

  // for digest auth, maybe our cached nonce value simply timed out...
  bool identityInvalid;
  nsISupports* sessionState = sessionStateGrip;
  rv = auth->ChallengeReceived(mAuthChannel, aChallenge, proxyAuth,
                               &sessionState, &*continuationState,
                               &identityInvalid);
  sessionStateGrip.swap(sessionState);
  if (NS_FAILED(rv)) return rv;

  LOG(("  identity invalid = %d\n", identityInvalid));

  if (mConnectionBased && identityInvalid) {
    // If the flag is set and identity is invalid, it means we received the
    // first challange for a new negotiation round after negotiating a
    // connection based auth failed (invalid password). The mConnectionBased
    // flag is set later for the newly received challenge, so here it reflects
    // the previous 401/7 response schema.
    rv = mAuthChannel->CloseStickyConnection();
    MOZ_ASSERT(NS_SUCCEEDED(rv));
    if (!proxyAuth) {
      // We must clear proxy ident in the following scenario + explanation:
      // - we are authenticating to an NTLM proxy and an NTLM server
      // - we successfully authenticated to the proxy, mProxyIdent keeps
      //   the user name/domain and password, the identity has also been cached
      // - we just threw away the connection because we are now asking for
      //   creds for the server (WWW auth)
      // - hence, we will have to auth to the proxy again as well
      // - if we didn't clear the proxy identity, it would be considered
      //   as non-valid and we would ask the user again ; clearing it forces
      //   use of the cached identity and not asking the user again
      ClearProxyIdent();
    }
  }

  mConnectionBased = !!(authFlags & nsIHttpAuthenticator::CONNECTION_BASED);

  // It's legal if the peer closes the connection after the first 401/7.
  // Making the connection sticky will prevent its restart giving the user
  // a 'network reset' error every time.  Hence, we mark the connection
  // as restartable.
  mAuthChannel->ConnectionRestartable(!authAtProgress);

  if (identityInvalid) {
    if (entry) {
      if (ident->Equals(entry->Identity())) {
        if (!identFromURI) {
          LOG(("  clearing bad auth cache entry\n"));
          // ok, we've already tried this user identity, so clear the
          // corresponding entry from the auth cache.
          authCache->ClearAuthEntry(scheme, host, port, realm, suffix);
          entry = nullptr;
          ident->Clear();
        }
      } else if (!identFromURI ||
                 (ident->User() == entry->Identity().User() &&
                  !(loadFlags & (nsIChannel::LOAD_ANONYMOUS |
                                 nsIChannel::LOAD_EXPLICIT_CREDENTIALS)))) {
        LOG(("  taking identity from auth cache\n"));
        // the password from the auth cache is more likely to be
        // correct than the one in the URL.  at least, we know that it
        // works with the given username.  it is possible for a server
        // to distinguish logons based on the supplied password alone,
        // but that would be quite unusual... and i don't think we need
        // to worry about such unorthodox cases.
        *ident = entry->Identity();
        identFromURI = false;
        if (entry->Creds()[0] != '\0') {
          LOG(("    using cached credentials!\n"));
          creds.Assign(entry->Creds());
          return entry->AddPath(path);
        }
      }
    } else if (!identFromURI) {
      // hmm... identity invalid, but no auth entry!  the realm probably
      // changed (see bug 201986).
      ident->Clear();
    }

    if (!entry && ident->IsEmpty()) {
      uint32_t level = nsIAuthPrompt2::LEVEL_NONE;
      if ((!proxyAuth && mUsingSSL) || (proxyAuth && mProxyUsingSSL)) {
        level = nsIAuthPrompt2::LEVEL_SECURE;
      } else if (authFlags & nsIHttpAuthenticator::IDENTITY_ENCRYPTED) {
        level = nsIAuthPrompt2::LEVEL_PW_ENCRYPTED;
      }

      // Collect statistics on how frequently the various types of HTTP
      // authentication are used over SSL and non-SSL connections.
      if (Telemetry::CanRecordPrereleaseData()) {
        if ("basic"_ns.Equals(aAuthType, nsCaseInsensitiveCStringComparator)) {
          Telemetry::Accumulate(
              Telemetry::HTTP_AUTH_TYPE_STATS,
              UsingSSL() ? HTTP_AUTH_BASIC_SECURE : HTTP_AUTH_BASIC_INSECURE);
        } else if ("digest"_ns.Equals(aAuthType,
                                      nsCaseInsensitiveCStringComparator)) {
          Telemetry::Accumulate(
              Telemetry::HTTP_AUTH_TYPE_STATS,
              UsingSSL() ? HTTP_AUTH_DIGEST_SECURE : HTTP_AUTH_DIGEST_INSECURE);
        } else if ("ntlm"_ns.Equals(aAuthType,
                                    nsCaseInsensitiveCStringComparator)) {
          Telemetry::Accumulate(
              Telemetry::HTTP_AUTH_TYPE_STATS,
              UsingSSL() ? HTTP_AUTH_NTLM_SECURE : HTTP_AUTH_NTLM_INSECURE);
        } else if ("negotiate"_ns.Equals(aAuthType,
                                         nsCaseInsensitiveCStringComparator)) {
          Telemetry::Accumulate(Telemetry::HTTP_AUTH_TYPE_STATS,
                                UsingSSL() ? HTTP_AUTH_NEGOTIATE_SECURE
                                           : HTTP_AUTH_NEGOTIATE_INSECURE);
        }
      }

      // Depending on the pref setting, the authentication dialog may be
      // blocked for all sub-resources, blocked for cross-origin
      // sub-resources, or always allowed for sub-resources.
      // For more details look at the bug 647010.
      // BlockPrompt will set mCrossOrigin parameter as well.
      if (BlockPrompt(proxyAuth)) {
        LOG((
            "nsHttpChannelAuthProvider::GetCredentialsForChallenge: "
            "Prompt is blocked [this=%p pref=%d img-pref=%d "
            "non-web-content-triggered-pref=%d]\n",
            this, StaticPrefs::network_auth_subresource_http_auth_allow(),
            StaticPrefs::
                network_auth_subresource_img_cross_origin_http_auth_allow(),
            StaticPrefs::
                network_auth_non_web_content_triggered_resources_http_auth_allow()));
        return NS_ERROR_ABORT;
      }

      // at this point we are forced to interact with the user to get
      // their username and password for this domain.
      rv = PromptForIdentity(level, proxyAuth, realm, aAuthType, authFlags,
                             *ident);
      if (NS_FAILED(rv)) return rv;
      identFromURI = false;
    }
  }

  if (identFromURI) {
    // Warn the user before automatically using the identity from the URL
    // to automatically log them into a site (see bug 232567).
    if (!ConfirmAuth("AutomaticAuth", false)) {
      // calling cancel here sets our mStatus and aborts the HTTP
      // transaction, which prevents OnDataAvailable events.
      rv = mAuthChannel->Cancel(NS_ERROR_ABORT);
      MOZ_ASSERT(NS_SUCCEEDED(rv));
      // this return code alone is not equivalent to Cancel, since
      // it only instructs our caller that authentication failed.
      // without an explicit call to Cancel, our caller would just
      // load the page that accompanies the HTTP auth challenge.
      return NS_ERROR_ABORT;
    }
  }

  //
  // get credentials for the given user:pass
  //
  // always store the credentials we're trying now so that they will be used
  // on subsequent links.  This will potentially remove good credentials from
  // the cache.  This is ok as we don't want to use cached credentials if the
  // user specified something on the URI or in another manner.  This is so
  // that we don't transparently authenticate as someone they're not
  // expecting to authenticate as.
  //
  nsCString result;
  rv = GenCredsAndSetEntry(auth, proxyAuth, scheme, host, port, path, realm,
                           aChallenge, *ident, sessionStateGrip, creds);
  return rv;
}

bool nsHttpChannelAuthProvider::BlockPrompt(bool proxyAuth) {
  // Verify that it's ok to prompt for credentials here, per spec
  // http://xhr.spec.whatwg.org/#the-send%28%29-method

  nsCOMPtr<nsIHttpChannelInternal> chanInternal =
      do_QueryInterface(mAuthChannel);
  MOZ_ASSERT(chanInternal);

  if (chanInternal->GetBlockAuthPrompt()) {
    LOG(
        ("nsHttpChannelAuthProvider::BlockPrompt: Prompt is blocked "
         "[this=%p channel=%p]\n",
         this, mAuthChannel));
    return true;
  }

  if (proxyAuth) {
    // Do not block auth-dialog if this is a proxy authentication.
    return false;
  }

  nsCOMPtr<nsIChannel> chan = do_QueryInterface(mAuthChannel);
  nsCOMPtr<nsILoadInfo> loadInfo = chan->LoadInfo();

  // We will treat loads w/o loadInfo as a top level document.
  bool topDoc = true;
  bool xhr = false;
  bool nonWebContent = false;

  if (loadInfo->GetExternalContentPolicyType() !=
      ExtContentPolicy::TYPE_DOCUMENT) {
    topDoc = false;
  }

  if (!topDoc) {
    nsCOMPtr<nsIPrincipal> triggeringPrinc = loadInfo->TriggeringPrincipal();
    if (triggeringPrinc->IsSystemPrincipal()) {
      nonWebContent = true;
    }
  }

  if (loadInfo->GetExternalContentPolicyType() ==
      ExtContentPolicy::TYPE_XMLHTTPREQUEST) {
    xhr = true;
  }

  if (!topDoc && !xhr) {
    nsCOMPtr<nsIURI> topURI;
    Unused << chanInternal->GetTopWindowURI(getter_AddRefs(topURI));
    if (topURI) {
      mCrossOrigin = !NS_SecurityCompareURIs(topURI, mURI, true);
    } else {
      nsIPrincipal* loadingPrinc = loadInfo->GetLoadingPrincipal();
      MOZ_ASSERT(loadingPrinc);
      mCrossOrigin = !loadingPrinc->IsSameOrigin(mURI);
    }
  }

  if (Telemetry::CanRecordPrereleaseData()) {
    if (topDoc) {
      Telemetry::Accumulate(Telemetry::HTTP_AUTH_DIALOG_STATS_3,
                            HTTP_AUTH_DIALOG_TOP_LEVEL_DOC);
    } else if (nonWebContent) {
      Telemetry::Accumulate(Telemetry::HTTP_AUTH_DIALOG_STATS_3,
                            HTTP_AUTH_DIALOG_NON_WEB_CONTENT);
    } else if (!mCrossOrigin) {
      if (xhr) {
        Telemetry::Accumulate(Telemetry::HTTP_AUTH_DIALOG_STATS_3,
                              HTTP_AUTH_DIALOG_SAME_ORIGIN_XHR);
      } else {
        Telemetry::Accumulate(Telemetry::HTTP_AUTH_DIALOG_STATS_3,
                              HTTP_AUTH_DIALOG_SAME_ORIGIN_SUBRESOURCE);
      }
    } else {
      Telemetry::Accumulate(
          Telemetry::HTTP_AUTH_DIALOG_STATS_3,
          static_cast<uint32_t>(loadInfo->GetExternalContentPolicyType()));
    }
  }

  if (!topDoc &&
      !StaticPrefs::
          network_auth_non_web_content_triggered_resources_http_auth_allow() &&
      nonWebContent) {
    return true;
  }

  switch (StaticPrefs::network_auth_subresource_http_auth_allow()) {
    case SUBRESOURCE_AUTH_DIALOG_DISALLOW_ALL:
      // Do not open the http-authentication credentials dialog for
      // the sub-resources.
      return !topDoc && !xhr;
    case SUBRESOURCE_AUTH_DIALOG_DISALLOW_CROSS_ORIGIN:
      // Open the http-authentication credentials dialog for
      // the sub-resources only if they are not cross-origin.
      return !topDoc && !xhr && mCrossOrigin;
    case SUBRESOURCE_AUTH_DIALOG_ALLOW_ALL:
      // Allow the http-authentication dialog for subresources.
      // If pref network.auth.subresource-img-cross-origin-http-auth-allow
      // is set, http-authentication dialog for image subresources is
      // blocked.
      if (mCrossOrigin &&
          !StaticPrefs::
              network_auth_subresource_img_cross_origin_http_auth_allow() &&
          loadInfo &&
          ((loadInfo->GetExternalContentPolicyType() ==
            ExtContentPolicy::TYPE_IMAGE) ||
           (loadInfo->GetExternalContentPolicyType() ==
            ExtContentPolicy::TYPE_IMAGESET))) {
        return true;
      }
      return false;
    default:
      // This is an invalid value.
      MOZ_ASSERT(false, "A non valid value!");
  }
  return false;
}

inline void GetAuthType(const nsACString& aChallenge, nsCString& authType) {
  auto spaceIndex = aChallenge.FindChar(' ');
  authType = Substring(aChallenge, 0, spaceIndex);
  // normalize to lowercase
  ToLowerCase(authType);
}

nsresult nsHttpChannelAuthProvider::GetAuthenticator(
    const nsACString& aChallenge, nsCString& authType,
    nsIHttpAuthenticator** auth) {
  LOG(("nsHttpChannelAuthProvider::GetAuthenticator [this=%p channel=%p]\n",
       this, mAuthChannel));

  GetAuthType(aChallenge, authType);

  nsCOMPtr<nsIHttpAuthenticator> authenticator;
#ifdef MOZ_AUTH_EXTENSION
  if (authType.EqualsLiteral("negotiate")) {
    authenticator = nsHttpNegotiateAuth::GetOrCreate();
  } else
#endif
      if (authType.EqualsLiteral("basic")) {
    authenticator = nsHttpBasicAuth::GetOrCreate();
  } else if (authType.EqualsLiteral("digest")) {
    authenticator = nsHttpDigestAuth::GetOrCreate();
  } else if (authType.EqualsLiteral("ntlm")) {
    authenticator = nsHttpNTLMAuth::GetOrCreate();
  } else if (authType.EqualsLiteral("mock_auth") &&
             PR_GetEnv("XPCSHELL_TEST_PROFILE_DIR")) {
    authenticator = MockHttpAuth::Create();
  } else {
    return NS_ERROR_FACTORY_NOT_REGISTERED;
  }

  if (!authenticator) {
    // If called during shutdown it's possible that the singleton authenticator
    // was already cleared so we have a null one here.
    return NS_ERROR_NOT_AVAILABLE;
  }

  MOZ_ASSERT(authenticator);
  authenticator.forget(auth);

  return NS_OK;
}

// buf contains "domain\user"
static void ParseUserDomain(const nsAString& buf, nsDependentSubstring& user,
                            nsDependentSubstring& domain) {
  auto backslashPos = buf.FindChar(u'\\');
  if (backslashPos != kNotFound) {
    domain.Rebind(buf, 0, backslashPos);
    user.Rebind(buf, backslashPos + 1);
  }
}

void nsHttpChannelAuthProvider::GetIdentityFromURI(uint32_t authFlags,
                                                   nsHttpAuthIdentity& ident) {
  LOG(("nsHttpChannelAuthProvider::GetIdentityFromURI [this=%p channel=%p]\n",
       this, mAuthChannel));

  nsAutoString userBuf;
  nsAutoString passBuf;

  // XXX i18n
  nsAutoCString buf;
  mURI->GetUsername(buf);
  if (!buf.IsEmpty()) {
    NS_UnescapeURL(buf);
    CopyUTF8toUTF16(buf, userBuf);
    mURI->GetPassword(buf);
    if (!buf.IsEmpty()) {
      NS_UnescapeURL(buf);
      CopyUTF8toUTF16(buf, passBuf);
    }
  }

  if (!userBuf.IsEmpty()) {
    nsDependentSubstring user(userBuf, 0);
    nsDependentSubstring domain(u""_ns, 0);

    if (authFlags & nsIHttpAuthenticator::IDENTITY_INCLUDES_DOMAIN) {
      ParseUserDomain(userBuf, user, domain);
    }

    ident = nsHttpAuthIdentity(domain, user, passBuf);
  }
}

static void OldParseRealm(const nsACString& aChallenge, nsACString& realm) {
  //
  // From RFC2617 section 1.2, the realm value is defined as such:
  //
  //    realm       = "realm" "=" realm-value
  //    realm-value = quoted-string
  //
  // but, we'll accept anything after the the "=" up to the first space, or
  // end-of-line, if the string is not quoted.
  //

  const nsCString& flat = PromiseFlatCString(aChallenge);
  const char* challenge = flat.get();

  const char* p = nsCRT::strcasestr(challenge, "realm=");
  if (p) {
    bool has_quote = false;
    p += 6;
    if (*p == '"') {
      has_quote = true;
      p++;
    }

    const char* end;
    if (has_quote) {
      end = p;
      while (*end) {
        if (*end == '\\') {
          // escaped character, store that one instead if not zero
          if (!*++end) break;
        } else if (*end == '\"') {
          // end of string
          break;
        }

        realm.Append(*end);
        ++end;
      }
    } else {
      // realm given without quotes
      end = strchr(p, ' ');
      if (end) {
        realm.Assign(p, end - p);
      } else {
        realm.Assign(p);
      }
    }
  }
}

void nsHttpChannelAuthProvider::ParseRealm(const nsACString& aChallenge,
                                           nsACString& realm) {
  //
  // From RFC2617 section 1.2, the realm value is defined as such:
  //
  //    realm       = "realm" "=" realm-value
  //    realm-value = quoted-string
  //
  // but, we'll accept anything after the the "=" up to the first space, or
  // end-of-line, if the string is not quoted.
  //

  if (!StaticPrefs::network_auth_use_new_parse_realm()) {
    OldParseRealm(aChallenge, realm);
    return;
  }

  Tokenizer t(aChallenge);

  // The challenge begins with the authType.
  // If we can't find that something has probably gone wrong.
  t.SkipWhites();
  nsDependentCSubstring authType;
  if (!t.ReadWord(authType)) {
    return;
  }

  // Will return true if the tokenizer advanced the cursor - false otherwise.
  auto readParam = [&](nsDependentCSubstring& key, nsAutoCString& value) {
    key.Rebind(EmptyCString(), 0);
    value.Truncate();

    t.SkipWhites();
    if (!t.ReadWord(key)) {
      return false;
    }
    t.SkipWhites();
    if (!t.CheckChar('=')) {
      return true;
    }
    t.SkipWhites();

    Tokenizer::Token token1;

    t.Record();
    if (!t.Next(token1)) {
      return true;
    }
    nsDependentCSubstring sub;
    bool hasQuote = false;
    if (token1.Equals(Tokenizer::Token::Char('"'))) {
      hasQuote = true;
    } else {
      t.Claim(sub, Tokenizer::ClaimInclusion::INCLUDE_LAST);
      value.Append(sub);
    }
    t.Record();
    Tokenizer::Token token2;
    while (t.Next(token2)) {
      if (hasQuote && token2.Equals(Tokenizer::Token::Char('"')) &&
          !token1.Equals(Tokenizer::Token::Char('\\'))) {
        break;
      }
      if (!hasQuote && (token2.Type() == Tokenizer::TokenType::TOKEN_WS ||
                        token2.Type() == Tokenizer::TokenType::TOKEN_EOL)) {
        break;
      }

      t.Claim(sub, Tokenizer::ClaimInclusion::INCLUDE_LAST);
      if (!sub.Equals(R"(\)")) {
        value.Append(sub);
      }
      t.Record();
      token1 = token2;
    }
    return true;
  };

  while (!t.CheckEOF()) {
    nsDependentCSubstring key;
    nsAutoCString value;
    // If we couldn't read anything, and the input isn't followed by a ,
    // then we exit.
    if (!readParam(key, value) && !t.Check(Tokenizer::Token::Char(','))) {
      break;
    }
    // When we find the first instance of realm we exit.
    // Theoretically there should be only one instance and we should fail
    // if there are more, but we're trying to preserve existing behaviour.
    if (key.Equals("realm"_ns, nsCaseInsensitiveCStringComparator)) {
      realm = value;
      break;
    }
  }
}

class nsHTTPAuthInformation : public nsAuthInformationHolder {
 public:
  nsHTTPAuthInformation(uint32_t aFlags, const nsString& aRealm,
                        const nsACString& aAuthType)
      : nsAuthInformationHolder(aFlags, aRealm, aAuthType) {}

  void SetToHttpAuthIdentity(uint32_t authFlags, nsHttpAuthIdentity& identity);
};

void nsHTTPAuthInformation::SetToHttpAuthIdentity(
    uint32_t authFlags, nsHttpAuthIdentity& identity) {
  identity = nsHttpAuthIdentity(Domain(), User(), Password());
}

nsresult nsHttpChannelAuthProvider::PromptForIdentity(
    uint32_t level, bool proxyAuth, const nsACString& realm,
    const nsACString& authType, uint32_t authFlags, nsHttpAuthIdentity& ident) {
  LOG(("nsHttpChannelAuthProvider::PromptForIdentity [this=%p channel=%p]\n",
       this, mAuthChannel));

  nsresult rv;

  nsCOMPtr<nsIInterfaceRequestor> callbacks;
  rv = mAuthChannel->GetNotificationCallbacks(getter_AddRefs(callbacks));
  if (NS_FAILED(rv)) return rv;

  nsCOMPtr<nsILoadGroup> loadGroup;
  rv = mAuthChannel->GetLoadGroup(getter_AddRefs(loadGroup));
  if (NS_FAILED(rv)) return rv;

  nsCOMPtr<nsIAuthPrompt2> authPrompt;
  GetAuthPrompt(callbacks, proxyAuth, getter_AddRefs(authPrompt));
  if (!authPrompt && loadGroup) {
    nsCOMPtr<nsIInterfaceRequestor> cbs;
    loadGroup->GetNotificationCallbacks(getter_AddRefs(cbs));
    GetAuthPrompt(cbs, proxyAuth, getter_AddRefs(authPrompt));
  }
  if (!authPrompt) return NS_ERROR_NO_INTERFACE;

  // XXX i18n: need to support non-ASCII realm strings (see bug 41489)
  NS_ConvertASCIItoUTF16 realmU(realm);

  // prompt the user...
  uint32_t promptFlags = 0;
  if (proxyAuth) {
    promptFlags |= nsIAuthInformation::AUTH_PROXY;
    if (mTriedProxyAuth) promptFlags |= nsIAuthInformation::PREVIOUS_FAILED;
    mTriedProxyAuth = true;
  } else {
    promptFlags |= nsIAuthInformation::AUTH_HOST;
    if (mTriedHostAuth) promptFlags |= nsIAuthInformation::PREVIOUS_FAILED;
    mTriedHostAuth = true;
  }

  if (authFlags & nsIHttpAuthenticator::IDENTITY_INCLUDES_DOMAIN) {
    promptFlags |= nsIAuthInformation::NEED_DOMAIN;
  }

  if (mCrossOrigin) {
    promptFlags |= nsIAuthInformation::CROSS_ORIGIN_SUB_RESOURCE;
  }

  RefPtr<nsHTTPAuthInformation> holder =
      new nsHTTPAuthInformation(promptFlags, realmU, authType);
  if (!holder) return NS_ERROR_OUT_OF_MEMORY;

  nsCOMPtr<nsIChannel> channel(do_QueryInterface(mAuthChannel, &rv));
  if (NS_FAILED(rv)) return rv;

  rv = authPrompt->AsyncPromptAuth(channel, this, nullptr, level, holder,
                                   getter_AddRefs(mAsyncPromptAuthCancelable));

  if (NS_SUCCEEDED(rv)) {
    // indicate using this error code that authentication prompt
    // result is expected asynchronously
    rv = NS_ERROR_IN_PROGRESS;
  } else {
    // Fall back to synchronous prompt
    bool retval = false;
    rv = authPrompt->PromptAuth(channel, level, holder, &retval);
    if (NS_FAILED(rv)) return rv;

    if (!retval) {
      rv = NS_ERROR_ABORT;
    } else {
      holder->SetToHttpAuthIdentity(authFlags, ident);
    }
  }

  // remember that we successfully showed the user an auth dialog
  if (!proxyAuth) mSuppressDefensiveAuth = true;

  if (mConnectionBased) {
    // Connection can be reset by the server in the meantime user is entering
    // the credentials.  Result would be just a "Connection was reset" error.
    // Hence, we drop the current regardless if the user would make it on time
    // to provide credentials.
    // It's OK to send the NTLM type 1 message (response to the plain "NTLM"
    // challenge) on a new connection.
    {
      DebugOnly<nsresult> rv = mAuthChannel->CloseStickyConnection();
      MOZ_ASSERT(NS_SUCCEEDED(rv));
    }
  }

  return rv;
}

NS_IMETHODIMP nsHttpChannelAuthProvider::OnAuthAvailable(
    nsISupports* aContext, nsIAuthInformation* aAuthInfo) {
  LOG(("nsHttpChannelAuthProvider::OnAuthAvailable [this=%p channel=%p]", this,
       mAuthChannel));

  mAsyncPromptAuthCancelable = nullptr;
  if (!mAuthChannel) return NS_OK;

  nsresult rv;

  nsAutoCString host;
  int32_t port;
  nsHttpAuthIdentity* ident;
  nsAutoCString path, scheme;
  nsISupports** continuationState;
  rv = GetAuthorizationMembers(mProxyAuth, scheme, host, port, path, ident,
                               continuationState);
  if (NS_FAILED(rv)) OnAuthCancelled(aContext, false);

  nsAutoCString realm;
  ParseRealm(mCurrentChallenge, realm);

  nsCOMPtr<nsIChannel> chan = do_QueryInterface(mAuthChannel);
  nsAutoCString suffix;
  if (!mProxyAuth) {
    // Fill only for non-proxy auth, proxy credentials are not OA-isolated.
    GetOriginAttributesSuffix(chan, suffix);
  }

  nsHttpAuthCache* authCache = gHttpHandler->AuthCache(mIsPrivate);
  nsHttpAuthEntry* entry = nullptr;
  Unused << authCache->GetAuthEntryForDomain(scheme, host, port, realm, suffix,
                                             &entry);

  nsCOMPtr<nsISupports> sessionStateGrip;
  if (entry) sessionStateGrip = entry->mMetaData;

  nsAuthInformationHolder* holder =
      static_cast<nsAuthInformationHolder*>(aAuthInfo);
  *ident =
      nsHttpAuthIdentity(holder->Domain(), holder->User(), holder->Password());

  nsAutoCString unused;
  nsCOMPtr<nsIHttpAuthenticator> auth;
  rv = GetAuthenticator(mCurrentChallenge, unused, getter_AddRefs(auth));
  if (NS_FAILED(rv)) {
    MOZ_ASSERT(false, "GetAuthenticator failed");
    OnAuthCancelled(aContext, true);
    return NS_OK;
  }

  nsCString creds;
  rv = GenCredsAndSetEntry(auth, mProxyAuth, scheme, host, port, path, realm,
                           mCurrentChallenge, *ident, sessionStateGrip, creds);

  mCurrentChallenge.Truncate();
  if (NS_FAILED(rv)) {
    OnAuthCancelled(aContext, true);
    return NS_OK;
  }

  return ContinueOnAuthAvailable(creds);
}

NS_IMETHODIMP nsHttpChannelAuthProvider::OnAuthCancelled(nsISupports* aContext,
                                                         bool userCancel) {
  LOG(("nsHttpChannelAuthProvider::OnAuthCancelled [this=%p channel=%p]", this,
       mAuthChannel));

  mAsyncPromptAuthCancelable = nullptr;
  if (!mAuthChannel) return NS_OK;

  // When user cancels or auth fails we want to close the connection for
  // connection based schemes like NTLM.  Some servers don't like re-negotiation
  // on the same connection.
  nsresult rv;
  if (mConnectionBased) {
    rv = mAuthChannel->CloseStickyConnection();
    MOZ_ASSERT(NS_SUCCEEDED(rv));
    mConnectionBased = false;
  }

  nsCOMPtr<nsIChannel> channel = do_QueryInterface(mAuthChannel);
  if (channel) {
    nsresult status;
    Unused << channel->GetStatus(&status);
    if (NS_FAILED(status)) {
      // If the channel is already cancelled, there is no need to deal with the
      // rest challenges.
      LOG(("  Clear mRemainingChallenges, since mAuthChannel is cancelled"));
      mRemainingChallenges.Truncate();
    }
  }

  if (userCancel) {
    if (!mRemainingChallenges.IsEmpty()) {
      // there are still some challenges to process, do so

      // Get rid of current continuationState to avoid reusing it in
      // next challenges since it is no longer relevant.
      if (mProxyAuth) {
        NS_IF_RELEASE(mProxyAuthContinuationState);
      } else {
        NS_IF_RELEASE(mAuthContinuationState);
      }
      nsAutoCString creds;
      rv = GetCredentials(mRemainingChallenges, mProxyAuth, creds);
      if (NS_SUCCEEDED(rv)) {
        // GetCredentials loaded the credentials from the cache or
        // some other way in a synchronous manner, process those
        // credentials now
        mRemainingChallenges.Truncate();
        return ContinueOnAuthAvailable(creds);
      }
      if (rv == NS_ERROR_IN_PROGRESS) {
        // GetCredentials successfully queued another authprompt for
        // a challenge from the list, we are now waiting for the user
        // to provide the credentials
        return NS_OK;
      }

      // otherwise, we failed...
    }

    mRemainingChallenges.Truncate();
  }

  rv = mAuthChannel->OnAuthCancelled(userCancel);
  MOZ_ASSERT(NS_SUCCEEDED(rv));

  return NS_OK;
}

NS_IMETHODIMP nsHttpChannelAuthProvider::OnCredsGenerated(
    const nsACString& aGeneratedCreds, uint32_t aFlags, nsresult aResult,
    nsISupports* aSessionState, nsISupports* aContinuationState) {
  nsresult rv;

  MOZ_ASSERT(NS_IsMainThread());

  // When channel is closed, do not proceed
  if (!mAuthChannel) {
    return NS_OK;
  }

  mGenerateCredentialsCancelable = nullptr;

  if (NS_FAILED(aResult)) {
    return OnAuthCancelled(nullptr, true);
  }

  // We want to update m(Proxy)AuthContinuationState in case it was changed by
  // nsHttpNegotiateAuth::GenerateCredentials
  nsCOMPtr<nsISupports> contState(aContinuationState);
  if (mProxyAuth) {
    contState.swap(mProxyAuthContinuationState);
  } else {
    contState.swap(mAuthContinuationState);
  }

  nsCOMPtr<nsIHttpAuthenticator> auth;
  nsAutoCString unused;
  rv = GetAuthenticator(mCurrentChallenge, unused, getter_AddRefs(auth));
  NS_ENSURE_SUCCESS(rv, rv);

  nsAutoCString host;
  int32_t port;
  nsHttpAuthIdentity* ident;
  nsAutoCString directory, scheme;
  nsISupports** unusedContinuationState;

  // Get realm from challenge
  nsAutoCString realm;
  ParseRealm(mCurrentChallenge, realm);

  rv = GetAuthorizationMembers(mProxyAuth, scheme, host, port, directory, ident,
                               unusedContinuationState);
  if (NS_FAILED(rv)) return rv;

  rv =
      UpdateCache(auth, scheme, host, port, directory, realm, mCurrentChallenge,
                  *ident, aGeneratedCreds, aFlags, aSessionState, mProxyAuth);
  MOZ_ASSERT(NS_SUCCEEDED(rv));
  mCurrentChallenge.Truncate();

  rv = ContinueOnAuthAvailable(aGeneratedCreds);
  MOZ_ASSERT(NS_SUCCEEDED(rv));
  return NS_OK;
}

nsresult nsHttpChannelAuthProvider::ContinueOnAuthAvailable(
    const nsACString& creds) {
  nsresult rv;
  if (mProxyAuth) {
    rv = mAuthChannel->SetProxyCredentials(creds);
  } else {
    rv = mAuthChannel->SetWWWCredentials(creds);
  }
  if (NS_FAILED(rv)) return rv;

  // drop our remaining list of challenges.  We don't need them, because we
  // have now authenticated against a challenge and will be sending that
  // information to the server (or proxy).  If it doesn't accept our
  // authentication it'll respond with failure and resend the challenge list
  mRemainingChallenges.Truncate();

  Unused << mAuthChannel->OnAuthAvailable();

  return NS_OK;
}

bool nsHttpChannelAuthProvider::ConfirmAuth(const char* bundleKey,
                                            bool doYesNoPrompt) {
  // skip prompting the user if
  //   1) prompts are disabled by pref
  //   2) we've already prompted the user
  //   3) we're not a toplevel channel
  //   4) the userpass length is less than the "phishy" threshold

  if (!StaticPrefs::network_auth_confirmAuth_enabled()) {
    return true;
  }

  uint32_t loadFlags;
  nsresult rv = mAuthChannel->GetLoadFlags(&loadFlags);
  if (NS_FAILED(rv)) return true;

  if (mSuppressDefensiveAuth ||
      !(loadFlags & nsIChannel::LOAD_INITIAL_DOCUMENT_URI)) {
    return true;
  }

  nsAutoCString userPass;
  rv = mURI->GetUserPass(userPass);
  if (NS_FAILED(rv) ||
      (userPass.Length() < gHttpHandler->PhishyUserPassLength())) {
    return true;
  }

  // we try to confirm by prompting the user.  if we cannot do so, then
  // assume the user said ok.  this is done to keep things working in
  // embedded builds, where the string bundle might not be present, etc.

  nsCOMPtr<nsIStringBundleService> bundleService =
      do_GetService(NS_STRINGBUNDLE_CONTRACTID);
  if (!bundleService) return true;

  nsCOMPtr<nsIStringBundle> bundle;
  bundleService->CreateBundle(NECKO_MSGS_URL, getter_AddRefs(bundle));
  if (!bundle) return true;

  nsAutoCString host;
  rv = mURI->GetHost(host);
  if (NS_FAILED(rv)) return true;

  nsAutoCString user;
  rv = mURI->GetUsername(user);
  if (NS_FAILED(rv)) return true;

  NS_ConvertUTF8toUTF16 ucsHost(host), ucsUser(user);

  size_t userLength = ucsUser.Length();
  if (userLength > MAX_DISPLAYED_USER_LENGTH) {
    size_t desiredLength = MAX_DISPLAYED_USER_LENGTH;
    // Don't cut off right before a low surrogate. Just include it.
    if (NS_IS_LOW_SURROGATE(ucsUser[desiredLength])) {
      desiredLength++;
    }
    ucsUser.Replace(desiredLength, userLength - desiredLength,
                    nsContentUtils::GetLocalizedEllipsis());
  }

  size_t hostLen = ucsHost.Length();
  if (hostLen > MAX_DISPLAYED_HOST_LENGTH) {
    size_t cutPoint = hostLen - MAX_DISPLAYED_HOST_LENGTH;
    // Likewise, don't cut off right before a low surrogate here.
    // Keep the low surrogate
    if (NS_IS_LOW_SURROGATE(ucsHost[cutPoint])) {
      cutPoint--;
    }
    // It's possible cutPoint was 1 and is now 0. Only insert the ellipsis
    // if we're actually removing anything.
    if (cutPoint > 0) {
      ucsHost.Replace(0, cutPoint, nsContentUtils::GetLocalizedEllipsis());
    }
  }

  AutoTArray<nsString, 2> strs = {ucsHost, ucsUser};

  nsAutoString msg;
  rv = bundle->FormatStringFromName(bundleKey, strs, msg);
  if (NS_FAILED(rv)) return true;

  nsCOMPtr<nsIInterfaceRequestor> callbacks;
  rv = mAuthChannel->GetNotificationCallbacks(getter_AddRefs(callbacks));
  if (NS_FAILED(rv)) return true;

  nsCOMPtr<nsILoadGroup> loadGroup;
  rv = mAuthChannel->GetLoadGroup(getter_AddRefs(loadGroup));
  if (NS_FAILED(rv)) return true;

  nsCOMPtr<nsIPromptService> promptSvc =
      do_GetService("@mozilla.org/prompter;1", &rv);
  if (NS_FAILED(rv) || !promptSvc) {
    return true;
  }

  // do not prompt again
  mSuppressDefensiveAuth = true;

  // Get current browsing context to use as prompt parent
  nsCOMPtr<nsIChannel> chan = do_QueryInterface(mAuthChannel);
  if (!chan) {
    return true;
  }

  nsCOMPtr<nsILoadInfo> loadInfo = chan->LoadInfo();
  RefPtr<mozilla::dom::BrowsingContext> browsingContext;
  loadInfo->GetBrowsingContext(getter_AddRefs(browsingContext));

  bool confirmed;
  if (doYesNoPrompt) {
    int32_t choice;
    bool checkState = false;
    rv = promptSvc->ConfirmExBC(
        browsingContext, StaticPrefs::prompts_modalType_confirmAuth(), nullptr,
        msg.get(),
        nsIPromptService::BUTTON_POS_1_DEFAULT +
            nsIPromptService::STD_YES_NO_BUTTONS,
        nullptr, nullptr, nullptr, nullptr, &checkState, &choice);
    if (NS_FAILED(rv)) return true;

    confirmed = choice == 0;
  } else {
    rv = promptSvc->ConfirmBC(browsingContext,
                              StaticPrefs::prompts_modalType_confirmAuth(),
                              nullptr, msg.get(), &confirmed);
    if (NS_FAILED(rv)) return true;
  }

  return confirmed;
}

void nsHttpChannelAuthProvider::SetAuthorizationHeader(
    nsHttpAuthCache* authCache, const nsHttpAtom& header,
    const nsACString& scheme, const nsACString& host, int32_t port,
    const nsACString& path, nsHttpAuthIdentity& ident) {
  nsHttpAuthEntry* entry = nullptr;
  nsresult rv;

  // set informations that depend on whether
  // we're authenticating against a proxy
  // or a webserver
  nsISupports** continuationState;

  nsAutoCString suffix;
  if (header == nsHttp::Proxy_Authorization) {
    continuationState = &mProxyAuthContinuationState;

    if (mProxyInfo) {
      nsAutoCString type;
      mProxyInfo->GetType(type);
      if (type.EqualsLiteral("https")) {
        // Let this be overriden by anything from the cache.
        auto const& pa = mProxyInfo->ProxyAuthorizationHeader();
        if (!pa.IsEmpty()) {
          rv = mAuthChannel->SetProxyCredentials(pa);
          MOZ_ASSERT(NS_SUCCEEDED(rv));
        }
      }
    }
  } else {
    continuationState = &mAuthContinuationState;

    nsCOMPtr<nsIChannel> chan = do_QueryInterface(mAuthChannel);
    GetOriginAttributesSuffix(chan, suffix);
  }

  rv = authCache->GetAuthEntryForPath(scheme, host, port, path, suffix, &entry);
  if (NS_SUCCEEDED(rv)) {
    // if we are trying to add a header for origin server auth and if the
    // URL contains an explicit username, then try the given username first.
    // we only want to do this, however, if we know the URL requires auth
    // based on the presence of an auth cache entry for this URL (which is
    // true since we are here).  but, if the username from the URL matches
    // the username from the cache, then we should prefer the password
    // stored in the cache since that is most likely to be valid.
    if (header == nsHttp::Authorization && entry->Domain()[0] == '\0') {
      GetIdentityFromURI(0, ident);
      // if the usernames match, then clear the ident so we will pick
      // up the one from the auth cache instead.
      // when this is undesired, specify LOAD_EXPLICIT_CREDENTIALS load
      // flag.
      if (ident.User() == entry->User()) {
        uint32_t loadFlags;
        if (NS_SUCCEEDED(mAuthChannel->GetLoadFlags(&loadFlags)) &&
            !(loadFlags & nsIChannel::LOAD_EXPLICIT_CREDENTIALS)) {
          ident.Clear();
        }
      }
    }
    bool identFromURI;
    if (ident.IsEmpty()) {
      ident = entry->Identity();
      identFromURI = false;
    } else {
      identFromURI = true;
    }

    nsCString temp;  // this must have the same lifetime as creds
    nsAutoCString creds(entry->Creds());
    // we can only send a preemptive Authorization header if we have either
    // stored credentials or a stored challenge from which to derive
    // credentials.  if the identity is from the URI, then we cannot use
    // the stored credentials.
    if ((creds.IsEmpty() || identFromURI) && !entry->Challenge().IsEmpty()) {
      nsCOMPtr<nsIHttpAuthenticator> auth;
      nsAutoCString unused;
      rv = GetAuthenticator(entry->Challenge(), unused, getter_AddRefs(auth));
      if (NS_SUCCEEDED(rv)) {
        bool proxyAuth = (header == nsHttp::Proxy_Authorization);
        rv = GenCredsAndSetEntry(auth, proxyAuth, scheme, host, port, path,
                                 entry->Realm(), entry->Challenge(), ident,
                                 entry->mMetaData, temp);
        if (NS_SUCCEEDED(rv)) creds = temp;

        // make sure the continuation state is null since we do not
        // support mixing preemptive and 'multirequest' authentication.
        NS_IF_RELEASE(*continuationState);
      }
    }
    if (!creds.IsEmpty()) {
      LOG(("   adding \"%s\" request header\n", header.get()));
      if (header == nsHttp::Proxy_Authorization) {
        rv = mAuthChannel->SetProxyCredentials(creds);
        MOZ_ASSERT(NS_SUCCEEDED(rv));
      } else {
        rv = mAuthChannel->SetWWWCredentials(creds);
        MOZ_ASSERT(NS_SUCCEEDED(rv));
      }

      // suppress defensive auth prompting for this channel since we know
      // that we already prompted at least once this session.  we only do
      // this for non-proxy auth since the URL's userpass is not used for
      // proxy auth.
      if (header == nsHttp::Authorization) mSuppressDefensiveAuth = true;
    } else {
      ident.Clear();  // don't remember the identity
    }
  }
}

nsresult nsHttpChannelAuthProvider::GetCurrentPath(nsACString& path) {
  nsresult rv;
  nsCOMPtr<nsIURL> url = do_QueryInterface(mURI);
  if (url) {
    rv = url->GetDirectory(path);
  } else {
    rv = mURI->GetPathQueryRef(path);
  }
  return rv;
}

NS_IMPL_ISUPPORTS(nsHttpChannelAuthProvider, nsICancelable,
                  nsIHttpChannelAuthProvider, nsIAuthPromptCallback,
                  nsIHttpAuthenticatorCallback)

}  // namespace mozilla::net