Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User/Server API key provider becomes a single 'API key' provider #6696

Merged
merged 1 commit into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* None.

### Breaking changes
* None.
* A new provider called `IdentityProviderAPIKey` replaces both `IdentityProviderUserAPIKey` and `IdentityProviderServerAPIKey` since those two did the same thing. If SDKs wish to keep the old behaviour without requiring users to make code changes, they can adapt both their existing server and user API key providers to use the new core type. ([#5914](https://github.com/realm/realm-core/issues/5914))

### Compatibility
* Fileformat: Generates files with format v23. Reads and automatically upgrade from fileformat v5.
Expand Down
6 changes: 2 additions & 4 deletions src/realm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2751,8 +2751,7 @@ typedef enum realm_auth_provider {
RLM_AUTH_PROVIDER_CUSTOM,
RLM_AUTH_PROVIDER_EMAIL_PASSWORD,
RLM_AUTH_PROVIDER_FUNCTION,
RLM_AUTH_PROVIDER_USER_API_KEY,
RLM_AUTH_PROVIDER_SERVER_API_KEY,
RLM_AUTH_PROVIDER_API_KEY,
} realm_auth_provider_e;

typedef struct realm_app_user_apikey {
Expand Down Expand Up @@ -2828,8 +2827,7 @@ RLM_API realm_app_credentials_t* realm_app_credentials_new_apple(const char* id_
RLM_API realm_app_credentials_t* realm_app_credentials_new_jwt(const char* jwt_token) RLM_API_NOEXCEPT;
RLM_API realm_app_credentials_t* realm_app_credentials_new_email_password(const char* email,
realm_string_t password) RLM_API_NOEXCEPT;
RLM_API realm_app_credentials_t* realm_app_credentials_new_user_api_key(const char* api_key) RLM_API_NOEXCEPT;
RLM_API realm_app_credentials_t* realm_app_credentials_new_server_api_key(const char* api_key) RLM_API_NOEXCEPT;
RLM_API realm_app_credentials_t* realm_app_credentials_new_api_key(const char* api_key) RLM_API_NOEXCEPT;
nicola-cab marked this conversation as resolved.
Show resolved Hide resolved

/**
* Create Custom Function authentication app credentials.
Expand Down
12 changes: 3 additions & 9 deletions src/realm/object-store/c_api/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ static_assert(realm_auth_provider_e(AuthProvider::APPLE) == RLM_AUTH_PROVIDER_AP
static_assert(realm_auth_provider_e(AuthProvider::CUSTOM) == RLM_AUTH_PROVIDER_CUSTOM);
static_assert(realm_auth_provider_e(AuthProvider::USERNAME_PASSWORD) == RLM_AUTH_PROVIDER_EMAIL_PASSWORD);
static_assert(realm_auth_provider_e(AuthProvider::FUNCTION) == RLM_AUTH_PROVIDER_FUNCTION);
static_assert(realm_auth_provider_e(AuthProvider::USER_API_KEY) == RLM_AUTH_PROVIDER_USER_API_KEY);
static_assert(realm_auth_provider_e(AuthProvider::SERVER_API_KEY) == RLM_AUTH_PROVIDER_SERVER_API_KEY);
static_assert(realm_auth_provider_e(AuthProvider::API_KEY) == RLM_AUTH_PROVIDER_API_KEY);


static realm_app_error_t to_capi(const AppError& error)
Expand Down Expand Up @@ -169,14 +168,9 @@ RLM_API realm_app_credentials_t* realm_app_credentials_new_function(const char*
});
}

RLM_API realm_app_credentials_t* realm_app_credentials_new_user_api_key(const char* api_key) noexcept
RLM_API realm_app_credentials_t* realm_app_credentials_new_api_key(const char* api_key) noexcept
{
return new realm_app_credentials_t(AppCredentials::user_api_key(api_key));
}

RLM_API realm_app_credentials_t* realm_app_credentials_new_server_api_key(const char* api_key) noexcept
{
return new realm_app_credentials_t(AppCredentials::server_api_key(api_key));
return new realm_app_credentials_t(AppCredentials::api_key(api_key));
}

RLM_API realm_auth_provider_e realm_auth_credentials_get_provider(realm_app_credentials_t* credentials) noexcept
Expand Down
25 changes: 7 additions & 18 deletions src/realm/object-store/sync/app_credentials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ IdentityProvider const IdentityProviderApple = "oauth2-apple";
IdentityProvider const IdentityProviderUsernamePassword = "local-userpass";
IdentityProvider const IdentityProviderCustom = "custom-token";
IdentityProvider const IdentityProviderFunction = "custom-function";
IdentityProvider const IdentityProviderUserAPIKey = "api-key";
IdentityProvider const IdentityProviderServerAPIKey = "api-key";
IdentityProvider const IdentityProviderAPIKey = "api-key";

IdentityProvider provider_type_from_enum(AuthProvider provider)
{
Expand All @@ -51,10 +50,8 @@ IdentityProvider provider_type_from_enum(AuthProvider provider)
return IdentityProviderUsernamePassword;
case AuthProvider::FUNCTION:
return IdentityProviderFunction;
case AuthProvider::USER_API_KEY:
return IdentityProviderUserAPIKey;
case AuthProvider::SERVER_API_KEY:
return IdentityProviderServerAPIKey;
case AuthProvider::API_KEY:
return IdentityProviderAPIKey;
}
throw InvalidArgument("unknown provider type in provider_type_from_enum");
}
Expand Down Expand Up @@ -82,11 +79,8 @@ AuthProvider enum_from_provider_type(const IdentityProvider& provider)
else if (provider == IdentityProviderFunction) {
return AuthProvider::FUNCTION;
}
else if (provider == IdentityProviderUserAPIKey) {
return AuthProvider::USER_API_KEY;
}
else if (provider == IdentityProviderServerAPIKey) {
return AuthProvider::SERVER_API_KEY;
else if (provider == IdentityProviderAPIKey) {
return AuthProvider::API_KEY;
}
else {
REALM_UNREACHABLE();
Expand Down Expand Up @@ -179,14 +173,9 @@ AppCredentials AppCredentials::function(const std::string& serialized_payload)
}


AppCredentials AppCredentials::user_api_key(std::string api_key)
{
return AppCredentials(AuthProvider::USER_API_KEY, {{"key", api_key}});
}

AppCredentials AppCredentials::server_api_key(std::string api_key)
AppCredentials AppCredentials::api_key(std::string api_key)
{
return AppCredentials(AuthProvider::SERVER_API_KEY, {{"key", api_key}});
return AppCredentials(AuthProvider::API_KEY, {{"key", api_key}});
}

AppCredentials::AppCredentials(const AppCredentials& credentials)
Expand Down
19 changes: 5 additions & 14 deletions src/realm/object-store/sync/app_credentials.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,8 @@ extern IdentityProvider const IdentityProviderApple;
extern IdentityProvider const IdentityProviderFunction;

// A credential which can be used to log in as a Stitch user
// using the User API Key authentication provider.
extern IdentityProvider const IdentityProviderUserAPIKey;

// A credential which can be used to log in as a Stitch user
// using the Server API Key authentication provider.
extern IdentityProvider const IdentityProviderServerAPIKey;
// using the Server's API Key authentication provider.
extern IdentityProvider const IdentityProviderAPIKey;

enum class AuthProvider {
ANONYMOUS,
Expand All @@ -79,8 +75,7 @@ enum class AuthProvider {
CUSTOM,
USERNAME_PASSWORD,
FUNCTION,
USER_API_KEY,
SERVER_API_KEY
API_KEY,
};

IdentityProvider provider_type_from_enum(AuthProvider provider);
Expand Down Expand Up @@ -118,12 +113,8 @@ struct AppCredentials {
// The payload is a MongoDB document as json
static AppCredentials function(const std::string& serialized_payload);

// Construct and return credentials with the user api key.
static AppCredentials user_api_key(std::string api_key);

// Construct and return credentials with the server api key.
static AppCredentials server_api_key(std::string api_key);

// Construct and return credentials with the api key.
static AppCredentials api_key(std::string api_key);

// The provider of the credential
AuthProvider provider() const;
Expand Down
16 changes: 5 additions & 11 deletions test/object-store/sync/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4484,18 +4484,12 @@ TEST_CASE("app: auth providers", "[sync][app]") {
CHECK(credentials.serialize_as_bson() == bson::BsonDocument{{"name", "mongo"}});
}

SECTION("auth providers user api key") {
auto credentials = AppCredentials::user_api_key("a key");
CHECK(credentials.provider() == AuthProvider::USER_API_KEY);
CHECK(credentials.provider_as_string() == IdentityProviderUserAPIKey);
CHECK(credentials.serialize_as_bson() == bson::BsonDocument{{"provider", "api-key"}, {"key", "a key"}});
}

SECTION("auth providers server api key") {
auto credentials = AppCredentials::server_api_key("a key");
CHECK(credentials.provider() == AuthProvider::SERVER_API_KEY);
CHECK(credentials.provider_as_string() == IdentityProviderServerAPIKey);
SECTION("auth providers api key") {
auto credentials = AppCredentials::api_key("a key");
CHECK(credentials.provider() == AuthProvider::API_KEY);
CHECK(credentials.provider_as_string() == IdentityProviderAPIKey);
CHECK(credentials.serialize_as_bson() == bson::BsonDocument{{"provider", "api-key"}, {"key", "a key"}});
CHECK(enum_from_provider_type(provider_type_from_enum(AuthProvider::API_KEY)) == AuthProvider::API_KEY);
}
}

Expand Down