void HTTPCredentialsTest::testBadCredentials() { HTTPRequest request; std::string scheme; std::string info; try { request.getCredentials(scheme, info); fail("no credentials - must throw"); } catch (NotAuthenticatedException&) { } request.setCredentials("Test", "SomeData"); request.getCredentials(scheme, info); assert (scheme == "Test"); assert (info == "SomeData"); try { HTTPBasicCredentials cred(request); fail("bad scheme - must throw"); } catch (NotAuthenticatedException&) { } }
HTTPBasicCredentials::HTTPBasicCredentials(const HTTPRequest& request) { static const int eof = std::char_traits<char>::eof(); std::string scheme; std::string info; request.getCredentials(scheme, info); if (icompare(scheme, SCHEME) == 0) { std::istringstream istr(info); Base64Decoder decoder(istr); int ch = decoder.get(); while (ch != eof && ch != ':') { _username += (char) ch; ch = decoder.get(); } if (ch == ':') ch = decoder.get(); while (ch != eof) { _password += (char) ch; ch = decoder.get(); } } else throw NotAuthenticatedException("Basic authentication expected"); }
void OAuth20Credentials::extractBearerToken(const HTTPRequest& request) { if (request.hasCredentials()) { std::string authScheme; std::string authInfo; request.getCredentials(authScheme, authInfo); if (icompare(authScheme, _scheme) == 0) { _bearerToken = authInfo; } else throw NotAuthenticatedException("No bearer token in Authorization header", authScheme); } else throw NotAuthenticatedException("No Authorization header found"); }
void HTTPCredentialsTest::testBasicCredentials() { HTTPRequest request; assert (!request.hasCredentials()); HTTPBasicCredentials cred("user", "secret"); cred.authenticate(request); assert (request.hasCredentials()); std::string scheme; std::string info; request.getCredentials(scheme, info); assert (scheme == "Basic"); assert (info == "dXNlcjpzZWNyZXQ="); HTTPBasicCredentials cred2(request); assert (cred2.getUsername() == "user"); assert (cred2.getPassword() == "secret"); }