lishuxu commented on code in PR #577: URL: https://github.com/apache/iceberg-cpp/pull/577#discussion_r2903681726
########## src/iceberg/catalog/rest/auth/oauth2_util.cc: ########## @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "iceberg/catalog/rest/auth/oauth2_util.h" + +#include <nlohmann/json.hpp> + +#include "iceberg/catalog/rest/auth/auth_session.h" +#include "iceberg/catalog/rest/error_handlers.h" +#include "iceberg/catalog/rest/http_client.h" +#include "iceberg/json_serde_internal.h" +#include "iceberg/util/json_util_internal.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest::auth { + +namespace { + +constexpr std::string_view kAccessToken = "access_token"; +constexpr std::string_view kTokenType = "token_type"; +constexpr std::string_view kExpiresIn = "expires_in"; +constexpr std::string_view kRefreshToken = "refresh_token"; +constexpr std::string_view kScope = "scope"; + +constexpr std::string_view kGrantType = "grant_type"; +constexpr std::string_view kClientCredentials = "client_credentials"; +constexpr std::string_view kClientId = "client_id"; +constexpr std::string_view kClientSecret = "client_secret"; + +} // namespace + +Status OAuthTokenResponse::Validate() const { + if (access_token.empty()) { + return ValidationFailed("OAuth2 token response missing required 'access_token'"); + } + if (token_type.empty()) { + return ValidationFailed("OAuth2 token response missing required 'token_type'"); + } + return {}; +} + +Result<OAuthTokenResponse> OAuthTokenResponseFromJsonString(const std::string& json_str) { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(json_str)); + + OAuthTokenResponse response; + ICEBERG_ASSIGN_OR_RAISE(response.access_token, + GetJsonValue<std::string>(json, kAccessToken)); + ICEBERG_ASSIGN_OR_RAISE(response.token_type, + GetJsonValue<std::string>(json, kTokenType)); + ICEBERG_ASSIGN_OR_RAISE(response.expires_in, + GetJsonValueOrDefault<int64_t>(json, kExpiresIn, 0)); Review Comment: Makes sense. Added a TODO to extract the JWT exp claim when expires_in is missing. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
