carloea2 commented on code in PR #4130: URL: https://github.com/apache/texera/pull/4130#discussion_r2621582774
########## common/auth/src/main/scala/org/apache/texera/auth/UploadTokenParser.scala: ########## @@ -0,0 +1,136 @@ +/* + * 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.texera.auth + +import org.apache.texera.auth.util.CryptoService +import org.apache.texera.config.AuthConfig + +import java.nio.charset.StandardCharsets +import java.util.Base64 + +/** + * Upload-token codec. + * + * Stateless, self-contained encrypted token that encodes: + * - uploadId + * - did (dataset id) + * - uid (user id) + * - filePath + * - physicalAddress (e.g. s3://bucket/key) + * + * Wire format (before AES-GCM encryption): + * v1|uploadId|did|uid|filePathB64|physicalB64 + */ +object UploadTokenParser { + + final case class UploadTokenPayload( + uploadId: String, + did: Int, + uid: Int, + filePath: String, + physicalAddress: String + ) + + private val Version = "v1" + private val Encoder = Base64.getUrlEncoder.withoutPadding() + private val Decoder = Base64.getUrlDecoder + + private val crypto: CryptoService = + CryptoService(AuthConfig.uploadTokenSecretKey) + + /** + * Build a payload (no expiration). + */ + def buildPayload( + did: Int, + uid: Int, + filePath: String, + uploadId: String, + physicalAddress: String + ): UploadTokenPayload = + UploadTokenPayload( + uploadId = uploadId, + did = did, + uid = uid, + filePath = filePath, + physicalAddress = physicalAddress + ) + + /** + * Encode a Payload into an encrypted, URL-safe token string. + */ + def encode(payload: UploadTokenPayload): String = { + val filePathB64 = Encoder.encodeToString( + payload.filePath.getBytes(StandardCharsets.UTF_8) + ) + val physicalB64 = Encoder.encodeToString( + payload.physicalAddress.getBytes(StandardCharsets.UTF_8) + ) + + val raw = + s"$Version|${payload.uploadId}|${payload.did}|${payload.uid}|$filePathB64|$physicalB64" + + crypto.encrypt(raw) + } + + /** + * Decode and decrypt a token string into a Payload. + * + * Throws IllegalArgumentException on: + * - invalid ciphertext + * - malformed structure + * - unsupported version + */ + def decode(token: String): UploadTokenPayload = { Review Comment: Still with JSON there will be some manual work to be done, how will you modularize this? ########## common/auth/src/main/scala/org/apache/texera/auth/UploadTokenParser.scala: ########## @@ -0,0 +1,136 @@ +/* + * 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.texera.auth + +import org.apache.texera.auth.util.CryptoService +import org.apache.texera.config.AuthConfig + +import java.nio.charset.StandardCharsets +import java.util.Base64 + +/** + * Upload-token codec. + * + * Stateless, self-contained encrypted token that encodes: + * - uploadId + * - did (dataset id) + * - uid (user id) + * - filePath + * - physicalAddress (e.g. s3://bucket/key) + * + * Wire format (before AES-GCM encryption): + * v1|uploadId|did|uid|filePathB64|physicalB64 + */ +object UploadTokenParser { + + final case class UploadTokenPayload( + uploadId: String, + did: Int, + uid: Int, + filePath: String, + physicalAddress: String + ) + + private val Version = "v1" + private val Encoder = Base64.getUrlEncoder.withoutPadding() + private val Decoder = Base64.getUrlDecoder + + private val crypto: CryptoService = + CryptoService(AuthConfig.uploadTokenSecretKey) + + /** + * Build a payload (no expiration). + */ + def buildPayload( + did: Int, + uid: Int, + filePath: String, + uploadId: String, + physicalAddress: String + ): UploadTokenPayload = + UploadTokenPayload( + uploadId = uploadId, + did = did, + uid = uid, + filePath = filePath, + physicalAddress = physicalAddress + ) + + /** + * Encode a Payload into an encrypted, URL-safe token string. + */ + def encode(payload: UploadTokenPayload): String = { + val filePathB64 = Encoder.encodeToString( + payload.filePath.getBytes(StandardCharsets.UTF_8) + ) + val physicalB64 = Encoder.encodeToString( + payload.physicalAddress.getBytes(StandardCharsets.UTF_8) + ) + + val raw = + s"$Version|${payload.uploadId}|${payload.did}|${payload.uid}|$filePathB64|$physicalB64" Review Comment: The concatenation and use of | is just for convenience, would you prefer a JSON approach? still the encryption will use the raw chars of the JSON. -- 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]
