Skip to content

Commit

Permalink
Simplified tokens management
Browse files Browse the repository at this point in the history
Following developments in Reva (cs3org/reva#2217),
the same token can and shall be used regardless if Reva is a source
or a destination or both.

Yet, it depends on the user and for now we still get it from
the system environment via getenv.
  • Loading branch information
glpatcern committed Jan 10, 2022
1 parent d10f74a commit f88aaa8
Showing 1 changed file with 6 additions and 33 deletions.
39 changes: 6 additions & 33 deletions src/utils/davix_cs3_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void Credentials::getCredentialMap(CredentialMap & cmap) const {
}
}

void Credentials::addCredentials(std::string uri, std::string token, bool token_write_access){
void Credentials::addCredentials(std::string uri, std::string token, bool token_write_access) {

if(credMap->find(uri) == credMap->end()){
Credential cred {token, token_write_access};
Expand All @@ -71,45 +71,18 @@ void Credentials::addCredentials(std::string uri, std::string token, bool token_
}


// Helper function
// To convert char* to string safely
std::string getEnvStr(std::string var){
char* val = getenv(var.c_str());
return val == NULL ? std::string() : std::string(val);
}

//
// Credential Provider
//

void CredentialProvider::updateCredentials(Credentials &creds, std::string uri, bool token_write_access){

void CredentialProvider::updateCredentials(Credentials &creds, std::string uri, bool token_write_access) {
//Assuming urls with +3rd prefix have been deprecated

std::string src_url, dst_url, token;

src_url = getEnvStr("SRC_URL");
dst_url = getEnvStr("DST_URL");

// Src and Dst env var need to be set as env var for the time being
// This will be removed in the future

if (src_url == "" || dst_url == ""){
throw DavixException(std::string("davix::reva"), StatusCode::EnvVarNotSet, "Source or Destination variable is not set");
char* token = getenv("REVA_TOKEN");
if(token == NULL) {
throw DavixException(std::string("davix::reva"), StatusCode::EnvVarNotSet, "REVA_TOKEN variable is not set");
}

// If any required token is not set we get an empty string
// When this happens copy fails with Authentication error

if(uri.compare(dst_url) <= 0 ){
token = getEnvStr("REVA_DST_TOKEN");
}
else if (uri.compare(src_url) <= 0 ){
token = getEnvStr("REVA_SRC_TOKEN");
}

creds.addCredentials(uri, token, token_write_access);

creds.addCredentials(uri, std::string(token), token_write_access);
}

} //reva
Expand Down

0 comments on commit f88aaa8

Please sign in to comment.