pmi commented on code in PR #405: URL: https://github.com/apache/unomi/pull/405#discussion_r903870276
########## graphql/cxs-impl/src/main/java/org/apache/unomi/graphql/servlet/auth/GraphQLServletSecurityValidator.java: ########## @@ -0,0 +1,144 @@ +/* + * 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.unomi.graphql.servlet.auth; + +import graphql.language.Definition; +import graphql.language.Document; +import graphql.language.Field; +import graphql.language.Node; +import graphql.language.OperationDefinition; +import graphql.parser.Parser; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.security.auth.Subject; +import javax.security.auth.callback.Callback; +import javax.security.auth.callback.NameCallback; +import javax.security.auth.callback.PasswordCallback; +import javax.security.auth.callback.UnsupportedCallbackException; +import javax.security.auth.login.LoginContext; +import javax.security.auth.login.LoginException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Base64; +import java.util.List; + +import static graphql.language.OperationDefinition.Operation.MUTATION; +import static graphql.language.OperationDefinition.Operation.QUERY; +import static graphql.language.OperationDefinition.Operation.SUBSCRIPTION; +import static org.osgi.service.http.HttpContext.AUTHENTICATION_TYPE; +import static org.osgi.service.http.HttpContext.REMOTE_USER; + +public class GraphQLServletSecurityValidator { + + private static final Logger LOG = LoggerFactory.getLogger(GraphQLServletSecurityValidator.class); + + private final Parser parser; + + public GraphQLServletSecurityValidator() { + parser = new Parser(); + } + + public boolean validate(String query, String operationName, HttpServletRequest req, HttpServletResponse res) throws IOException { + if (isPublicOperation(query)) { + return true; + } else if (req.getHeader("Authorization") == null) { + res.addHeader("WWW-Authenticate", "Basic realm=\"karaf\""); + res.sendError(HttpServletResponse.SC_UNAUTHORIZED); + return false; + } + + if (isAuthenticatedUser(req)) { + return true; + } else { + res.sendError(HttpServletResponse.SC_UNAUTHORIZED); + return false; + } + } + + private boolean isPublicOperation(String query) { + final Document queryDoc = parser.parseDocument(query); + final Definition<?> def = queryDoc.getDefinitions().get(0); + if (def instanceof OperationDefinition) { + OperationDefinition opDef = (OperationDefinition) def; + if ("IntrospectionQuery".equals(opDef.getName()) + || SUBSCRIPTION.equals(opDef.getOperation())) { Review Comment: It is public now so I left it this way, but that can be changed later. @sergehuber What do you think ? -- 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]
