Skip to content

Commit 6d82251

Browse files
committed
Switch redactions test case to OAuth 2.0
1 parent dd76336 commit 6d82251

15 files changed

+127
-50
lines changed

test/redactions.testcore/changeset_download_moderator.case

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ Request-Method: GET
44
Request-URI: /api/0.6/changeset/2/download?show_redactions=true
55
Http-Host: cgimap.example.com
66
Date: 2017-03-13T16:47:00Z
7-
Http-Authorization: OAuth realm="http://cgimap.example.com/api/0.6/changeset/2/download", oauth_consumer_key="heqfjrcolc", oauth_token="scgncknxqr", oauth_nonce="dqndteqmzduesxyjetrd", oauth_timestamp="1489423620", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_signature="2KQpmh%2FAJi9xTpZvCIo82inmdho%3D"
7+
Http-Authorization: Bearer 1yi2RI2WhIVMLoLaDLg0nrPJPU4WQSIX4Hh_jxfRRxI
88
---
99
Status: 200 OK
1010
Content-Type: application/xml; charset=utf-8
11-
!Content-Disposition:
11+
!Content-Disposition:
1212
---
1313
<osmChange version="0.6" generator="***" copyright="***" attribution="***" license="***">
1414
<modify>

test/redactions.testcore/node_history_moderator.case

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Request-Method: GET
33
Request-URI: /api/0.6/node/1/history?show_redactions=true
44
Http-Host: cgimap.example.com
55
Date: 2017-02-03T17:11:00Z
6-
Http-Authorization: OAuth realm="http://cgimap.example.com/api/0.6/node/1/history", oauth_consumer_key="heqfjrcolc", oauth_token="scgncknxqr", oauth_nonce="ylxApRr7uw94cVGRwxbx", oauth_timestamp="1486141860", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_signature="IcgPfe%2F4C4Rh5HlnZQg388bn5Fo%3D"
6+
Http-Authorization: Bearer 1yi2RI2WhIVMLoLaDLg0nrPJPU4WQSIX4Hh_jxfRRxI
77
---
88
Status: 200 OK
99
Content-Type: application/xml; charset=utf-8

test/redactions.testcore/node_version_moderator.case

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Request-Method: GET
33
Request-URI: /api/0.6/node/1/2?show_redactions=true
44
Http-Host: cgimap.example.com
55
Date: 2017-01-23T20:25:00Z
6-
Http-Authorization: OAuth realm="http://cgimap.example.com/api/0.6/node/1/2", oauth_consumer_key="heqfjrcolc", oauth_token="scgncknxqr", oauth_nonce="ykvbwrcver", oauth_timestamp="1485203100", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_signature="NzUgxE%2B%2BueNwfnHtbLyJBTxdVus%3D"
6+
Http-Authorization: Bearer 1yi2RI2WhIVMLoLaDLg0nrPJPU4WQSIX4Hh_jxfRRxI
77
---
88
Status: 200 OK
99
Content-Type: application/xml; charset=utf-8

test/redactions.testcore/nodes_moderator.case

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Request-Method: GET
33
Request-URI: /api/0.6/nodes?nodes=1v1,1v2&show_redactions=true
44
Http-Host: cgimap.example.com
55
Date: 2017-02-03T17:22:00Z
6-
Http-Authorization: OAuth realm="http://cgimap.example.com/api/0.6/nodes", oauth_consumer_key="heqfjrcolc", oauth_token="scgncknxqr", oauth_nonce="X1VcT5srCUFk7NMbDBBi", oauth_timestamp="1486142520", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_signature="LanUZRlaRV7uJYaXgTJ30Mqog3M%3D"
6+
Http-Authorization: Bearer 1yi2RI2WhIVMLoLaDLg0nrPJPU4WQSIX4Hh_jxfRRxI
77
---
88
Status: 200 OK
99
Content-Type: application/xml; charset=utf-8

test/redactions.testcore/oauth.json

-11
This file was deleted.

test/redactions.testcore/oauth2.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"tokens": {
3+
"1yi2RI2WhIVMLoLaDLg0nrPJPU4WQSIX4Hh_jxfRRxI": {
4+
"expired": false,
5+
"revoked": false,
6+
"api_write": false,
7+
"user_id": 2
8+
}
9+
}
10+
}

test/redactions.testcore/relation_history_moderator.case

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Request-Method: GET
33
Request-URI: /api/0.6/relation/1/history?show_redactions=true
44
Http-Host: cgimap.example.com
55
Date: 2017-02-03T17:15:00Z
6-
Http-Authorization: OAuth realm="http://cgimap.example.com/api/0.6/relation/1/history", oauth_consumer_key="heqfjrcolc", oauth_token="scgncknxqr", oauth_nonce="i89B012BEg0Y5KHiiG5j", oauth_timestamp="1486142100", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_signature="dI%2F6nEplWIcCUtwXul3LYC8ZuZI%3D"
6+
Http-Authorization: Bearer 1yi2RI2WhIVMLoLaDLg0nrPJPU4WQSIX4Hh_jxfRRxI
77
---
88
Status: 200 OK
99
Content-Type: application/xml; charset=utf-8

test/redactions.testcore/relation_version_moderator.case

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Request-Method: GET
44
Request-URI: /api/0.6/relation/1/2?show_redactions=true
55
Http-Host: cgimap.example.com
66
Date: 2017-02-03T16:37:00Z
7-
Http-Authorization: OAuth realm="http://cgimap.example.com/api/0.6/relation/1/2", oauth_consumer_key="heqfjrcolc", oauth_token="scgncknxqr", oauth_nonce="OFP9OGKNWTfBUw7hvB3A", oauth_timestamp="1486139820", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_signature="N9V4%2FqNx5an7gOmmL4MhDBfKec0%3D"
7+
Http-Authorization: Bearer 1yi2RI2WhIVMLoLaDLg0nrPJPU4WQSIX4Hh_jxfRRxI
88
---
99
Status: 200 OK
1010
Content-Type: application/xml; charset=utf-8

test/redactions.testcore/relations_moderator.case

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Request-Method: GET
33
Request-URI: /api/0.6/relations?relations=1v1,1v2&show_redactions=true
44
Http-Host: cgimap.example.com
55
Date: 2017-02-03T17:26:00Z
6-
Http-Authorization: OAuth realm="http://cgimap.example.com/api/0.6/relations", oauth_consumer_key="heqfjrcolc", oauth_token="scgncknxqr", oauth_nonce="s5ikn2rEXP8sEY4yWioL", oauth_timestamp="1486142760", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_signature="OQMjxYyDtdnAkz%2Fz%2FYVN7cQDj6w%3D"
6+
Http-Authorization: Bearer 1yi2RI2WhIVMLoLaDLg0nrPJPU4WQSIX4Hh_jxfRRxI
77
---
88
Status: 200 OK
99
Content-Type: application/xml; charset=utf-8

test/redactions.testcore/way_history_moderator.case

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Request-Method: GET
33
Request-URI: /api/0.6/way/1/history?show_redactions=true
44
Http-Host: cgimap.example.com
55
Date: 2017-02-03T17:13:00Z
6-
Http-Authorization: OAuth realm="http://cgimap.example.com/api/0.6/way/1/history", oauth_consumer_key="heqfjrcolc", oauth_token="scgncknxqr", oauth_nonce="EsHFWX0Ru330NARIKysY", oauth_timestamp="1486141980", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_signature="Mkx4Sg%2BXWQkd7P0lhmPyszo2zIo%3D"
6+
Http-Authorization: Bearer 1yi2RI2WhIVMLoLaDLg0nrPJPU4WQSIX4Hh_jxfRRxI
77
---
88
Status: 200 OK
99
Content-Type: application/xml; charset=utf-8

test/redactions.testcore/way_version_moderator.case

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Request-Method: GET
44
Request-URI: /api/0.6/way/1/2?show_redactions=true
55
Http-Host: cgimap.example.com
66
Date: 2017-02-03T16:31:00Z
7-
Http-Authorization: OAuth realm="http://cgimap.example.com/api/0.6/way/1/2", oauth_consumer_key="heqfjrcolc", oauth_token="scgncknxqr", oauth_nonce="ng8dUT94UOvYmX98sAof", oauth_timestamp="1486139460", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_signature="QnliNV2%2BjeQWVDPZUXwTxrc%2BhDQ%3D"
7+
Http-Authorization: Bearer 1yi2RI2WhIVMLoLaDLg0nrPJPU4WQSIX4Hh_jxfRRxI
88
---
99
Status: 200 OK
1010
Content-Type: application/xml; charset=utf-8

test/redactions.testcore/ways_moderator.case

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Request-Method: GET
33
Request-URI: /api/0.6/ways?ways=1v1,1v2&show_redactions=true
44
Http-Host: cgimap.example.com
55
Date: 2017-02-03T17:24:00Z
6-
Http-Authorization: OAuth realm="http://cgimap.example.com/api/0.6/ways", oauth_consumer_key="heqfjrcolc", oauth_token="scgncknxqr", oauth_nonce="sD8ADXbFprXuraBKHyH5", oauth_timestamp="1486142640", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_signature="fQdcQw1OdVF%2BEhrPrFzxCD3c5Y0%3D"
6+
Http-Authorization: Bearer 1yi2RI2WhIVMLoLaDLg0nrPJPU4WQSIX4Hh_jxfRRxI
77
---
88
Status: 200 OK
99
Content-Type: application/xml; charset=utf-8

test/staticxml.cpp

+29-13
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,14 @@ inline void write_element<relation>(const relation &r, output_formatter &formatt
312312
}
313313

314314
struct static_data_selection : public data_selection {
315-
explicit static_data_selection(database& db) : static_data_selection(db, {}) {}
315+
explicit static_data_selection(database& db) : static_data_selection(db, {}, {}) {}
316316

317-
explicit static_data_selection(database& db, user_roles_t m_user_roles)
317+
explicit static_data_selection(database& db, user_roles_t m_user_roles, oauth2_tokens m_oauth2_tokens)
318318
: m_db(db)
319319
, m_include_changeset_comments(false)
320320
, m_redactions_visible(false)
321-
, m_user_roles(m_user_roles) {}
321+
, m_user_roles(m_user_roles)
322+
, m_oauth2_tokens(m_oauth2_tokens){}
322323

323324
~static_data_selection() override = default;
324325

@@ -591,6 +592,16 @@ struct static_data_selection : public data_selection {
591592
std::optional< osm_user_id_t > get_user_id_for_oauth2_token(
592593
const std::string &token_id, bool &expired, bool &revoked,
593594
bool &allow_api_write) {
595+
596+
auto itr = m_oauth2_tokens.find(token_id);
597+
if (itr != m_oauth2_tokens.end())
598+
{
599+
expired = itr->second.expired;
600+
revoked = itr->second.revoked;
601+
allow_api_write = itr->second.api_write;
602+
return itr->second.user_id;
603+
}
604+
594605
expired = false;
595606
revoked = false;
596607
allow_api_write = false;
@@ -751,6 +762,7 @@ struct static_data_selection : public data_selection {
751762
std::set<osm_edition_t> m_historic_nodes, m_historic_ways, m_historic_relations;
752763
bool m_include_changeset_comments, m_redactions_visible;
753764
user_roles_t m_user_roles;
765+
oauth2_tokens m_oauth2_tokens;
754766
};
755767

756768
template <>
@@ -769,16 +781,17 @@ const std::map<id_version, relation> &static_data_selection::map_of<relation>()
769781
}
770782

771783
struct factory : public data_selection::factory {
772-
explicit factory(const std::string &file) : factory(file, {}) {}
784+
explicit factory(const std::string &file) : factory(file, {}, {}) {}
773785

774-
explicit factory(const std::string &file, user_roles_t user_roles)
775-
: m_database(parse_xml(file.c_str())),
776-
m_user_roles(user_roles) {}
786+
explicit factory(const std::string &file, user_roles_t user_roles, oauth2_tokens oauth2_tokens)
787+
: m_database(parse_xml(file.c_str()))
788+
, m_user_roles(user_roles)
789+
, m_oauth2_tokens(oauth2_tokens) {}
777790

778791
~factory() override = default;
779792

780793
std::unique_ptr<data_selection> make_selection(Transaction_Owner_Base&) const override {
781-
return std::make_unique<static_data_selection>(*m_database, m_user_roles);
794+
return std::make_unique<static_data_selection>(*m_database, m_user_roles, m_oauth2_tokens);
782795
}
783796

784797
std::unique_ptr<Transaction_Owner_Base> get_default_transaction() override {
@@ -788,15 +801,17 @@ struct factory : public data_selection::factory {
788801
private:
789802
std::unique_ptr<database> m_database;
790803
user_roles_t m_user_roles;
804+
oauth2_tokens m_oauth2_tokens;
791805
};
792806

793807
struct staticxml_backend : public backend {
794-
staticxml_backend() : staticxml_backend(user_roles_t{}) {}
808+
staticxml_backend() : staticxml_backend(user_roles_t{}, oauth2_tokens{}) {}
795809

796-
staticxml_backend(user_roles_t user_roles) {
810+
staticxml_backend(user_roles_t user_roles, oauth2_tokens oauth2_tokens) {
797811
m_options.add_options()("file", po::value<std::string>()->required(),
798812
"file to load static OSM XML from.");
799813
m_user_roles = user_roles;
814+
m_oauth2_tokens = oauth2_tokens;
800815
}
801816

802817
~staticxml_backend() override = default;
@@ -806,7 +821,7 @@ struct staticxml_backend : public backend {
806821

807822
std::unique_ptr<data_selection::factory> create(const po::variables_map &opts) override {
808823
std::string file = opts["file"].as<std::string>();
809-
return std::make_unique<factory>(file, m_user_roles);
824+
return std::make_unique<factory>(file, m_user_roles, m_oauth2_tokens);
810825
}
811826

812827
std::unique_ptr<data_update::factory> create_data_update(const po::variables_map &) override {
@@ -817,11 +832,12 @@ struct staticxml_backend : public backend {
817832
std::string m_name{"staticxml"};
818833
po::options_description m_options{"Static XML backend options"};
819834
user_roles_t m_user_roles;
835+
oauth2_tokens m_oauth2_tokens;
820836
};
821837

822838
} // anonymous namespace
823839

824840

825-
std::unique_ptr<backend> make_staticxml_backend(user_roles_t user_roles) {
826-
return std::make_unique<staticxml_backend>(user_roles);
841+
std::unique_ptr<backend> make_staticxml_backend(user_roles_t user_roles, oauth2_tokens oauth2_tokens) {
842+
return std::make_unique<staticxml_backend>(user_roles, oauth2_tokens);
827843
}

test/staticxml.hpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,21 @@
1212

1313
#include "cgimap/backend.hpp"
1414

15+
#include <map>
1516
#include <memory>
1617

18+
struct oauth2_token_detail_t
19+
{
20+
bool expired;
21+
bool revoked;
22+
bool api_write;
23+
osm_user_id_t user_id;
24+
};
25+
26+
using oauth2_tokens = std::map<std::string, oauth2_token_detail_t>;
27+
1728
using user_roles_t = std::map<osm_user_id_t, std::set<osm_user_role_t> >;
1829

19-
std::unique_ptr<backend> make_staticxml_backend(user_roles_t = {});
30+
std::unique_ptr<backend> make_staticxml_backend(user_roles_t = {}, oauth2_tokens = {});
2031

2132
#endif /* STATICXML_BACKEND_HPP */

test/test_core.cpp

+65-14
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ osm_user_role_t parse_role(const std::string &str) {
526526
}
527527
}
528528

529-
user_roles_t get_user_roles(const pt::ptree &config)
529+
user_roles_t parse_user_roles(const pt::ptree &config)
530530
{
531531
user_roles_t user_roles;
532532
boost::optional< const pt::ptree& > users = config.get_child_optional("users");
@@ -553,6 +553,65 @@ user_roles_t get_user_roles(const pt::ptree &config)
553553
return user_roles;
554554
}
555555

556+
user_roles_t get_user_roles(const fs::path &roles_file)
557+
{
558+
if (fs::is_regular_file(roles_file))
559+
{
560+
try
561+
{
562+
pt::ptree config;
563+
pt::read_json(roles_file.string(), config);
564+
return parse_user_roles(config);
565+
}
566+
catch (const std::exception &ex)
567+
{
568+
throw std::runtime_error(
569+
fmt::format("{}, while reading expected JSON.", ex.what()));
570+
}
571+
}
572+
return {};
573+
}
574+
575+
576+
oauth2_tokens parse_oauth2_tokens(const pt::ptree &config)
577+
{
578+
oauth2_tokens oauth2_tokens;
579+
boost::optional< const pt::ptree& > tokens = config.get_child_optional("tokens");
580+
if (tokens)
581+
{
582+
for (const auto &entry : *tokens)
583+
{
584+
oauth2_token_detail_t detail;
585+
auto token = entry.first;
586+
detail.api_write = entry.second.get<bool>("api_write", false);
587+
detail.expired = entry.second.get<bool>("expired", true);
588+
detail.revoked = entry.second.get<bool>("revoked", true);
589+
detail.user_id = entry.second.get<osm_user_id_t>("user_id", {});
590+
oauth2_tokens[token] = std::move(detail);
591+
}
592+
}
593+
return oauth2_tokens;
594+
}
595+
596+
oauth2_tokens get_oauth2_tokens(const fs::path &oauth2_file)
597+
{
598+
if (fs::is_regular_file(oauth2_file))
599+
{
600+
try
601+
{
602+
pt::ptree config;
603+
pt::read_json(oauth2_file.string(), config);
604+
return parse_oauth2_tokens(config);
605+
}
606+
catch (const std::exception &ex)
607+
{
608+
throw std::runtime_error(
609+
fmt::format("{}, while reading expected JSON.", ex.what()));
610+
}
611+
}
612+
return {};
613+
}
614+
556615

557616
int main(int argc, char *argv[]) {
558617
if (argc != 2) {
@@ -562,11 +621,12 @@ int main(int argc, char *argv[]) {
562621

563622
fs::path test_directory = argv[1];
564623
fs::path data_file = test_directory / "data.osm";
565-
fs::path oauth_file = test_directory / "oauth.json";
624+
fs::path oauth2_file = test_directory / "oauth2.json";
566625
fs::path roles_file = test_directory / "roles.json";
567626
std::vector<fs::path> test_cases;
568627

569628
user_roles_t user_roles;
629+
oauth2_tokens oauth2_tokens;
570630

571631
try {
572632
if (fs::is_directory(test_directory) == false) {
@@ -588,17 +648,8 @@ int main(int argc, char *argv[]) {
588648
}
589649
}
590650

591-
if (fs::is_regular_file(roles_file)) {
592-
pt::ptree config;
593-
594-
try {
595-
pt::read_json(roles_file.string(), config);
596-
} catch (const std::exception &ex) {
597-
throw std::runtime_error
598-
(fmt::format("{}, while reading expected JSON.", ex.what()));
599-
}
600-
user_roles = get_user_roles(config);
601-
}
651+
user_roles = get_user_roles(roles_file);
652+
oauth2_tokens = get_oauth2_tokens(oauth2_file);
602653

603654
} catch (const std::exception &e) {
604655
std::cerr << "EXCEPTION: " << e.what() << std::endl;
@@ -614,7 +665,7 @@ int main(int argc, char *argv[]) {
614665
vm.insert(std::make_pair(std::string("file"),
615666
po::variable_value(data_file.native(), false)));
616667

617-
auto data_backend = make_staticxml_backend(user_roles);
668+
auto data_backend = make_staticxml_backend(user_roles, oauth2_tokens);
618669
auto factory = data_backend->create(vm);
619670
null_rate_limiter limiter;
620671
routes route;

0 commit comments

Comments
 (0)