Github user necouchman commented on a diff in the pull request:

    
https://github.com/apache/incubator-guacamole-client/pull/183#discussion_r135947971
  
    --- Diff: 
guacamole-ext/src/main/java/org/apache/guacamole/properties/CipherGuacamoleProperty.java
 ---
    @@ -0,0 +1,92 @@
    +/*
    + * 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.guacamole.properties;
    +
    +import java.io.BufferedInputStream;
    +import java.io.File;
    +import java.io.FileInputStream;
    +import java.io.FileNotFoundException;
    +import java.io.InputStream;
    +import java.io.IOException;
    +import java.lang.IllegalArgumentException;
    +import java.security.InvalidKeyException;
    +import java.security.KeyFactory;
    +import java.security.NoSuchAlgorithmException;
    +import java.security.PrivateKey;
    +import java.security.spec.InvalidKeySpecException;
    +import java.security.spec.KeySpec;
    +import java.security.spec.PKCS8EncodedKeySpec;
    +import javax.crypto.Cipher;
    +import javax.crypto.NoSuchPaddingException;
    +import org.apache.guacamole.GuacamoleException;
    +import org.apache.guacamole.environment.Environment;
    +import org.apache.guacamole.environment.LocalEnvironment;
    +
    +/**
    + * A GuacamoleProperty whose value is derived from a private key file.
    + */
    +public abstract class CipherGuacamoleProperty implements 
GuacamoleProperty<Cipher>  {
    +
    +    @Override
    +    public Cipher parseValue(String value) throws GuacamoleException {
    +
    +        try {
    +
    +            final Environment environment = new LocalEnvironment();
    +
    +            // Open and read the file specified in the configuration.
    +            File keyFile = new File(environment.getGuacamoleHome(), value);
    +            InputStream keyInput = new BufferedInputStream(new 
FileInputStream(keyFile));
    +            final byte[] keyBytes = new byte[(int) keyFile.length()];
    +            keyInput.read(keyBytes);
    +            keyInput.close();
    +
    +            // Set up decryption infrastructure
    +            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    +            KeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
    +            final PrivateKey privateKey = 
keyFactory.generatePrivate(keySpec);
    +            final Cipher cipher = 
Cipher.getInstance(privateKey.getAlgorithm());
    +            cipher.init(Cipher.DECRYPT_MODE, privateKey);
    +
    +            return cipher;
    +
    +        }
    +        catch (FileNotFoundException e) {
    +            throw new GuacamoleException("Could not find the specified key 
file.", e);
    +        }
    +        catch (IOException e) {
    +            throw new GuacamoleException("Could not read in the specified 
key file.", e);
    +        }
    +        catch (NoSuchAlgorithmException e) {
    +            throw new GuacamoleException("Specified algorithm does not 
exist.", e);
    +        }
    +        catch (InvalidKeyException e) {
    +            throw new GuacamoleException("Specified key is invalid.", e);
    +        }
    +        catch (InvalidKeySpecException e) {
    +            throw new GuacamoleException("Invalid KeySpec 
initialization.", e);
    +        }
    +        catch (NoSuchPaddingException e) {
    +            throw new GuacamoleException("No such padding exception.", e);
    +        }
    +
    --- End diff --
    
    When all of those exceptions were caught and dealt with directly in the 
AuthenticationProviderService class, the mehtod just returned null rather than 
throwing a GuacamoleException.  Returning null just means that the password is 
not added to the credential object, so it isn't available as a token later on 
inside the client.
    
    Since creating a more generic type, here, I figured, while the behavior of 
just returning a null value and not killing authentication entirely is fine for 
the CAS extension, it may not be desirable across the board for whatever stuff 
may come later that might use this property, so instead of returning null, 
here, I'm throwing GuacamoleException.  This means that, if those exceptions 
are thrown while CAS is using this property, it'll kill CAS authentication if 
this Cipher property fails to parse for one of these reasons.  I consider that 
behavior less desirable than just not having the password available to the 
user, as authentication has technically still succeeded.
    
    I suppose I could capture the GuacamoleException inside the CAS module when 
pulling this property and just return null instead of allowing the 
GuacamoleException to pass through...


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to