From commits-return-17641-archive-asf-public=cust-asf.ponee.io@pulsar.apache.org Sat Nov 17 03:57:20 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id CA288180676 for ; Sat, 17 Nov 2018 03:57:19 +0100 (CET) Received: (qmail 60932 invoked by uid 500); 17 Nov 2018 02:57:18 -0000 Mailing-List: contact commits-help@pulsar.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@pulsar.apache.org Delivered-To: mailing list commits@pulsar.apache.org Received: (qmail 60913 invoked by uid 99); 17 Nov 2018 02:57:18 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 17 Nov 2018 02:57:18 +0000 From: GitBox To: commits@pulsar.apache.org Subject: [GitHub] maskit commented on a change in pull request #2888: PIP-25: Token based authentication Message-ID: <154242343821.6859.16900336602931588730.gitbox@gitbox.apache.org> Date: Sat, 17 Nov 2018 02:57:18 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit maskit commented on a change in pull request #2888: PIP-25: Token based authentication URL: https://github.com/apache/pulsar/pull/2888#discussion_r234393838 ########## File path: pulsar-broker-common/src/main/java/org/apache/pulsar/broker/authentication/AuthenticationProviderToken.java ########## @@ -0,0 +1,147 @@ +/** + * 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.pulsar.broker.authentication; + +import com.google.common.base.Charsets; + +import io.jsonwebtoken.Claims; +import io.jsonwebtoken.Jwt; +import io.jsonwebtoken.JwtException; +import io.jsonwebtoken.Jwts; + +import java.io.IOException; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.security.Key; + +import javax.naming.AuthenticationException; + +import lombok.extern.slf4j.Slf4j; + +import org.apache.pulsar.broker.ServiceConfiguration; +import org.apache.pulsar.broker.authentication.utils.AuthTokenUtils; + +@Slf4j +public class AuthenticationProviderToken implements AuthenticationProvider { + + public final static String HTTP_HEADER_NAME = "Authorization"; + final static String HTTP_HEADER_VALUE_PREFIX = "Bearer "; + + // When simmetric key is configured + final static String CONF_TOKEN_SECRET_KEY = "tokenSecretKey"; + + // When public/private key pair is configured + final static String CONF_TOKEN_PUBLIC_KEY = "tokenPublicKey"; + + private Key validationKey; + + @Override + public void close() throws IOException { + // noop + } + + @Override + public void initialize(ServiceConfiguration config) throws IOException { + this.validationKey = getValidationKey(config); + } + + @Override + public String getAuthMethodName() { + return "token"; + } + + @Override + public String authenticate(AuthenticationDataSource authData) throws AuthenticationException { + String token = null; + + if (authData.hasDataFromCommand()) { + // Authenticate Pulsar binary connection + token = authData.getCommandData(); + } else if (authData.hasDataFromHttp()) { + // Authentication HTTP request. The format here should be compliant to RFC-6750 + // (https://tools.ietf.org/html/rfc6750#section-2.1). Eg: + // + // Authorization: Bearer xxxxxxxxxxxxx + String httpHeaderValue = authData.getHttpHeader(HTTP_HEADER_NAME); + if (!httpHeaderValue.startsWith(HTTP_HEADER_VALUE_PREFIX)) { + throw new AuthenticationException("Invalid HTTP Authorization header"); + } + + // Remove prefix + token = httpHeaderValue.substring(HTTP_HEADER_VALUE_PREFIX.length()); + } else { + throw new AuthenticationException("No token credentials passed"); + } + + // Validate the token + try { + @SuppressWarnings("unchecked") + Jwt jwt = Jwts.parser() + .setSigningKey(validationKey) + .parse(token); + + return jwt.getBody().getSubject(); + } catch (JwtException e) { + throw new AuthenticationException("Failed to authentication token: " + e.getMessage()); + } + } + + /** + * Try to get the validation key for tokens from several possible config options. + */ + private static Key getValidationKey(ServiceConfiguration conf) throws IOException { + final boolean isPublicKey; + final String validationKeyConfig; + + if (conf.getProperty(CONF_TOKEN_SECRET_KEY) != null) { + isPublicKey = false; + validationKeyConfig = (String) conf.getProperty(CONF_TOKEN_SECRET_KEY); + } else if (conf.getProperty(CONF_TOKEN_PUBLIC_KEY) != null) { + isPublicKey = true; + validationKeyConfig = (String) conf.getProperty(CONF_TOKEN_PUBLIC_KEY); + } else { + throw new IOException("No secret key was provided for token authentication"); + } + + final String validationKey; + + if (validationKeyConfig.startsWith("data:")) { + log.info("Reading token validation key from config file"); + validationKey = validationKeyConfig.substring("data:".length()); Review comment: I’m ok with parsing the schemes here and having a limitation in its format for now, but I still think this data scheme parsing is incorrect. It always need a comma after the colon. https://tools.ietf.org/html/rfc2397#section-3 Also, base64 encoding is not mandatory. You can pass URL encoded string instead. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: users@infra.apache.org With regards, Apache Git Services