| 638 | namespace header { |
| 639 | |
| 640 | Try<WWWAuthenticate> WWWAuthenticate::create(const string& value) |
| 641 | { |
| 642 | // Set `maxTokens` as 2 since auth-param quoted string may |
| 643 | // contain space (e.g., "Basic realm="Registry Realm"). |
| 644 | vector<string> tokens = strings::tokenize(value, " ", 2); |
| 645 | if (tokens.size() != 2) { |
| 646 | return Error("Unexpected WWW-Authenticate header format: '" + value + "'"); |
| 647 | } |
| 648 | |
| 649 | hashmap<string, string> authParam; |
| 650 | foreach (const string& token, strings::split(tokens[1], ",")) { |
| 651 | vector<string> split = strings::split(token, "="); |
| 652 | if (split.size() != 2) { |
| 653 | return Error( |
| 654 | "Unexpected auth-param format: '" + |
| 655 | token + "' in '" + tokens[1] + "'"); |
| 656 | } |
| 657 | |
| 658 | // Auth-param values can be a quoted-string or directive values. |
| 659 | // Please see section "3.2.2.4 Directive values and quoted-string": |
| 660 | // https://tools.ietf.org/html/rfc2617. |
| 661 | authParam[split[0]] = strings::trim(split[1], strings::ANY, "\""); |
| 662 | } |
| 663 | |
| 664 | // The realm directive (case-insensitive) is required for all |
| 665 | // authentication schemes that issue a challenge. |
| 666 | if (!authParam.contains("realm")) { |
| 667 | return Error( |
| 668 | "Unexpected auth-param '" + |
| 669 | tokens[1] + "': 'realm' is not defined"); |
| 670 | } |
| 671 | |
| 672 | return WWWAuthenticate(tokens[0], authParam); |
| 673 | } |
| 674 | |
| 675 | |
| 676 | string WWWAuthenticate::authScheme() |