You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/05/20 22:38:12 UTC

[GitHub] [iceberg] kbendick commented on a diff in pull request #4830: Core: Add session catalog implementation for REST

kbendick commented on code in PR #4830:
URL: https://github.com/apache/iceberg/pull/4830#discussion_r878580457


##########
core/src/main/java/org/apache/iceberg/rest/OAuth2Util.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.
+ */
+
+package org.apache.iceberg.rest;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Pattern;
+import org.apache.commons.compress.utils.Sets;
+import org.apache.iceberg.relocated.com.google.common.base.Joiner;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.base.Splitter;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.util.Pair;
+
+public class OAuth2Util {
+  private OAuth2Util() {
+  }
+
+  // valid scope tokens are from ascii 0x21 to 0x7E, excluding 0x22 (") and 0x5C (\)
+  private static final Pattern VALID_SCOPE_TOKEN = Pattern.compile("^[!-~&&[^\"\\\\]]+$");
+  private static final Splitter SCOPE_DELIMITER = Splitter.on(" ");
+  private static final Joiner SCOPE_JOINER = Joiner.on(" ");
+
+  private static final Splitter CREDENTIAL_SPLITTER = Splitter.on(":").limit(2).trimResults();
+  static final String GRANT_TYPE = "grant_type";
+  static final String CLIENT_CREDENTIALS = "client_credentials";
+  static final String TOKEN_EXCHANGE = "urn:ietf:params:oauth:grant-type:token-exchange";
+  private static final String SCOPE = "scope";
+  private static final String CATALOG = "catalog";
+
+  // Client credentials flow
+  private static final String CLIENT_ID = "client_id";
+  private static final String CLIENT_SECRET = "client_secret";
+
+  // Token exchange flow
+  private static final String SUBJECT_TOKEN = "subject_token";
+  private static final String SUBJECT_TOKEN_TYPE = "subject_token_type";
+  private static final String ACTOR_TOKEN = "actor_token";
+  private static final String ACTOR_TOKEN_TYPE = "actor_token_type";
+  private static final Set<String> VALID_TOKEN_TYPES = Sets.newHashSet(
+      "urn:ietf:params:oauth:token-type:access_token",
+      "urn:ietf:params:oauth:token-type:refresh_token",
+      "urn:ietf:params:oauth:token-type:id_token",
+      "urn:ietf:params:oauth:token-type:saml1",
+      "urn:ietf:params:oauth:token-type:saml2",
+      "urn:ietf:params:oauth:token-type:jwt");
+
+  public static boolean isValidScopeToken(String scopeToken) {
+    return VALID_SCOPE_TOKEN.matcher(scopeToken).matches();
+  }
+
+  public static List<String> parseScope(String scope) {
+    return SCOPE_DELIMITER.splitToList(scope);
+  }
+
+  public static String toScope(List<String> scopes) {
+    return SCOPE_JOINER.join(scopes);
+  }
+
+  public static Map<String, String> tokenExchangeRequest(String subjectToken, String subjectTokenType,
+                                                         List<String> scopes) {
+    return tokenExchangeRequest(subjectToken, subjectTokenType, null, null, scopes);
+  }
+
+  public static Map<String, String> tokenExchangeRequest(String subjectToken, String subjectTokenType,
+                                                         String actorToken, String actorTokenType,
+                                                         List<String> scopes) {
+    Preconditions.checkArgument(VALID_TOKEN_TYPES.contains(subjectTokenType),
+        "Invalid token type: %s", subjectTokenType);
+    Preconditions.checkArgument(actorToken == null || VALID_TOKEN_TYPES.contains(actorTokenType),
+        "Invalid token type: %s", actorTokenType);
+
+    ImmutableMap.Builder<String, String> formData = ImmutableMap.builder();
+    formData.put(GRANT_TYPE, TOKEN_EXCHANGE);
+    formData.put(SCOPE, toScope(scopes));
+    formData.put(SUBJECT_TOKEN, subjectToken);
+    formData.put(SUBJECT_TOKEN_TYPE, subjectTokenType);
+    if (actorToken != null) {
+      formData.put(ACTOR_TOKEN, actorToken);
+      formData.put(ACTOR_TOKEN_TYPE, actorTokenType);
+    }
+
+    return formData.build();
+  }
+
+  private static Pair<String, String> parseCredential(String credential) {
+    Preconditions.checkNotNull(credential, "Invalid credential: null");
+    List<String> parts = CREDENTIAL_SPLITTER.splitToList(credential);
+    switch (parts.size()) {
+      case 2:
+        // client ID and client secret
+        return Pair.of(parts.get(0), parts.get(1));
+      case 1:
+        // client ID
+        return Pair.of(null, parts.get(0));

Review Comment:
   Is it intended that `client ID` is sometimes pair[0] and sometimes pair[1]?
   
   That's how it looks presently based on the comments and return statements.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org